c1ddf1c003d7dcc4865139f30ada26ac53de8f20
[tpg/acess2.git] / Tools / udibuild / src / udiprops.c
1 /*
2  * udibuild - UDI Compilation Utility
3  * - By John Hodge (thePowersGang)
4  *
5  * udiprops.c
6  * - udiprops.txt parsing (for udibuild only)
7  */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <assert.h>
12 #include <stdarg.h>
13 #include <ctype.h>
14 #include "include/udiprops.h"
15
16 // === CODE ===
17 static int _get_token_sym(const char *str, const char **outstr, ...)
18 {
19         va_list args;
20         va_start(args, outstr);
21         const char *sym;
22         for( int idx = 0; (sym = va_arg(args, const char *)); idx ++ )
23         {
24                 size_t len = strlen(sym);
25                 if( memcmp(str, sym, len) != 0 )
26                         continue ;
27                 if( str[len] && !isspace(str[len]) )
28                         continue ;
29                 
30                 // Found it!
31                 *outstr = str + len;
32                 while( isspace(**outstr) )
33                         (*outstr) ++;
34                 return idx;
35         }
36         va_end(args);
37
38         const char *end = str;
39         while( !isspace(*end) )
40                 end ++;
41 //      fprintf(stderr, "udiprops: Unknown token '%.*s'\n", end-str, str);
42
43         *outstr = NULL;
44         return -1;
45 }
46
47 static void rtrim(char *str)
48 {
49         char *pos = str;
50         while( *pos )
51                 pos ++;
52         while( pos != str && isspace(pos[-1]) )
53                 *--pos = '\0';
54 }
55
56 tUdiprops *Udiprops_LoadBuild(const char *Filename)
57 {
58         FILE *fp = fopen(Filename, "r");
59         if( !fp ) {
60                 perror("Udiprops_LoadBuild");
61                 return NULL;
62         }
63
64         char    *current_compile_opts = NULL;   
65          int    n_srcfiles = 0;
66         tUdiprops *ret = calloc( 1, sizeof(tUdiprops) );
67
68         char buf[512];
69         while( fgets(buf, sizeof(buf)-1, fp) )
70         {
71                 char *str = buf;
72                 {
73                         char *hash = strchr(str, '#');
74                         if( hash )      *hash = '\0';
75                 }
76                 rtrim(str);
77                 if( !str[0] )   continue ;
78                 
79                 int sym = _get_token_sym(str, (const char**)&str,
80                         "source_files", "compile_options", "source_requires", NULL);
81                 switch(sym)
82                 {
83                 case 0: // source_files
84                         for(char *file = strtok(str, " \t"); file; file = strtok(NULL, " \t") )
85                         {
86                                 tUdiprops_Srcfile *srcfile = malloc(sizeof(tUdiprops_Srcfile)+strlen(file)+1);
87                                 srcfile->CompileOpts = current_compile_opts;
88                                 srcfile->Filename = (void*)(srcfile+1);
89                                 strcpy((char*)srcfile->Filename, file);
90                                 
91                                 n_srcfiles ++;
92                                 ret->SourceFiles = realloc(ret->SourceFiles, (n_srcfiles+1)*sizeof(void*));
93                                 ret->SourceFiles[n_srcfiles-1] = srcfile;
94                                 ret->SourceFiles[n_srcfiles] = NULL;
95                         }
96                         break;
97                 case 1: // compile_options
98                         current_compile_opts = malloc(strlen(str)+1);
99                         strcpy(current_compile_opts, str);
100                         break;
101                 case 2: // source_requires
102                         // TODO: Use source_requires
103                         break;
104                 }
105         }
106         
107         // "Intentional" memory leak
108         // - current_compile_opts not freed, and shared between srcfiles
109         // - If two compile_options statements appear in a row, one is definitely leaked
110
111         fclose(fp);
112         return ret;
113 }
114

UCC git Repository :: git.ucc.asn.au