37 struct nidstable
*result
;
41 struct nidinfo currnid
;
45 void nids_free (struct nidstable
*nids
)
52 hashpool_destroy (nids
->pool
);
55 fixedpool_destroy (nids
->infopool
, NULL
, NULL
);
56 nids
->infopool
= NULL
;
62 void start_hndl (void *data
, const char *el
, const char **attr
)
64 struct xml_data
*d
= (struct xml_data
*) data
;
66 d
->last
= XMLE_DEFAULT
;
68 if (strcmp (el
, "LIBRARY") == 0) {
69 d
->scope
= XMLS_LIBRARY
;
70 } else if (strcmp (el
, "NAME") == 0) {
72 } else if (strcmp (el
, "FUNCTION") == 0) {
73 d
->currnid
.isvariable
= 0;
74 d
->scope
= XMLS_FUNCTION
;
75 } else if (strcmp (el
, "VARIABLE") == 0) {
76 d
->currnid
.isvariable
= 1;
77 d
->scope
= XMLS_VARIABLE
;
78 } else if (strcmp (el
, "NID") == 0) {
80 } else if (strcmp (el
, "NUMARGS") == 0) {
81 d
->last
= XMLE_NUMARGS
;
86 void end_hndl (void *data
, const char *el
)
88 struct xml_data
*d
= (struct xml_data
*) data
;
90 d
->last
= XMLE_DEFAULT
;
92 if (strcmp (el
, "LIBRARY") == 0) {
95 } else if (strcmp (el
, "FUNCTION") == 0 || strcmp (el
, "VARIABLE") == 0) {
96 d
->scope
= XMLS_LIBRARY
;
97 if (d
->currnid
.name
&& d
->currnid
.nid
&& d
->curlib
) {
98 struct nidinfo
*info
= hashtable_searchhash (d
->curlib
, NULL
, NULL
, d
->currnid
.nid
);
100 if (strcmp (info
->name
, d
->currnid
.name
)) {
101 error (__FILE__
": NID `0x%08X' repeated in library `%s'", d
->currnid
.nid
, d
->libname
);
105 info
= fixedpool_alloc (d
->result
->infopool
);
106 memcpy (info
, &d
->currnid
, sizeof (struct nidinfo
));
107 hashtable_inserthash (d
->curlib
, NULL
, info
, d
->currnid
.nid
);
110 error (__FILE__
": missing function or variable definition");
113 d
->currnid
.name
= NULL
;
115 d
->currnid
.numargs
= -1;
120 const char *dup_string (struct xml_data
*d
, const char *txt
, size_t len
)
124 result
= &d
->result
->buffer
[d
->buffer_pos
];
125 memcpy (result
, txt
, len
);
127 d
->buffer_pos
+= len
+ 1;
129 return (const char *) result
;
133 void char_hndl (void *data
, const char *txt
, int txtlen
)
135 struct xml_data
*d
= (struct xml_data
*) data
;
140 if (d
->last
== XMLE_NAME
) {
141 if (d
->currnid
.name
) {
142 error (__FILE__
": repeated name in function/variable");
145 d
->currnid
.name
= dup_string (d
, txt
, txtlen
);
147 } else if (d
->last
== XMLE_NID
|| d
->last
== XMLE_NUMARGS
) {
150 if (txtlen
> sizeof (buffer
) - 1)
151 txtlen
= sizeof (buffer
) - 1;
152 memcpy (buffer
, txt
, txtlen
);
153 buffer
[txtlen
] = '\0';
155 if (d
->last
== XMLE_NID
) {
156 if (d
->currnid
.nid
) {
157 error (__FILE__
": nid repeated in function/variable");
161 sscanf (buffer
, "0x%X", &d
->currnid
.nid
);
164 d
->currnid
.numargs
= -1;
165 sscanf (buffer
, "%d", &d
->currnid
.numargs
);
170 if (d
->last
== XMLE_NAME
) {
171 d
->libname
= dup_string (d
, txt
, txtlen
);
173 error (__FILE__
": current lib is not null");
176 d
->curlib
= hashtable_search (d
->result
->libs
, (void *) d
->libname
, NULL
);
178 d
->curlib
= hashtable_alloc (d
->result
->pool
, 128, NULL
, &hashtable_pointer_compare
);
179 hashtable_insert (d
->result
->libs
, (void *) d
->libname
, d
->curlib
);
187 struct nidstable
*nids_load (const char *xmlpath
)
190 struct xml_data data
;
194 buf
= read_file (xmlpath
, &size
);
199 p
= XML_ParserCreate (NULL
);
201 error (__FILE__
": can't create XML parser");
209 data
.currnid
.name
= NULL
;
210 data
.currnid
.nid
= 0;
211 data
.currnid
.numargs
= 0;
212 data
.scope
= XMLS_LIBRARY
;
213 data
.last
= XMLE_DEFAULT
;
216 (struct nidstable
*) xmalloc (sizeof (struct nidstable
));
218 hashpool_create (256, 8192);
220 hashtable_alloc (data
.result
->pool
, 32, &hashtable_hash_string
,
221 &hashtable_string_compare
);
222 data
.result
->infopool
= fixedpool_create (sizeof (struct nidinfo
), 8192, 0);
225 data
.result
->buffer
= buf
;
226 buf
= xmalloc (size
);
228 memcpy (buf
, data
.result
->buffer
, size
);
230 XML_SetUserData (p
, (void *) &data
);
231 XML_SetElementHandler (p
, &start_hndl
, &end_hndl
);
232 XML_SetCharacterDataHandler (p
, &char_hndl
);
234 if (!XML_Parse (p
, buf
, size
, 1)) {
235 error (__FILE__
": parse error at line %d:\n %s\n", XML_GetCurrentLineNumber (p
),
236 XML_ErrorString (XML_GetErrorCode (p
)));
244 nids_free (data
.result
);
252 void print_level2 (void *key
, void *value
, unsigned int hash
, void *arg
)
254 struct nidinfo
*info
= value
;
255 report (" NID: 0x%08X ", info
->nid
);
256 if (info
->isvariable
) report ("(Variable)");
257 else report ("Num args: %d", info
->numargs
);
258 report (" Name: %s\n", info
->name
);
262 void print_level1 (void *key
, void *value
, unsigned int hash
, void *arg
)
264 hashtable lib
= (hashtable
) value
;
265 report (" %s:\n", (char *) key
);
266 hashtable_traverse (lib
, &print_level2
, NULL
);
270 void nids_print (struct nidstable
*nids
)
272 report ("Libraries:\n");
273 hashtable_traverse (nids
->libs
, &print_level1
, NULL
);
277 struct nidinfo
*nids_find (struct nidstable
*nids
, const char *library
, unsigned int nid
)
281 lib
= hashtable_search (nids
->libs
, (void *) library
, NULL
);
282 if (lib
) return hashtable_searchhash (lib
, NULL
, NULL
, nid
);
288 int main (int argc
, char **argv
)
290 struct nidstable
*nids
= NULL
;
292 nids
= nids_load (argv
[1]);
300 #endif /* TEST_NIDS */