MCPcopy Create free account
hub / github.com/abbeycode/UnrarKit / ReadTextFile

Function ReadTextFile

Libraries/unrar/filestr.cpp:3–148  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1#include "rar.hpp"
2
3bool ReadTextFile(
4 const wchar *Name,
5 StringList *List,
6 bool Config,
7 bool AbortOnError,
8 RAR_CHARSET SrcCharset,
9 bool Unquote,
10 bool SkipComments,
11 bool ExpandEnvStr)
12{
13 wchar FileName[NM];
14 *FileName=0;
15
16 if (Name!=NULL)
17 if (Config)
18 GetConfigName(Name,FileName,ASIZE(FileName),true,false);
19 else
20 wcsncpyz(FileName,Name,ASIZE(FileName));
21
22 File SrcFile;
23 if (*FileName!=0)
24 {
25 bool OpenCode=AbortOnError ? SrcFile.WOpen(FileName):SrcFile.Open(FileName,0);
26
27 if (!OpenCode)
28 {
29 if (AbortOnError)
30 ErrHandler.Exit(RARX_OPEN);
31 return false;
32 }
33 }
34 else
35 SrcFile.SetHandleType(FILE_HANDLESTD);
36
37 uint DataSize=0,ReadSize;
38 const int ReadBlock=4096;
39
40 Array<byte> Data(ReadBlock);
41 while ((ReadSize=SrcFile.Read(&Data[DataSize],ReadBlock))!=0)
42 {
43 DataSize+=ReadSize;
44 Data.Add(ReadSize); // Always have ReadBlock available for next data.
45 }
46 // Set to really read size, so we can zero terminate it correctly.
47 Data.Alloc(DataSize);
48
49 int LittleEndian=DataSize>=2 && Data[0]==255 && Data[1]==254 ? 1:0;
50 int BigEndian=DataSize>=2 && Data[0]==254 && Data[1]==255 ? 1:0;
51 bool Utf8=DataSize>=3 && Data[0]==0xef && Data[1]==0xbb && Data[2]==0xbf;
52
53 if (SrcCharset==RCH_DEFAULT)
54 SrcCharset=DetectTextEncoding(&Data[0],DataSize);
55
56 Array<wchar> DataW;
57
58 if (SrcCharset==RCH_DEFAULT || SrcCharset==RCH_OEM || SrcCharset==RCH_ANSI)
59 {
60 Data.Push(0); // Zero terminate.

Callers 3

ParseArgMethod · 0.85
ReadConfigMethod · 0.85
ProcessSwitchMethod · 0.85

Calls 15

GetConfigNameFunction · 0.85
wcsncpyzFunction · 0.85
DetectTextEncodingFunction · 0.85
CharToWideFunction · 0.85
UtfToWideFunction · 0.85
WOpenMethod · 0.80
ExitMethod · 0.80
SetHandleTypeMethod · 0.80
AddMethod · 0.80
AllocMethod · 0.80
PushMethod · 0.80
AddStringMethod · 0.80

Tested by

no test coverage detected