MCPcopy Create free account
hub / github.com/WinMerge/winmerge / LoadFromFile

Method LoadFromFile

Src/DiffTextBuffer.cpp:199–373  ·  view source on GitHub ↗

* @brief Load file from disk into buffer * * @param [in] pszFileNameInit File to load * @param [in] infoUnpacker Unpacker plugin * @param [in] sToFindUnpacker String for finding unpacker plugin * @param [out] readOnly Loading was lossy so file should be read-only * @param [in] nCrlfStyle EOL style used * @param [in] encoding Encoding used * @param [out] sError Error message returned * @re

Source from the content-addressed store, hash-verified

197 * @note If this method fails, it calls InitNew so the CDiffTextBuffer is in a valid state
198 */
199int CDiffTextBuffer::LoadFromFile(const tchar_t* pszFileNameInit,
200 PackingInfo& infoUnpacker, const tchar_t* sToFindUnpacker, bool & readOnly,
201 CRLFSTYLE nCrlfStyle, const FileTextEncoding & encoding, String &sError)
202{
203 ASSERT(!m_bInit);
204 ASSERT(m_aLines.size() == 0);
205
206 // Unpacking the file here, save the result in a temporary file
207 m_strTempFileName = pszFileNameInit;
208 if (!infoUnpacker.Unpacking(m_nThisPane, &m_unpackerSubcodes, m_strTempFileName, sToFindUnpacker, { m_strTempFileName }))
209 {
210 InitNew(); // leave crystal editor in valid, empty state
211 return FileLoadResult::FRESULT_ERROR_UNPACK;
212 }
213
214 // we will load the transformed file
215 const tchar_t* pszFileName = m_strTempFileName.c_str();
216
217 String sExt;
218 FileLoadResult::flags_t nRetVal = FileLoadResult::FRESULT_OK;
219
220 // Set encoding based on extension, if we know one
221 paths::SplitFilename(pszFileName, nullptr, nullptr, &sExt);
222 CrystalLineParser::TextDefinition *def =
223 CrystalLineParser::GetTextType(sExt.c_str());
224 if (def && def->encoding != -1)
225 m_nSourceEncoding = def->encoding;
226
227 UniFile *pufile = new UniMemFile;
228
229 // Now we only use the UniFile interface
230 // which is something we could implement for HTTP and/or FTP files
231
232 if (!pufile->OpenReadOnly(pszFileName))
233 {
234 nRetVal = FileLoadResult::FRESULT_ERROR;
235 UniFile::UniError uniErr = pufile->GetLastUniError();
236 if (uniErr.HasError())
237 {
238 sError = uniErr.GetError();
239 }
240 InitNew(); // leave crystal editor in valid, empty state
241 }
242 else
243 {
244 if (!m_unpackerSubcodes.empty())
245 {
246 // re-detect codepage
247 int iGuessEncodingType = GetOptionsMgr()->GetInt(OPT_CP_DETECT);
248 FileTextEncoding encoding2 = codepage_detect::Guess(pszFileName, iGuessEncodingType);
249 pufile->SetUnicoding(encoding2.m_unicoding);
250 pufile->SetCodepage(encoding2.m_codepage);
251 pufile->SetBom(encoding2.m_bom);
252 if (encoding2.m_bom)
253 pufile->ReadBom();
254 }
255 else
256 {

Callers 2

LoadFileMethod · 0.80
OnOpenDocumentMethod · 0.80

Calls 15

SplitFilenameFunction · 0.85
GetTextTypeFunction · 0.85
GuessFunction · 0.85
GetTextFileStyleFunction · 0.85
IsTextFileStylePureFunction · 0.85
TFileClass · 0.85
ErrorFunction · 0.85
UnpackingMethod · 0.80
OpenReadOnlyMethod · 0.80
HasErrorMethod · 0.80
SetBomMethod · 0.80
ReadBomMethod · 0.80

Tested by

no test coverage detected