BOOL CRegKey::EnumKey(DWORD iSubkey,CStringW& strName,LPWSTR lpszClass,LPDWORD lpcchClass,PFILETIME lpftLastWrite) const { if (IsUnicodeSystem()) { DWORD cb=100,cb2=cb; LONG ret=::RegEnumKeyExW(m_hKey,iSubkey,strName.GetBuffer(cb/2),&cb2,0,lpszClass,lpcchClass,lpftLastWrite); while (ret==ERROR_MORE_DATA) { cb+=100; cb2=cb; ret=::RegEnumKeyExW(m_hKey,iSubkey,strName.GetBuffer(cb/2),&cb2,0,lpszClass,lpcchClass,lpftLastWrite); } if (ret==ERROR_SUCCESS) { strName.FreeExtra(cb2); return TRUE; } return FALSE; } else { DWORD cb=100,cb2=cb; char* pClass=NULL; DWORD dwClass=0; if (lpszClass!=NULL) { pClass=new char[*lpcchClass+2]; dwClass=*lpcchClass; } char* pName=new char[cb]; LONG ret=::RegEnumKeyEx(m_hKey,iSubkey,pName,&cb2,0,pClass,&dwClass,lpftLastWrite); while (ret==ERROR_MORE_DATA) { cb+=100; cb2=cb; delete[] pName; pName=new char[cb]; ret=::RegEnumKeyEx(m_hKey,iSubkey,pName,&cb2,0,pClass,&dwClass,lpftLastWrite); } if (ret!=ERROR_SUCCESS) { delete[] pName; if (pClass!=NULL) delete[] pClass; return FALSE; } strName=pName; if (pClass!=NULL) { MultiByteToWideChar(CP_ACP,0,pClass,dwClass,lpszClass,*lpcchClass); delete[] pClass; } return TRUE; } }
BOOL CStdioFileEx::ReadWideString(CStringW& rString) { _ASSERTE(m_pStream); rString = L"";// empty string without deallocating if(m_bIsUnicodeText) { // If at position 0, discard byte-order mark before reading if(GetPosition() == 0) { wchar_t bom; Read(&bom, sizeof(wchar_t)); } const int nMaxSize = 128; LPWSTR lpsz = rString.GetBuffer(nMaxSize); LPWSTR lpszResult; int nLen = 0; for (;;) { lpszResult = fgetws(lpsz, nMaxSize+1, m_pStream); rString.ReleaseBuffer(); // handle error/eof case if (lpszResult == NULL && !feof(m_pStream)) { Afx_clearerr_s(m_pStream); AfxThrowFileException(CFileException::genericException, _doserrno, m_strFileName); } // if string is read completely or EOF if (lpszResult == NULL || (nLen = (int)lstrlenW(lpsz)) < nMaxSize || lpsz[nLen-1] == '\n') break; nLen = rString.GetLength(); lpsz = rString.GetBuffer(nMaxSize + nLen) + nLen; } //remove crlf if exist. nLen = rString.GetLength(); if (nLen > 1 && rString.Mid(nLen-2) == L"\r\n") { rString.GetBufferSetLength(nLen-2); } return rString.GetLength() > 0; } else { CStringA ansiString; BOOL bRetval = ReadAnsiString(ansiString); //setlocale(LC_ALL, "chs_chn.936");//no need rString = ansiString; return bRetval; } }
BOOL ShellFunctions::RunRegistryCommand(HKEY hKey,LPCWSTR szFile) { if (!IsUnicodeSystem()) return RunRegistryCommand(hKey,W2A(szFile)); PROCESS_INFORMATION pi; STARTUPINFOW si; CRegKey CommandKey; CStringW ExecuteStr; CStringW CommandLine; int i; if (CommandKey.OpenKey(hKey,"command",CRegKey::openExist|CRegKey::samAll)!=ERROR_SUCCESS) return FALSE; if (CommandKey.QueryValue(L"",ExecuteStr)<2) return FALSE; i=ExecuteStr.FindFirst(L'%'); while (i!=-1) { if (ExecuteStr[i+1]==L'1') { CommandLine.Copy(ExecuteStr,i); CommandLine<<szFile; CommandLine<<((LPCWSTR)ExecuteStr+i+2); break; } i=ExecuteStr.FindNext(L'%',i); } if (i==-1) CommandLine=ExecuteStr; if (!ExpandEnvironmentStringsW(CommandLine,ExecuteStr.GetBuffer(1000),1000)) ExecuteStr.Swap(CommandLine); si.cb=sizeof(STARTUPINFOW); si.lpReserved=NULL; si.cbReserved2=0; si.lpReserved2=NULL; si.lpDesktop=NULL; si.lpTitle=NULL; si.dwFlags=STARTF_USESHOWWINDOW; si.wShowWindow=SW_SHOWDEFAULT; if (!CreateProcessW(NULL,ExecuteStr.GetBuffer(),NULL, NULL,FALSE,CREATE_DEFAULT_ERROR_MODE|NORMAL_PRIORITY_CLASS, NULL,NULL,&si,&pi)) { CommandKey.CloseKey(); return FALSE; } CloseHandle(pi.hThread); CloseHandle(pi.hProcess); CommandKey.CloseKey(); return TRUE; }
CStringW DecodeDoubleEncodedUtf8(LPCWSTR pszFileName) { size_t nChars = wcslen(pszFileName); // Check if all characters are valid for UTF-8 value range // for (UINT i = 0; i < nChars; i++) { if ((_TUCHAR)pszFileName[i] > 0xFFU) return pszFileName; // string is already using Unicode character value range; return original } // Transform Unicode string to UTF-8 byte sequence // CStringA strA; #pragma warning(disable : 4267) LPSTR pszA = strA.GetBuffer(nChars); for (UINT i = 0; i < nChars; i++) pszA[i] = (CHAR)pszFileName[i]; strA.ReleaseBuffer(nChars); // Decode the string with UTF-8 // CStringW strW; LPWSTR pszW = strW.GetBuffer(nChars); int iNewChars = utf8towc(strA, nChars, pszW, nChars); #pragma warning(disable : 4267) if (iNewChars < 0) { strW.ReleaseBuffer(0); return pszFileName; // conversion error (not a valid UTF-8 string); return original } strW.ReleaseBuffer(iNewChars); return strW; }
CStringW InformApp::GetProfileString(LPCSTR section, LPCWSTR entry, LPCWSTR defaultValue) { if (theOS.IsWindows9X()) { CString entryA(entry), defaultValueA(defaultValue); return CStringW(CWinApp::GetProfileString(section,entryA,defaultValueA)); } if (m_pszRegistryKey == NULL) return defaultValue; HKEY secKey = GetSectionKey(section); if (secKey == NULL) return defaultValue; CStringW value; DWORD type, count; LONG result = ::RegQueryValueExW(secKey,entry,NULL,&type,NULL,&count); if (result == ERROR_SUCCESS) { result = ::RegQueryValueExW(secKey,entry,NULL,&type, (LPBYTE)value.GetBuffer(count/sizeof(WCHAR)),&count); value.ReleaseBuffer(); } ::RegCloseKey(secKey); if (result == ERROR_SUCCESS) return value; return defaultValue; }
CStringW ConvertStringToUnicode(const char* pSrc) { CStringW strResult; UINT nCodePage = CP_GB18030; if (!IsValidCodePage(CP_GB18030)) { nCodePage = _AtlGetConversionACP(); } int iLength = ::MultiByteToWideChar(nCodePage, 0, pSrc, -1, NULL, 0); if (iLength) { iLength = ::MultiByteToWideChar(nCodePage, 0, pSrc, -1, strResult.GetBuffer(iLength), iLength); strResult.GetBuffer()[iLength] = 0; strResult.ReleaseBufferSetLength(iLength); } return strResult; }
void CLogOptionsDlg::saveOptions() { int nLevel = SendDlgItemMessage(IDC_CBXLEVELS,CB_GETCURSEL,0,0); int nSize = SendDlgItemMessage( IDC_MSGCLASSES, WM_GETTEXTLENGTH, 0, 0 ); CStringW strClasses; GetDlgItemText(IDC_MSGCLASSES, strClasses.GetBuffer(nSize), nSize+1 ); strClasses.ReleaseBuffer(); nSize = SendDlgItemMessage( IDC_MSGEXCLUDE, WM_GETTEXTLENGTH, 0, 0 ); CStringW strExcludes; GetDlgItemText(IDC_MSGEXCLUDE, strExcludes.GetBuffer(nSize), nSize+1 ); strExcludes.ReleaseBuffer(); LOGCONF().setMinSeverity(nLevel); LOGCONF().setEnabledCategories( CStringA(strClasses)); LOGCONF().setDisabledCategories( CStringA(strExcludes) ); LOGCONF().saveToFile(); }
CString NPStringToCString(NPString npstr) { int nWide = MultiByteToWideChar(CP_UTF8, 0, npstr.UTF8Characters, npstr.UTF8Length + 1, NULL, 0); if (nWide == 0) return CString(); CStringW wstr; MultiByteToWideChar(CP_UTF8, 0, npstr.UTF8Characters, npstr.UTF8Length + 1, wstr.GetBuffer(nWide), nWide); wstr.ReleaseBuffer(); return CString(CW2T(wstr)); }
CStringW Util::String::GBKToUnicode( CStringA gbk ) { CStringW strUnicode; DWORD dwMinSize = 0; dwMinSize = MultiByteToWideChar(936, NULL, gbk, gbk.GetLength(),NULL, 0); strUnicode.GetBufferSetLength(dwMinSize); LPWSTR lpszStr = strUnicode.GetBuffer(); INT ok = MultiByteToWideChar(936, NULL, gbk, gbk.GetLength(), lpszStr, dwMinSize); strUnicode.ReleaseBuffer(); return strUnicode; }
CStringW CTime::FormatGmt(LPCWSTR pFormat) const { CStringW str; struct tm Time; if (localtime_s(&Time,&m_time)) return szEmpty; wcsftime(str.GetBuffer(1000),1000,pFormat,&Time); str.FreeExtra(); return str; }
UINT CWnd::GetDlgItemText(int nIDDlgItem,CStringW& str) { HWND hCtrl=::GetDlgItem(m_hWnd,nIDDlgItem); UINT len=(UINT)::SendMessage(hCtrl,WM_GETTEXTLENGTH,0,0); if (IsUnicodeSystem()) return ::GetWindowTextW(hCtrl,str.GetBuffer(len),len+1); char* pText=new char[len+2]; ::GetWindowTextA(hCtrl,pText,len+1); str.Copy(pText,len); delete[] pText; return len; }
BOOL CListCtrl::GetItemText(CStringW& str,int nItem, int nSubItem) const { LV_ITEMW li; li.mask=LVIF_TEXT; li.iItem=nItem; li.iSubItem=nSubItem; li.pszText=str.GetBuffer(1000); li.cchTextMax=1000; int nRet=(int)::SendMessage(m_hWnd,LVM_GETITEMTEXTW,nItem,(LPARAM)&li); if (nRet>0) str.FreeExtra(nRet); else str.Empty(); return nRet>0; }
EXTERN_C BOOL WINAPI RouteTheCall( IN HWND hWndOwner, IN HINSTANCE hInstance, IN LPCSTR lpStringArg, IN INT Show) { CStringW path = lpStringArg; PathRemoveBlanksW(path.GetBuffer()); path.ReleaseBuffer(); path = L"\"" + path + L"\""; ShellExecuteW(NULL, L"open", L"explorer.exe", path.GetString(), NULL, SW_SHOWNORMAL); return TRUE; }
CString OptUtf8ToStr(LPCSTR psz, int iLen) { CStringW wstr; int iMaxWideStrLen = iLen; LPWSTR pwsz = wstr.GetBuffer(iMaxWideStrLen); int iWideChars = utf8towc(psz, iLen, pwsz, iMaxWideStrLen); if (iWideChars <= 0) { // invalid UTF8 string... wstr.ReleaseBuffer(0); wstr = psz; // convert with local codepage } else wstr.ReleaseBuffer(iWideChars); return wstr; // just return the string }
CString OptUtf8ToStr(const CStringA& rastr) { CStringW wstr; int iMaxWideStrLen = rastr.GetLength(); LPWSTR pwsz = wstr.GetBuffer(iMaxWideStrLen); int iWideChars = utf8towc(rastr, rastr.GetLength(), pwsz, iMaxWideStrLen); if (iWideChars <= 0) { // invalid UTF8 string... wstr.ReleaseBuffer(0); wstr = rastr; // convert with local codepage } else wstr.ReleaseBuffer(iWideChars); return wstr; // just return the string }
int CWnd::GetWindowText(CStringW& str) const { if (IsUnicodeSystem()) { int len=::GetWindowTextLengthW(m_hWnd); len=::GetWindowTextW(m_hWnd,str.GetBuffer(len),len+1); return len; } int len=::GetWindowTextLength(m_hWnd); char* pText=new char[len+2]; ::GetWindowTextA(m_hWnd,pText,len+1); str.Copy(pText,len); delete[] pText; return len; }
BOOL CRegKey::EnumValue(DWORD iValue,CStringW& strName,LPDWORD lpdwType,LPBYTE lpbData,LPDWORD lpcbData) const { if (IsUnicodeSystem()) { DWORD cb=2048; LONG ret=::RegEnumValueW(m_hKey,iValue,strName.GetBuffer(2048/2),&cb,0,lpdwType,lpbData,lpcbData); strName.FreeExtra(); return ret==ERROR_SUCCESS; } else { DWORD cb=2048; char name[2048]; LONG ret=::RegEnumValueA(m_hKey,iValue,name,&cb,0,lpdwType,lpbData,lpcbData); strName=name; return ret==ERROR_SUCCESS; } }
//Read UTF-8 format file to an unicode text buffer. BOOL Utility::ReadU8FileToUText(LPCTSTR pszFile, CStringW& strOutText) { CStringA strUTF8; CFile fNib; //Read an UTF-8 text file to pU8Buffer fNib.Open(pszFile, CFile::modeRead); int nSize = (int)fNib.GetLength(); char *pU8Buffer = strUTF8.GetBuffer(nSize + 1); //UTF-8 buffer fNib.Read(pU8Buffer, nSize); strUTF8.ReleaseBuffer(); fNib.Close(); //Convert pU8Buffer from UTF-8 to Unicode int count = MultiByteToWideChar(CP_UTF8, 0, strUTF8, -1 ,NULL,0); LPWSTR pszUnicode = strOutText.GetBuffer(count); MultiByteToWideChar(CP_UTF8, 0, pU8Buffer, -1, pszUnicode, count); strOutText.ReleaseBuffer(); return TRUE; }
int CStatusBarCtrl::GetText(CStringW& str,int nPane,int* pType) const { int ret; int len=(int)::SendMessage(m_hWnd,SB_GETTEXTLENGTH,(WPARAM)nPane,0); if (IsUnicodeSystem()) ret=(int)::SendMessageW(m_hWnd,SB_GETTEXTW,(WPARAM)nPane,(LPARAM)str.GetBuffer(len)); else { char* pText=new char[len+2]; ret=(int)::SendMessageA(m_hWnd,SB_GETTEXTA,(WPARAM)nPane,(LPARAM)pText); str.Copy(pText,len); delete[] pText; } if (pType!=NULL) *pType=HIWORD(ret); return ret; }
BOOL CRegKey::QueryValue(LPCWSTR lpszValueName,CStringW& strData) const { if (!IsUnicodeSystem()) { CString strA; if (QueryValue(W2A(lpszValueName),strA)) { strData=strA; return TRUE; } return FALSE; } DWORD dwLength=0; DWORD dwType=REG_SZ; if (::RegQueryValueExW(m_hKey,lpszValueName,NULL,&dwType,NULL,&dwLength)!=ERROR_SUCCESS) { strData.Empty(); return FALSE; } if (dwType!=REG_SZ && dwType!=REG_EXPAND_SZ) { strData.Empty(); return FALSE; } if (dwLength<=1) { strData.Empty(); return TRUE; } if (::RegQueryValueExW(m_hKey,lpszValueName,NULL,NULL,(LPBYTE)strData.GetBuffer(dwLength/2-1),&dwLength)!=ERROR_SUCCESS) { strData.Empty(); return FALSE; } return TRUE; }
void CBatchTextDlg::SaveTextFile(CString &strFile, CStringW &strContent) { CFile file; file.Open(strFile, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate); file.SeekToEnd(); int nLen; std::vector<BYTE> Buf; switch (m_ctrlEnc.GetCurSel()) { case 0: nLen = WideCharToMultiByte(CODEPAGE_GB2312, 0, strContent, -1, NULL, 0, NULL, NULL) - 1; Buf.resize(nLen); if (nLen != 0) { WideCharToMultiByte(CODEPAGE_GB2312, 0, strContent, -1, (LPSTR)&Buf[0], nLen, NULL, NULL); } break; case 1: Buf.resize(strContent.GetLength() * 2 + 2); Buf[0] = 0xFE; Buf[1] = 0xFF; CopyMemory(&Buf[2], strContent.GetBuffer(), Buf.size()); break; case 2: nLen = WideCharToMultiByte(CP_UTF8, 0, strContent, -1, NULL, 0, NULL, NULL) - 1; Buf.resize(nLen + 3); Buf[0] = 0xEF; Buf[1] = 0xBB; Buf[2] = 0xBF; WideCharToMultiByte(CP_UTF8, 0, strContent, -1, (LPSTR)&Buf[3], nLen, NULL, NULL); break; } file.Write(&Buf[0], Buf.size()); }
//##ModelId=474D3076034C bool CClip_ImportExport::PlaceCF_TEXT_AND_CF_UNICODETEXT_OnClipboard(CStringA &csCF_TEXT, CStringW &csCF_UNICODETEXT) { bool bRet = false; if(OpenClipboard(theApp.m_MainhWnd)) { EmptyClipboard(); if(csCF_TEXT.IsEmpty() == FALSE) { long lLen = csCF_TEXT.GetLength(); HGLOBAL hGlobal = NewGlobalP(csCF_TEXT.GetBuffer(lLen), lLen+1); csCF_TEXT.ReleaseBuffer(); SetClipboardData(CF_TEXT, hGlobal); bRet = true; } if(csCF_UNICODETEXT.IsEmpty() == FALSE) { long lLen = csCF_UNICODETEXT.GetLength() * sizeof(wchar_t); HGLOBAL hGlobal = NewGlobalP(csCF_UNICODETEXT.GetBuffer(lLen), lLen+1); csCF_UNICODETEXT.ReleaseBuffer(); SetClipboardData(CF_UNICODETEXT, hGlobal); bRet = true; } CloseClipboard(); } else { Log(_T("Error opening clipboard")); } return bRet; }
//将当前屏幕保存到文件 void CaptureScreen(const char *filename = "") { static const CString defaultDir = "D:/ScreenCapture/"; //默认目录 char folder[500] = ""; GetPrivateProfileString("ScreenCapture", "fatherFolder", defaultDir, folder, 500, ".\\config.ini"); if (folder[strlen(folder) - 1] != '/' && folder[strlen(folder) - 1] != '\\') strcat(folder, "/"); //添加目录分隔符 CString lastFolder = folder; if (!PathFileExists(folder)) { //文件不存在则需要创建 _mkdir(folder); //建立父级目录 if (!PathFileExists(folder)) {//如果文件夹仍然不存在,即创建失败 strcpy(folder, defaultDir); _mkdir(folder); //建立父级目录 WritePrivateProfileString("ScreenCapture", "#温馨提示" , "设定目录不可写,已自动设置为默认目录", ".\\config.ini"); } WritePrivateProfileString("ScreenCapture", "fatherFolder", folder, ".\\config.ini"); } CString fullPathName = filename; //如果没有传递文件名的参数则用配置文件中的路径和当前日期和时间生成文件名 if (filename == 0 || filename[0] == 0) { time_t now_time = time(NULL); struct tm *newtime = localtime(&now_time); char _date[128], _time[128]; strftime(_date, 128, "%Y%m%d", newtime); strftime(_time, 128, "%H%M%S", newtime); CString now = ""; now.Format("%s_%s", _date, _time); captureImgPath = (CString)folder + _date; _mkdir(captureImgPath);//建立子级目录,目录名为日期 SYSTEMTIME currentTime; GetSystemTime(¤tTime); //获取当前时间的毫秒数 fullPathName.Format("%s/%s-%d.png", captureImgPath, now, currentTime.wMilliseconds); } //开始获取屏幕内容 CDC *pDC = CDC::FromHandle(::GetDC(NULL));//获取当前整个屏幕DC int Width = pDC->GetDeviceCaps(HORZRES); int Height = pDC->GetDeviceCaps(VERTRES); CDC memDC;//内存DC memDC.CreateCompatibleDC(pDC); CBitmap memBitmap; memBitmap.CreateCompatibleBitmap(pDC, Width, Height);//建立和屏幕兼容的bitmap CBitmap *oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC //以下代码保存memDC中的位图到png文件 Bitmap mbitmap(HBITMAP(memBitmap), 0);//从CBitmap中得到HBitmap CLSID imgCode = { 0 }; //图像编码 if (-1 != GetEncoderClsid(L"image/png", imgCode)) { //选择编码 USES_CONVERSION; CStringW ws = A2W(fullPathName); mbitmap.Save(ws.GetBuffer(), &imgCode);//保存 } memDC.SelectObject(oldmemBitmap); //弹出通知栏气泡 CString ps = "\n您可按下Ctrl+Alt+1打开截图所在文件夹哟"; //附加信息 if (lastFolder != folder) //如果用户设置的目录不可写则提示 ps = "\n啊哦,设定目录<" + CString(lastFolder) + ">不可写,已自动设置为默认目录:" + folder; static CDesktopCaptureDlg* pDlg = static_cast<CDesktopCaptureDlg*>(AfxGetMainWnd()); pDlg->showTips("截图成功", "已将截图文件保存到:" + fullPathName + ps); }
CStringW CMpeg2DataParser::ConvertString(BYTE* pBuffer, size_t uLength) { static const UINT16 codepages[0x20] = { 20269, // 00 - Default ISO/IEC 6937 28595, // 01 - ISO 8859-5 Cyrillic 28596, // 02 - ISO 8859-6 Arabic 28597, // 03 - ISO 8859-7 Greek 28598, // 04 - ISO 8859-8 Hebrew 28599, // 05 - ISO 8859-9 Latin 5 28591, // 06 - ??? - ISO/IEC 8859-10 - Latin alphabet No. 6 28591, // 07 - ??? - ISO/IEC 8859-11 - Latin/Thai (draft only) 28591, // 08 - reserved 28603, // 09 - ISO 8859-13 - Estonian 28591, // 0a - ??? - ISO/IEC 8859-14 - Latin alphabet No. 8 (Celtic) 28605, // 0b - ISO 8859-15 Latin 9 28591, // 0c - reserved 28591, // 0d - reserved 28591, // 0e - reserved 28591, // 0f - reserved 0, // 10 - See codepages10 array 28591, // 11 - ??? - ISO/IEC 10646 - Basic Multilingual Plane (BMP) 28591, // 12 - ??? - KSX1001-2004 - Korean Character Set 20936, // 13 - Chinese Simplified (GB2312-80) 950, // 14 - Chinese Traditional (Big5) 28591, // 15 - ??? - UTF-8 encoding of ISO/IEC 10646 - Basic Multilingual Plane (BMP) 28591, // 16 - reserved 28591, // 17 - reserved 28591, // 18 - reserved 28591, // 19 - reserved 28591, // 1a - reserved 28591, // 1b - reserved 28591, // 1c - reserved 28591, // 1d - reserved 28591, // 1e - reserved 28591 // 1f - TODO! }; static const UINT16 codepages10[0x10] = { 28591, // 00 - reserved 28591, // 01 - ISO 8859-1 Western European 28592, // 02 - ISO 8859-2 Central European 28593, // 03 - ISO 8859-3 Latin 3 28594, // 04 - ISO 8859-4 Baltic 28595, // 05 - ISO 8859-5 Cyrillic 28596, // 06 - ISO 8859-6 Arabic 28597, // 07 - ISO 8859-7 Greek 28598, // 08 - ISO 8859-8 Hebrew 28599, // 09 - ISO 8859-9 Turkish 28591, // 0a - ??? - ISO/IEC 8859-10 28591, // 0b - ??? - ISO/IEC 8859-11 28591, // 0c - ??? - ISO/IEC 8859-12 28603, // 0d - ISO 8859-13 Estonian 28591, // 0e - ??? - ISO/IEC 8859-14 28605, // 0f - ISO 8859-15 Latin 9 // 0x10 to 0xFF - reserved for future use }; CStringW strResult; if (uLength > 0) { UINT cp = CP_ACP; int nDestSize; if (pBuffer[0] == 0x10) { pBuffer++; uLength--; if (pBuffer[0] == 0x00) { cp = codepages10[pBuffer[1]]; } else { // if (pBuffer[0] > 0x00) // reserved for future use, use default codepage cp = codepages[0]; } pBuffer += 2; uLength -= 2; } else if (pBuffer[0] < 0x20) { cp = codepages[pBuffer[0]]; pBuffer++; uLength--; } else { // No code page indication, use the default cp = codepages[0]; } // Work around a bug in MS MultiByteToWideChar with ISO/IEC 6937 and take care of the Euro symbol special case (step 1/2)... CArray<size_t> euroSymbolPos; if (cp == 20269) { BYTE tmp; for (size_t i = 0; i < uLength - 1; i++) { if (pBuffer[i] >= 0xC1 && pBuffer[i] <= 0xCF && pBuffer[i] != 0xC9 && pBuffer[i] != 0xCC) { // Swap the current char with the next one tmp = pBuffer[i]; pBuffer[i] = pBuffer[i + 1]; pBuffer[++i] = tmp; } else if (pBuffer[i] == 0xA4) { // € was added as 0xA4 in the DVB spec euroSymbolPos.Add(i); } } // Handle last symbol if it's a € if (pBuffer[uLength - 1] == 0xA4) { euroSymbolPos.Add(uLength - 1); } } nDestSize = MultiByteToWideChar(cp, MB_PRECOMPOSED, (LPCSTR)pBuffer, (int)uLength, nullptr, 0); if (nDestSize > 0) { LPWSTR strResultBuff = strResult.GetBuffer(nDestSize); MultiByteToWideChar(cp, MB_PRECOMPOSED, (LPCSTR)pBuffer, (int)uLength, strResultBuff, nDestSize); // Work around a bug in MS MultiByteToWideChar with ISO/IEC 6937 and take care of the Euro symbol special case (step 2/2)... if (cp == 20269) { for (size_t i = 0, len = (size_t)nDestSize; i < len; i++) { switch (strResultBuff[i]) { case 0x60: // grave accent strResultBuff[i] = 0x0300; break; case 0xb4: // acute accent strResultBuff[i] = 0x0301; break; case 0x5e: // circumflex accent strResultBuff[i] = 0x0302; break; case 0x7e: // tilde strResultBuff[i] = 0x0303; break; case 0xaf: // macron strResultBuff[i] = 0x0304; break; case 0xf8f8: // dot strResultBuff[i] = 0x0307; break; } } for (INT_PTR i = 0, len = euroSymbolPos.GetCount(); i < len; i++) { strResultBuff[euroSymbolPos[i]] = _T('€'); } } // Some strings seems to be null-terminated, we need to take that into account. while (nDestSize > 0 && strResultBuff[nDestSize - 1] == L'\0') { nDestSize--; } strResult.ReleaseBuffer(nDestSize); } } return strResult; }
std::wstring wloadString(UINT resid) { CStringW s; s.LoadString(resid); return s.GetBuffer(); }
int CBoxScript::ParseScriptText(LPCSTR pstrText, int nCount, CStringA& strScriptText, int nIncludeFlagIndex) { CStringA strTempText; if(nCount >= 2 && (BYTE)pstrText[0] == 0xff && (BYTE)pstrText[1] == 0xfe) { /* int _nTempCount = WideCharToMultiByte(_AtlGetConversionACP(), 0, LPWSTR(pstrText + 2), (nCount - 2) / 2, NULL, 0, NULL, NULL); LPSTR _pstr = strTempText.GetBuffer(_nTempCount); WideCharToMultiByte(_AtlGetConversionACP(), 0, LPWSTR(pstrText + 2), (nCount - 2) / 2, _pstr, _nTempCount, NULL, NULL); strTempText.ReleaseBuffer(_nTempCount); */ //发现是Unicode则检查是否已经存在CodePage,存在就转换到当前CodePage,否则转换成UTF8同时设定m_CodePage为65001 if (m_uiCodePage == 0) m_uiCodePage = CP_UTF8; int _nTempCount = WideCharToMultiByte(m_uiCodePage, 0, LPWSTR(pstrText + 2), (nCount - 2) / 2, NULL, 0, NULL, NULL); LPSTR _pstr = strTempText.GetBuffer(_nTempCount); WideCharToMultiByte(m_uiCodePage, 0, LPWSTR(pstrText + 2), (nCount - 2) / 2, _pstr, _nTempCount, NULL, NULL); strTempText.ReleaseBuffer(_nTempCount); pstrText = strTempText; nCount = strTempText.GetLength(); } else if(nCount >= 3 && (BYTE)pstrText[0] == 0xEF && (BYTE)pstrText[1] == 0xBB && (BYTE)pstrText[2] == 0xBF) { pstrText += 3; nCount -= 3; if (m_uiCodePage && m_uiCodePage != CP_UTF8) { CStringW strTempTextW; int _nTempCount = MultiByteToWideChar(CP_UTF8, 0, pstrText, nCount, NULL, 0); LPWSTR _pstrW = strTempTextW.GetBuffer(_nTempCount); MultiByteToWideChar(CP_UTF8, 0, pstrText, nCount, _pstrW, _nTempCount); strTempTextW.ReleaseBuffer(_nTempCount); _nTempCount = WideCharToMultiByte(m_uiCodePage, 0, strTempTextW, strTempTextW.GetLength(), NULL, 0, NULL, NULL); LPSTR _pstr = strTempText.GetBuffer(_nTempCount); WideCharToMultiByte(m_uiCodePage, 0, strTempTextW, strTempTextW.GetLength(), _pstr, _nTempCount, NULL, NULL); strTempText.ReleaseBuffer(_nTempCount); pstrText = strTempText; nCount = strTempText.GetLength(); } else m_uiCodePage = CP_UTF8; } else { UINT uiCodePage = ParseScriptTextCodePage(pstrText, nCount); if (uiCodePage == 0) uiCodePage = GetACP(); //if (m_uiCodePage == 0) // m_uiCodePage = CP_UTF8; if (m_uiCodePage && m_uiCodePage != uiCodePage) { CStringW strTempTextW; int _nTempCount = MultiByteToWideChar(uiCodePage, 0, pstrText, nCount, NULL, 0); LPWSTR _pstrW = strTempTextW.GetBuffer(_nTempCount); MultiByteToWideChar(uiCodePage, 0, pstrText, nCount, _pstrW, _nTempCount); strTempTextW.ReleaseBuffer(_nTempCount); _nTempCount = WideCharToMultiByte(m_uiCodePage, 0, strTempTextW, strTempTextW.GetLength(), NULL, 0, NULL, NULL); LPSTR _pstr = strTempText.GetBuffer(_nTempCount); WideCharToMultiByte(m_uiCodePage, 0, strTempTextW, strTempTextW.GetLength(), _pstr, _nTempCount, NULL, NULL); strTempText.ReleaseBuffer(_nTempCount); pstrText = strTempText; nCount = strTempText.GetLength(); } else { m_uiCodePage = uiCodePage; } } static struct { char *pstrName; int nSize; }CmdName[] = { {"#include", 8}, {"#language", 9}, {"#debug", 6}, {"#timeout", 8}, {"#transaction", 12}, {"#codepage", 9} }; #define CMD_COUNT (sizeof(CmdName) / sizeof(CmdName[0])) int i; LPCSTR pstrTemp, pstrTemp1; int nTempCount; int nLineCount = 1; while(nCount > 0 && IsBlank(pstrText[0])) { if(pstrText[0] == '\n') nLineCount ++; pstrText ++; nCount --; } while(nCount > 0 && pstrText[0] == '#') { for(i = 0; i < CMD_COUNT; i ++) if(nCount > CmdName[i].nSize && !_strnicmp(pstrText, CmdName[i].pstrName, CmdName[i].nSize) && IsBlankChar(pstrText[CmdName[i].nSize])) break; if(i == CMD_COUNT) break; pstrText += CmdName[i].nSize; nCount -= CmdName[i].nSize; while(nCount > 0 && IsBlankChar(pstrText[0])) { pstrText ++; nCount --; } if(nCount > 0 && pstrText[0] == '\"') { pstrText ++; nCount --; } pstrTemp = pstrText; nTempCount = nCount; while(nTempCount > 0 && !IsLineChar(pstrTemp[0])) { pstrTemp ++; nTempCount --; } pstrTemp1 = pstrTemp; while(pstrTemp1 > pstrText && IsBlankChar(pstrTemp1[0])) pstrTemp1 --; if(pstrTemp1 > pstrText && pstrTemp1[-1] == '\"') pstrTemp1 --; CStringA strValue; strValue.SetString(pstrText, (int)(pstrTemp1 - pstrText)); pstrText = pstrTemp; nCount = nTempCount; switch(i) { case 0: strValue.MakeLower(); if(LoadScriptFile(BOX_CA2CT(strValue), strScriptText, nLineCount)) return 500; break; case 1: m_strLanguage = strValue; break; case 2: m_bEnableDebug = !strValue.CompareNoCase("on") || !strValue.CompareNoCase("true"); if(!strValue.CompareNoCase("step")) m_bStepDebug = TRUE; break; case 3: m_pHost->m_nTimeout = atoi(strValue); break; case 4: if(!strValue.CompareNoCase("Required")) m_nTransaction = 3; else if(!strValue.CompareNoCase("Requires_New")) m_nTransaction = 2; else if(!strValue.CompareNoCase("Supported")) m_nTransaction = 1; else if(!strValue.CompareNoCase("Not_Supported")) m_nTransaction = 0; break; case 5: //(uiCodePage) break; } while(nCount > 0 && IsBlank(pstrText[0])) { if(pstrText[0] == '\n') nLineCount ++; pstrText ++; nCount --; } } AddLineMap(nIncludeFlagIndex, nLineCount); strScriptText.Append(pstrText, nCount); strScriptText += _T("\r\n"); m_nScriptLine ++; pstrTemp = pstrText; nTempCount = nCount; while(nTempCount > 0) { if(pstrTemp[0] == '\n') m_nScriptLine ++; pstrTemp ++; nTempCount --; } return 0; }
long CBoxScript::Load(LPCTSTR pstrFile) { m_error.Clear(); m_strBasePath.Empty(); m_arrayIncludeFlags.RemoveAll(); m_nDiskFileCount = 0; m_nCacheFileCount = 0; m_nScriptLine = 0; CScriptHost* pNowScript = CScriptHost::GetCurrentScript(); if(pNowScript != NULL) m_strBasePath = pNowScript->m_strScriptName; CBoxPath path; if(!m_strBasePath.IsEmpty() && pstrFile[0] != _T('\\')) path.Combine(m_strBasePath.Left(m_strBasePath.ReverseFind(_T('\\')) + 1), pstrFile); else path.Combine(pstrFile); InitScript(); CStringA strScriptText; int iResult = LoadScriptFile(path.m_strPath, strScriptText); m_strBasePath.Empty(); if(iResult != 0) return iResult; if(m_nCacheFileCount) { m_bStepDebug = FALSE; m_bEnableDebug = FALSE; } m_pHost->SetFileName(path.m_strPath); if(!m_pHost->Create(m_strLanguage, m_bEnableDebug, m_bStepDebug, m_nTransaction)) { GetScriptErrorInfo(); return 500; } m_pHost->SetScriptSite(); if (m_uiCodePage) { CStringW strTempText; int _nTempCount = MultiByteToWideChar(m_uiCodePage, 0, strScriptText, strScriptText.GetLength(), NULL, 0); LPWSTR _pstr = strTempText.GetBuffer(_nTempCount); MultiByteToWideChar(m_uiCodePage, 0, strScriptText, strScriptText.GetLength(), _pstr, _nTempCount); strTempText.ReleaseBuffer(_nTempCount); if(m_pHost->Load(strTempText) != S_OK) { GetScriptErrorInfo(); return 500; } } else { if(m_pHost->Load(BOX_CA2CT(strScriptText)) != S_OK) { GetScriptErrorInfo(); return 500; } } if(AfterParse() != 0) return 500; return 0; }