utf8_conv_result_t utf8_wchar(const std::string &utf8, std::wstring &wide) { // allocate space for worst-case wide.resize(utf8.size()); wchar_t const* dst_start = wide.c_str(); char const* src_start = utf8.c_str(); ConversionResult ret; // TODO: 3 refactor this to use wchar_t as a template // it would cause less code to be generated without // relying on dead-code elimination and fix msvc constant // expression warning if (sizeof(wchar_t) == sizeof(UTF32)) { ret = ConvertUTF8toUTF32((const UTF8**)&src_start, (const UTF8*)src_start + utf8.size(), (UTF32**)&dst_start, (UTF32*)dst_start + wide.size() , lenientConversion); wide.resize(dst_start - wide.c_str()); return (utf8_conv_result_t)ret; } else if (sizeof(wchar_t) == sizeof(UTF16)) { ret = ConvertUTF8toUTF16((const UTF8**)&src_start, (const UTF8*)src_start + utf8.size(), (UTF16**)&dst_start, (UTF16*)dst_start + wide.size() , lenientConversion); wide.resize(dst_start - wide.c_str()); return (utf8_conv_result_t)ret; } else { return source_illegal; } }
bool gbk_to_utf16( std::wstring& dest, const std::string& src ) { #ifdef _WIN32 dest.resize(src.size()); const string old_locale = setlocale(LC_CTYPE, NULL); setlocale(LC_CTYPE, ".936"); size_t unicodeLength = mbstowcs(const_cast<wchar_t*>(dest.data()), src.c_str(), src.size()); setlocale(LC_CTYPE, old_locale.c_str()); if (unicodeLength == -1) { dest = L""; return false; } dest.resize(unicodeLength); #else string strTmp; if (!gbk_to_utf8(strTmp, src)) return false; utf8_to_utf16(dest, strTmp); if (dest==L"") { return false; } #endif return true; }
inline int utf8_wchar(const std::string &utf8, std::wstring &wide) { // allocate space for worst-case wide.resize(utf8.size()); wchar_t const* dst_start = wide.c_str(); char const* src_start = utf8.c_str(); ConversionResult ret; if (sizeof(wchar_t) == sizeof(UTF32)) { ret = ConvertUTF8toUTF32((const UTF8**)&src_start, (const UTF8*)src_start + utf8.size(), (UTF32**)&dst_start, (UTF32*)dst_start + wide.size() , lenientConversion); wide.resize(dst_start - wide.c_str()); return ret; } else if (sizeof(wchar_t) == sizeof(UTF16)) { ret = ConvertUTF8toUTF16((const UTF8**)&src_start, (const UTF8*)src_start + utf8.size(), (UTF16**)&dst_start, (UTF16*)dst_start + wide.size() , lenientConversion); wide.resize(dst_start - wide.c_str()); return ret; } else { return sourceIllegal; } }
int ISO8601::ToString(const struct tm& value, std::wstring& str) { int len = MAX_DATETIME_LENGTH; str.resize(len, '\0'); int r = ISO8601::ToString(value, const_cast<wchar_t *>(str.c_str()), &len); str.resize(len); return (errno = r); }
static void readEntities(const std::string &strValue, std::wstring &strResult) { size_t size = 0; std::string strMulti; readEntities(strValue, strMulti); strResult.resize(strMulti.size()); mbstowcs_s(&size, &strResult[0], strResult.size(), strMulti.c_str(), strMulti.size()); strResult.resize(size); }
int UTF8::ToUnicode(const char *utf8_data, int len, std::wstring& str) { int wlen = (len + 1) * 4; str.resize(wlen, L'\0'); int r = UTF8::ToUnicode(utf8_data, len, (wchar_t *)str.c_str(), &wlen); if (r) return r; str.resize(wlen, L'\0'); return (errno = 0); }
bool token::codepage_to_wstring_append( uint cp, std::wstring& dst ) const { uints i = dst.length(); dst.resize(i+len()); wchar_t* data = const_cast<wchar_t*>(dst.data()); uint n = MultiByteToWideChar( cp, 0, ptr(), (uint)len(), data+i, (uint)len() ); dst.resize(i+n); return n>0; }
bool Reader::namedValue(const char *name, std::wstring &value) { std::string text; bool bOK = namedValue(name, text); if (bOK) { size_t size = text.size(); value.resize(' ', size * 2 + 1); mbstowcs_s(&size, &value[0], value.size(), text.c_str(), text.size()); value.resize(size); } return bOK; }
/////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// // // // Parse a date string // // /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// void dataHandlerDateTimeBase::parseDate( std::wstring dateString, imbxInt32* pYear, imbxInt32* pMonth, imbxInt32* pDay) { PUNTOEXE_FUNCTION_START(L"dataHandlerDateTimeBase::parseDate"); if(dateString.size()<8) dateString.resize(8, L'0'); std::wstring dateYear=dateString.substr(0, 4); std::wstring dateMonth=dateString.substr(4, 2); std::wstring dateDay=dateString.substr(6, 2); std::wistringstream yearStream(dateYear); yearStream >> (*pYear); std::wistringstream monthStream(dateMonth); monthStream >> (*pMonth); std::wistringstream dayStream(dateDay); dayStream >> (*pDay); PUNTOEXE_FUNCTION_END(); }
static LSTATUS get_value(HKEY hKey, const wchar_t* lpValueName, std::wstring& value) { DWORD type = REG_NONE, size = 0; LSTATUS res = RegQueryValueExW(hKey, lpValueName, NULL, &type, NULL, &size); size /= sizeof(wchar_t); if (res == ERROR_SUCCESS && ((type != REG_EXPAND_SZ && type != REG_SZ) || size > max_string_size)) return ERROR_INVALID_DATA; if (size == 0) return res; value.resize(size); res = RegQueryValueExW(hKey, lpValueName, NULL, &type, reinterpret_cast< LPBYTE >(&value[0]), &size); value.resize(std::wcslen(value.c_str())); // remove extra terminating zero return res; }
bool CRegUtil::GetValue(const wchar_t* const name, std::wstring& strValue) { const int nSize = 1024; std::vector<BYTE> buffer; DWORD dwLength = 0; DWORD dwType = REG_NONE; buffer.resize(nSize); void* pBuffer = &buffer[0]; LONG lResult = ::RegQueryValueEx(m_hKey, name, 0, &dwType, (LPBYTE)pBuffer, &dwLength); if((lResult != ERROR_MORE_DATA && lResult != ERROR_SUCCESS) || (dwType != REG_SZ) || dwLength == 0) { return false; } if(lResult == ERROR_MORE_DATA) { buffer.resize(dwLength); pBuffer = &buffer[0]; lResult = ::RegQueryValueEx(m_hKey, name, 0, &dwType, (LPBYTE)pBuffer, &dwLength); } if(lResult != ERROR_SUCCESS) return false; strValue.assign((const wchar_t*)pBuffer, dwLength / sizeof(wchar_t)); if(strValue.at(strValue.size() - 1) == 0) strValue.resize(strValue.size() - 1); return true; }
bool Cx_StringTable::GetValue(std::wstring& value, const std::wstring& module, const std::wstring& id) { if (!m_loaded) { LoadFiles(x3::GetTranslationsPath(L"strings")); LoadFiles(x3::RelToAbsWithPlugin(L"../translations/strings", false)); } IT_ITEM it = Find(module); value.resize(0); if (it != m_groups.end()) { Cx_ConfigSection group(it->group); ASSERT(group.IsNotNull()); Cx_ConfigSection sec(group.GetSection(L"string", L"id", id.c_str(), false)); value = sec->GetString(L"value"); ReplaceLf(value); } return !value.empty(); }
/* ** Copies files and folders from one location to another. ** */ bool System::CopyFiles(std::wstring from, std::wstring to, bool bMove) { // If given "from" path ends with path separator, remove it (Workaround for XP: error code 1026) size_t len; while (len = from.size(), len > 0 && PathUtil::IsSeparator(from[len - 1])) { from.resize(len - 1); } // The strings must end with double \0 from.append(1, L'\0'); to.append(1, L'\0'); SHFILEOPSTRUCT fo = { nullptr, bMove ? FO_MOVE : FO_COPY, from.c_str(), to.c_str(), FOF_NO_UI | FOF_NOCONFIRMATION | FOF_ALLOWUNDO }; int result = SHFileOperation(&fo); if (result != 0) { LogErrorF(L"Copy error: From %s to %s (%i)", from.c_str(), to.c_str(), result); return false; } return true; }
int _tmain(int argc, _TCHAR* argv[]) { dir = argv[0]; dir.resize(dir.find_last_of(_T("\\")) + 1); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
/* ** GetFromLetras ** ** Download lyrics from Letras. ** */ bool CLyrics::GetFromLetras(const std::wstring& artist, const std::wstring& title, std::wstring& data) { bool ret = false; std::wstring url = L"http://letras.terra.com.br/winamp.php?musica=" + title; url += L"&artista="; url += artist; data = CInternet::DownloadUrl(url, CP_ACP); if (!data.empty()) { std::wstring::size_type pos = data.find(L"\"letra\""); pos = data.find(L"<p>", pos); if (pos != std::wstring::npos) { pos += 3; data.erase(0, pos); pos = data.find(L"</p>"); data.resize(pos); CInternet::DecodeReferences(data); while ((pos = data.find(L"<br/>"), pos) != std::wstring::npos) { data.erase(pos, 5); } ret = true; } } return ret; }
// vsprintfのUNICODE(std::wstring)版 int unicode_format_va( std::wstring& str,LPCWSTR format,va_list va ) { int len=_vscwprintf( format,va ); str.resize( len ); _vsnwprintf_s( const_cast<wchar_t*>(str.c_str()),_TRUNCATE,str.size(),format,va ); return len; }
utf8_conv_result_t utf8_wchar(std::string const& utf8, std::wstring &wide) { // allocate space for worst-case wide.resize(utf8.size()); char const* src_start = utf8.c_str(); return convert_to_wide<sizeof(wchar_t)>::convert( &src_start, src_start + utf8.size(), wide); }
std::wstring chomp(std::wstring s) { size_t n = s.size(); while (n > 0 && s[n - 1] < L' ') --n; s.resize(n); return s; }
bool Cx_TextUtil::ReadTextFile(BYTE head[5], std::wstring& content, const std::wstring& filename, ULONG nLenLimitMB, UINT codepage) { ZeroMemory(head, sizeof(BYTE) * 5); content.resize(0); bool bRet = false; HANDLE hFile = ::CreateFileW(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if (INVALID_HANDLE_VALUE == hFile) { LOG_ERROR2(LOGHEAD L"IDS_OPEN_FAIL", filename << L", " << GetSystemErrorString(GetLastError())); } else { DWORD dwLength = ::GetFileSize(hFile, NULL); HGLOBAL hBuffer = NULL; if (dwLength != INVALID_FILE_SIZE) { if (dwLength > nLenLimitMB * 1024L * 1024L) { LOG_WARNING2(LOGHEAD L"IDS_HUGE_FILE", (dwLength / (1024.0*1024.0)) << L"MB, " << filename); dwLength = nLenLimitMB * 1024L * 1024L; } hBuffer = GlobalAlloc(GHND, dwLength + 8); } if (hBuffer != NULL) { LPBYTE pBuffer = (LPBYTE)GlobalLock(hBuffer); if (pBuffer != NULL) { DWORD dwBytesRead = 0; ::ReadFile(hFile, pBuffer, dwLength, &dwBytesRead, NULL); if (dwBytesRead > 0) { CopyMemory(head, pBuffer, sizeof(BYTE) * min(5, dwBytesRead)); bRet = GetFileContent(content, pBuffer, dwBytesRead, codepage); if (!bRet) { LOG_WARNING2(LOGHEAD L"IDS_NOT_ANSIFILE", filename); } } GlobalUnlock(hBuffer); } GlobalFree(hBuffer); } ::CloseHandle(hFile); } return bRet; }
void rtrim( std::wstring &s ) { std::wstring::size_type pos( s.find_last_not_of( L" \t" ) ); if( pos != std::wstring::npos ) { s.resize( pos+1 ); } else { s.clear(); } }
static void ansi_wide(std::string &ansi, std::wstring &wide) { if (ansi.size() == 0) return ; wide.resize(ansi.size()); std::size_t size = ansi.size(); mbstowcs((wchar_t*)wide.c_str(), ansi.c_str(), size); }
//---------------------------------------------------------------------------------- // 将单字符 string 转换为宽字符 wstring inline void Ascii2WideString( const std::string& szStr, std::wstring& wszStr ) { int nLength = MultiByteToWideChar( CP_ACP, 0, szStr.c_str(), -1, NULL, NULL ); wszStr.resize(nLength); LPWSTR lpwszStr = new wchar_t[nLength]; MultiByteToWideChar( CP_ACP, 0, szStr.c_str(), -1, lpwszStr, nLength ); wszStr = lpwszStr; delete [] lpwszStr; }
HRESULT StripFormatSpecifier( std::wstring& text, FormatOptions& fmtopt ) { size_t textlen = text.size(); if( textlen > 2 && text[textlen - 2] == ',') { fmtopt.specifier = text[textlen - 1]; text.resize(textlen - 2); } return S_OK; }
bool string_to_wstring(const std::string &str,std::wstring &wstr) { int nLen = (int)str.length(); wstr.resize(nLen,L' '); int nResult = MultiByteToWideChar(CP_ACP,0,(LPCSTR)str.c_str(),nLen,(LPWSTR)wstr.c_str(),nLen); if (nResult == 0) { return false; } return true; }
void strprintf(std::wstring& sOut, const wchar_t* fmt, ...) { if (sOut.size() < maxFormatString) { sOut.resize(maxFormatString); } va_list args; va_start(args, fmt); _vsnwprintf_s((wchar_t*)sOut.data(), sOut.size(), maxFormatString, fmt, args); va_end(args); }
// If 'str' ends with a colon followed by some digits, then remove the colon and digits. For example: // "c:\bob\abc.txt:123:456" will be changed to "c:\bob\abc.txt:123" // "c:\bob\abc.txt:123" will be changed to "c:\bob\abc.txt" // "c:\bob\abc.txt" will not be changed // (Note: we could do this with a regular expression, but there is no regex library currently // built into brackets-shell, and I don't want to add one just for this simple case). void StripColonNumber(std::wstring& str) { bool gotDigits = false; int index; for (index = str.size() - 1; index >= 0; index--) { if (!isdigit(str[index])) break; gotDigits = true; } if (gotDigits && index >= 0 && str[index] == ':') { str.resize(index); } }
std::wstring Gosu::Win::appDirectory() { static std::wstring result; if (!result.empty()) return result; result = appFilename(); std::wstring::size_type lastDelim = result.find_last_of(L"\\/"); if (lastDelim != std::wstring::npos) result.resize(lastDelim + 1); else result = L""; return result; }
BOOL PluginPlayer::StringToWString(const std::string &str,std::wstring &wstr) { LOG4CPLUS_WARN(m_rtspClient.pTestLogger,"StringToWString1"); int nLen = (int)str.length(); wstr.resize(nLen,L' '); int nResult = MultiByteToWideChar(CP_ACP,0,(LPCSTR)str.c_str(),nLen,(LPWSTR)wstr.c_str(),nLen); if (nResult == 0) { LOG4CPLUS_WARN(m_rtspClient.pTestLogger,"StringToWString2"); return FALSE; } LOG4CPLUS_WARN(m_rtspClient.pTestLogger,"StringToWString22"); return TRUE; }
bool SplitPathBasenameExtension( const std::wstring& filename, std::wstring& path, std::wstring& basename, std::wstring& extension ) { if (filename.empty()) { ERROUT("Empty filename passed in"); return false; } // // Separate the basename and path, looking for special cases // std::wstring::size_type found = filename.find_last_of(L"/\\"); // Trailing directory separator = no basename (or extension) if (found == filename.length() - 1) { path = filename; basename = L""; extension = L""; return true; } // Does filename hae no preceding path? if (found == std::wstring::npos) { path = L""; basename = filename; } else { // Path is present path = filename.substr(0, found + 1); basename = filename.substr(found + 1); } // // Parse extension from basename // found = basename.rfind(L"."); // Leading period, trailing period, or no period = no extension if ( found == 0 || found == basename.length() - 1 || found == std::wstring::npos ) { extension = L""; return true; } extension = basename.substr(found + 1); basename.resize(found); return true; }
void wideCut(std::wstring &ws, size_t max_length) { size_t i = 0; int remained_len = max_length; for (; i < ws.length(); ++i) { remained_len -= wcwidth(ws[i]); if (remained_len < 0) { ws.resize(i); break; } } }