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

View File

@ -47,4 +47,12 @@ void fileMkdirp(char *path) {
buffer[i] = '\0';
_mkdir(buffer);
}
}
void assetReadString(FILE *file, char *buffer) {
size_t length;
fseek(file, 0, SEEK_END);// Seek to the end
length = ftell(file);// Get our current position (the end)
fseek(file, 0, SEEK_SET);// Reset the seek
fread(buffer, 1, length, file);// Read all the bytes
}

View File

@ -19,4 +19,6 @@
void fileNormalizeSlashes(char *string);
void fileMkdirp(char *path);
void fileMkdirp(char *path);
void assetReadString(FILE *file, char *buffer);

8
tools/utils/image.h Normal file
View File

@ -0,0 +1,8 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once

94
tools/utils/xml.c Normal file
View File

@ -0,0 +1,94 @@
/**
* 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;
}
}
}

34
tools/utils/xml.h Normal file
View File

@ -0,0 +1,34 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "common.h"
#include "file.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
typedef struct {
char *start;
char *internal;
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);