From fa272d9d93662a0afeae09b2467282673149ae71 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Mon, 18 Nov 2024 13:25:00 +0300 Subject: [PATCH] public: add optional feature for COM_ParseFileSafe to ignore shell like hash symbol prefixed comments --- engine/menu_int.h | 1 + engine/mobility_int.h | 1 + public/crtlib.c | 4 +-- public/crtlib.h | 4 ++- public/tests/test_parsefile.c | 64 ++++++++++++++++++++++------------- 5 files changed, 48 insertions(+), 26 deletions(-) diff --git a/engine/menu_int.h b/engine/menu_int.h index abeeb020..8f68c43f 100644 --- a/engine/menu_int.h +++ b/engine/menu_int.h @@ -36,6 +36,7 @@ typedef int HIMAGE; // handle to a graphic // flags for COM_ParseFileSafe #define PFILE_IGNOREBRACKET (1<<0) #define PFILE_HANDLECOLON (1<<1) +#define PFILE_IGNOREHASHCMT (1<<2) typedef struct ui_globalvars_s { diff --git a/engine/mobility_int.h b/engine/mobility_int.h index 71ce05fa..44d4686b 100644 --- a/engine/mobility_int.h +++ b/engine/mobility_int.h @@ -54,6 +54,7 @@ extern "C" { // flags for COM_ParseFileSafe #define PFILE_IGNOREBRACKET (1<<0) #define PFILE_HANDLECOLON (1<<1) +#define PFILE_IGNOREHASHCMT (1<<2) typedef struct mobile_engfuncs_s { diff --git a/public/crtlib.c b/public/crtlib.c index 5fdaebfc..d21c0345 100644 --- a/public/crtlib.c +++ b/public/crtlib.c @@ -758,8 +758,8 @@ skipwhite: data++; } - // skip // comments - if( c == '/' && data[1] == '/' ) + // skip // or #, if requested, comments + if(( c == '/' && data[1] == '/' ) || ( c == '#' && FBitSet( flags, PFILE_IGNOREHASHCMT ))) { while( *data && *data != '\n' ) data++; diff --git a/public/crtlib.h b/public/crtlib.h index 5ea2b48b..7ff487a6 100644 --- a/public/crtlib.h +++ b/public/crtlib.h @@ -39,9 +39,11 @@ enum }; // a1ba: not using BIT macro, so flags can be copypasted into -// exported APIs headers and will get nice warning in case of changing values +// exported APIs headers and will not get warning in case of changing values #define PFILE_IGNOREBRACKET (1<<0) #define PFILE_HANDLECOLON (1<<1) +#define PFILE_IGNOREHASHCMT (1<<2) + #define PFILE_TOKEN_MAX_LENGTH 1024 #define PFILE_FS_TOKEN_MAX_LENGTH 512 diff --git a/public/tests/test_parsefile.c b/public/tests/test_parsefile.c index d7823660..a1201743 100644 --- a/public/tests/test_parsefile.c +++ b/public/tests/test_parsefile.c @@ -1,34 +1,52 @@ #include "crtlib.h" +static const char *test_file = +"q asdf \"qwerty\" \"f \\\"f\" meowmeow\n" +"// comment \"stuff ignored\"\n" +"bark // ignore\n" +"bashlikecomment #notignored test\n" +"#ignore comment\n" +"thisshall #be ignored\n" +"test_sentinel\n"; + int main( void ) { - char *file = (char *)"q asdf \"qwerty\" \"f \\\"f\" meowmeow\n// comment \"stuff ignored\"\nbark"; - int len; - char buf[5]; + int i; + char *file = (char *)test_file; + struct test + { + int bufsize; + const char *expected; + int expected_len; + int flags; + } testdata[] = + { + { 5, "q", 1 }, + { 5, "asdf", 4 }, + { 5, "qwer", -1 }, + { 5, "f \"f", 4 }, + { 5, "meow", -1 }, + { 5, "bark", 4 }, + { 32, "bashlikecomment", 15 }, + { 32, "#notignored", 11 }, + { 32, "test", 4, PFILE_IGNOREHASHCMT }, + { 32, "thisshall", 9, PFILE_IGNOREHASHCMT }, + { 32, "test_sentinel", 13, PFILE_IGNOREHASHCMT }, + }; - file = COM_ParseFileSafe( file, buf, sizeof( buf ), 0, &len, NULL ); - if( !( !Q_strcmp( buf, "q" ) && len == 1 )) - return 1; + for( i = 0; i < sizeof( testdata ) / sizeof( testdata[0] ); i++ ) + { + string buf; + int len; - file = COM_ParseFileSafe( file, buf, sizeof( buf ), 0, &len, NULL ); - if( !( !Q_strcmp( buf, "asdf" ) && len == 4 )) - return 2; + file = COM_ParseFileSafe( file, buf, testdata[i].bufsize, testdata[i].flags, &len, NULL ); - file = COM_ParseFileSafe( file, buf, sizeof( buf ), 0, &len, NULL ); - if( !( !Q_strcmp( buf, "qwer" ) && len == -1 )) - return 3; + if( file == NULL ) + return i; - file = COM_ParseFileSafe( file, buf, sizeof( buf ), 0, &len, NULL ); - if( !( !Q_strcmp( buf, "f \"f" ) && len == 4 )) - return 4; - - file = COM_ParseFileSafe( file, buf, sizeof( buf ), 0, &len, NULL ); - if( !( !Q_strcmp( buf, "meow" ) && len == -1 )) - return 5; - - file = COM_ParseFileSafe( file, buf, sizeof( buf ), 0, &len, NULL ); - if( !( !Q_strcmp( buf, "bark" ) && len == 4 )) - return 6; + if( !( !Q_strcmp( buf, testdata[i].expected ) && len == testdata[i].expected_len )) + return i; + } return 0; }