Version 22.49.9: Oct 18, 2022 Code refactoring related to XML.

This commit is contained in:
acanas 2022-10-18 20:57:31 +02:00
parent 9f6d54d0d0
commit c574a04071
3 changed files with 73 additions and 65 deletions

View File

@ -606,10 +606,11 @@ TODO: Fix bug: error al enviar un mensaje a dos recipientes, error on duplicate
TODO: Attach pdf files in multimedia. TODO: Attach pdf files in multimedia.
*/ */
#define Log_PLATFORM_VERSION "SWAD 22.49.7 (2022-10-18)" #define Log_PLATFORM_VERSION "SWAD 22.49.9 (2022-10-18)"
#define CSS_FILE "swad22.49.4.css" #define CSS_FILE "swad22.49.4.css"
#define JS_FILE "swad22.49.js" #define JS_FILE "swad22.49.js"
/* /*
Version 22.49.9: Oct 18, 2022 Code refactoring related to XML. (333257 lines)
Version 22.49.8: Oct 18, 2022 Code refactoring related to parameters. (333253 lines) Version 22.49.8: Oct 18, 2022 Code refactoring related to parameters. (333253 lines)
Version 22.49.7: Oct 18, 2022 Code refactoring related to database. (333218 lines) Version 22.49.7: Oct 18, 2022 Code refactoring related to database. (333218 lines)
Version 22.49.6: Oct 18, 2022 Code refactoring related to alerts. (333197 lines) Version 22.49.6: Oct 18, 2022 Code refactoring related to alerts. (333197 lines)

View File

@ -124,10 +124,11 @@ struct Globals
bool UsesAJAX; // Do not generate full HTML page, only the content of a div bool UsesAJAX; // Do not generate full HTML page, only the content of a div
bool IsAJAXAutoRefresh; // It's an automatic refresh drom time to time bool IsAJAXAutoRefresh; // It's an automatic refresh drom time to time
} Action; } Action;
time_t StartExecutionTimeUTC; time_t StartExecutionTimeUTC;
struct Dat_DateTime Now; struct Dat_DateTime Now;
struct Dat_Date Yesterday; struct Dat_Date Yesterday;
const char *XMLPtr;
struct struct
{ {
char FileName[PATH_MAX + 1]; char FileName[PATH_MAX + 1];

View File

@ -42,6 +42,12 @@
extern struct Globals Gbl; extern struct Globals Gbl;
/*****************************************************************************/
/************************* Private global variables **************************/
/*****************************************************************************/
static const char *XML_Ptr;
/*****************************************************************************/ /*****************************************************************************/
/***************************** Private prototypes ****************************/ /***************************** Private prototypes ****************************/
/*****************************************************************************/ /*****************************************************************************/
@ -92,7 +98,7 @@ void XML_GetTree (const char *XMLBuffer,struct XMLElement **XMLRootElem)
if ((*XMLRootElem = calloc (1,sizeof (**XMLRootElem))) == NULL) if ((*XMLRootElem = calloc (1,sizeof (**XMLRootElem))) == NULL)
Err_NotEnoughMemoryExit (); Err_NotEnoughMemoryExit ();
Gbl.XMLPtr = XMLBuffer; XML_Ptr = XMLBuffer;
XML_GetElement (*XMLRootElem); XML_GetElement (*XMLRootElem);
} }
@ -113,33 +119,33 @@ static void XML_GetElement (struct XMLElement *ParentElem)
/* /*
<parent...> element content </parent> <parent...> element content </parent>
^ ^
Gbl.XMLPtr XML_Ptr
*/ */
/* Skip spaces */ /* Skip spaces */
XML_SkipSpaces (); XML_SkipSpaces ();
StartContent = Gbl.XMLPtr; StartContent = XML_Ptr;
ContentLength = strcspn (StartContent,"<"); ContentLength = strcspn (StartContent,"<");
Gbl.XMLPtr += ContentLength; XML_Ptr += ContentLength;
while (*Gbl.XMLPtr == '<') // For each child until parent end tag while (*XML_Ptr == '<') // For each child until parent end tag
{ {
Gbl.XMLPtr++; XML_Ptr++;
if (*Gbl.XMLPtr == '/') // Parent end tag if (*XML_Ptr == '/') // Parent end tag
{ {
/* /*
<parent> element content </parent> <parent> element content </parent>
^ ^
Gbl.XMLPtr XML_Ptr
*/ */
/***** Check tag name *****/ /***** Check tag name *****/
Gbl.XMLPtr++; XML_Ptr++;
/* /*
<parent> element content </parent> <parent> element content </parent>
^ ^
Gbl.XMLPtr XML_Ptr
*/ */
EndTagNameLength = strcspn (Gbl.XMLPtr,">"); EndTagNameLength = strcspn (XML_Ptr,">");
if (ParentElem->TagNameLength != EndTagNameLength) if (ParentElem->TagNameLength != EndTagNameLength)
{ {
snprintf (ErrorTxt,sizeof (ErrorTxt), snprintf (ErrorTxt,sizeof (ErrorTxt),
@ -147,7 +153,7 @@ static void XML_GetElement (struct XMLElement *ParentElem)
ParentElem->TagName); ParentElem->TagName);
Err_ShowErrorAndExit (ErrorTxt); Err_ShowErrorAndExit (ErrorTxt);
} }
if (strncmp (ParentElem->TagName,Gbl.XMLPtr,EndTagNameLength)) // XML tags are case sensitive if (strncmp (ParentElem->TagName,XML_Ptr,EndTagNameLength)) // XML tags are case sensitive
{ {
snprintf (ErrorTxt,sizeof (ErrorTxt), snprintf (ErrorTxt,sizeof (ErrorTxt),
"XML syntax error. Expect end tag &lt;/%s&gt;.", "XML syntax error. Expect end tag &lt;/%s&gt;.",
@ -156,12 +162,12 @@ static void XML_GetElement (struct XMLElement *ParentElem)
} }
// End of parent element found! // End of parent element found!
Gbl.XMLPtr += EndTagNameLength; XML_Ptr += EndTagNameLength;
Gbl.XMLPtr++; XML_Ptr++;
/* /*
<parent> element content </parent> <parent> element content </parent>
^ ^
Gbl.XMLPtr XML_Ptr
*/ */
/* Remove trailing spaces in content */ /* Remove trailing spaces in content */
for (Ptr = StartContent + ContentLength - 1; for (Ptr = StartContent + ContentLength - 1;
@ -182,20 +188,20 @@ static void XML_GetElement (struct XMLElement *ParentElem)
return; return;
} }
else if (*Gbl.XMLPtr == '!' || else if (*XML_Ptr == '!' ||
*Gbl.XMLPtr == '?') // Skip <!...> and <?...> *XML_Ptr == '?') // Skip <!...> and <?...>
{ {
TagLength = strcspn (Gbl.XMLPtr,">"); TagLength = strcspn (XML_Ptr,">");
Gbl.XMLPtr += TagLength; XML_Ptr += TagLength;
if (*Gbl.XMLPtr == '>') if (*XML_Ptr == '>')
Gbl.XMLPtr++; XML_Ptr++;
} }
else // New start tag else // New start tag
{ {
/* /*
<parent><child attribute1="value" attribute2="value">...</child>...</parent> <parent><child attribute1="value" attribute2="value">...</child>...</parent>
^ ^
Gbl.XMLPtr XML_Ptr
*/ */
/***** Allocate space for the child element *****/ /***** Allocate space for the child element *****/
if ((ChildElem = calloc (1,sizeof (*ChildElem))) == NULL) if ((ChildElem = calloc (1,sizeof (*ChildElem))) == NULL)
@ -209,44 +215,44 @@ static void XML_GetElement (struct XMLElement *ParentElem)
ParentElem->LastChild = ChildElem; ParentElem->LastChild = ChildElem;
/***** Get child tag name *****/ /***** Get child tag name *****/
ChildElem->TagNameLength = strcspn (Gbl.XMLPtr,">/ \t"); ChildElem->TagNameLength = strcspn (XML_Ptr,">/ \t");
if ((ChildElem->TagName = malloc (ChildElem->TagNameLength + 1)) == NULL) if ((ChildElem->TagName = malloc (ChildElem->TagNameLength + 1)) == NULL)
Err_NotEnoughMemoryExit (); Err_NotEnoughMemoryExit ();
strncpy (ChildElem->TagName,Gbl.XMLPtr,ChildElem->TagNameLength); strncpy (ChildElem->TagName,XML_Ptr,ChildElem->TagNameLength);
ChildElem->TagName[ChildElem->TagNameLength] = '\0'; ChildElem->TagName[ChildElem->TagNameLength] = '\0';
Gbl.XMLPtr += ChildElem->TagNameLength; XML_Ptr += ChildElem->TagNameLength;
/* /*
<parent><child attribute1="value" attribute2="value">...</child>...</parent> <parent><child attribute1="value" attribute2="value">...</child>...</parent>
^ ^
Gbl.XMLPtr XML_Ptr
*/ */
/* Check if end of start tag found */ /* Check if end of start tag found */
if (*Gbl.XMLPtr == '>') // End of start tag if (*XML_Ptr == '>') // End of start tag
{ {
Gbl.XMLPtr++; XML_Ptr++;
/* /*
<parent><child>...</child>...</parent> <parent><child>...</child>...</parent>
^ ^
Gbl.XMLPtr XML_Ptr
*/ */
XML_GetElement (ChildElem); XML_GetElement (ChildElem);
} }
else if (*Gbl.XMLPtr == '/') // Unary tag? else if (*XML_Ptr == '/') // Unary tag?
{ {
Gbl.XMLPtr++; XML_Ptr++;
/* /*
<parent><child/>...</parent> <parent><child/>...</parent>
^ ^
Gbl.XMLPtr XML_Ptr
*/ */
if (*Gbl.XMLPtr != '>') // Here it should be the end of start tag if (*XML_Ptr != '>') // Here it should be the end of start tag
Err_ShowErrorAndExit ("XML syntax error. Expect &gt; ending unary tag."); Err_ShowErrorAndExit ("XML syntax error. Expect &gt; ending unary tag.");
Gbl.XMLPtr++; XML_Ptr++;
/* /*
<parent><child/>...</parent> <parent><child/>...</parent>
^ ^
Gbl.XMLPtr XML_Ptr
*/ */
} }
else // Begin of an attribute else // Begin of an attribute
@ -254,13 +260,13 @@ static void XML_GetElement (struct XMLElement *ParentElem)
/* /*
<parent><child attribute1="value" attribute2="value">...</child>...</parent> <parent><child attribute1="value" attribute2="value">...</child>...</parent>
^ ^
Gbl.XMLPtr XML_Ptr
*/ */
XML_GetAttributes (ChildElem); XML_GetAttributes (ChildElem);
/* /*
<parent><child attribute1="value" attribute2="value">...</child>...</parent> <parent><child attribute1="value" attribute2="value">...</child>...</parent>
^ ^
Gbl.XMLPtr XML_Ptr
*/ */
XML_GetElement (ChildElem); XML_GetElement (ChildElem);
} }
@ -285,30 +291,30 @@ static void XML_GetAttributes (struct XMLElement *Elem)
<parent><child attribute1="value" attribute2="value">...</child>...</parent> <parent><child attribute1="value" attribute2="value">...</child>...</parent>
^ ^
| |
Gbl.XMLPtr XML_Ptr
*/ */
do do
{ {
/* Skip spaces */ /* Skip spaces */
XML_SkipSpaces (); XML_SkipSpaces ();
if (*Gbl.XMLPtr == '/') // End of unary tag? if (*XML_Ptr == '/') // End of unary tag?
{ {
Gbl.XMLPtr++; XML_Ptr++;
if (*Gbl.XMLPtr == '>') if (*XML_Ptr == '>')
{ {
Gbl.XMLPtr++; XML_Ptr++;
EndOfStartTag = true; EndOfStartTag = true;
} }
else else
Err_ShowErrorAndExit ("XML syntax error. Expect &gt; ending unary tag with attributes."); Err_ShowErrorAndExit ("XML syntax error. Expect &gt; ending unary tag with attributes.");
} }
else if (*Gbl.XMLPtr == '>') // End of start tag? else if (*XML_Ptr == '>') // End of start tag?
{ {
Gbl.XMLPtr++; XML_Ptr++;
EndOfStartTag = true; EndOfStartTag = true;
} }
else if (*Gbl.XMLPtr == '\0') else if (*XML_Ptr == '\0')
Err_ShowErrorAndExit ("XML syntax error. Unexpected end of file."); Err_ShowErrorAndExit ("XML syntax error. Unexpected end of file.");
else else
{ {
@ -316,7 +322,7 @@ static void XML_GetAttributes (struct XMLElement *Elem)
<parent><child attribute1="value" attribute2="value">...</child>...</parent> <parent><child attribute1="value" attribute2="value">...</child>...</parent>
^ ^
| |
Gbl.XMLPtr XML_Ptr
*/ */
/***** Allocate space for the attribute *****/ /***** Allocate space for the attribute *****/
if ((Attribute = calloc (1,sizeof (*Attribute))) == NULL) if ((Attribute = calloc (1,sizeof (*Attribute))) == NULL)
@ -330,29 +336,29 @@ static void XML_GetAttributes (struct XMLElement *Elem)
Elem->LastAttribute = Attribute; Elem->LastAttribute = Attribute;
/***** Get attribute name *****/ /***** Get attribute name *****/
Attribute->AttributeNameLength = strcspn (Gbl.XMLPtr,"="); Attribute->AttributeNameLength = strcspn (XML_Ptr,"=");
if ((Attribute->AttributeName = malloc (Attribute->AttributeNameLength + 1)) == NULL) if ((Attribute->AttributeName = malloc (Attribute->AttributeNameLength + 1)) == NULL)
Err_NotEnoughMemoryExit (); Err_NotEnoughMemoryExit ();
strncpy (Attribute->AttributeName,Gbl.XMLPtr,Attribute->AttributeNameLength); strncpy (Attribute->AttributeName,XML_Ptr,Attribute->AttributeNameLength);
Attribute->AttributeName[Attribute->AttributeNameLength] = '\0'; Attribute->AttributeName[Attribute->AttributeNameLength] = '\0';
Gbl.XMLPtr += Attribute->AttributeNameLength; XML_Ptr += Attribute->AttributeNameLength;
/* End of attribute name: /* End of attribute name:
<parent><child attribute1="value" attribute2="value">...</child>...</parent> <parent><child attribute1="value" attribute2="value">...</child>...</parent>
^ ^
| |
Gbl.XMLPtr XML_Ptr
*/ */
/***** Get attribute content *****/ /***** Get attribute content *****/
Gbl.XMLPtr++; XML_Ptr++;
if (*Gbl.XMLPtr == '\"') if (*XML_Ptr == '\"')
{ {
Gbl.XMLPtr++; XML_Ptr++;
Attribute->ContentLength = strcspn (Gbl.XMLPtr,"\""); Attribute->ContentLength = strcspn (XML_Ptr,"\"");
} }
else if (*Gbl.XMLPtr == '\'') else if (*XML_Ptr == '\'')
{ {
Gbl.XMLPtr++; XML_Ptr++;
Attribute->ContentLength = strcspn (Gbl.XMLPtr,"'"); Attribute->ContentLength = strcspn (XML_Ptr,"'");
} }
else else
{ {
@ -365,16 +371,16 @@ static void XML_GetAttributes (struct XMLElement *Elem)
if ((Attribute->Content = malloc (Attribute->ContentLength + 1)) == NULL) if ((Attribute->Content = malloc (Attribute->ContentLength + 1)) == NULL)
Err_NotEnoughMemoryExit (); Err_NotEnoughMemoryExit ();
strncpy (Attribute->Content,Gbl.XMLPtr,Attribute->ContentLength); strncpy (Attribute->Content,XML_Ptr,Attribute->ContentLength);
Attribute->Content[Attribute->ContentLength] = '\0'; Attribute->Content[Attribute->ContentLength] = '\0';
Gbl.XMLPtr += Attribute->ContentLength; XML_Ptr += Attribute->ContentLength;
Gbl.XMLPtr++; XML_Ptr++;
/* End of attribute content /* End of attribute content
<parent><child attribute1="value" attribute2="value">...</child>...</parent> <parent><child attribute1="value" attribute2="value">...</child>...</parent>
^ ^
| |
Gbl.XMLPtr XML_Ptr
*/ */
} }
} }
@ -387,8 +393,8 @@ static void XML_GetAttributes (struct XMLElement *Elem)
static void XML_SkipSpaces (void) static void XML_SkipSpaces (void)
{ {
while (isspace ((int) *Gbl.XMLPtr)) while (isspace ((int) *XML_Ptr))
Gbl.XMLPtr++; XML_Ptr++;
} }
/*****************************************************************************/ /*****************************************************************************/