/** * Copyright (c) 2021 Dominic Masters * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ #include "xml.h" void xmlParseElement(xmlnode_t *node, char *string) { char c; int32_t i, j; uint8_t state; node->attributeCount = 0; i = 0; state = XML_STATE_NOTHING; while(c = string[i++]) { switch(state) { case XML_STATE_NOTHING: if(c != '<') continue; node->start = string + (i - 1); state = XML_STATE_PARSING_NAME; break; case XML_STATE_PARSING_NAME: if(c == ' ' || c == '\n' || c == '\r') continue; j = i - 1; while(c = string[j++]) { if(c == ' ') break; node->name[j] = c; } i = j; state = XML_STATE_PARSING_ATTRIBUTES; break; case XML_STATE_PARSING_ATTRIBUTES: if(c == ' ' || c == '\n' || c == '\r') continue; if(c == '>') { node->internal = string + i; break; continue; } // Parse Name node->attributeNames[node->attributeCount] = string + (i - 1); node->attributeNameLengths[node->attributeCount] = 0; while(c == ' ' && c == '\n' && c == '\r' && c != '>' && c != '=' && c != '\0') { c = string[i++]; node->attributeNameLengths[node->attributeCount]++; } if(c == '>') { i--; node->attributeValues[node->attributeCount] = NULL; node->attributeValueLengths[node->attributeCount] = 0; node->attributeCount++; continue; } // Wait for = sign while(c == ' ' || c == '\n' || c == '\r') c = string[i++]; // Handle booleans if(c != '=') { node->attributeValues[node->attributeCount] = NULL; node->attributeValueLengths[node->attributeCount] = 0; node->attributeCount++; i--; continue; } node->attributeValues[node->attributeCount] = string + i; node->attributeValueLengths[node->attributeCount] = 0; do { c = string[i++]; node->attributeNameLengths[node->attributeCount]++; if(c == '\0' || c == '"') break; if(c == '\\') { i++; node->attributeNameLengths[node->attributeCount]++; } } while(c); node->attributeCount++; break; default: break; } } }