|
|
/*==================================================================================================================
|
|
|
Copyright(c) 2012 ORIGIN.
|
|
|
Unpublished - All rights reserved
|
|
|
====================================================================================================================
|
|
|
File description:
|
|
|
Filename: string_utils.c
|
|
|
Module : Common module.
|
|
|
|
|
|
This file includes some operations of the string.
|
|
|
|
|
|
====================================================================================================================
|
|
|
Date Name Description of Change
|
|
|
3-Feb-2015 Ray li Initialize creation
|
|
|
$HISTORY$
|
|
|
===================================================================================================================*/
|
|
|
#ifndef _cplusplus
|
|
|
#ifndef _CRT_SECURE_NO_DEPRECATE
|
|
|
#define _CRT_SECURE_NO_DEPRECATE
|
|
|
#endif
|
|
|
#endif
|
|
|
|
|
|
//#include <tc/tc_util.h>
|
|
|
//#include <itk/mem.h>
|
|
|
//#include <stdlib.h>
|
|
|
//#include <ctype.h>
|
|
|
#include "string_utils.h"
|
|
|
#include <io.h>
|
|
|
|
|
|
void Split( string strArg, string spliter, vector<string> &ans )
|
|
|
{
|
|
|
ans.clear();
|
|
|
size_t index0;
|
|
|
string one_arg;
|
|
|
if ( strArg.find_first_not_of(' ') == string::npos )
|
|
|
strArg = "";
|
|
|
while( strArg.size()>0 )
|
|
|
{
|
|
|
index0 = strArg.find(spliter);
|
|
|
//printf(" index0:%d", (int)index0);
|
|
|
if( index0 != string::npos )
|
|
|
{
|
|
|
if (index0 == 0) {
|
|
|
strArg = strArg.substr(index0 + spliter.size());
|
|
|
ans.push_back("");
|
|
|
}
|
|
|
else{
|
|
|
one_arg = strArg.substr(0, index0);
|
|
|
strArg = strArg.substr(index0 + spliter.size());
|
|
|
ans.push_back(one_arg);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
ans.push_back( strArg );
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
/*void Split(string strArg, char spliter, vector<string> &ans)
|
|
|
{
|
|
|
ans.clear();
|
|
|
size_t index0 = 0;
|
|
|
string one_arg;
|
|
|
if (strArg.find_first_not_of(' ') == string::npos)
|
|
|
strArg = "";
|
|
|
while (strArg.size() > 0)
|
|
|
{
|
|
|
index0 = strArg.find_first_of(spliter);
|
|
|
if (index0 != string::npos)
|
|
|
{
|
|
|
one_arg = strArg.substr(0, index0);
|
|
|
strArg = strArg.substr(index0 + 1);
|
|
|
ans.push_back(one_arg);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
ans.push_back(strArg);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}*/
|
|
|
char* GSTR_clone( char **dst, const char *src )
|
|
|
{
|
|
|
char *retVal = NULL;
|
|
|
int srcLen = 0;
|
|
|
|
|
|
*dst = NULL;
|
|
|
if (src == NULL)
|
|
|
return NULL;
|
|
|
|
|
|
srcLen = (int)tc_strlen( src ) + 1;
|
|
|
*dst = (char*)MEM_alloc( srcLen * sizeof(char) );
|
|
|
retVal = tc_strncpy( *dst, src, srcLen );
|
|
|
(*dst)[srcLen - 1] = '\0';
|
|
|
|
|
|
return retVal;
|
|
|
}
|
|
|
|
|
|
char* GSTR_copy( char *dst, const char *src, int dstSize )
|
|
|
{
|
|
|
char *retVal = tc_strncpy( dst, src, dstSize );
|
|
|
dst[dstSize - 1] = '\0';
|
|
|
return retVal;
|
|
|
}
|
|
|
|
|
|
char* GSTR_int_to_string( char **dst, int value )
|
|
|
{
|
|
|
char strVal[128 + 1];
|
|
|
|
|
|
*dst = NULL;
|
|
|
memset( strVal, 0, sizeof(strVal)/sizeof(char) );
|
|
|
sprintf( strVal, "%d", value );
|
|
|
|
|
|
return GSTR_clone( dst, strVal );
|
|
|
}
|
|
|
|
|
|
void GSTR_format_int_to_string( char *dst, int digitNum, int value )
|
|
|
{
|
|
|
char sNum[WSO_desc_size_c + 1];
|
|
|
sprintf( sNum, "%%0%dd", digitNum );
|
|
|
sprintf( dst, sNum, value );
|
|
|
}
|
|
|
|
|
|
void GSTR_format_string( const char *dst, int m, const char *fill_char, char **out )
|
|
|
{
|
|
|
char sNum[WSO_name_size_c + 1] = {0};
|
|
|
char sNew[WSO_name_size_c + 1] = {0};
|
|
|
sprintf( sNum, "%%%d.%ds", m, m );
|
|
|
sprintf( sNew, sNum, dst );
|
|
|
STRNG_replace_str( sNew, " ", fill_char, out );
|
|
|
}
|
|
|
|
|
|
|
|
|
char* GSTR_string_append( const char *s1, const char *s2 )
|
|
|
{
|
|
|
char *s = NULL;
|
|
|
if (s1 == NULL || s2 == NULL)
|
|
|
{
|
|
|
GSTR_clone(&s, s1 == NULL ? (s2 == NULL ? "" : s2) : s1 );
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
int size = (int)tc_strlen(s1) + (int)tc_strlen(s2) + 1;
|
|
|
s = (char *)MEM_alloc( size );
|
|
|
tc_strcpy( s, s1 );
|
|
|
tc_strcat( s, s2 );
|
|
|
s[size - 1] = '\0';
|
|
|
}
|
|
|
return s;
|
|
|
}
|
|
|
|
|
|
logical GSTR_is_float(const char *str)
|
|
|
{
|
|
|
logical isfloat = true;
|
|
|
char *pStr = (char *)str;
|
|
|
logical hasPositive = false;
|
|
|
logical hasMinus = false;
|
|
|
logical hasDot = false;
|
|
|
|
|
|
if (str == NULL)
|
|
|
return false;
|
|
|
|
|
|
while (*pStr != '\0' && isfloat == true)
|
|
|
{
|
|
|
if ( (*pStr >= '0' && *pStr <= '9'))
|
|
|
{
|
|
|
//continue;
|
|
|
}
|
|
|
else if ( *pStr == '+' )
|
|
|
{
|
|
|
isfloat = (hasPositive ? false : (hasPositive = true));
|
|
|
}
|
|
|
else if ( *pStr == '-' )
|
|
|
{
|
|
|
isfloat = (hasMinus ? false : (hasMinus = true));
|
|
|
}
|
|
|
else if ( *pStr == '.' )
|
|
|
{
|
|
|
isfloat = (hasDot ? false : (hasDot = true));
|
|
|
}
|
|
|
else
|
|
|
isfloat = false;
|
|
|
|
|
|
pStr ++;
|
|
|
}
|
|
|
return isfloat;
|
|
|
}
|
|
|
|
|
|
logical GSTR_is_number(const char *str)
|
|
|
{
|
|
|
logical is_number = true;
|
|
|
char *pStr = (char *)str;
|
|
|
if (str == NULL)
|
|
|
return false;
|
|
|
|
|
|
while (*pStr != '\0')
|
|
|
{
|
|
|
if ( !( (*pStr >= '0' && *pStr <= '9') || *pStr == '-' ) )
|
|
|
{
|
|
|
is_number = false;
|
|
|
break;
|
|
|
}
|
|
|
pStr ++;
|
|
|
}
|
|
|
return is_number;
|
|
|
}
|
|
|
|
|
|
logical GSTR_is_ascii(char ch)
|
|
|
{
|
|
|
return ((unsigned int)ch) < 128;
|
|
|
}
|
|
|
|
|
|
int GSTR_trim_l( char *str, char s )
|
|
|
{
|
|
|
int count = 0;
|
|
|
char *pointer = str, *poffset = NULL;
|
|
|
if (str == NULL || str[0] == '\0')
|
|
|
return 0;
|
|
|
|
|
|
while ( *pointer != '\0' )
|
|
|
{
|
|
|
if ( *pointer != s )
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
count++;
|
|
|
pointer++;
|
|
|
}
|
|
|
if (count == 0)
|
|
|
return 0;
|
|
|
|
|
|
poffset = str + count;
|
|
|
pointer = str;
|
|
|
while ( *poffset != '\0' )
|
|
|
{
|
|
|
*pointer = *poffset;
|
|
|
pointer ++;
|
|
|
poffset ++;
|
|
|
}
|
|
|
*pointer = '\0';
|
|
|
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
int GSTR_trim_r( char *str, char s )
|
|
|
{
|
|
|
int count = 0;
|
|
|
char *pointer = NULL;
|
|
|
if (str == NULL || str[0] == '\0')
|
|
|
return 0;
|
|
|
|
|
|
pointer = str + ((int) strlen(str) - 1);
|
|
|
|
|
|
while ( pointer != str )
|
|
|
{
|
|
|
if ( *pointer != s )
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
*pointer = '\0';
|
|
|
|
|
|
count++;
|
|
|
pointer--;
|
|
|
}
|
|
|
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
void GSTR_trim_float( char *floatValue )
|
|
|
{
|
|
|
if ( !IS_EMPTY(floatValue) && tc_strstr(floatValue, ".") != NULL )
|
|
|
{
|
|
|
int len = 0;
|
|
|
GSTR_trim_r(floatValue, '0');
|
|
|
len = (int)tc_strlen(floatValue);
|
|
|
if (floatValue[ len - 1 ] == '.')
|
|
|
floatValue[ len - 1 ] = '\0';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int contains(const char *parent, char target) {
|
|
|
for (int i = 0; i < strlen(parent); i++) {
|
|
|
if (parent[i] == target)
|
|
|
return i;
|
|
|
}
|
|
|
return -1;
|
|
|
}
|
|
|
bool contains1(char **array, char *str) {
|
|
|
//printf("containsA:%d==%d", sizeof(array), sizeof(array[0]));
|
|
|
for (int i = 0; i < sizeof(array) / sizeof(array[0]); i++) {
|
|
|
//printf(" containsA:%s==%s\n", array[i], str);
|
|
|
if (strcmp(array[i], str) == 0)
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
bool contains2(char **array, int len, const char *str) {
|
|
|
printf("containsB:%d", len);
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
printf(" containsB:%s==%s\n", array[i], str);
|
|
|
if (strcmp(array[i], str) == 0)
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
bool contains3(vector<string> array, const char *str) {
|
|
|
printf("contains3:%d", array.size());
|
|
|
for (int i = 0; i < array.size(); i++) {
|
|
|
printf(" contains3:%s==%s\n", array[i].c_str(), str);
|
|
|
if (strcmp(array[i].c_str(), "null") == 0) {
|
|
|
if (strcmp("", str) == 0)
|
|
|
return true;
|
|
|
}else if (strcmp(array[i].c_str(), str) == 0)
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
string replace(string str, string s1, string s2) {
|
|
|
int pos;
|
|
|
pos = str.find(s1);
|
|
|
while (pos != -1) {
|
|
|
// str.length()<29><><EFBFBD>ַ<EFBFBD><D6B7>ij<EFBFBD><C4B3>ȣ<EFBFBD>ע<EFBFBD><D7A2>str<74><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>string<6E><67><EFBFBD><EFBFBD>
|
|
|
str.replace(pos, string(s1).length(), s2);
|
|
|
pos = str.find(s1);
|
|
|
}
|
|
|
return str;
|
|
|
}
|
|
|
|
|
|
string GbkToUtf8(const char *src_str) {
|
|
|
int len = MultiByteToWideChar(CP_ACP, 0, src_str, -1, NULL, 0);
|
|
|
wchar_t* wstr = new wchar_t[len + 1];
|
|
|
memset(wstr, 0, len + 1);
|
|
|
MultiByteToWideChar(CP_ACP, 0, src_str, -1, wstr, len);
|
|
|
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
|
|
|
char* str = new char[len + 1];
|
|
|
memset(str, 0, len + 1);
|
|
|
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
|
|
|
string strTemp = str;
|
|
|
if (wstr) delete[] wstr;
|
|
|
if (str) delete[] str;
|
|
|
return strTemp;
|
|
|
}
|
|
|
string stringToUTF8(const std::string str)
|
|
|
{
|
|
|
int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
|
|
|
|
|
|
wchar_t * pwBuf = new wchar_t[nwLen + 1];//һ<><D2BB>Ҫ<EFBFBD><D2AA>1<EFBFBD><31><EFBFBD><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2>
|
|
|
ZeroMemory(pwBuf, nwLen * 2 + 2);
|
|
|
|
|
|
::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
|
|
|
|
|
|
int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
|
|
|
|
|
|
char * pBuf = new char[nLen + 1];
|
|
|
ZeroMemory(pBuf, nLen + 1);
|
|
|
|
|
|
::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
|
|
|
|
|
|
std::string retStr(pBuf);
|
|
|
|
|
|
delete[]pwBuf;
|
|
|
delete[]pBuf;
|
|
|
|
|
|
pwBuf = NULL;
|
|
|
pBuf = NULL;
|
|
|
|
|
|
return retStr;
|
|
|
}
|
|
|
|
|
|
string Utf8ToGbk(const char *src_str) {
|
|
|
int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0);
|
|
|
wchar_t* wszGBK = new wchar_t[len + 1];
|
|
|
memset(wszGBK, 0, len * 2 + 2);
|
|
|
MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wszGBK, len);
|
|
|
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
|
|
|
char* szGBK = new char[len + 1];
|
|
|
memset(szGBK, 0, len + 1);
|
|
|
WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
|
|
|
string strTemp(szGBK);
|
|
|
if (wszGBK) delete[] wszGBK;
|
|
|
if (szGBK) delete[] szGBK;
|
|
|
return strTemp;
|
|
|
}
|
|
|
void createDir(){
|
|
|
string folderPath = "";
|
|
|
folderPath = getenv("TEMP");
|
|
|
folderPath.append("\\tmplog");
|
|
|
if (!access(folderPath.c_str(), 0))
|
|
|
{
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>·<EFBFBD><EFBFBD>");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
string command = "mkdir -p ";
|
|
|
command = command.append(folderPath);
|
|
|
try
|
|
|
{
|
|
|
system(command.c_str());
|
|
|
}
|
|
|
catch (...)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
}
|
|
|
} |