17 #define YYERROR_VERBOSE 1
19 /* #define YYSTYPE void * */
23 #include "res_parse.h"
25 #ifdef HAVE_LIBDMALLOC
32 static Resource
*parsed_res
;
33 static Resource
*current_res
;
38 #define f(x) current_res->flags |= x
51 %token
<sval
> STRING INCLUDE
57 : { current_res
= parsed_res
= resource_create
(NULL
); }
62 : { current_res
= resource_create
(current_res
); }
64 { $$
= current_res
; current_res
= current_res
->parent
; }
67 res_item_zm
: res_item res_item_zm |
;
69 : STRING
{ resource_add_val
(current_res
, 0, $1, 0); f
(FLAG_V
); }
70 | STRING
'=' STRING
{ resource_add_val
(current_res
, $1, $3, 0); f
(FLAG_NV
); }
71 | INCLUDE
{ resource_add_val
(current_res
, 0, $1, 0); f
(FLAG_S
); }
72 | res
{ resource_add_val
(current_res
, 0, 0, $1); f
(FLAG_S
); }
73 | STRING
'=' res
{ resource_add_val
(current_res
, $1, 0, $3); f
(FLAG_NS
); }
79 static char *res_filename
= 0;
80 static FILE *res_file
= 0;
81 static const char **res_strings
= 0;
82 static int res_string_idx
= 0;
86 res_parse_getchars
(char *buf
, int max_size
)
90 int i
= fgetc
(res_file
);
97 if
(res_strings
[0] == 0)
99 buf
[0] = res_strings
[0][res_string_idx
];
113 resource_parse
(char *filename
, const char **strings
)
118 res_filename
= filename
;
119 res_file
= fopen
(filename
, "r");
120 if
(res_file
== NULL
)
129 res_strings
= strings
;
147 resource_create
(Resource
*parent
)
149 Resource
*rv
= (Resource
*)malloc
(sizeof
(Resource
));
153 rv
->v
= (ResourceVal
*)malloc
(sizeof
(ResourceVal
));
158 resource_add_val
(Resource
*n
, char *name
, char *value
, Resource
*subres
)
160 n
->v
= (ResourceVal
*)realloc
(n
->v
, sizeof
(ResourceVal
)*(n
->c
+1));
161 n
->v
[n
->c
].name
= name
;
162 n
->v
[n
->c
].value
= value
;
163 n
->v
[n
->c
].subres
= subres
;
168 resource_value
(Resource
*res
, char *name
)
171 if
(res
== 0 || name
== 0)
173 for
(i
=0; i
<res
->c
; i
++)
174 if
(res
->v
[i
].name
&& res
->v
[i
].value
175 && NSTRCMP
(res
->v
[i
].name
, name
) == 0)
176 return res
->v
[i
].value
;
181 resource_subres
(Resource
*res
, char *name
)
184 if
(res
== 0 || name
== 0)
186 for
(i
=0; i
<res
->c
; i
++)
187 if
(res
->v
[i
].name
&& res
->v
[i
].subres
188 && NSTRCMP
(res
->v
[i
].name
, name
) == 0)
189 return res
->v
[i
].subres
;
196 fprintf
(stderr
, "Error: %s around line %d: %s\n",
197 res_file ? res_filename
: "internal strings",
203 dump_res
(Resource
*n
, int l
)
206 for
(i
=0; i
<n
->c
; i
++)
210 printf
("%*cn[%s] = {\n", l
, ' ', n
->v
[i
].name? n
->v
[i
].name
:"");
211 dump_res
(n
->v
[i
].subres
, l
+3);
212 printf
("%*c}\n", l
, ' ');
215 printf
("%*cn[%s] = v[%s]\n", l
, ' ', n
->v
[i
].name? n
->v
[i
].name
:"", n
->v
[i
].value? n
->v
[i
].value
:"");
220 resource_dump
(Resource
*r
)