From c574a040715753897cafe8992eba979225b80663 Mon Sep 17 00:00:00 2001 From: acanas Date: Tue, 18 Oct 2022 20:57:31 +0200 Subject: [PATCH] Version 22.49.9: Oct 18, 2022 Code refactoring related to XML. --- swad_changelog.h | 3 +- swad_global.h | 3 +- swad_xml.c | 132 +++++++++++++++++++++++++---------------------- 3 files changed, 73 insertions(+), 65 deletions(-) diff --git a/swad_changelog.h b/swad_changelog.h index 3c90d0ade..f8bcefc76 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -606,10 +606,11 @@ TODO: Fix bug: error al enviar un mensaje a dos recipientes, error on duplicate 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 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.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) diff --git a/swad_global.h b/swad_global.h index be43b4ba5..6e6e07c54 100644 --- a/swad_global.h +++ b/swad_global.h @@ -124,10 +124,11 @@ struct Globals 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 } Action; + time_t StartExecutionTimeUTC; struct Dat_DateTime Now; struct Dat_Date Yesterday; - const char *XMLPtr; + struct { char FileName[PATH_MAX + 1]; diff --git a/swad_xml.c b/swad_xml.c index 6a083459f..86ef1539d 100644 --- a/swad_xml.c +++ b/swad_xml.c @@ -42,6 +42,12 @@ extern struct Globals Gbl; +/*****************************************************************************/ +/************************* Private global variables **************************/ +/*****************************************************************************/ + +static const char *XML_Ptr; + /*****************************************************************************/ /***************************** Private prototypes ****************************/ /*****************************************************************************/ @@ -92,7 +98,7 @@ void XML_GetTree (const char *XMLBuffer,struct XMLElement **XMLRootElem) if ((*XMLRootElem = calloc (1,sizeof (**XMLRootElem))) == NULL) Err_NotEnoughMemoryExit (); - Gbl.XMLPtr = XMLBuffer; + XML_Ptr = XMLBuffer; XML_GetElement (*XMLRootElem); } @@ -113,33 +119,33 @@ static void XML_GetElement (struct XMLElement *ParentElem) /* element content ^ - Gbl.XMLPtr + XML_Ptr */ /* Skip spaces */ XML_SkipSpaces (); - StartContent = Gbl.XMLPtr; + StartContent = XML_Ptr; 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++; - if (*Gbl.XMLPtr == '/') // Parent end tag + XML_Ptr++; + if (*XML_Ptr == '/') // Parent end tag { /* element content ^ - Gbl.XMLPtr + XML_Ptr */ /***** Check tag name *****/ - Gbl.XMLPtr++; + XML_Ptr++; /* element content ^ - Gbl.XMLPtr + XML_Ptr */ - EndTagNameLength = strcspn (Gbl.XMLPtr,">"); + EndTagNameLength = strcspn (XML_Ptr,">"); if (ParentElem->TagNameLength != EndTagNameLength) { snprintf (ErrorTxt,sizeof (ErrorTxt), @@ -147,7 +153,7 @@ static void XML_GetElement (struct XMLElement *ParentElem) ParentElem->TagName); 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), "XML syntax error. Expect end tag </%s>.", @@ -156,12 +162,12 @@ static void XML_GetElement (struct XMLElement *ParentElem) } // End of parent element found! - Gbl.XMLPtr += EndTagNameLength; - Gbl.XMLPtr++; + XML_Ptr += EndTagNameLength; + XML_Ptr++; /* element content ^ - Gbl.XMLPtr + XML_Ptr */ /* Remove trailing spaces in content */ for (Ptr = StartContent + ContentLength - 1; @@ -182,20 +188,20 @@ static void XML_GetElement (struct XMLElement *ParentElem) return; } - else if (*Gbl.XMLPtr == '!' || - *Gbl.XMLPtr == '?') // Skip and + else if (*XML_Ptr == '!' || + *XML_Ptr == '?') // Skip and { - TagLength = strcspn (Gbl.XMLPtr,">"); - Gbl.XMLPtr += TagLength; - if (*Gbl.XMLPtr == '>') - Gbl.XMLPtr++; + TagLength = strcspn (XML_Ptr,">"); + XML_Ptr += TagLength; + if (*XML_Ptr == '>') + XML_Ptr++; } else // New start tag { /* ...... ^ - Gbl.XMLPtr + XML_Ptr */ /***** Allocate space for the child element *****/ if ((ChildElem = calloc (1,sizeof (*ChildElem))) == NULL) @@ -209,44 +215,44 @@ static void XML_GetElement (struct XMLElement *ParentElem) ParentElem->LastChild = ChildElem; /***** Get child tag name *****/ - ChildElem->TagNameLength = strcspn (Gbl.XMLPtr,">/ \t"); + ChildElem->TagNameLength = strcspn (XML_Ptr,">/ \t"); if ((ChildElem->TagName = malloc (ChildElem->TagNameLength + 1)) == NULL) Err_NotEnoughMemoryExit (); - strncpy (ChildElem->TagName,Gbl.XMLPtr,ChildElem->TagNameLength); + strncpy (ChildElem->TagName,XML_Ptr,ChildElem->TagNameLength); ChildElem->TagName[ChildElem->TagNameLength] = '\0'; - Gbl.XMLPtr += ChildElem->TagNameLength; + XML_Ptr += ChildElem->TagNameLength; /* ...... ^ - Gbl.XMLPtr + XML_Ptr */ /* 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++; /* ...... ^ - Gbl.XMLPtr + XML_Ptr */ XML_GetElement (ChildElem); } - else if (*Gbl.XMLPtr == '/') // Unary tag? + else if (*XML_Ptr == '/') // Unary tag? { - Gbl.XMLPtr++; + XML_Ptr++; /* ... ^ - 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 > ending unary tag."); - Gbl.XMLPtr++; + XML_Ptr++; /* ... ^ - Gbl.XMLPtr + XML_Ptr */ } else // Begin of an attribute @@ -254,13 +260,13 @@ static void XML_GetElement (struct XMLElement *ParentElem) /* ...... ^ - Gbl.XMLPtr + XML_Ptr */ XML_GetAttributes (ChildElem); /* ...... ^ - Gbl.XMLPtr + XML_Ptr */ XML_GetElement (ChildElem); } @@ -285,30 +291,30 @@ static void XML_GetAttributes (struct XMLElement *Elem) ...... ^ | - Gbl.XMLPtr + XML_Ptr */ do { /* Skip spaces */ XML_SkipSpaces (); - if (*Gbl.XMLPtr == '/') // End of unary tag? + if (*XML_Ptr == '/') // End of unary tag? { - Gbl.XMLPtr++; - if (*Gbl.XMLPtr == '>') + XML_Ptr++; + if (*XML_Ptr == '>') { - Gbl.XMLPtr++; + XML_Ptr++; EndOfStartTag = true; } else Err_ShowErrorAndExit ("XML syntax error. Expect > 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; } - else if (*Gbl.XMLPtr == '\0') + else if (*XML_Ptr == '\0') Err_ShowErrorAndExit ("XML syntax error. Unexpected end of file."); else { @@ -316,7 +322,7 @@ static void XML_GetAttributes (struct XMLElement *Elem) ...... ^ | - Gbl.XMLPtr + XML_Ptr */ /***** Allocate space for the attribute *****/ if ((Attribute = calloc (1,sizeof (*Attribute))) == NULL) @@ -330,29 +336,29 @@ static void XML_GetAttributes (struct XMLElement *Elem) Elem->LastAttribute = Attribute; /***** Get attribute name *****/ - Attribute->AttributeNameLength = strcspn (Gbl.XMLPtr,"="); + Attribute->AttributeNameLength = strcspn (XML_Ptr,"="); if ((Attribute->AttributeName = malloc (Attribute->AttributeNameLength + 1)) == NULL) Err_NotEnoughMemoryExit (); - strncpy (Attribute->AttributeName,Gbl.XMLPtr,Attribute->AttributeNameLength); + strncpy (Attribute->AttributeName,XML_Ptr,Attribute->AttributeNameLength); Attribute->AttributeName[Attribute->AttributeNameLength] = '\0'; - Gbl.XMLPtr += Attribute->AttributeNameLength; + XML_Ptr += Attribute->AttributeNameLength; /* End of attribute name: ...... ^ | - Gbl.XMLPtr + XML_Ptr */ /***** Get attribute content *****/ - Gbl.XMLPtr++; - if (*Gbl.XMLPtr == '\"') + XML_Ptr++; + if (*XML_Ptr == '\"') { - Gbl.XMLPtr++; - Attribute->ContentLength = strcspn (Gbl.XMLPtr,"\""); + XML_Ptr++; + Attribute->ContentLength = strcspn (XML_Ptr,"\""); } - else if (*Gbl.XMLPtr == '\'') + else if (*XML_Ptr == '\'') { - Gbl.XMLPtr++; - Attribute->ContentLength = strcspn (Gbl.XMLPtr,"'"); + XML_Ptr++; + Attribute->ContentLength = strcspn (XML_Ptr,"'"); } else { @@ -365,16 +371,16 @@ static void XML_GetAttributes (struct XMLElement *Elem) if ((Attribute->Content = malloc (Attribute->ContentLength + 1)) == NULL) Err_NotEnoughMemoryExit (); - strncpy (Attribute->Content,Gbl.XMLPtr,Attribute->ContentLength); + strncpy (Attribute->Content,XML_Ptr,Attribute->ContentLength); Attribute->Content[Attribute->ContentLength] = '\0'; - Gbl.XMLPtr += Attribute->ContentLength; + XML_Ptr += Attribute->ContentLength; - Gbl.XMLPtr++; + XML_Ptr++; /* End of attribute content ...... ^ | - Gbl.XMLPtr + XML_Ptr */ } } @@ -387,8 +393,8 @@ static void XML_GetAttributes (struct XMLElement *Elem) static void XML_SkipSpaces (void) { - while (isspace ((int) *Gbl.XMLPtr)) - Gbl.XMLPtr++; + while (isspace ((int) *XML_Ptr)) + XML_Ptr++; } /*****************************************************************************/