I hate my life

This commit is contained in:
2021-11-05 10:55:10 -07:00
parent f6ea081b2d
commit 715ecd3a73
16 changed files with 522 additions and 86 deletions

140
src/file/xml2.c Normal file
View File

@ -0,0 +1,140 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "xml2.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 == ' ' || c == '\n' || c == '\r' || c == '>') break;
node->name[j - i] = c;
}
node->name[j-i] = '\0';
i = j - 1;
state = XML_STATE_PARSING_ATTRIBUTES;
break;
case XML_STATE_PARSING_ATTRIBUTES:
if(c == ' ' || c == '\n' || c == '\r') continue;
if(c == '>') {
node->internalStart = string + i;
state = XML_STATE_DONE;
break;
}
// 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++;
continue;
}
// Skip the "
i++;
node->attributeValues[node->attributeCount] = string + i;
node->attributeValueLengths[node->attributeCount] = 0;
do {
c = string[i++];
if(c == '\0' || c == '"') break;
if(c == '\\') {
i++;
node->attributeValueLengths[node->attributeCount]++;
}
node->attributeValueLengths[node->attributeCount]++;
} while(c);
node->attributeCount++;
break;
default:
break;
}
}
}
void xmlGetAttributeName(xmlnode_t *xml, uint8_t attribute, char *buffer) {
int32_t i;
buffer[0] = '\0';
for(i = 0; i < xml->attributeNameLengths[attribute]; i++) {
buffer[i] = xml->attributeNames[attribute][i];
}
buffer[xml->attributeNameLengths[attribute]] = '\0';
}
void xmlGetAttributeValue(xmlnode_t *xml, uint8_t attribute, char *buffer) {
int32_t i;
buffer[0] = '\0';
for(i = 0; i < xml->attributeValueLengths[attribute]; i++) {
buffer[i] = xml->attributeValues[attribute][i];
}
buffer[xml->attributeValueLengths[attribute]] = '\0';
}
uint8_t xmlGetAttributeByName(xmlnode_t *xml, char *name) {
uint8_t i;
int32_t len = strlen(name);
for(i = 0; i < xml->attributeCount; i++) {
if(xml->attributeNameLengths[i] != len) continue;
if(memcmp(name, xml->attributeNames[i], len) == 0) return i;
}
return 0xFF;
}
void xmlFindChildNode(xmlnode_t *node, char** start, char** end) {
char c;
int32_t i;
i = 0;
while(c = node->internalStart[i++]) {
if(c == '<') break;
}
*start = node->internalStart + i - 1;
// Now the part I am dreading.
}

42
src/file/xml2.h Normal file
View File

@ -0,0 +1,42 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "../libs.h"
#define XML_NODE_CHILD_MAX 32
#define XML_NODE_NAME_MAX 32
#define XML_NODE_ATTRIBUTES_MAX 32
#define XML_STATE_NOTHING 0x00
#define XML_STATE_PARSING_NAME 0x01
#define XML_STATE_PARSING_ATTRIBUTES 0x02
#define XML_STATE_DONE 0x03
typedef struct {
char *start;
char *internalStart;
char name[XML_NODE_NAME_MAX];
char *attributeNames[XML_NODE_ATTRIBUTES_MAX];
uint8_t attributeNameLengths[XML_NODE_ATTRIBUTES_MAX];
char *attributeValues[XML_NODE_ATTRIBUTES_MAX];
uint8_t attributeValueLengths[XML_NODE_ATTRIBUTES_MAX];
uint8_t attributeCount;
} xmlnode_t;
void xmlParseElement(xmlnode_t *node, char *string);
void xmlGetAttributeName(xmlnode_t *xml, uint8_t attribute, char *buffer);
void xmlGetAttributeValue(xmlnode_t *xml, uint8_t attribute, char *buffer);
uint8_t xmlGetAttributeByName(xmlnode_t *xml, char *name);
void xmlFindChildNode(xmlnode_t *node, char** start, char** end);

View File

@ -7,7 +7,19 @@
#include "game.h"
bool sandboxGameInit(sandboxgame_t *game) {
xmlnode_t node;
char *string = "<xml attribute=\"value\" something=\"else\"><child>Hello World</child></xml>";
char bufferTest[64];
xmlParseElement(&node, string;
xmlGetAttributeValue(&node, xmlGetAttributeByName(&node, "something"), bufferTest);
char *start = xmlFindChildNode(&node);
quadInit(&game->quad, 0, 0,0,0,0, 500,500,1,1);
assetManagerInit(&game->manager);

View File

@ -17,6 +17,8 @@
#include "../../ui/breakpoint.h"
#include "../../file/assetmanager.h"
#include "../../file/xml2.h"
typedef struct {
engine_t engine;
shader_t shader;