| 1717 | |
| 1718 | |
| 1719 | char* msConvertWideStringToUTF8 (const wchar_t* string, const char* encoding) { |
| 1720 | #ifdef USE_ICONV |
| 1721 | |
| 1722 | char* output = NULL; |
| 1723 | char* errormessage = NULL; |
| 1724 | iconv_t cd = NULL; |
| 1725 | size_t nStr; |
| 1726 | size_t nInSize; |
| 1727 | size_t nOutSize; |
| 1728 | size_t iconv_status = -1; |
| 1729 | size_t nBufferSize; |
| 1730 | |
| 1731 | char* pszUTF8 = NULL; |
| 1732 | const wchar_t* pwszWide = NULL; |
| 1733 | |
| 1734 | if (string != NULL) |
| 1735 | { |
| 1736 | nStr = wcslen (string); |
| 1737 | nBufferSize = ((nStr * 6) + 1); |
| 1738 | output = (char*) msSmallMalloc (nBufferSize); |
| 1739 | |
| 1740 | if (nStr == 0) { |
| 1741 | /* return an empty 8 byte string */ |
| 1742 | output[0] = '\0'; |
| 1743 | return output; |
| 1744 | } |
| 1745 | |
| 1746 | cd = iconv_open("UTF-8", encoding); |
| 1747 | |
| 1748 | nOutSize = nBufferSize; |
| 1749 | if ((iconv_t)-1 != cd) |
| 1750 | { |
| 1751 | nInSize = sizeof (wchar_t)*nStr; |
| 1752 | pszUTF8 = output; |
| 1753 | pwszWide = string; |
| 1754 | iconv_status = iconv(cd, (char **)&pwszWide, &nInSize, &pszUTF8, &nOutSize); |
| 1755 | if ((size_t)-1 == iconv_status) { |
| 1756 | switch (errno) { |
| 1757 | case E2BIG: |
| 1758 | errormessage = "There is not sufficient room in buffer"; |
| 1759 | break; |
| 1760 | case EILSEQ: |
| 1761 | errormessage = "An invalid multibyte sequence has been encountered in the input"; |
| 1762 | break; |
| 1763 | case EINVAL: |
| 1764 | errormessage = "An incomplete multibyte sequence has been encountered in the input"; |
| 1765 | break; |
| 1766 | default: |
| 1767 | errormessage = "Unknown"; |
| 1768 | break; |
| 1769 | } |
| 1770 | msSetError(MS_MISCERR, "Unable to convert string in encoding '%s' to UTF8 %s", |
| 1771 | "msConvertWideStringToUTF8()", |
| 1772 | encoding,errormessage); |
| 1773 | iconv_close(cd); |
| 1774 | msFree(output); |
| 1775 | return NULL; |
| 1776 | } |
no test coverage detected