void Utf8ToAnsi(std::string& strDest, const char* src) { ASSERT(src != NULL); char* str = NULL; Utf8ToAnsi(&str, src); strDest = str; SAFE_ARRYDELETE(str); }
void Utf8ToTChar(TCHAR** dest, const char* src) { #ifdef _UNICODE Utf8ToUnicode(dest, src); #else //多字节 Utf8ToAnsi(dest, src); #endif }
bool Utf8ToAnsi( std::string & str_src, std::string & str_dst ) { if( str_src.empty() ) { str_dst.clear(); return true; } Utf8ToAnsi( str_src.c_str(), &str_dst, 512 ); return true; }
std::string Utf8ToAnsi(const std::string& strUtf8) { std::string strAnsi; CHAR * lpszAnsi = Utf8ToAnsi(strUtf8.c_str()); if (lpszAnsi != NULL) { strAnsi = lpszAnsi; delete []lpszAnsi; } return strAnsi; }
CString GetServerDetailInfo(CString strServer, CString &strTitle) { strServer.Insert(0, "http://"); CString strReturn = "Unknown", szAllData, szData; CInternetSession ss(_T("session"), 0, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD);//设置不缓冲 CHttpFile *pF = NULL; try { ss.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 10 * 1000); pF = (CHttpFile *)ss.OpenURL(strServer, 1, INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_TRANSFER_ASCII | INTERNET_FLAG_NO_AUTO_REDIRECT); pF->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, szAllData, 0); int nPos1 = szAllData.Find("Server: ", 0); if (nPos1 == -1) { strReturn = "Unknown"; } else { nPos1 += 8; int nPos2 = szAllData.Find("\r\n", nPos1); strReturn = szAllData.Mid(nPos1, nPos2 - nPos1); if (strReturn.Find("Microsoft", 0) != -1) { strReturn.Insert(0, "Windows, "); } } szAllData.Empty(); while(pF->ReadString(szData)) szAllData += szData; szAllData.MakeLower(); nPos1 = szAllData.Find("<title>", 0); if (nPos1 != -1) { int nPos2 = szAllData.Find("</title>", nPos1); if (nPos2 != -1) { strTitle = szAllData.Mid(nPos1 + 7, nPos2 - nPos1 - 7); if (IsTextUTF8(strTitle.GetBuffer(0), strTitle.GetLength())) Utf8ToAnsi(strTitle); } } } catch(...) { strReturn = ""; } if (pF != NULL) { pF->Close(); delete pF; pF = NULL; } if (ss != NULL) { ss.Close(); delete ss; } return strReturn; }
void CWin32InputBox::InitDialog() { // Set the button captions for (size_t i=0;i<sizeof(definputbox_buttonids)/sizeof(definputbox_buttonids[0]);i++) ::SetDlgItemText(_param->hDlg, (int) definputbox_buttonids[i], definputbox_buttonnames[i]); // Set other controls ::SetWindowTextA(_param->hDlg, Utf8ToAnsi(_param->szTitle).c_str()); ::SetDlgItemTextA(_param->hDlg, definputbox_id_prompt, Utf8ToAnsi(_param->szPrompt).c_str()); HWND hwndEdit1 = ::GetDlgItem(_param->hDlg, definputbox_id_edit1); HWND hwndEdit2 = ::GetDlgItem(_param->hDlg, definputbox_id_edit2); if (_param->bMultiline) _hwndEditCtrl = hwndEdit2; else _hwndEditCtrl = hwndEdit1; ::SetWindowTextA(_hwndEditCtrl, Utf8ToAnsi(_param->szResult).c_str()); RECT rectDlg, rectEdit1, rectEdit2; ::GetWindowRect(_param->hDlg, &rectDlg); ::GetWindowRect(hwndEdit1, &rectEdit1); ::GetWindowRect(hwndEdit2, &rectEdit2); if (_param->bMultiline) { ::ShowWindow(hwndEdit1, SW_HIDE); ::SetWindowPos( hwndEdit2, HWND_NOTOPMOST, rectEdit1.left - rectDlg.left, (rectEdit1.top - rectDlg.top) - (rectEdit1.bottom - rectEdit1.top), 0, 0, SWP_NOSIZE | SWP_NOZORDER); ::SetWindowPos( _param->hDlg, HWND_NOTOPMOST, 0, 0, rectDlg.right - rectDlg.left, rectDlg.bottom - rectDlg.top - (rectEdit1.bottom - rectEdit1.top), SWP_NOMOVE); } else { ::SetWindowPos( _param->hDlg, HWND_NOTOPMOST, 0, 0, rectDlg.right - rectDlg.left, rectEdit1.bottom - rectDlg.top + 5, SWP_NOMOVE); ::ShowWindow(hwndEdit2, SW_HIDE); } }
int PCGetFileSize(const char* fullPath) { int filesize = -1; #ifdef _POSIX struct stat _si; int pathLen,pathSize; char *fp; pathLen = (int) strlen(fullPath); pathSize = pathLen+1; fp = (char*) MALLOC(pathSize); Utf8ToAnsi(fp, fullPath, pathSize); if (stat(fp, &_si) != -1) { if (!((_si.st_mode & S_IFDIR) == S_IFDIR)) { filesize = _si.st_size; } } FREE(fp); #endif #ifdef WIN32 WIN32_FIND_DATA FileData; HANDLE* hSearch; /* must MMS_FREE */ int pathLen,pathSize; char *fp; pathLen = (int) strlen(fullPath); pathSize = pathLen+1; fp = (char*) MALLOC(pathSize); Utf8ToAnsi(fp, fullPath, pathSize); hSearch = MALLOC(sizeof(HANDLE)); *hSearch = FindFirstFile(fp, &FileData); FREE(fp); if (*hSearch == INVALID_HANDLE_VALUE) { filesize = 0; } else { filesize = FileData.nFileSizeLow; } FindClose(*hSearch); FREE(hSearch); #endif #ifdef UNDER_CE WIN32_FIND_DATA FileData; HANDLE* hSearch; /* must MMS_FREE */ wchar_t* wdirectory; /* must MMS_FREE */ int wPathLen; int wPathSize; int fullPathLen; int fullPathSize; char* fp; fullPathLen = (int) strlen(fullPath); fullPathSize = fullPathLen + 1; fp = (char*) MALLOC(fullPathSize); Utf8ToAnsi(fp, fullPath, fullPathSize); hSearch = MALLOC(sizeof(HANDLE)); wPathLen = fullPathLen * 2; wPathSize = fullPathSize * 2; wdirectory = (wchar_t*)MALLOC(wPathSize); if (mbstowcs(wdirectory,fp,wPathSize) == -1) { filesize = -1; } else { *hSearch = FindFirstFile(wdirectory, &FileData); if (*hSearch == INVALID_HANDLE_VALUE) { filesize = -1; } else { FindClose(*hSearch); filesize = FileData.nFileSizeLow; } } FREE(fp); FREE(wdirectory); FREE(hSearch); #endif return filesize; }
int PCGetFileDirType(char* directory) { #ifdef WIN32 DWORD _si; int dirLen,dirSize; char *fullpath; dirLen = (int) strlen(directory); dirSize = dirLen+1; fullpath = (char*) MALLOC(dirSize); Utf8ToAnsi(fullpath, directory, dirSize); _si = GetFileAttributes(fullpath); FREE(fullpath); if (_si == 0xFFFFFFFF) { return 0; } if ((_si & FILE_ATTRIBUTE_DIRECTORY) == 0) { return 1; } else { return 2; } #endif #ifdef UNDER_CE wchar_t* wfullPath; DWORD _si; int mbDirSize; int wPathSize; int dirLen,dirSize; char *fullpath; int retVal = 0; dirLen = (int) strlen(directory); dirSize = dirLen+1; fullpath = (char*) MALLOC(dirSize); Utf8ToAnsi(fullpath, directory, dirSize); mbDirSize = (int) strlen(fullpath) + 1; wPathSize = mbDirSize * 2; wfullPath = (wchar_t*)MALLOC(wPathSize); if (mbstowcs(wfullPath,fullpath,wPathSize) == -1) { retVal = 0; } else { _si = GetFileAttributes(wfullPath); if (_si == 0xFFFFFFFF) { retVal = 0; } else { if ((_si & FILE_ATTRIBUTE_DIRECTORY) == 0) { retVal = 1; } else { retVal = 2; } } } FREE(fullpath); FREE(wfullPath); return retVal; #endif #ifdef _POSIX struct stat _si; int dirLen,dirSize; char *fullpath; int pathExists; int retVal = 0; dirLen = (int) strlen(directory); dirSize = dirLen+1; fullpath = (char*) MALLOC(dirSize); Utf8ToAnsi(fullpath, directory, dirSize); pathExists = stat (fullpath, &_si); FREE(fullpath); if (pathExists != -1) { if ((_si.st_mode & S_IFDIR) == S_IFDIR) { retVal = 2; } else { retVal = 1; } } return retVal; #endif }
//--------------------------------------------------------------------------- bool TSyslogMessage::FromStringSyslogd(char * p, int size, sockaddr_in * from_addr) { if( from_addr ) SourceAddr = inet_ntoa(from_addr->sin_addr); else SourceAddr = ""; PRI = -1; // not exist if( *p == '<' ) { for(int i=1; i<5 && p[i]; i++) { if( p[i] == '>' ) { PRI = atoi(p+1); p += i + 1; break; } if( ! isdigit(p[i]) ) break; } } if( PRI >= 0 ) { // invalid facility number not allowed: message filtering mechanism will fail // replace invalid facility number by LOGALERT if( LOG_FAC(PRI) >= LOG_NFACILITIES ) PRI = LOG_PRI(PRI) | LOG_LOGALERT; Facility = getcodetext(LOG_FAC(PRI) << 3, facilitynames); Priority = getcodetext(LOG_PRI(PRI), prioritynames); } if( IsValidSyslogDate(p) ) { DateStr = String(p, 15); p += 16; // including space } else { // month names in english in compliance to syslog rfc TFormatSettings fs; GetLocaleFormatSettings(0x0409, fs); // usa DateStr = FormatDateTime("mmm dd hh:nn:ss", Now(), fs); } // try to find host name for(int i=0; i<255 && p[i]; i++) { if( p[i] == ' ' ) { // found HostName = String(p, i); p += i + 1; break; } else if( p[i] == ':' || p[i] == '[' || p[i] == ']' ) { // host name not exist - this is program name break; } } // try to find program name for(int i=0; i<(48+2) && p[i]; i++) { if( p[i] == ':' && p[i+1] == ' ' ) { // found Tag = String(p, i); p += i + 2; break; } } // and now - process message // Replace all tabs by spaces, // cut all carriage returns and line feeds, // replace all chars 0..32 by '.' Msg = ""; for(int i=0; p[i]; i++) { if( p[i] == '\t' || p[i] == '\n' ) Msg += ' '; else if( p[i] == '\r' ) ; else if( p[i] >= 0 && p[i] < 32 ) Msg += '.'; else Msg += p[i]; } if( MainCfg.bReceiveUTF8 ) Msg = Utf8ToAnsi(Msg); return true; }