Fixed some bugs and added parsing

This commit is contained in:
2023-06-16 15:48:33 -07:00
parent 2964aa4a95
commit e6350d99e9
17 changed files with 157 additions and 42 deletions

View File

@ -31,6 +31,10 @@ void Xml::load(Xml *xml, std::string data, size_t *j) {
size_t i = *j;
while(c = data[i++]) {
if(insideTag) {
xml->outerXml += c;
}
switch(doing) {
case XML_PARSE_STATE_DOING_NOTHING:
if(c == '>') continue;
@ -49,15 +53,23 @@ void Xml::load(Xml *xml, std::string data, size_t *j) {
Xml::load(child, data, &i);
xml->children.push_back(child);
doing = XML_PARSE_STATE_PARSING_CHILD;
// Remove last char since we kinda already parsed it.
xml->innerXml += child->outerXml;
xml->outerXml = xml->outerXml.substr(0, xml->outerXml.size()-1);
xml->outerXml += child->outerXml;
}
} else {
doing = XML_PARSE_STATE_PARSING_TAG_NAME;
level++;
insideTag = true;
xml->outerXml += c;
}
continue;
}
xml->innerXml += c;
if(Xml::isWhitespace(c)) continue;
doing = XML_PARSE_STATE_PARSING_VALUE;
buffer += c;
@ -123,7 +135,9 @@ void Xml::load(Xml *xml, std::string data, size_t *j) {
case XML_PARSE_STATE_PARSING_ATTRIBUTE_VALUE:
// Parse the attribute value until we find a quote mark.
if(c == '"') {
if(c == '\\') {
c = data[i++];
} else if(c == '"') {
doing = XML_PARSE_STATE_LOOKING_FOR_ATTRIBUTE;
xml->attributes[attrKey] = buffer;
buffer = "";
@ -144,6 +158,8 @@ void Xml::load(Xml *xml, std::string data, size_t *j) {
continue;
}
xml->innerXml += c;
if(Xml::isWhitespace(c)) {
if(!valueIsInWhitespace) {
bufferWhitespaces.clear();
@ -183,9 +199,16 @@ void Xml::load(Xml *xml, std::string data, size_t *j) {
i -= 1;
Xml::load(child, data, &i);
xml->children.push_back(child);
xml->innerXml += child->outerXml;
xml->outerXml = xml->outerXml.substr(0, xml->outerXml.size()-1);
xml->outerXml += child->outerXml;
}
if(Xml::isWhitespace(c)) continue;
if(Xml::isWhitespace(c)) {
xml->innerXml += c;
continue;
}
// In HTML Spec there's a chance for there to be a value here, but not
// in the XML spec.

View File

@ -32,6 +32,8 @@ namespace Dawn {
std::string node;
std::string value;
std::string innerXml;
std::string outerXml;
std::map<std::string, std::string> attributes;
std::vector<Xml*> children;

View File

@ -12,6 +12,13 @@ namespace Dawn {
};
static inline std::string stringParser(std::string v, std::string *error) {
// Replace slashes and quotes
v = stringReplaceAll(v, "\\", "\\\\\\");
v = stringReplaceAll(v, "\"", "\\\"");
// Newlines.
v = stringReplaceAll(v, "\n", "\\n");
return "\"" + v + "\"";
};

View File

@ -125,4 +125,26 @@ static inline std::string stringToLowercase(const std::string &str) {
}
);
return data;
}
/**
* Replace all instances of a string with another string within a string.
*
* @param str String to replace the contents of.
* @param needle Needle to look for.
* @param replace String to replace the needle with.
* @return A new string instance with the replacements made.
*/
static inline std::string stringReplaceAll(
const std::string &str,
const std::string &needle,
const std::string &replace
) {
std::string newString = str;
size_t startPos = 0;
while((startPos = newString.find(needle, startPos)) != std::string::npos) {
newString.replace(startPos, needle.length(), replace);
startPos += replace.length();
}
return newString;
}