12 struct hashtable
*libs
;
33 struct nidstable
*result
;
34 struct hashtable
*curlib
;
43 void nids_free_callback (void *key
, void *value
, unsigned int hash
, void *arg
)
45 struct hashtable
*lib
= (struct hashtable
*) value
;
46 hashtable_free (lib
, NULL
, NULL
);
49 void nids_free (struct nidstable
*nids
)
51 if (nids
->libs
) hashtable_free (nids
->libs
, &nids_free_callback
, NULL
);
53 if (nids
->buffer
) free (nids
->buffer
);
60 void start_hndl (void *data
, const char *el
, const char **attr
)
62 struct xml_data
*d
= (struct xml_data
*) data
;
64 d
->last
= XMLE_DEFAULT
;
66 if (strcmp (el
, "LIBRARY") == 0) {
67 d
->scope
= XMLS_LIBRARY
;
68 } else if (strcmp (el
, "NAME") == 0) {
70 } else if (strcmp (el
, "FUNCTION") == 0) {
71 d
->scope
= XMLS_FUNCTION
;
72 } else if (strcmp (el
, "VARIABLE") == 0) {
73 d
->scope
= XMLS_VARIABLE
;
74 } else if (strcmp (el
, "NID") == 0) {
80 void end_hndl (void *data
, const char *el
)
82 struct xml_data
*d
= (struct xml_data
*) data
;
84 d
->last
= XMLE_DEFAULT
;
86 if (strcmp (el
, "LIBRARY") == 0) {
89 } else if (strcmp (el
, "FUNCTION") == 0 || strcmp (el
, "VARIABLE") == 0) {
90 d
->scope
= XMLS_LIBRARY
;
91 if (d
->name
&& d
->nid
&& d
->curlib
) {
92 const char *name
= hashtable_searchh (d
->curlib
, NULL
, NULL
, d
->nid
);
94 if (strcmp (name
, d
->name
)) {
95 error (__FILE__
": NID `0x%08X' repeated in library `%s'", d
->nid
, d
->libname
);
99 hashtable_inserth (d
->curlib
, NULL
, (void *) d
->name
, d
->nid
);
102 error (__FILE__
": missing function or variable definition");
111 const char *dup_string (struct xml_data
*d
, const char *txt
, size_t len
)
115 result
= &d
->result
->buffer
[d
->buffer_pos
];
116 memcpy (result
, txt
, len
);
118 d
->buffer_pos
+= len
+ 1;
120 return (const char *) result
;
124 void char_hndl (void *data
, const char *txt
, int txtlen
)
126 struct xml_data
*d
= (struct xml_data
*) data
;
131 if (d
->last
== XMLE_NAME
) {
133 error (__FILE__
": repeated name in function/variable");
136 d
->name
= dup_string (d
, txt
, txtlen
);
138 } else if (d
->last
== XMLE_NID
) {
141 if (txtlen
> sizeof (buffer
) - 1)
142 txtlen
= sizeof (buffer
) - 1;
143 memcpy (buffer
, txt
, txtlen
);
144 buffer
[txtlen
] = '\0';
147 error (__FILE__
": nid repeated in function/variable");
151 sscanf (buffer
, "0x%X", &d
->nid
);
156 if (d
->last
== XMLE_NAME
) {
157 d
->libname
= dup_string (d
, txt
, txtlen
);
159 error (__FILE__
": current lib is not null");
162 d
->curlib
= hashtable_search (d
->result
->libs
, (void *) d
->libname
, NULL
);
164 d
->curlib
= hashtable_create (128, NULL
, &hashtable_pointer_compare
);
165 hashtable_insert (d
->result
->libs
, (void *) d
->libname
, d
->curlib
);
173 struct nidstable
*nids_load (const char *xmlpath
)
176 struct xml_data data
;
180 buf
= read_file (xmlpath
, &size
);
185 p
= XML_ParserCreate (NULL
);
187 error (__FILE__
": can't create XML parser");
197 data
.scope
= XMLS_LIBRARY
;
198 data
.last
= XMLE_DEFAULT
;
200 data
.result
= (struct nidstable
*) xmalloc (sizeof (struct nidstable
));
201 data
.result
->libs
= hashtable_create (32, &hashtable_hash_string
, &hashtable_string_compare
);
204 data
.result
->buffer
= xmalloc (size
);
206 XML_SetUserData (p
, (void *) &data
);
207 XML_SetElementHandler (p
, &start_hndl
, &end_hndl
);
208 XML_SetCharacterDataHandler (p
, &char_hndl
);
210 if (!XML_Parse (p
, buf
, size
, 1)) {
211 error (__FILE__
": parse error at line %d:\n %s\n", XML_GetCurrentLineNumber (p
),
212 XML_ErrorString (XML_GetErrorCode (p
)));
220 nids_free (data
.result
);
228 void print_level1 (void *key
, void *value
, unsigned int hash
, void *arg
)
230 report (" NID: 0x%08X - Name: %s\n", hash
, (char *) value
);
234 void print_level0 (void *key
, void *value
, unsigned int hash
, void *arg
)
236 struct hashtable
*lib
= (struct hashtable
*) value
;
237 report (" %s:\n", (char *) key
);
238 hashtable_traverse (lib
, &print_level1
, NULL
);
242 void nids_print (struct nidstable
*nids
)
244 report ("Libraries:\n");
245 hashtable_traverse (nids
->libs
, &print_level0
, NULL
);
249 const char *nids_find (struct nidstable
*nids
, const char *library
, unsigned int nid
)
251 struct hashtable
*lib
;
253 lib
= hashtable_search (nids
->libs
, (void *) library
, NULL
);
254 if (lib
) return hashtable_searchh (lib
, NULL
, NULL
, nid
);
260 int main (int argc
, char **argv
)
262 struct nidstable
*nids
= NULL
;
264 nids
= nids_load (argv
[1]);
272 #endif /* TEST_NIDS */