* xmlAddIDInternal: * @attr: the attribute holding the ID * @value: the attribute (ID) value * @idPtr: pointer to resulting ID * * Register a new id declaration * * Returns 1 on success, 0 if the ID already exists, -1 if a memory * allocation fails. */
| 2319 | * allocation fails. |
| 2320 | */ |
| 2321 | static int |
| 2322 | xmlAddIDInternal(xmlAttrPtr attr, const xmlChar *value, xmlIDPtr *idPtr) { |
| 2323 | xmlDocPtr doc; |
| 2324 | xmlIDPtr id; |
| 2325 | xmlIDTablePtr table; |
| 2326 | int ret; |
| 2327 | |
| 2328 | if (idPtr != NULL) |
| 2329 | *idPtr = NULL; |
| 2330 | if ((value == NULL) || (value[0] == 0)) |
| 2331 | return(0); |
| 2332 | if (attr == NULL) |
| 2333 | return(0); |
| 2334 | |
| 2335 | doc = attr->doc; |
| 2336 | if (doc == NULL) |
| 2337 | return(0); |
| 2338 | |
| 2339 | /* |
| 2340 | * Create the ID table if needed. |
| 2341 | */ |
| 2342 | table = (xmlIDTablePtr) doc->ids; |
| 2343 | if (table == NULL) { |
| 2344 | doc->ids = table = xmlHashCreateDict(0, doc->dict); |
| 2345 | if (table == NULL) |
| 2346 | return(-1); |
| 2347 | } else { |
| 2348 | id = xmlHashLookup(table, value); |
| 2349 | if (id != NULL) |
| 2350 | return(0); |
| 2351 | } |
| 2352 | |
| 2353 | id = (xmlIDPtr) xmlMalloc(sizeof(xmlID)); |
| 2354 | if (id == NULL) |
| 2355 | return(-1); |
| 2356 | memset(id, 0, sizeof(*id)); |
| 2357 | |
| 2358 | /* |
| 2359 | * fill the structure. |
| 2360 | */ |
| 2361 | id->doc = doc; |
| 2362 | id->value = xmlStrdup(value); |
| 2363 | if (id->value == NULL) { |
| 2364 | xmlFreeID(id); |
| 2365 | return(-1); |
| 2366 | } |
| 2367 | |
| 2368 | if (attr->id != NULL) |
| 2369 | xmlRemoveID(doc, attr); |
| 2370 | |
| 2371 | if (xmlHashAddEntry(table, value, id) < 0) { |
| 2372 | xmlFreeID(id); |
| 2373 | return(-1); |
| 2374 | } |
| 2375 | |
| 2376 | ret = 1; |
| 2377 | if (idPtr != NULL) |
| 2378 | *idPtr = id; |
no test coverage detected