2 * udibuild - UDI Compilation Utility
3 * - By John Hodge (thePowersGang)
6 * - udiprops.txt parsing (for udibuild only)
14 #include "include/udiprops.h"
17 static int _get_token_sym(const char *str, const char **outstr, ...)
20 va_start(args, outstr);
22 for( int idx = 0; (sym = va_arg(args, const char *)); idx ++ )
24 size_t len = strlen(sym);
25 if( memcmp(str, sym, len) != 0 )
27 if( str[len] && !isspace(str[len]) )
32 while( isspace(**outstr) )
38 const char *end = str;
39 while( !isspace(*end) )
41 // fprintf(stderr, "udiprops: Unknown token '%.*s'\n", end-str, str);
47 static void rtrim(char *str)
52 while( pos != str && isspace(pos[-1]) )
56 static char *my_strdup(const char *str)
58 char *ret = malloc(strlen(str)+1);
59 return strcpy(ret, str);
62 tUdiprops *Udiprops_LoadBuild(const char *Filename)
64 FILE *fp = fopen(Filename, "r");
66 perror("Udiprops_LoadBuild");
70 char *current_compile_opts = NULL;
72 tUdiprops *ret = calloc( 1, sizeof(tUdiprops) );
75 while( fgets(buf, sizeof(buf)-1, fp) )
79 char *hash = strchr(str, '#');
80 if( hash ) *hash = '\0';
83 if( !str[0] ) continue ;
85 int sym = _get_token_sym(str, (const char**)&str,
86 "source_files", "compile_options", "source_requires",
91 case 0: // source_files
92 for(char *file = strtok(str, " \t"); file; file = strtok(NULL, " \t") )
94 tUdiprops_Srcfile *srcfile = malloc(sizeof(tUdiprops_Srcfile)+strlen(file)+1);
95 srcfile->CompileOpts = current_compile_opts;
96 srcfile->Filename = (void*)(srcfile+1);
97 strcpy((char*)srcfile->Filename, file);
100 ret->SourceFiles = realloc(ret->SourceFiles, n_srcfiles*sizeof(void*));
101 ret->SourceFiles[n_srcfiles-1] = srcfile;
104 case 1: // compile_options
105 current_compile_opts = my_strdup(str);
107 case 2: // source_requires
108 // TODO: Use source_requires
111 ret->ModuleName = my_strdup(str);
116 ret->nSourceFiles = n_srcfiles;
118 // "Intentional" memory leak
119 // - current_compile_opts not freed, and shared between srcfiles
120 // - If two compile_options statements appear in a row, one is definitely leaked