diff --git a/aegisub/libaegisub/common/charset_conv.cpp b/aegisub/libaegisub/common/charset_conv.cpp index 0b743a3d2..b255f41ea 100644 --- a/aegisub/libaegisub/common/charset_conv.cpp +++ b/aegisub/libaegisub/common/charset_conv.cpp @@ -431,5 +431,13 @@ size_t IconvWrapper::SrcStrLen(const char* str) { size_t IconvWrapper::DstStrLen(const char* str) { return mbstrlen(str, toNulLen); } + +bool IsConversionSupported(const char *src, const char *dst) { + iconv_t cd = iconv_open(dst, src); + bool supported = cd != iconv_invalid; + iconv_close(cd); + return supported; +} + } } diff --git a/aegisub/libaegisub/include/libaegisub/charset_conv.h b/aegisub/libaegisub/include/libaegisub/charset_conv.h index 4a4dc1862..a434f7359 100644 --- a/aegisub/libaegisub/include/libaegisub/charset_conv.h +++ b/aegisub/libaegisub/include/libaegisub/charset_conv.h @@ -22,12 +22,12 @@ #ifndef LAGI_PRE #include -#include #include #include #endif #include +#include namespace agi { namespace charset { @@ -39,21 +39,9 @@ DEFINE_SIMPLE_EXCEPTION_NOINNER(BufferTooSmall, ConversionFailure, "iconv/failed DEFINE_SIMPLE_EXCEPTION_NOINNER(BadInput, ConversionFailure, "iconv/failed/EILSEQ") DEFINE_SIMPLE_EXCEPTION_NOINNER(BadOutput, ConversionFailure, "iconv/failed/EINVAL") -/// @brief Get a list of support encodings with user-friendly names -template -T const& GetEncodingsList() { - static T nameList; - if (nameList.empty()) { -# define ADD(pretty, real) nameList.push_back(pretty) -# include -# undef ADD - } - return nameList; -} - typedef void* iconv_t; -// Helper class that abstracts away the differences betwen libiconv and +// Helper class that abstracts away the differences between libiconv and // POSIX iconv implementations class Converter; @@ -61,7 +49,7 @@ class Converter; class IconvWrapper { size_t toNulLen; size_t fromNulLen; - std::auto_ptr conv; + agi::scoped_ptr conv; public: /// @brief Create a converter @@ -103,5 +91,23 @@ public: size_t DstStrLen(const char* str); }; +/// Is the conversion from src to dst supported by the linked iconv library? +/// @param src Source encoding name +/// @param dst Destination encoding name +/// @return false if either charset is not supported or the conversion cannot be done directly, true otherwise +bool IsConversionSupported(const char *src, const char *dst); + +/// Get a list of supported encodings with user-friendly names +template +T const& GetEncodingsList() { + static T name_list; + if (name_list.empty()) { +# define ADD(pretty, real) if (IsConversionSupported(real, "utf-8")) name_list.push_back(pretty) +# include +# undef ADD + } + return name_list; +} + } }