diff --git a/audio/mixer.cpp b/audio/mixer.cpp index 45d6c4c3b2..ac621cd380 100644 --- a/audio/mixer.cpp +++ b/audio/mixer.cpp @@ -98,7 +98,7 @@ Clip *clip_load(const char *filename) { uint8_t *filedata; size_t size; filedata = VFSReadFile(filename, &size); - num_samples = (int)stb_vorbis_decode_memory(filedata, size, &num_channels, &data); + num_samples = (int)stb_vorbis_decode_memory(filedata, (int)size, &num_channels, &data); if (num_samples <= 0) return NULL; sample_rate = 44100; diff --git a/base/basictypes.h b/base/basictypes.h index bb86be3668..d8de8c0713 100644 --- a/base/basictypes.h +++ b/base/basictypes.h @@ -99,6 +99,6 @@ inline uint64 swap64(const uint8* _pData) {return swap64(*(const uint64*)_pData) #endif // For really basic windows code compat -#ifndef TCHAR +#ifndef _TCHAR_DEFINED typedef char TCHAR; #endif diff --git a/base/logging.h b/base/logging.h index 2e0dbd3992..95bfe176d0 100644 --- a/base/logging.h +++ b/base/logging.h @@ -64,12 +64,12 @@ inline void Crash() { #ifdef _WIN32 -void __ods__(const char *p); +void OutputDebugStringUTF8(const char *p); -#define ILOG(...) {char temp[512]; char *p = temp; p += sprintf(p, "I: %s:%i: ", __FILE__, __LINE__); p += sprintf(p, "I: " __VA_ARGS__); p += sprintf(p, "\n"); __ods__(temp);} -#define WLOG(...) {char temp[512]; char *p = temp; p += sprintf(p, "W: %s:%i: ", __FILE__, __LINE__); p += sprintf(p, "W: " __VA_ARGS__); p += sprintf(p, "\n"); __ods__(temp);} -#define ELOG(...) {char temp[512]; char *p = temp; p += sprintf(p, "E: %s:%i: ", __FILE__, __LINE__); p += sprintf(p, "E: " __VA_ARGS__); p += sprintf(p, "\n"); __ods__(temp);} -#define FLOG(...) {char temp[512]; char *p = temp; p += sprintf(p, "F: %s:%i: ", __FILE__, __LINE__); p += sprintf(p, "F: " __VA_ARGS__); p += sprintf(p, "\n"); __ods__(temp); Crash();} +#define ILOG(...) {char temp[512]; char *p = temp; p += sprintf(p, "I: %s:%i: ", __FILE__, __LINE__); p += sprintf(p, "I: " __VA_ARGS__); p += sprintf(p, "\n"); OutputDebugStringUTF8(temp);} +#define WLOG(...) {char temp[512]; char *p = temp; p += sprintf(p, "W: %s:%i: ", __FILE__, __LINE__); p += sprintf(p, "W: " __VA_ARGS__); p += sprintf(p, "\n"); OutputDebugStringUTF8(temp);} +#define ELOG(...) {char temp[512]; char *p = temp; p += sprintf(p, "E: %s:%i: ", __FILE__, __LINE__); p += sprintf(p, "E: " __VA_ARGS__); p += sprintf(p, "\n"); OutputDebugStringUTF8(temp);} +#define FLOG(...) {char temp[512]; char *p = temp; p += sprintf(p, "F: %s:%i: ", __FILE__, __LINE__); p += sprintf(p, "F: " __VA_ARGS__); p += sprintf(p, "\n"); OutputDebugStringUTF8(temp); Crash();} // TODO: Win32 version using OutputDebugString #else diff --git a/base/stringutil.cpp b/base/stringutil.cpp index fa5f4a4c07..8e14ede96c 100644 --- a/base/stringutil.cpp +++ b/base/stringutil.cpp @@ -1,5 +1,7 @@ #ifdef _WIN32 #include +#undef min +#undef max #endif #include #include @@ -8,6 +10,7 @@ #include #include +#include #include #include "base/buffer.h" @@ -17,9 +20,14 @@ // Function Cross-Compatibility #define strcasecmp _stricmp -void __ods__(const char *p) { - OutputDebugString(p); +void OutputDebugStringUTF8(const char *p) { + wchar_t temp[2048]; + int len = std::min(2047, (int)strlen(p)); + int size = (int)MultiByteToWideChar(CP_UTF8, 0, p, len, NULL, 0); + MultiByteToWideChar(CP_UTF8, 0, p, len, temp, size); + OutputDebugString(temp); } + #endif unsigned int parseHex(const char *_szValue) diff --git a/data/compression.cpp b/data/compression.cpp index 63ce4b1dc7..f2bc6494ee 100644 --- a/data/compression.cpp +++ b/data/compression.cpp @@ -25,7 +25,7 @@ bool compress_string(const std::string& str, std::string *dest, int compressionl } zs.next_in = (Bytef*)str.data(); - zs.avail_in = str.size(); // set the z_stream's input + zs.avail_in = (uInt)str.size(); // set the z_stream's input int ret; char outbuffer[32768]; @@ -72,7 +72,7 @@ bool decompress_string(const std::string& str, std::string *dest) { } zs.next_in = (Bytef*)str.data(); - zs.avail_in = str.size(); + zs.avail_in = (uInt)str.size(); int ret; char outbuffer[32768]; diff --git a/file/chunk_file.cpp b/file/chunk_file.cpp index 396b07753b..6d61f11841 100644 --- a/file/chunk_file.cpp +++ b/file/chunk_file.cpp @@ -214,23 +214,17 @@ void ChunkFile::writeWString(String str) { } */ +// Takes utf-8 void ChunkFile::writeWString(const std::string &str) { unsigned short *text; size_t len = str.length(); -#ifdef UNICODE -#error - text = str.c_str(); -#else text = new unsigned short[len+1]; for (int i=0; ifullName = path; @@ -207,7 +205,7 @@ bool getFileInfo(const char *path, FileInfo *fileInfo) } std::string getFileExtension(const std::string &fn) { - int pos = fn.rfind("."); + int pos = (int)fn.rfind("."); if (pos < 0) return ""; std::string ext = fn.substr(pos+1); for (size_t i = 0; i < ext.size(); i++) { @@ -248,7 +246,8 @@ size_t getFilesInDir(const char *directory, std::vector *files, const // Find the first file in the directory. WIN32_FIND_DATA ffd; #ifdef UNICODE - HANDLE hFind = FindFirstFile((std::wstring(directory) + "\\*").c_str(), &ffd); + + HANDLE hFind = FindFirstFile((ConvertUTF8ToWString(directory) + L"\\*").c_str(), &ffd); #else HANDLE hFind = FindFirstFile((std::string(directory) + "\\*").c_str(), &ffd); #endif @@ -259,7 +258,7 @@ size_t getFilesInDir(const char *directory, std::vector *files, const // windows loop do { - const std::string virtualName(ffd.cFileName); + const std::string virtualName = ConvertWStringToUTF8(ffd.cFileName); #else struct dirent_large { struct dirent entry; char padding[FILENAME_MAX+1]; }; struct dirent_large diren; @@ -316,7 +315,7 @@ size_t getFilesInDir(const char *directory, std::vector *files, const void deleteFile(const char *file) { #ifdef _WIN32 - if (!DeleteFile(file)) { + if (!DeleteFile(ConvertUTF8ToWString(file).c_str())) { ELOG("Error deleting %s: %i", file, GetLastError()); } #else @@ -330,7 +329,7 @@ void deleteFile(const char *file) void deleteDir(const char *dir) { #ifdef _WIN32 - if (!RemoveDirectory(dir)) { + if (!RemoveDirectory(ConvertUTF8ToWString(dir).c_str())) { ELOG("Error deleting directory %s: %i", dir, GetLastError()); } #else @@ -390,7 +389,7 @@ std::vector getWindowsDrives() auto drive = buff.data(); while (*drive) { - std::string str(drive); + std::string str(ConvertWStringToUTF8(drive)); str.pop_back(); // we don't want the final backslash str += "/"; drives.push_back(str); diff --git a/math/expression_parser.cpp b/math/expression_parser.cpp index 505d1160c7..d95f4f0b23 100644 --- a/math/expression_parser.cpp +++ b/math/expression_parser.cpp @@ -147,7 +147,7 @@ bool parseNumber(char* str, int defaultrad, int len, uint32& result) return true; } -ExpressionOpcodeType getExpressionOpcode(char* str, int& ReturnLen, ExpressionOpcodeType LastOpcode) +ExpressionOpcodeType getExpressionOpcode(const char* str, int& ReturnLen, ExpressionOpcodeType LastOpcode) { int longestlen = 0; ExpressionOpcodeType result = EXOP_NONE; @@ -185,12 +185,12 @@ bool isAlphaNum(char c) } } -bool initPostfixExpression(char* infix, IExpressionFunctions* funcs, PostfixExpression& dest) +bool initPostfixExpression(const char* infix, IExpressionFunctions* funcs, PostfixExpression& dest) { expressionError[0] = 0; int infixPos = 0; - int infixLen = strlen(infix); + int infixLen = (int)strlen(infix); ExpressionOpcodeType lastOpcode = EXOP_NONE; std::vector opcodeStack; dest.clear(); diff --git a/math/expression_parser.h b/math/expression_parser.h index 7d1f8e67da..bbf7b78306 100644 --- a/math/expression_parser.h +++ b/math/expression_parser.h @@ -15,7 +15,7 @@ public: virtual bool getMemoryValue(uint32 address, int size, uint32& dest, char* error) = 0; }; -bool initPostfixExpression(char* infix, IExpressionFunctions* funcs, PostfixExpression& dest); +bool initPostfixExpression(const char* infix, IExpressionFunctions* funcs, PostfixExpression& dest); bool parsePostfixExpression(PostfixExpression& exp, IExpressionFunctions* funcs, uint32& dest); -bool parseExpression(char* exp, IExpressionFunctions* funcs, uint32& dest); +bool parseExpression(const char* exp, IExpressionFunctions* funcs, uint32& dest); const char* getExpressionError(); diff --git a/midi/midi_input.cpp b/midi/midi_input.cpp index d42ccff13d..cb11625e19 100644 --- a/midi/midi_input.cpp +++ b/midi/midi_input.cpp @@ -11,14 +11,16 @@ #include "base/basictypes.h" #include "base/logging.h" #include "midi/midi_input.h" +#include "util/text/utf8.h" std::vector MidiInGetDevices() { int numDevs = midiInGetNumDevs(); + std::vector devices; for (int i = 0; i < numDevs; i++) { MIDIINCAPS caps; midiInGetDevCaps(i, &caps, sizeof(caps)); - devices.push_back(caps.szPname); + devices.push_back(ConvertWStringToUTF8(caps.szPname)); } return devices; } diff --git a/native.vcxproj b/native.vcxproj index 264f78d557..b67686a97c 100644 --- a/native.vcxproj +++ b/native.vcxproj @@ -27,25 +27,25 @@ StaticLibrary true - MultiByte + Unicode StaticLibrary true - MultiByte + Unicode StaticLibrary false false - MultiByte + Unicode v100 StaticLibrary false false - MultiByte + Unicode diff --git a/net/resolve.cpp b/net/resolve.cpp index 304f3356cb..27fee57fea 100644 --- a/net/resolve.cpp +++ b/net/resolve.cpp @@ -96,8 +96,7 @@ bool DNSResolve(const std::string &host, const std::string &service, addrinfo ** *res = NULL; int result = getaddrinfo(host.c_str(), servicep, &hints, res); - if (result == EAI_AGAIN) - { + if (result == EAI_AGAIN) { // Temporary failure. Since this already blocks, let's just try once more. #ifdef _WIN32 Sleep(1); @@ -107,9 +106,8 @@ bool DNSResolve(const std::string &host, const std::string &service, addrinfo ** result = getaddrinfo(host.c_str(), servicep, &hints, res); } - if (result != 0) - { - error = gai_strerror(result); + if (result != 0) { + error = gai_strerrorA(result); if (*res != NULL) freeaddrinfo(*res); *res = NULL; diff --git a/util/text/utf8.cpp b/util/text/utf8.cpp index e77eb076bb..378fd236c6 100644 --- a/util/text/utf8.cpp +++ b/util/text/utf8.cpp @@ -11,11 +11,21 @@ with these routines reserved for higher performance on data known to be valid. */ + +#ifdef _WIN32 +#include +#undef min +#undef max +#endif + #include #include #include #include +#include +#include + #include "base/basictypes.h" #include "utf8.h" @@ -407,4 +417,47 @@ int UTF8StringNonASCIICount(const char *utf8string) { bool UTF8StringHasNonASCII(const char *utf8string) { return UTF8StringNonASCIICount(utf8string) > 0; -} \ No newline at end of file +} + +#ifdef _WIN32 + +std::string ConvertWStringToUTF8(const wchar_t *wstr) { + int len = (int)wcslen(wstr); + int size = (int)WideCharToMultiByte(CP_UTF8, 0, wstr, len, 0, 0, NULL, NULL); + std::string s; + s.resize(size); + if (size > 0) { + WideCharToMultiByte(CP_UTF8, 0, wstr, len, &s[0], size, NULL, NULL); + } + return s; +} + +std::string ConvertWStringToUTF8(const std::wstring &wstr) { + int len = (int)wstr.size(); + int size = (int)WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), len, 0, 0, NULL, NULL); + std::string s; + s.resize(size); + if (size > 0) { + WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), len, &s[0], size, NULL, NULL); + } + return s; +} + +void ConvertUTF8ToWString(wchar_t *dest, size_t destSize, const std::string &source) { + int len = (int)source.size(); + int size = (int)MultiByteToWideChar(CP_UTF8, 0, source.c_str(), len, NULL, 0); + MultiByteToWideChar(CP_UTF8, 0, source.c_str(), len, dest, std::min((int)destSize, size)); +} + +std::wstring ConvertUTF8ToWString(const std::string &source) { + int len = (int)source.size(); + int size = (int)MultiByteToWideChar(CP_UTF8, 0, source.c_str(), len, NULL, 0); + std::wstring str; + str.resize(size); + if (size > 0) { + MultiByteToWideChar(CP_UTF8, 0, source.c_str(), len, &str[0], size); + } + return str; +} + +#endif diff --git a/util/text/utf8.h b/util/text/utf8.h index 5d46a67648..1cfd55aed0 100644 --- a/util/text/utf8.h +++ b/util/text/utf8.h @@ -14,7 +14,10 @@ // Further modified, and C++ stuff added, by hrydgard@gmail.com. +#pragma once + #include "base/basictypes.h" +#include uint32_t u8_nextchar(const char *s, int *i); int u8_wc_toutf8(char *dest, uint32_t ch); @@ -45,4 +48,16 @@ private: int UTF8StringNonASCIICount(const char *utf8string); -bool UTF8StringHasNonASCII(const char *utf8string); \ No newline at end of file +bool UTF8StringHasNonASCII(const char *utf8string); + + +// UTF8 to Win32 UCS-16 +// Should be used when calling Win32 api calls +#ifdef _WIN32 + +std::string ConvertWStringToUTF8(const std::wstring &wstr); +std::string ConvertWStringToUTF8(const wchar_t *wstr); +void ConvertUTF8ToWString(wchar_t *dest, size_t destSize, const std::string &source); +std::wstring ConvertUTF8ToWString(const std::string &source); + +#endif \ No newline at end of file