//.h
/**
* @brief String opeartion.
* @file strconv.h
* @author loach
* @Email < loachmr@sina.com >
*
* Copyright (C) 1996-2010 SINA Corporation, All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#ifndef __strConv_H__
#define __strConv_H__
#ifdef __cplusplus
extern "C" {
#endif
/*
** Convert an ansi string to microsoft unicode, based on the
** current codepage settings for file apis.
**
** Space to hold the returned string is obtained
** from malloc. please free
*/
int lo_C2W(wchar_t** pout , const char *zinname);
int lo_W2C(char** pout ,const wchar_t *zWide);
int lo_W2Utf8(char** pout ,const wchar_t *zWide);
/*
** Convert multibyte character string to UTF-8. Space to hold the
** returned string is obtained from malloc().
*/
int lo_C2Utf8(char** pout , const char *zinname);
#ifdef __cplusplus
}
#endif
#endif //__strConv_H__
//.cpp
/**
* @brief String opeartion.
* @file strconv.cpp
* @author loach
* @Email < loachmr@sina.com >
*
* Copyright (C) 1996-2010 SINA Corporation, All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "strconv.h"
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
#if (defined(WIN32) || defined(_WIN32) )
#include <windows.h>
/*
** Convert an ansi string to microsoft unicode, based on the
** current codepage settings for file apis.
**
** Space to hold the returned string is obtained
** from malloc.
*/
int lo_C2W(wchar_t** pout ,const char *zinname)
{
wchar_t *zMbcsname = 0;
int codepage = 0;
int nByte = 0;
if( !zinname || * zinname == '/0' )
return 0;
codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
nByte = MultiByteToWideChar(codepage, 0, zinname, -1, NULL,0);
zMbcsname = (wchar_t*)malloc( sizeof(wchar_t)*(nByte+1) );
if( zMbcsname == 0 )
return 0;
nByte = MultiByteToWideChar(codepage, 0, zinname, -1, zMbcsname, nByte);
if( nByte > 0 )
{
zMbcsname[nByte] = 0;
}
*pout = zMbcsname;
return nByte;
}
/*
** Convert microsoft unicode to multibyte character string, based on the
** user's Ansi codepage.
**
** Space to hold the returned string is obtained from
** malloc().
*/
int lo_W2C(char** pout ,const wchar_t *zWide)
{
char *zname = 0;
int codepage = 0;
int nByte = 0;
if( !zWide || *zWide == '/0' )
return 0;
codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
nByte = WideCharToMultiByte(codepage, 0, zWide, -1, 0, 0, 0, 0);
zname = (char*)malloc( nByte + 1 );
if( zname == 0 ) return 0;
nByte = WideCharToMultiByte(codepage, 0, zWide, -1, zname, nByte+1, 0, 0);
if( nByte > 0 ) zname[nByte] = '/0';
*pout = zname;
return nByte;
}
int lo_W2Utf8(char** pout , const wchar_t *zWide)
{
int nByte = 0;
char *zname = 0;
if( !zWide || *zWide == '/0' )
return 0;
nByte = WideCharToMultiByte(CP_UTF8, 0, zWide, -1, 0, 0, 0, 0);
zname = (char*)malloc( nByte +1 );
if( zname == 0 )
{
return 0;
}
nByte = WideCharToMultiByte(CP_UTF8, 0, zWide, -1, zname, nByte+1, 0, 0);
if( nByte > 0 )
{
zname[nByte] = '/0';
}
*pout = zname ;
return nByte;
}
/*
** Convert multibyte character string to UTF-8. Space to hold the
** returned string is obtained from malloc().
*/
int lo_C2Utf8(char** pout , const char *zinname)
{
wchar_t *zTmp = 0;
int outlen = 0;
outlen = lo_C2W(&zTmp , zinname);
if( !zTmp || outlen <= 0 )
{
return 0;
}
outlen = lo_W2Utf8(pout , zTmp);
free( zTmp );
return outlen ;
}
#else
// for other platform
#endif
#ifdef __cplusplus
}
#endif