34 struct nidstable
*result
;
43 void nids_free (struct nidstable
*nids
)
46 if (nids
->buffer
) free (nids
->buffer
);
48 if (nids
->pool
) hashpool_destroy (nids
->pool
);
55 void start_hndl (void *data
, const char *el
, const char **attr
)
57 struct xml_data
*d
= (struct xml_data
*) data
;
59 d
->last
= XMLE_DEFAULT
;
61 if (strcmp (el
, "LIBRARY") == 0) {
62 d
->scope
= XMLS_LIBRARY
;
63 } else if (strcmp (el
, "NAME") == 0) {
65 } else if (strcmp (el
, "FUNCTION") == 0) {
66 d
->scope
= XMLS_FUNCTION
;
67 } else if (strcmp (el
, "VARIABLE") == 0) {
68 d
->scope
= XMLS_VARIABLE
;
69 } else if (strcmp (el
, "NID") == 0) {
75 void end_hndl (void *data
, const char *el
)
77 struct xml_data
*d
= (struct xml_data
*) data
;
79 d
->last
= XMLE_DEFAULT
;
81 if (strcmp (el
, "LIBRARY") == 0) {
84 } else if (strcmp (el
, "FUNCTION") == 0 || strcmp (el
, "VARIABLE") == 0) {
85 d
->scope
= XMLS_LIBRARY
;
86 if (d
->name
&& d
->nid
&& d
->curlib
) {
87 const char *name
= hashtable_searchhash (d
->curlib
, NULL
, NULL
, d
->nid
);
89 if (strcmp (name
, d
->name
)) {
90 error (__FILE__
": NID `0x%08X' repeated in library `%s'", d
->nid
, d
->libname
);
94 hashtable_inserthash (d
->curlib
, NULL
, (void *) d
->name
, d
->nid
);
97 error (__FILE__
": missing function or variable definition");
106 const char *dup_string (struct xml_data
*d
, const char *txt
, size_t len
)
110 result
= &d
->result
->buffer
[d
->buffer_pos
];
111 memcpy (result
, txt
, len
);
113 d
->buffer_pos
+= len
+ 1;
115 return (const char *) result
;
119 void char_hndl (void *data
, const char *txt
, int txtlen
)
121 struct xml_data
*d
= (struct xml_data
*) data
;
126 if (d
->last
== XMLE_NAME
) {
128 error (__FILE__
": repeated name in function/variable");
131 d
->name
= dup_string (d
, txt
, txtlen
);
133 } else if (d
->last
== XMLE_NID
) {
136 if (txtlen
> sizeof (buffer
) - 1)
137 txtlen
= sizeof (buffer
) - 1;
138 memcpy (buffer
, txt
, txtlen
);
139 buffer
[txtlen
] = '\0';
142 error (__FILE__
": nid repeated in function/variable");
146 sscanf (buffer
, "0x%X", &d
->nid
);
151 if (d
->last
== XMLE_NAME
) {
152 d
->libname
= dup_string (d
, txt
, txtlen
);
154 error (__FILE__
": current lib is not null");
157 d
->curlib
= hashtable_search (d
->result
->libs
, (void *) d
->libname
, NULL
);
159 d
->curlib
= hashtable_alloc (d
->result
->pool
, 128, NULL
, &hashtable_pointer_compare
);
160 hashtable_insert (d
->result
->libs
, (void *) d
->libname
, d
->curlib
);
168 struct nidstable
*nids_load (const char *xmlpath
)
171 struct xml_data data
;
175 buf
= read_file (xmlpath
, &size
);
180 p
= XML_ParserCreate (NULL
);
182 error (__FILE__
": can't create XML parser");
192 data
.scope
= XMLS_LIBRARY
;
193 data
.last
= XMLE_DEFAULT
;
195 data
.result
= (struct nidstable
*) xmalloc (sizeof (struct nidstable
));
196 data
.result
->pool
= hashpool_create (256, 8192);
197 data
.result
->libs
= hashtable_alloc (data
.result
->pool
, 32, &hashtable_hash_string
, &hashtable_string_compare
);
200 data
.result
->buffer
= buf
;
201 buf
= xmalloc (size
);
203 memcpy (buf
, data
.result
->buffer
, size
);
205 XML_SetUserData (p
, (void *) &data
);
206 XML_SetElementHandler (p
, &start_hndl
, &end_hndl
);
207 XML_SetCharacterDataHandler (p
, &char_hndl
);
209 if (!XML_Parse (p
, buf
, size
, 1)) {
210 error (__FILE__
": parse error at line %d:\n %s\n", XML_GetCurrentLineNumber (p
),
211 XML_ErrorString (XML_GetErrorCode (p
)));
219 nids_free (data
.result
);
227 void print_level1 (void *key
, void *value
, unsigned int hash
, void *arg
)
229 report (" NID: 0x%08X - Name: %s\n", hash
, (char *) value
);
233 void print_level0 (void *key
, void *value
, unsigned int hash
, void *arg
)
235 hashtable lib
= (hashtable
) value
;
236 report (" %s:\n", (char *) key
);
237 hashtable_traverse (lib
, &print_level1
, NULL
);
241 void nids_print (struct nidstable
*nids
)
243 report ("Libraries:\n");
244 hashtable_traverse (nids
->libs
, &print_level0
, NULL
);
248 const char *nids_find (struct nidstable
*nids
, const char *library
, unsigned int nid
)
252 lib
= hashtable_search (nids
->libs
, (void *) library
, NULL
);
253 if (lib
) return hashtable_searchhash (lib
, NULL
, NULL
, nid
);
259 int main (int argc
, char **argv
)
261 struct nidstable
*nids
= NULL
;
263 nids
= nids_load (argv
[1]);
271 #endif /* TEST_NIDS */