wstring string_to_wstring( string& s )
{
if( s.empty() ) return _T("");
int retval = ::MultiByteToWideChar(CP_ACP,0,s.c_str(),s.size(),0,0);
wstring re;
re.resize( retval );
::MultiByteToWideChar(CP_ACP,0,s.c_str(),s.size(),&re[0],re.size());
return re;
}
string wstring_to_string( wstring& ws )
{
if( ws.empty() ) return "";
int retval = ::WideCharToMultiByte(CP_ACP,0,ws.c_str(),ws.size(),0,0,0,0);
string re;
re.resize( retval ); ::WideCharToMultiByte(CP_ACP,0,ws.c_str(),ws.size(),&re[0],re.size(),0,0);
return re;
}
string wstring_to_string( wchar_t* ws, size_t sz )
{
if( ws == NULL || sz == 0 ) return "";
int retval = ::WideCharToMultiByte(CP_ACP,0,ws,sz,0,0,0,0);
string re;
re.resize( retval ); ::WideCharToMultiByte(CP_ACP,0,ws,sz,&re[0],re.size(),0,0);
return re;
}
wstring utf8_to_wstring( string& s )
{
if( s.empty() ) return _T("");
int retval = ::MultiByteToWideChar(CP_UTF8,0,s.c_str(),s.size(),0,0);
wstring re;
re.resize( retval ); ::MultiByteToWideChar(CP_UTF8,0,s.c_str(),s.size(),&re[0],re.size());
return re;
}
string wstring_to_utf8( wstring& ws )
{
if( ws.empty() ) return "";
int retval = ::WideCharToMultiByte(CP_UTF8,0,ws.c_str(),ws.size(),0,0,0,0);
string re;
re.resize( retval ); ::WideCharToMultiByte(CP_UTF8,0,ws.c_str(),ws.size(),&re[0],re.size(),0,0);
return re;
}
string wstring_to_utf8( const wchar_t* ws, size_t sz )
{
if( ws == NULL || sz == 0 ) return "";
int retval = ::WideCharToMultiByte(CP_UTF8,0,ws,sz,0,0,0,0);
string re;
re.resize( retval ); ::WideCharToMultiByte(CP_UTF8,0,ws,sz,&re[0],re.size(),0,0);
return re;
}
string from_base64(string &base64)
{
int nSize = ATL::Base64DecodeGetRequiredLength(base64.size());
byte* pData = new byte[nSize+1];
ATL::Base64Decode(base64.c_str(), base64.size(), pData, &nSize);
pData[nSize] = 0;
string str = (char *)pData;
delete []pData;
return wstring_to_string(utf8_to_wstring(str));
}
wstring utf8_to_unicode(const string& utf8string)
{
int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);
if (widesize == ERROR_NO_UNICODE_TRANSLATION)
{
throw exception("Invalid UTF-8 sequence.");
}
if (widesize == 0)
{
throw exception("Error in conversion.");
}
vector<wchar_t> resultstring(widesize);
int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize);
if (convresult != widesize)
{
throw exception("La falla!");
}
return wstring(&resultstring[0]);
}
wstring acsi_to_widebyte(string& strascii)
{
int widesize = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);
if (widesize == ERROR_NO_UNICODE_TRANSLATION)
{
throw exception("Invalid UTF-8 sequence.");
}
if (widesize == 0)
{
throw exception("Error in conversion.");
}
vector<wchar_t> resultstring(widesize);
int convresult = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);
if (convresult != widesize)
{
throw exception("La falla!");
}
return wstring(&resultstring[0]);
}
string unicode_to_utf8(const wstring& widestring)
{
int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);
if (utf8size == 0)
{
throw exception("Error in conversion.");
}
vector<char> resultstring(utf8size);
int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);
if (convresult != utf8size)
{
throw exception("La falla!");
}
return string(&resultstring[0]);
}
string ascii_to_utf8(string& strAsciiCode)
{
string strRet("");
wstring wstr = acsi_to_widebyte(strAsciiCode);
strRet = unicode_to_utf8(wstr);
return strRet;
}
int split( const string& str,vector<string>& subs, const char& sep )
{
string temp = str;
temp += sep;
int count = 0;
size_t pos = temp.find( sep );
size_t from = 0;
while ( pos != temp.npos )
{
string sub_str = temp.substr( from, pos - from );
{
++ count;
subs.push_back( sub_str );
}
from = pos + 1;
pos = temp.find( sep, from );
}
return count;
}
int splitw( const wstring& str, vector<wstring>& subs, const wchar_t& sep )
{
wstring temp = str;
temp += sep;
int count = 0;
size_t pos = temp.find( sep );
size_t from = 0;
while ( pos != temp.npos )
{
wstring sub_str = temp.substr( from, pos - from );
{
++ count;
subs.push_back( sub_str );
}
from = pos + 1;
pos = temp.find( sep, from );
}
return count;
}
string& replace_all_s(string& str, const string& old_value,const string& new_value)
{
while(true)
{
string::size_type pos(0);
if( (pos = str.find( old_value ) )!=string::npos )
str.replace(pos,old_value.length(),new_value);
else
break;
}
return str;
}
wstring& replace_all_ws(wstring& str, const wstring& old_value,const wstring& new_value)
{
while(true)
{
wstring::size_type pos(0);
if( (pos = str.find( old_value ) )!=wstring::npos )
str.replace(pos,old_value.length(),new_value);
else
break;
}
return str;
}
string& replace_all_distinct_s(string& str,const string& old_value,const string& new_value)
{
for(string::size_type pos(0); pos!=string::npos; pos+=new_value.length())
{
if( (pos=str.find(old_value,pos))!=string::npos )
str.replace(pos,old_value.length(),new_value);
else break;
}
return str;
}
wstring& replace_all_distinct_ws(wstring& str,const wstring& old_value,const wstring& new_value)
{
for(wstring::size_type pos(0); pos!=wstring::npos; pos+=new_value.length() )
{
if( (pos=str.find(old_value,pos))!=wstring::npos )
str.replace(pos,old_value.length(),new_value);
else
break;
}
return str;
}