15 #define YYERROR_VERBOSE 1
17 /* #define YYSTYPE void * */
21 #include "res_parse.h"
23 #ifdef HAVE_LIBDMALLOC
27 static Resource
*parsed_res
;
28 static Resource
*current_res
;
30 int reserror
(const char *);
33 #define f(x) current_res->flags |= x
46 %token
<sval
> STRING INCLUDE
52 : { current_res
= parsed_res
= resource_create
(NULL
); }
57 : { current_res
= resource_create
(current_res
); }
59 { $$
= current_res
; current_res
= current_res
->parent
; }
62 res_item_zm
: res_item res_item_zm |
;
64 : STRING
{ resource_add_val
(current_res
, 0, $1, 0); f
(FLAG_V
); }
65 | STRING
'=' STRING
{ resource_add_val
(current_res
, $1, $3, 0); f
(FLAG_NV
); }
66 | INCLUDE
{ resource_add_val
(current_res
, 0, $1, 0); f
(FLAG_S
); }
67 | res
{ resource_add_val
(current_res
, 0, 0, $1); f
(FLAG_S
); }
68 | STRING
'=' res
{ resource_add_val
(current_res
, $1, 0, $3); f
(FLAG_NS
); }
74 static const char *res_filename
= NULL
;
75 static FILE *res_file
= NULL
;
76 static const char **res_strings
= NULL
;
77 static int res_string_idx
= 0;
81 res_parse_getchars
(char *buf
, int max_size
)
85 int i
= fgetc
(res_file
);
92 if
(res_strings
[0] == 0)
94 buf
[0] = res_strings
[0][res_string_idx
];
108 resource_parse
(const char *filename
, const char **strings
)
113 res_file
= fopen
(filename
, "r");
114 if
(res_file
== NULL
)
119 res_filename
= filename
;
123 res_strings
= strings
;
145 resource_create
(Resource
*parent
)
147 Resource
*rv
= (Resource
*)malloc
(sizeof
(Resource
));
151 rv
->v
= (ResourceVal
*)malloc
(sizeof
(ResourceVal
));
156 resource_add_val
(Resource
*n
, char *name
, char *value
, Resource
*subres
)
158 n
->v
= (ResourceVal
*)realloc
(n
->v
, sizeof
(ResourceVal
)*(n
->c
+1));
159 n
->v
[n
->c
].name
= name
;
160 n
->v
[n
->c
].value
= value
;
161 n
->v
[n
->c
].subres
= subres
;
166 resource_value
(const Resource
*res
, char *name
)
169 if
(res
== 0 || name
== 0)
171 for
(i
=0; i
<res
->c
; i
++)
172 if
(res
->v
[i
].name
&& res
->v
[i
].value
173 && NSTRCMP
(res
->v
[i
].name
, name
) == 0)
174 return res
->v
[i
].value
;
179 resource_subres
(const Resource
*res
, const char *name
)
182 if
(res
== 0 || name
== 0)
184 for
(i
=0; i
<res
->c
; i
++)
185 if
(res
->v
[i
].name
&& res
->v
[i
].subres
186 && NSTRCMP
(res
->v
[i
].name
, name
) == 0)
187 return res
->v
[i
].subres
;
192 reserror
(const char *str
)
194 fprintf
(stderr
, "Error: %s around line %d: %s\n",
195 res_file ? res_filename
: "internal strings",
201 dump_res
(Resource
*n
, int l
)
204 for
(i
=0; i
<n
->c
; i
++)
208 printf
("%*cn[%s] = {\n", l
, ' ', n
->v
[i
].name? n
->v
[i
].name
:"");
209 dump_res
(n
->v
[i
].subres
, l
+3);
210 printf
("%*c}\n", l
, ' ');
213 printf
("%*cn[%s] = v[%s]\n", l
, ' ', n
->v
[i
].name? n
->v
[i
].name
:"", n
->v
[i
].value? n
->v
[i
].value
:"");
218 resource_dump
(Resource
*r
)