summaryrefslogtreecommitdiff
path: root/lib/win32
diff options
context:
space:
mode:
authorChris Wilson <chris+github@qwirx.com>2007-03-10 17:20:40 +0000
committerChris Wilson <chris+github@qwirx.com>2007-03-10 17:20:40 +0000
commitdd1026364150b2b80194015aa5ffafdd86b50fdb (patch)
tree896b2e152b1114c5146c1fc5aab1b9052de2deec /lib/win32
parenta9c4ae701ac2e2a5d5d62d82be4059e96fb6cb64 (diff)
Expanded character set conversion API to allow arbitrary conversions
(needed to handle command lines with international encodings) (refs #3, merges [1038])
Diffstat (limited to 'lib/win32')
-rw-r--r--lib/win32/emu.cpp62
-rw-r--r--lib/win32/emu.h8
2 files changed, 32 insertions, 38 deletions
diff --git a/lib/win32/emu.cpp b/lib/win32/emu.cpp
index cfe73ddd..7e6cbcd0 100644
--- a/lib/win32/emu.cpp
+++ b/lib/win32/emu.cpp
@@ -374,23 +374,25 @@ char* ConvertFromWideString(const WCHAR* pString, unsigned int codepage)
// --------------------------------------------------------------------------
//
// Function
-// Name: ConvertUtf8ToConsole
-// Purpose: Converts a string from UTF-8 to the console
-// code page. On success, replaces contents of rDest
-// and returns true. In case of fire, logs the error
-// and returns false.
-// Created: 4th February 2006
+// Name: ConvertEncoding(const std::string&, int,
+// std::string&, int)
+// Purpose: Converts a string from one code page to another.
+// On success, replaces contents of rDest and returns
+// true. In case of fire, logs the error and returns
+// false.
+// Created: 15th October 2006
//
// --------------------------------------------------------------------------
-bool ConvertUtf8ToConsole(const char* pString, std::string& rDest)
+bool ConvertEncoding(const std::string& rSource, int sourceCodePage,
+ std::string& rDest, int destCodePage)
{
- WCHAR* pWide = ConvertUtf8ToWideString(pString);
+ WCHAR* pWide = ConvertToWideString(rSource.c_str(), sourceCodePage);
if (pWide == NULL)
{
return false;
}
- char* pConsole = ConvertFromWideString(pWide, GetConsoleOutputCP());
+ char* pConsole = ConvertFromWideString(pWide, destCodePage);
delete [] pWide;
if (!pConsole)
@@ -404,39 +406,25 @@ bool ConvertUtf8ToConsole(const char* pString, std::string& rDest)
return true;
}
-// --------------------------------------------------------------------------
-//
-// Function
-// Name: ConvertConsoleToUtf8
-// Purpose: Converts a string from the console code page
-// to UTF-8. On success, replaces contents of rDest
-// and returns true. In case of fire, logs the error
-// and returns false.
-// Created: 4th February 2006
-//
-// --------------------------------------------------------------------------
-bool ConvertConsoleToUtf8(const char* pString, std::string& rDest)
+bool ConvertToUtf8(const char* pString, std::string& rDest, int sourceCodePage)
{
- WCHAR* pWide = ConvertToWideString(pString, GetConsoleCP());
- if (pWide == NULL)
- {
- return false;
- }
-
- char* pConsole = ConvertFromWideString(pWide, CP_UTF8);
- delete [] pWide;
-
- if (!pConsole)
- {
- return false;
- }
+ return ConvertEncoding(pString, sourceCodePage, rDest, CP_UTF8);
+}
- rDest = pConsole;
- delete [] pConsole;
+bool ConvertFromUtf8(const char* pString, std::string& rDest, int destCodePage)
+{
+ return ConvertEncoding(pString, CP_UTF8, rDest, destCodePage);
+}
- return true;
+bool ConvertConsoleToUtf8(const char* pString, std::string& rDest)
+{
+ return ConvertEncoding(pString, GetConsoleCP(), rDest, CP_UTF8);
}
+bool ConvertUtf8ToConsole(const char* pString, std::string& rDest)
+{
+ return ConvertEncoding(pString, CP_UTF8, rDest, GetConsoleOutputCP());
+}
// --------------------------------------------------------------------------
//
diff --git a/lib/win32/emu.h b/lib/win32/emu.h
index d898968a..5b3e2280 100644
--- a/lib/win32/emu.h
+++ b/lib/win32/emu.h
@@ -254,7 +254,7 @@ struct itimerval
int emu_mkdir(const char* pPathName);
-inline int mkdir(const char *pPathName, mode_t mode)
+inline int mkdir(const char *pPathName, mode_t mode = 0)
{
return emu_mkdir(pPathName);
}
@@ -398,6 +398,12 @@ bool ConvertTime_tToFileTime(const time_t from, FILETIME *pTo);
int poll(struct pollfd *ufds, unsigned long nfds, int timeout);
bool EnableBackupRights( void );
+bool ConvertEncoding (const std::string& rSource, int sourceCodePage,
+ std::string& rDest, int destCodePage);
+bool ConvertToUtf8 (const std::string& rSource, std::string& rDest,
+ int sourceCodePage);
+bool ConvertFromUtf8 (const std::string& rSource, std::string& rDest,
+ int destCodePage);
bool ConvertUtf8ToConsole(const char* pString, std::string& rDest);
bool ConvertConsoleToUtf8(const char* pString, std::string& rDest);