fixed instance loading forget to append basepath.. other path fixes (windows)
[csRadar.git] / vdf.h
diff --git a/vdf.h b/vdf.h
index ddfc2d34300cc87751183c45dc64bec543439a95..fb242d13cd71239d34519ce583e9c2cf05cf0292 100644 (file)
--- a/vdf.h
+++ b/vdf.h
@@ -1,17 +1,21 @@
+// This software is not affiliated with Valve Corporation
+//   We are not affiliated, associated, authorized, endorsed by, or in any way officially 
+//   connected with Valve Corporation, or any of its subsidiaries or its affiliates. 
+// 
+//   All trademarks are property of their respective owners
+
+// VDF aka Keyvalue text format parser / writer
+
 #define vdf_foreach( NODE, STR, AS ) \
-int __vdf_it_##__LINE__ = 0; \
+int __vdf_it_##AS = 0; \
 vdf_node * AS;\
-while( (AS = vdf_next( NODE, STR, &__vdf_it_##__LINE__ )) )
+while( (AS = vdf_next( NODE, STR, &__vdf_it_##AS )) )
 
 #define kv_foreach( NODE, STR, AS ) \
-int __kv_it_##__LINE__ = 0; \
+int __kv_it_##AS = 0; \
 const char * AS;\
-while( (AS = kv_iter( NODE, STR, &__kv_it_##__LINE__ )) )
+while( (AS = kv_iter( NODE, STR, &__kv_it_##AS )) )
 
-#include <stdio.h>
-#include <stdint.h>
-#include <string.h>
-#include <ctype.h>
 
 // TYPES
 // ==================================================================================================================
@@ -63,6 +67,7 @@ void kv_double_array( vdf_node *node, const char *key, u32 count, double *arr );
 
 // INTERNAL API
 // ==================================================================================================================
+#ifdef VALVE_IMPLEMENTATION
 
 // Add keyvalue pair to node
 void vdf_kv_append( vdf_node *p, const char *k, const char *v );
@@ -86,6 +91,8 @@ void vdf_parse_feedbuffer( vdf_ctx *ctx, char *buf );
 // Formatting
 void vdf_out_indent( const int n, FILE *file );
 
+#endif
+
 // IMPLEMENTATION
 // ==================================================================================================================
 
@@ -103,8 +110,13 @@ struct vdf_node
        
        vdf_node **nodes;
        vdf_kv  *pairs;
+       
+       u32             user;
+       u32             user1;
 };
 
+#ifdef VALVE_IMPLEMENTATION
+
 vdf_node *vdf_next( vdf_node *node, const char *name, int *it )
 {      
        if( !node ) 
@@ -112,7 +124,7 @@ vdf_node *vdf_next( vdf_node *node, const char *name, int *it )
 
        for( int i = it? *it: 0; i < csr_sb_count( node->nodes ); i ++ )
        {
-               if( !strcmp( name, node->nodes[i]->name ))
+               if( !name || !strcmp( name, node->nodes[i]->name ))
                {
                        if( it ) *it = i+1;
                        return node->nodes[i];
@@ -417,7 +429,7 @@ void vdf_parse_string( vdf_ctx *ctx )
                
                if( vdf_line_control( ctx ) )
                {
-                       fprintf( stderr, "Unexpected end of line character (Line: %u)\n", ctx->lines );
+                       log_error( "Unexpected end of line character (Line: %u)\n", ctx->lines );
                        return;
                }
        
@@ -431,7 +443,7 @@ int vdf_parse_structure( vdf_ctx *ctx )
        {
                if( ctx->st.tokens[0] || !ctx->st.expect_decl )
                {
-                       fprintf( stderr, "Unexpected token '{' (Line: %u)\n", ctx->lines );
+                       log_error( "Unexpected token '{' (Line: %u)\n", ctx->lines );
                        ctx->errors ++;
                }
                
@@ -447,7 +459,7 @@ int vdf_parse_structure( vdf_ctx *ctx )
        {
                if( !ctx->st.pnode->parent )
                {
-                       fprintf( stderr, "Unexpected token '}' (Line: %u)\n", ctx->lines );
+                       log_error( "Unexpected token '}' (Line: %u)\n", ctx->lines );
                        ctx->errors ++;
                }
                else
@@ -468,7 +480,7 @@ void vdf_parse_begin_token( vdf_ctx *ctx, char *ptr )
 
        if( ctx->st.expect_decl )
        {
-               fprintf( stderr, "Unexpected token '%s' (Line: %u)\n", ctx->name, ctx->lines );
+               log_error( "Unexpected token '%s' (Line: %u)\n", ctx->name, ctx->lines );
                ctx->errors ++;
        }
 }
@@ -534,24 +546,37 @@ void vdf_parse_feedbuffer( vdf_ctx *ctx, char *buf )
        }
 }
 
-vdf_node *vdf_open_file( const char *fn )
-{      
+int vdf_load_into( const char *fn, vdf_node *node )
+{
        char *text_src = csr_textasset_read( fn );
        
        if( !text_src )
        {
-               fprintf( stderr, "vdf open failed\n" );
-               return NULL;
+               return 0;
        }
        
        vdf_ctx ctx = {0};
-       ctx.root = ctx.st.pnode = vdf_create_node( NULL, NULL );
+       ctx.root = ctx.st.pnode = node;
        
        vdf_newln( &ctx );
        vdf_parse_feedbuffer( &ctx, text_src );
-       
        free( text_src );
-       return ctx.root;
+       
+       return 1;
+}
+
+vdf_node *vdf_open_file( const char *fn )
+{      
+       vdf_node *root = vdf_create_node( NULL, NULL );
+       if( vdf_load_into( fn, root ) )
+       {
+               return root;
+       }
+       else
+       {
+               vdf_free_r( root );
+               return NULL;
+       }
 }
 
 // OUTPUT
@@ -600,3 +625,5 @@ void vdf_print( vdf_node *node )
 {
        vdf_out( node, -1, 0, stdout );
 }
+
+#endif