diff options
| author | Tannin <devnull@localhost> | 2014-09-19 22:01:37 +0200 |
|---|---|---|
| committer | Tannin <devnull@localhost> | 2014-09-19 22:01:37 +0200 |
| commit | 5f1022917b655650f842dc8f34967716081685ef (patch) | |
| tree | fa7b84e80154e4f0258d0fa2259d129b098cf260 /src/shared | |
| parent | b77dcb220616282e00d2cd3e9b7cff1d076d0701 (diff) | |
- removed a use of wostringstream which seems to have been involved in a bug. Still not sure how
- rewrote widechar <-> multibyte conversion functions to handle arbitrary string lengths
Diffstat (limited to 'src/shared')
| -rw-r--r-- | src/shared/util.cpp | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/src/shared/util.cpp b/src/shared/util.cpp index 61ca9cdc..4bc5a8a4 100644 --- a/src/shared/util.cpp +++ b/src/shared/util.cpp @@ -25,6 +25,7 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. #include <algorithm>
#include <DbgHelp.h>
#include <set>
+#include <boost/scoped_array.hpp>
namespace MOShared {
@@ -64,24 +65,28 @@ bool FileExists(const std::wstring &searchPath, const std::wstring &filename) std::string ToString(const std::wstring &source, bool utf8)
{
- char buffer[MAX_PATH];
- if (utf8) {
- ::WideCharToMultiByte(CP_UTF8, 0, source.c_str(), -1, buffer, MAX_PATH, NULL, NULL);
- } else {
- ::WideCharToMultiByte(GetACP(), 0, source.c_str(), -1, buffer, MAX_PATH, NULL, NULL);
+ std::string result;
+ UINT codepage = utf8 ? CP_UTF8 : GetACP();
+ int sizeRequired = ::WideCharToMultiByte(codepage, 0, &source[0], -1, NULL, 0, NULL, NULL);
+ if (sizeRequired == 0) {
+ throw windows_error("failed to convert string to multibyte");
}
- return std::string(buffer);
+ result.resize(sizeRequired, '\0');
+ ::WideCharToMultiByte(codepage, 0, &source[0], (int)source.size(), &result[0], sizeRequired, NULL, NULL);
+ return result;
}
std::wstring ToWString(const std::string &source, bool utf8)
{
- wchar_t buffer[MAX_PATH];
- if (utf8) {
- ::MultiByteToWideChar(CP_UTF8, 0, source.c_str(), -1, buffer, MAX_PATH);
- } else {
- ::MultiByteToWideChar(GetACP(), 0, source.c_str(), -1, buffer, MAX_PATH);
+ std::wstring result;
+ UINT codepage = utf8 ? CP_UTF8 : GetACP();
+ int sizeRequired = ::MultiByteToWideChar(codepage, 0, &source[0], (int)source.size(), NULL, 0);
+ if (sizeRequired == 0) {
+ throw windows_error("failed to convert string to wide character");
}
- return std::wstring(buffer);
+ result.resize(sizeRequired, L'\0');
+ ::MultiByteToWideChar(codepage, 0, &source[0], (int)source.size(), &result[0], sizeRequired);
+ return result;
}
std::string &ToLower(std::string &text)
|
