DumontEXE 0.0.1
win32_unicode.cpp
00001 /* 
00002    win32_Unicode.cpp
00003 
00004    Copyright (C) 2002-2004 René Nyffenegger
00005 
00006    This source code is provided 'as-is', without any express or implied
00007    warranty. In no event will the author be held liable for any damages
00008    arising from the use of this software.
00009 
00010    Permission is granted to anyone to use this software for any purpose,
00011    including commercial applications, and to alter it and redistribute it
00012    freely, subject to the following restrictions:
00013 
00014    1. The origin of this source code must not be misrepresented; you must not
00015       claim that you wrote the original source code. If you use this source code
00016       in a product, an acknowledgment in the product documentation would be
00017       appreciated but is not required.
00018 
00019    2. Altered source versions must be plainly marked as such, and must not be
00020       misrepresented as being the original source code.
00021 
00022    3. This notice may not be removed or altered from any source distribution.
00023 
00024    René Nyffenegger rene.nyffenegger@adp-gmbh.ch
00025 */
00026 
00027 #include "win32_Unicode.h"
00028 #include <windows.h>
00029 
00030 std::string ws2s(std::wstring const& s) {
00031 
00032   int lengthOfStr = ::WideCharToMultiByte(
00033     CP_ACP,
00034     0,
00035     s.c_str(),
00036    -1,  // number of chars in string. -1 indicates that string is 0 terminated
00037     0,
00038     0,
00039     0,
00040     0);
00041 
00042 
00043   char* r=new char[lengthOfStr];
00044 
00045   ::WideCharToMultiByte(
00046     CP_ACP,
00047     0,
00048     s.c_str(),
00049    -1,
00050     r,
00051     lengthOfStr,
00052     0,
00053     0);
00054 
00055   std::string ret=r;
00056   delete [] r;
00057 
00058   return ret;
00059 }
00060 
00061 std::wstring s2ws(std::string const& s, unsigned int codepage) {
00062 
00063 #ifdef _DEBUG
00064   if (codepage != CP_ACP) {
00065   if (! ::IsValidCodePage(codepage))
00066     throw "Invalid codepage given to stdStringToStdWstring";
00067   } 
00068 #endif
00069 
00070   int lengthOfWStr = ::MultiByteToWideChar(
00071       //CP_ACP,
00072       codepage,
00073       //0,
00074       MB_PRECOMPOSED,
00075       s.c_str(),
00076      -1,
00077      0,
00078      0);
00079 
00080 
00081   wchar_t* r=new wchar_t[lengthOfWStr];
00082 
00083   ::MultiByteToWideChar(
00084       codepage,
00085       //0,
00086       MB_PRECOMPOSED,
00087       s.c_str(),
00088      -1,
00089       r,
00090       lengthOfWStr);
00091 
00092   std::wstring ret=r;
00093   delete [] r;
00094 
00095   return ret;
00096 }
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties




~ ~ ~ ~ ~ ~
Source Code without Comments is like a Cranberry Garland
without the berries. Comment your Code!
 
Commence Database User Support Group Forum
http://newsgroup.showoff-db.org/
~ ~ ~ ~ ~ ~
Author: Mark Petryk
Lorimark Solutions, LLC
mark@lorimarksolutions.com