您的位置:首页 > 其它

新浪围脖开放平台六"字符串转化"

2010-12-11 12:14 281 查看
//.h

/**
*  @brief char convertion
*  @file  NAStingUNIT.h
*  @author welbon
*  @Email < libo.weng@gmail.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.
*
*/

#pragma once

#include <stdio.h>
#include <tchar.h>
#include <string>

/** 字符串转化类
*
* @author loach
*
* @date 2008-07-09
*/
class CNAStringUNIT
{
public:
/** Convert  utf8 to microsoft unicode
*/
std::wstring NA_Utf8ToUnicode(const char *zFilename);

/*
** Convert microsoft unicode to UTF-8.  Space to hold the returned string is
** obtained from malloc().
*/
std::string NA_UnicodeToUtf8(const wchar_t *zWideFilename);

/*
** 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.
*/
std::wstring NA_MbcsToUnicode(const char *zFilename);

/*
** Convert microsoft unicode to multibyte character string, based on the
** user's Ansi codepage.
**
** Space to hold the returned string is obtained from
** malloc().
*/
std::string NA_UnicodeToMbcs(const wchar_t *zWideFilename);

/*
** Convert multibyte character string to UTF-8.  Space to hold the
** returned string is obtained from malloc().
*/
std::string NA_MbcsToUtf8(const char *zFilename);

/*
** Convert UTF-8 to multibyte character string.  Space to hold the
** returned string is obtained from malloc().
*/
std::string NA_Utf8ToMbcs(const char *zFilename);

/** 是否是邮箱地址 */
bool NA_isemail(const char *str);

/** 是否是字符串*/
bool NA_isalpha(const char *str);

/** 是否是数字 */
bool NA_isalnum(const char* str);

/** 是否是0-9数字 */
bool NA_isdigit(const char *str);
};


//.cpp

/**
*  @brief char convertion
*  @file  NAStingUNIT.cpp
*  @author welbon
*  @Email < libo.weng@gmail.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 "Stdafx.h"
#include "NAStringUNIT.h"
#include <malloc.h>
#include <windows.h>

std::wstring  CNAStringUNIT::NA_Utf8ToUnicode(const char *zFilename)
{
int nChar;
wchar_t *zWideFilename;

nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
int nSize = nChar*sizeof(zWideFilename[0]);
zWideFilename = new wchar_t[nSize+1];
if( zWideFilename==0 ){
return 0;
}
nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nSize);
if( nChar==0 ){
delete zWideFilename;
zWideFilename = 0;
}
else
zWideFilename[nChar] = 0;
std::wstring strFileName = zWideFilename;
delete []zWideFilename;
return strFileName;
}

/*
** Convert microsoft unicode to UTF-8.  Space to hold the returned string is
** obtained from sqliteMalloc().
*/
std::string CNAStringUNIT::NA_UnicodeToUtf8(const wchar_t *zWideFilename)
{
int nByte;
char *zFilename;

nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
zFilename = new char[nByte +1];
if( zFilename==0 ){
return 0;
}
nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte+1, 	0, 0);
if( nByte == 0 ){
delete zFilename;
zFilename = 0;
}
else zFilename[nByte]='/0';
std::string strFileName = zFilename;
delete []zFilename;
return strFileName;
}

/*
** 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 sqliteMalloc.
*/
std::wstring CNAStringUNIT::NA_MbcsToUnicode(const char *zFilename)
{
int nByte;
wchar_t *zMbcsFilename;
int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;

nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0);//*sizeof(wchar_t);
int nSize = nByte*sizeof(zMbcsFilename[0]);
zMbcsFilename = new wchar_t[nSize+1];
if( zMbcsFilename==0 ){
return 0;
}
nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nSize);
if( nByte==0 ){
delete zMbcsFilename;
zMbcsFilename = 0;
}
else
zMbcsFilename[nByte]=0;
std::wstring strFileName = zMbcsFilename;
delete []zMbcsFilename;
return strFileName;

}

/*
** Convert microsoft unicode to multibyte character string, based on the
** user's Ansi codepage.
**
** Space to hold the returned string is obtained from
** sqliteMalloc().
*/
std::string CNAStringUNIT::NA_UnicodeToMbcs(const wchar_t *zWideFilename)
{
int nByte;
char *zFilename;
int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;

nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
zFilename = new char[ nByte+1];
if( zFilename==0 ){
return 0;
}
nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte+1, 0, 0);
if( nByte == 0 ){
delete zFilename;
zFilename = 0;
}
else
zFilename[nByte]='/0';
std::string strFileName = zFilename;
delete []zFilename;
return strFileName;
}

/*
** Convert multibyte character string to UTF-8.  Space to hold the
** returned string is obtained from sqliteMalloc().
*/
std::string CNAStringUNIT::NA_MbcsToUtf8(const char *zFilename)
{
//  char *zFilenameUtf8;

std::wstring zTmpWide = NA_MbcsToUnicode(zFilename);
if( zTmpWide.empty() ){
return std::string("");
}
return NA_UnicodeToUtf8(zTmpWide.c_str());
}

/*
** Convert UTF-8 to multibyte character string.  Space to hold the
** returned string is obtained from sqliteMalloc().
*/
std::string CNAStringUNIT::NA_Utf8ToMbcs(const char *zFilename)
{

std::wstring zTmpWide = NA_Utf8ToUnicode(zFilename);
if( zTmpWide.empty() ){
return std::string("");
}
return NA_UnicodeToMbcs(zTmpWide.c_str());
}

/** 是否是邮箱地址 */
bool CNAStringUNIT::NA_isemail(const char *str)
{
if(!str)
return false;

const char *tmpstr = strchr(str, '@');
if(!tmpstr || tmpstr==str)
return false;

const char *t = str;
while(t!=tmpstr)
{
if(!isalnum(*t) && *t!='-' && *t!='_' && *t!='.')
break;
t++;
}
if(t!=tmpstr)
return false;

t++;
while(*t)
{
if(!isalnum(*t) && *t!='-' && *t!='.')
{
return false;
}
t++;
}

return true;
}
bool CNAStringUNIT::NA_isalpha(const char *str)
{
if(!str)
return false;

while(isalpha(*str))str++;
return *str=='/0';
}

bool CNAStringUNIT::NA_isalnum(const char* str)
{
if(!str)
return false;

while(isalnum(*str))str++;
return *str=='/0';
}

bool CNAStringUNIT::NA_isdigit(const char *str)
{
if(!str)
return false;

while(isdigit(*str))str++;

return *str=='/0';
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息