* xmlFindCharEncodingHandler: * @name: a string describing the char encoding. * * Search in the registered set the handler able to read/write that encoding. * * Returns the handler or NULL if not found */
| 1568 | * Returns the handler or NULL if not found |
| 1569 | */ |
| 1570 | xmlCharEncodingHandlerPtr |
| 1571 | xmlFindCharEncodingHandler(const char *name) { |
| 1572 | const char *nalias; |
| 1573 | const char *norig; |
| 1574 | xmlCharEncoding alias; |
| 1575 | #ifdef LIBXML_ICONV_ENABLED |
| 1576 | xmlCharEncodingHandlerPtr enc; |
| 1577 | iconv_t icv_in, icv_out; |
| 1578 | #endif /* LIBXML_ICONV_ENABLED */ |
| 1579 | char upper[100]; |
| 1580 | int i; |
| 1581 | |
| 1582 | if (handlers == NULL) xmlInitCharEncodingHandlers(); |
| 1583 | if (name == NULL) return(xmlDefaultCharEncodingHandler); |
| 1584 | if (name[0] == 0) return(xmlDefaultCharEncodingHandler); |
| 1585 | |
| 1586 | /* |
| 1587 | * Do the alias resolution |
| 1588 | */ |
| 1589 | norig = name; |
| 1590 | nalias = xmlGetEncodingAlias(name); |
| 1591 | if (nalias != NULL) |
| 1592 | name = nalias; |
| 1593 | |
| 1594 | /* |
| 1595 | * Check first for directly registered encoding names |
| 1596 | */ |
| 1597 | for (i = 0;i < 99;i++) { |
| 1598 | upper[i] = toupper(name[i]); |
| 1599 | if (upper[i] == 0) break; |
| 1600 | } |
| 1601 | upper[i] = 0; |
| 1602 | |
| 1603 | for (i = 0;i < nbCharEncodingHandler; i++) |
| 1604 | if (!strcmp(upper, handlers[i]->name)) { |
| 1605 | #ifdef DEBUG_ENCODING |
| 1606 | xmlGenericError(xmlGenericErrorContext, |
| 1607 | "Found registered handler for encoding %s\n", name); |
| 1608 | #endif |
| 1609 | return(handlers[i]); |
| 1610 | } |
| 1611 | |
| 1612 | #ifdef LIBXML_ICONV_ENABLED |
| 1613 | /* check whether iconv can handle this */ |
| 1614 | icv_in = iconv_open("UTF-8", name); |
| 1615 | icv_out = iconv_open(name, "UTF-8"); |
| 1616 | if (icv_in == (iconv_t) -1) { |
| 1617 | icv_in = iconv_open("UTF-8", upper); |
| 1618 | } |
| 1619 | if (icv_out == (iconv_t) -1) { |
| 1620 | icv_out = iconv_open(upper, "UTF-8"); |
| 1621 | } |
| 1622 | if ((icv_in != (iconv_t) -1) && (icv_out != (iconv_t) -1)) { |
| 1623 | enc = (xmlCharEncodingHandlerPtr) |
| 1624 | xmlMalloc(sizeof(xmlCharEncodingHandler)); |
| 1625 | if (enc == NULL) { |
| 1626 | iconv_close(icv_in); |
| 1627 | iconv_close(icv_out); |