String FileUtilities::ReadCompleteTextFile(const String &sFilename) { File oFile; oFile.Open(sFilename, File::OTReadOnly); // Read file shared_ptr<ByteBuffer> pBuffer = oFile.ReadFile(); if (!pBuffer || pBuffer->GetSize() == 0) { // Could not read from this file. return ""; } FileEncoding file_encoding = ANSI; bool is_utf8 = false; // check if utf8 bom exists const unsigned char *unsigned_char_buffer = (const unsigned char*) pBuffer->GetCharBuffer(); if (pBuffer->GetSize() >= 3 && *unsigned_char_buffer == 0xef && *(unsigned_char_buffer + 1) == 0xbb && *(unsigned_char_buffer + 2) == 0xbf) file_encoding = UTF8; else if (pBuffer->GetSize() >= 2 && *unsigned_char_buffer == 0xff && *(unsigned_char_buffer + 1) == 0xfe) file_encoding = UTF16; switch (file_encoding) { case ANSI: { AnsiString sRetVal((const char*) unsigned_char_buffer, pBuffer->GetSize()); return sRetVal; } case UTF8: { AnsiString raw_data((const char*) unsigned_char_buffer, pBuffer->GetSize()); String utf8_data; Unicode::MultiByteToWide(raw_data, utf8_data); return utf8_data; } case UTF16: { int iChars = pBuffer->GetSize() / sizeof(TCHAR); String sRetVal((const wchar_t*) pBuffer->GetCharBuffer() +1, iChars -1); return sRetVal; } default: throw new std::logic_error(Formatter::FormatAsAnsi("Unsupported encoding type: {0}", file_encoding)); } }
CStdString LocalTempCopyOfFile::FixUnicodeFileNameForXP(CStdString sFileName) { char *ansistr; int lenW = (int)sFileName.length(); int lenA = ::WideCharToMultiByte(CP_ACP, 0, sFileName.c_str(), lenW, 0, 0, NULL, NULL); if (lenA > 0) { ansistr = new char[lenA + 1]; // allocate a final null terminator as well BOOL bDefaultCharUsed = FALSE; ::WideCharToMultiByte(CP_ACP, 0, sFileName.c_str(), lenW, ansistr, lenA, NULL, &bDefaultCharUsed); ansistr[lenA] = 0; // Set the null terminator CStdString sRetVal(ansistr); if ( bDefaultCharUsed && IsFullyMessedUpString(sRetVal) ) { TCHAR szTempFileBuffer[MAX_PATH]; ::GetTempFileName(m_sTempFolder, _T("wmtemp"), rand(), szTempFileBuffer ); sRetVal = ::PathFindFileName(szTempFileBuffer); sRetVal = RemoveExtension(sRetVal); sRetVal += ::PathFindExtension(sFileName.c_str()); } delete[] ansistr; return sRetVal; } return _T(""); }
CStdString OfflineDataStore::ReplaceSlashes(const CStdString& sDocID) { CStdString sRetVal(sDocID); sRetVal.Replace(_T("\\"), _T("/")); return sRetVal; }