summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorKasullian <tomkasull@gmail.com>2025-09-08 23:20:06 -0400
committerKasullian <tomkasull@gmail.com>2025-09-08 23:20:06 -0400
commitba6d14a7737ef7cd02b61586e39ca2162df3018d (patch)
treebfca13d4e4dd539fc357583ebc0347ec0c1144d0 /src/util
parentf8b92ce3aa08b1445c9f956d8166830946562d12 (diff)
glew, map parsing for windows
Diffstat (limited to 'src/util')
-rw-r--r--src/util/config/config.cpp103
1 files changed, 79 insertions, 24 deletions
diff --git a/src/util/config/config.cpp b/src/util/config/config.cpp
index 1601afe..6c2d021 100644
--- a/src/util/config/config.cpp
+++ b/src/util/config/config.cpp
@@ -12,20 +12,24 @@ void cfg_seterr( CFG_PARSER* p, const char* fmt, ... ) {
}
inline U8 is_whitespace( char c ) {
- return c == ' ' || c == '\t' || c == '\n';
+ return c == ' ' || c == '\t' || c == '\n' || c == '\r'; // Added \r
}
inline void trim_whitespace( char* buf ) {
- U32 i;
- for( i = 0; !!buf[i]; ++i )
- if( !is_whitespace( buf[i] ) ) break;
- for( U32 i2 = i; !!buf[i2]; ++i2 ) {
- if( is_whitespace( buf[i2] ) ) {
- buf[i2 - i] = 0;
- return;
- }
- buf[i2 - i] = buf[i2];
- }
+ if (!buf || !*buf) return;
+
+ // Find first non-whitespace character
+ U32 start = 0;
+ while( buf[start] && is_whitespace( buf[start] ) ) start++;
+
+ // Find last non-whitespace character
+ U32 end = strlen( buf );
+ while( end > start && is_whitespace( buf[end - 1] ) ) end--;
+
+ // Move string to beginning and null-terminate
+ U32 len = end - start;
+ memmove( buf, buf + start, len );
+ buf[len] = '\0';
}
inline void init_cfg_node( CFG_NODE* node, const char* name, CFG_NODE* parent, U8 type ) {
@@ -179,65 +183,116 @@ void parse_section( CFG_PARSER* parser, CFG_SECTION* current_section ) {
char* next_token;
while( fgets( line, sizeof(line), parser->file ) ) {
- token = strtok( line, " \t\n" );
+ parser->linen++;
+
+ // Remove all whitespace including \r\n
+ trim_whitespace( line );
+
+ // Skip empty lines
+ if( strlen(line) == 0 ) continue;
+
+ // Use a copy for tokenization to preserve original
+ char line_copy[8192];
+ strcpy( line_copy, line );
+
+ token = strtok( line_copy, " \t\r\n" ); // Added \r
if( !token ) continue;
- if( strcmp( token, "{" ) == 0 )
+
+ if( strcmp( token, "{" ) == 0 ) {
continue;
- else if( strcmp( token, "}" ) == 0 ) {
+ } else if( strcmp( token, "}" ) == 0 ) {
return;
} else if( strcmp( token, cfg_types[CFGT_SECTION].def ) == 0 ) {
- next_token = strtok( NULL, " \t\n" );
+ next_token = strtok( NULL, " \t\r\n" );
+ if( !next_token ) {
+ cfg_seterr( parser, "Missing section name at line %d", parser->linen );
+ return;
+ }
+
char sectname[64];
- strcpy( sectname, next_token );
+ strncpy( sectname, next_token, sizeof(sectname) - 1 );
+ sectname[sizeof(sectname) - 1] = '\0';
trim_whitespace( sectname );
CFG_SECTION* new_section = cfg_section_new( sectname, (CFG_NODE*)current_section );
- strtok( NULL, " \t\n" );
+
+ // Look for opening brace
+ char* brace = strtok( NULL, " \t\r\n" );
+ if( !brace || strcmp( brace, "{" ) != 0 ) {
+ cfg_seterr( parser, "Expected '{' after section name at line %d", parser->linen );
+ return;
+ }
+
parse_section( parser, new_section );
} else {
+ // Parse variable declaration
char name[64];
- strcpy( name, token );
+ strncpy( name, token, sizeof(name) - 1 );
+ name[sizeof(name) - 1] = '\0';
trim_whitespace( name );
+ // Find the variable name (between type and =)
token = strtok( NULL, "=[" );
- if( !token )
+ if( !token ) {
+ cfg_seterr( parser, "Invalid variable declaration at line %d", parser->linen );
continue;
+ }
char varname[64];
- strcpy( varname, token );
+ strncpy( varname, token, sizeof(varname) - 1 );
+ varname[sizeof(varname) - 1] = '\0';
trim_whitespace( varname );
+ // Find matching parser
+ bool found = false;
for( I32 i = 0; i < sizeof(cfg_types) / sizeof(CFG_TYPE); ++i ) {
const CFG_TYPE* fn = &cfg_types[i];
- if( strncmp( name, fn->def, strlen( fn->def ) ) == 0 ) {
+ if( strcmp( name, fn->def ) == 0 ) {
+ // Reset strtok to work on original line for parser
+ strcpy( line_copy, line );
+ strtok( line_copy, " \t\r\n" ); // Skip type
+ strtok( NULL, "=[" ); // Skip variable name
+
fn->parser( parser, current_section, varname );
+ found = true;
break;
}
}
+
+ if( !found ) {
+ cfg_seterr( parser, "Unknown type '%s' at line %d", name, parser->linen );
+ return;
+ }
if( parser->iserr ) {
dlog( "parse_section() : %s parse error:\n - %s\n", name, parser->err );
return;
}
}
-
- parser->linen++;
}
}
CFG_SECTION* cfg_load( const char* path ) {
- FILE* f = fopen( path, "rb" );
+ FILE* f = fopen( path, "r" ); // Changed from "rb" to "r" for text mode
if( !f )
return 0;
CFG_PARSER p;
+ memset( &p, 0, sizeof(p) ); // Initialize all fields
p.iserr = 0;
p.file = f;
p.linen = 0;
p.root = cfg_section_new( "root", 0 );
+
parse_section( &p, p.root );
fclose( f );
+
+ if( p.iserr ) {
+ cfg_free( (CFG_NODE*)p.root );
+ return 0;
+ }
+
return p.root;
}