diff options
Diffstat (limited to 'Plugins/utils/utf8_helpers.h')
-rw-r--r-- | Plugins/utils/utf8_helpers.h | 180 |
1 files changed, 142 insertions, 38 deletions
diff --git a/Plugins/utils/utf8_helpers.h b/Plugins/utils/utf8_helpers.h index 149efcf..1b7785d 100644 --- a/Plugins/utils/utf8_helpers.h +++ b/Plugins/utils/utf8_helpers.h @@ -141,17 +141,7 @@ public: #ifdef UNICODE
- int size = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
- if (size <= 0)
- throw _T("Could not convert string to WCHAR");
-
- WCHAR *tmp = (WCHAR *) mir_alloc(size * sizeof(WCHAR));
- if (tmp == NULL)
- throw _T("mir_alloc returned NULL");
-
- MultiByteToWideChar(CP_ACP, 0, str, -1, tmp, size);
-
- tchar = tmp;
+ tchar = mir_a2u(str);
#else
@@ -209,15 +199,7 @@ public: #else
- int size = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
- if (size <= 0)
- throw _T("Could not convert string to ACP");
-
- tchar = (TCHAR *) mir_alloc(size);
- if (tchar == NULL)
- throw _T("mir_alloc returned NULL");
-
- WideCharToMultiByte(CP_ACP, 0, str, -1, tchar, size, NULL, NULL);
+ tchar = mir_u2a(str);
#endif
}
@@ -259,6 +241,42 @@ private: +class CharToWchar
+{
+public:
+ CharToWchar(const char *str) : wchar(NULL)
+ {
+ if (str == NULL)
+ return;
+
+ wchar = mir_a2u(str);
+ }
+
+
+ ~CharToWchar()
+ {
+ if (wchar != NULL)
+ mir_free(wchar);
+ }
+
+ WCHAR *detach()
+ {
+ WCHAR *ret = wchar;
+ wchar = NULL;
+ return ret;
+ }
+
+ operator const WCHAR *()
+ {
+ return wchar;
+ }
+
+private:
+ WCHAR *wchar;
+};
+
+
+
class TcharToChar
{
public:
@@ -269,15 +287,7 @@ public: #ifdef UNICODE
- int size = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
- if (size <= 0)
- throw _T("Could not convert string to ACP");
-
- val = (char *) mir_alloc(size);
- if (val == NULL)
- throw _T("mir_alloc returned NULL");
-
- WideCharToMultiByte(CP_ACP, 0, str, -1, val, size, NULL, NULL);
+ val = mir_u2a(str);
#else
@@ -335,15 +345,7 @@ public: #else
- int size = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
- if (size <= 0)
- throw _T("Could not convert string to WCHAR");
-
- val = (WCHAR *) mir_alloc(size * sizeof(WCHAR));
- if (val == NULL)
- throw _T("mir_alloc returned NULL");
-
- MultiByteToWideChar(CP_UTF8, 0, str, -1, val, size);
+ val = mir_a2u(str);
#endif
}
@@ -383,4 +385,106 @@ private: };
+
+
+class BstrToTchar
+{
+public:
+ BstrToTchar() : bstr(NULL)
+#ifndef UNICODE
+ , tchar(NULL)
+#endif
+ {
+ }
+
+ BstrToTchar(const WCHAR *str) : bstr(NULL)
+#ifndef UNICODE
+ , tchar(NULL)
+#endif
+ {
+ if (str == NULL)
+ return;
+
+ bstr = SysAllocString(str);
+ }
+
+ BstrToTchar(const char *str) : bstr(NULL)
+#ifndef UNICODE
+ , tchar(NULL)
+#endif
+ {
+ if (str == NULL)
+ return;
+
+ bstr = SysAllocString(CharToWchar(str));
+ }
+
+
+ ~BstrToTchar()
+ {
+ if (bstr != NULL)
+ SysFreeString(bstr);
+
+#ifndef UNICODE
+ freeTchar();
+#endif
+ }
+
+ BSTR detach()
+ {
+ BSTR ret = bstr;
+ bstr = NULL;
+ return ret;
+ }
+
+ operator const TCHAR *()
+ {
+#ifdef UNICODE
+
+ return bstr;
+
+#else
+
+ if (tchar == NULL)
+ tchar = mir_u2a(bstr);
+
+ return tchar;
+
+#endif
+ }
+
+ operator const BSTR()
+ {
+ return bstr;
+ }
+
+ operator BSTR *()
+ {
+#ifndef UNICODE
+ freeTchar();
+#endif
+
+ return &bstr;
+ }
+
+private:
+ BSTR bstr;
+
+#ifndef UNICODE
+
+ TCHAR *tchar;
+
+ void freeTchar()
+ {
+ if (tchar != NULL)
+ {
+ mir_free(tchar);
+ tchar = NULL;
+ }
+ }
+
+#endif
+};
+
+
#endif // __UTF8_HELPERS_H__
|