bool EncodingDetector::DetectEncoding(const wxString& filename, bool convert_to_wxstring) { wxFile file(filename); if (!file.IsOpened()) return false; size_t size = file.Length(); if (size == 0) { file.Close(); return false; } wxByte* buffer = (wxByte*) malloc(sizeof(wxByte) * (size + 4)); if (!buffer) { file.Close(); return false; } buffer[size + 0] = 0; buffer[size + 1] = 0; buffer[size + 2] = 0; buffer[size + 3] = 0; size_t readBytes = file.Read((void*)buffer, size); bool result = false; if (readBytes > 0) result = DetectEncoding(buffer, size, convert_to_wxstring); file.Close(); free(buffer); return result; }
Encoding::Encoding(const wxString& filename) : m_isOK(false), m_usesBOM(false), m_encoding(wxFONTENCODING_DEFAULT) { m_isOK = DetectEncoding(filename); }
void WFileReader::InitFileInternal(bool detectEnc) { if (detectEnc){ this->mEnc = DetectEncoding(mFile.c_str()); } if (mEnc == FileEncoding::ASCII) { mIsUFile = false; mAFile.open(mFile); } else if (mEnc == FileEncoding::UTF8) { mIsUFile = true; mUFile.open(mFile); #ifdef _WIN32 mUFile.imbue(locale(locale::classic(), new codecvt_utf8<wchar_t>)); #else mUFile.imbue(std::locale("en_US.UTF-8")); #endif } else { mIsUFile = true; mUFile.open(mFile); #ifdef _WIN32 mUFile.imbue(locale(locale::classic(), new codecvt_utf16<wchar_t>)); #else mUFile.imbue(std::locale("en_US.UTF-8")); #endif } }
EncodingDetector::EncodingDetector(const QByteArray& buffer) : m_IsOK(false), m_UseBOM(false), m_BOMSizeInBytes(0) { m_Encoding = ApplicationManager::settings()->defaultEncoding(); m_IsOK = DetectEncoding(buffer); }
EncodingDetector::EncodingDetector(const wxByte* buffer, size_t size, bool useLog) : nsUniversalDetector(NS_FILTER_ALL), m_IsOK(false), m_UseBOM(false), m_UseLog(useLog), m_BOMSizeInBytes(0), m_ConvStr(wxEmptyString) { m_Encoding = wxLocale::GetSystemEncoding(); m_IsOK = DetectEncoding(buffer, size); }
EncodingDetector::EncodingDetector(LoaderBase* fileLdr, bool useLog) : nsUniversalDetector(NS_FILTER_ALL), m_IsOK(false), m_UseBOM(false), m_UseLog(useLog), m_BOMSizeInBytes(0), m_ConvStr(wxEmptyString) { m_Encoding = wxLocale::GetSystemEncoding(); m_IsOK = DetectEncoding((wxByte*)fileLdr->GetData(), fileLdr->GetLength()); }
bool QPlasmaTextDoc::loadFile(QString filename) { if (filename.right(4).toLower() == ".elf") { // Encrypted Log File... We have to handle it specially hsElfStream S; if (!S.open(filename.toUtf8().data(), fmRead)) return false; fEditor->clear(); while (!S.eof()) fEditor->append(~S.readLine() + "\n"); fEditor->setReadOnly(true); } else if (plEncryptedStream::IsFileEncrypted(filename.toUtf8().data())) { plEncryptedStream S(PlasmaVer::pvUnknown); if (!S.open(filename.toUtf8().data(), fmRead, plEncryptedStream::kEncAuto)) return false; if (S.getEncType() == plEncryptedStream::kEncDroid) { if (!GetEncryptionKeyFromUser(this, fDroidKey)) return false; S.setKey(fDroidKey); fEncryption = kEncDroid; } else if (S.getEncType() == plEncryptedStream::kEncXtea) { fEncryption = kEncXtea; } else if (S.getEncType() == plEncryptedStream::kEncAES) { fEncryption = kEncAes; } fEncoding = DetectEncoding(&S); fEditor->setText(LoadData(&S, fEncoding)); } else { hsFileStream S(PlasmaVer::pvUnknown); if (!S.open(filename.toUtf8().data(), fmRead)) return false; fEncryption = kEncNone; fEncoding = DetectEncoding(&S); fEditor->setText(LoadData(&S, fEncoding)); } fEditor->SendScintilla(QsciScintillaBase::SCI_SETSAVEPOINT); return QPlasmaDocument::loadFile(filename); }