OpenClonk
CStdFile Class Reference

#include <CStdFile.h>

Inheritance diagram for CStdFile:
[legend]
Collaboration diagram for CStdFile:
[legend]

Public Member Functions

 CStdFile ()
 
 ~CStdFile () override
 
bool Create (const char *szFileName, bool fCompressed=false, bool fExecutable=false, bool fMemory=false)
 
bool Open (const char *szFileName, bool fCompressed=false)
 
bool Append (const char *szFilename, bool text=false)
 
bool Close (StdBuf **ppMemory=nullptr)
 
bool Default ()
 
bool Read (void *pBuffer, size_t iSize) override
 
bool Read (void *pBuffer, size_t iSize, size_t *ipFSize)
 
bool Write (const void *pBuffer, int iSize)
 
bool WriteString (const char *szStr)
 
bool Rewind ()
 
bool Advance (int iOffset) override
 
int Seek (long int offset, int whence)
 
long int Tell ()
 
bool IsOpen () const
 
bool Flush ()
 
size_t AccessedEntrySize () const override
 

Public Attributes

bool Status
 
char Name [_MAX_PATH_LEN]
 

Protected Member Functions

void ClearBuffer ()
 
int LoadBuffer ()
 
bool SaveBuffer ()
 

Protected Attributes

FILE * hFile
 
gzFile hgzFile
 
StdBufpMemory
 
int MemoryPtr
 
BYTE Buffer [CStdFileBufSize]
 
int BufferLoad
 
int BufferPtr
 
bool ModeWrite
 
StdThreadCheck thread_check
 

Detailed Description

Definition at line 38 of file CStdFile.h.

Constructor & Destructor Documentation

◆ CStdFile()

CStdFile::CStdFile ( )

Definition at line 32 of file CStdFile.cpp.

33 {
34  thread_check.Set();
35  Status=false;
36  hFile=nullptr;
37  hgzFile=nullptr;
38  pMemory=nullptr;
39  ClearBuffer();
40  ModeWrite=false;
41  Name[0]=0;
42 }
bool ModeWrite
Definition: CStdFile.h:52
bool Status
Definition: CStdFile.h:43
gzFile hgzFile
Definition: CStdFile.h:47
void ClearBuffer()
Definition: CStdFile.cpp:234
char Name[_MAX_PATH_LEN]
Definition: CStdFile.h:44
StdBuf * pMemory
Definition: CStdFile.h:48
FILE * hFile
Definition: CStdFile.h:46
StdThreadCheck thread_check
Definition: CStdFile.h:53
void Set()
Definition: StdSync.h:294

References ClearBuffer(), hFile, hgzFile, ModeWrite, Name, pMemory, StdThreadCheck::Set(), Status, and thread_check.

Here is the call graph for this function:

◆ ~CStdFile()

CStdFile::~CStdFile ( )
override

Definition at line 44 of file CStdFile.cpp.

45 {
46  Close();
47 }
bool Close(StdBuf **ppMemory=nullptr)
Definition: CStdFile.cpp:151

References Close().

Here is the call graph for this function:

Member Function Documentation

◆ AccessedEntrySize()

size_t CStdFile::AccessedEntrySize ( ) const
overridevirtual

Implements CStdStream.

Definition at line 347 of file CStdFile.cpp.

348 {
349  if (hFile)
350  return FileSize(fileno(hFile));
351  assert(!hgzFile);
352  return 0;
353 }
size_t FileSize(const char *fname)

References FileSize(), hFile, and hgzFile.

Here is the call graph for this function:

◆ Advance()

bool CStdFile::Advance ( int  iOffset)
overridevirtual

Implements CStdStream.

Definition at line 285 of file CStdFile.cpp.

286 {
288  if (ModeWrite) return false;
289  while (iOffset > 0)
290  {
291  // Valid data in the buffer: Transfer as much as possible
292  if (BufferLoad > BufferPtr)
293  {
294  int transfer = std::min(BufferLoad-BufferPtr,iOffset);
295  BufferPtr += transfer;
296  iOffset -= transfer;
297  }
298  // Buffer empty: Load or skip
299  else
300  {
301  if (hFile) return !fseek(hFile, iOffset, SEEK_CUR); // uncompressed: Just skip
302  if (LoadBuffer()<=0) return false; // compressed: Read...
303  }
304  }
305  return true;
306 }
int BufferPtr
Definition: CStdFile.h:51
int BufferLoad
Definition: CStdFile.h:51
int LoadBuffer()
Definition: CStdFile.cpp:213
void Check()
Definition: StdSync.h:295

References BufferLoad, BufferPtr, StdThreadCheck::Check(), hFile, LoadBuffer(), ModeWrite, and thread_check.

Here is the call graph for this function:

◆ Append()

bool CStdFile::Append ( const char *  szFilename,
bool  text = false 
)

Definition at line 132 of file CStdFile.cpp.

133 {
134  SCopy(szFilename,Name,_MAX_PATH);
135  thread_check.Set();
136  // Set modes
137  ModeWrite=true;
138  // Open standard file
139 #ifdef _WIN32
140  if (!(hFile = _wfopen(GetWideChar(Name), text ? L"at" : L"ab"))) return false;
141 #else
142  if (!(hFile=fopen(Name,text ? "at" : "ab"))) return false;
143 #endif
144  // Reset buffer
145  ClearBuffer();
146  // Set status
147  Status=true;
148  return true;
149 }
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
#define _MAX_PATH
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:152

References _MAX_PATH, ClearBuffer(), GetWideChar(), hFile, ModeWrite, Name, SCopy(), StdThreadCheck::Set(), Status, C4ScriptGuiWindowPropertyName::text, and thread_check.

Referenced by C4TableGraph::DumpToFile(), and C4MessageInput::ProcessCommand().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ClearBuffer()

void CStdFile::ClearBuffer ( )
protected

Definition at line 234 of file CStdFile.cpp.

235 {
238 }

References BufferLoad, BufferPtr, StdThreadCheck::Check(), and thread_check.

Referenced by Append(), Create(), CStdFile(), Open(), and Rewind().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Close()

bool CStdFile::Close ( StdBuf **  ppMemory = nullptr)

Definition at line 151 of file CStdFile.cpp.

152 {
154  bool rval=true;
155  Status=false;
156  Name[0]=0;
157  // Save buffer if in write mode
158  if (ModeWrite && BufferLoad) if (!SaveBuffer()) rval=false;
159  // Close file(s)
160  if (hgzFile) if (c4_gzclose(hgzFile)!=Z_OK) rval=false;
161  if (hFile) if (fclose(hFile)!=0) rval=false;
162  if (pMemory)
163  {
164  if (ppMemory)
165  { *ppMemory = pMemory; pMemory = nullptr; }
166  else
167  delete pMemory;
168  }
169  MemoryPtr=0;
170  hgzFile=nullptr; hFile=nullptr;
171  return !!rval;
172 }
bool SaveBuffer()
Definition: CStdFile.cpp:222
int MemoryPtr
Definition: CStdFile.h:49
int ZEXPORT c4_gzclose(gzFile file)
Definition: gzio.c:964

References BufferLoad, c4_gzclose(), StdThreadCheck::Check(), hFile, hgzFile, MemoryPtr, ModeWrite, Name, pMemory, SaveBuffer(), Status, and thread_check.

Referenced by C4Playback::Clear(), C4ConfigGeneral::CreateSaveFolder(), C4UpdatePackage::Execute(), C4Group::ExtractEntry(), GetFileCRC(), GetFileSHA1(), main(), C4UpdatePackage::MakeUpdate(), C4Group::Open(), C4MessageInput::ProcessCommand(), C4Group::Save(), C4PXSSystem::Save(), CSurface8::Save(), C4Record::Stop(), and ~CStdFile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Create()

bool CStdFile::Create ( const char *  szFileName,
bool  fCompressed = false,
bool  fExecutable = false,
bool  fMemory = false 
)

Definition at line 49 of file CStdFile.cpp.

50 {
51  thread_check.Set();
52  // Set modes
53  ModeWrite=true;
54  // Open in memory?
55  if (fMemory)
56  {
57  if (!(pMemory = new StdBuf())) return false;
58  MemoryPtr = 0;
59 
60  StdStrBuf buf;
61  buf.Format("<%p>", pMemory);
62  SCopy(buf.getData(), Name, _MAX_PATH);
63  }
64  // Open standard file
65  else
66  {
67  SCopy(szFilename,Name,_MAX_PATH);
68  int flags = _O_BINARY|O_CLOEXEC|O_CREAT|O_WRONLY|O_TRUNC;
69 #ifdef _WIN32
70  int mode = _S_IREAD|_S_IWRITE;
71  int fd = _wopen(GetWideChar(Name), flags, mode);
72 #else
73  mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
74  if (fExecutable)
75  mode |= S_IXUSR|S_IXGRP|S_IXOTH;
76  int fd = open(Name, flags, mode);
77 #endif
78  if (fd == -1) return false;
79  if (fCompressed)
80  {
81  if (!(hgzFile = c4_gzdopen(fd,"wb1"))) return false;
82  }
83  else
84  {
85  if (!(hFile = fdopen(fd,"wb"))) return false;
86  }
87  }
88  // Reset buffer
89  ClearBuffer();
90  // Set status
91  Status=true;
92  return true;
93 }
#define _O_BINARY
#define O_CLOEXEC
Definition: StdBuf.h:30
const char * getData() const
Definition: StdBuf.h:442
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:174
gzFile ZEXPORT c4_gzdopen(int fd, const char *mode)
Definition: gzio.c:228

References _MAX_PATH, _O_BINARY, c4_gzdopen(), ClearBuffer(), StdStrBuf::Format(), StdStrBuf::getData(), GetWideChar(), hFile, hgzFile, MemoryPtr, ModeWrite, Name, O_CLOEXEC, pMemory, SCopy(), StdThreadCheck::Set(), Status, and thread_check.

Referenced by C4ConfigGeneral::CreateSaveFolder(), C4TableGraph::DumpToFile(), C4UpdatePackage::Execute(), C4Group::ExtractEntry(), main(), C4UpdatePackage::MakeUpdate(), C4Playback::Open(), C4Group::Open(), C4Group::Save(), C4PXSSystem::Save(), CSurface8::Save(), and C4Record::Start().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Default()

bool CStdFile::Default ( )

Definition at line 174 of file CStdFile.cpp.

175 {
176  Status=false;
177  Name[0]=0;
178  hgzFile=nullptr;
179  hFile=nullptr;
180  pMemory=nullptr;
181  MemoryPtr=0;
183  thread_check.Set();
184  return true;
185 }

References BufferLoad, BufferPtr, hFile, hgzFile, MemoryPtr, Name, pMemory, StdThreadCheck::Set(), Status, and thread_check.

Here is the call graph for this function:

◆ Flush()

bool CStdFile::Flush ( )
inline

Definition at line 70 of file CStdFile.h.

70 { if (ModeWrite && BufferLoad) return SaveBuffer(); else return true; }

References BufferLoad, ModeWrite, and SaveBuffer().

Referenced by LogSilent(), C4Record::Rec(), and C4UpdatePackage::WriteLog().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsOpen()

bool CStdFile::IsOpen ( ) const
inline

Definition at line 68 of file CStdFile.h.

68 { return hFile || hgzFile; }

References hFile, and hgzFile.

◆ LoadBuffer()

int CStdFile::LoadBuffer ( )
protected

Definition at line 213 of file CStdFile.cpp.

214 {
216  if (hFile) BufferLoad = fread(Buffer,1,CStdFileBufSize,hFile);
218  BufferPtr=0;
219  return BufferLoad;
220 }
const int CStdFileBufSize
Definition: CStdFile.h:26
BYTE Buffer[CStdFileBufSize]
Definition: CStdFile.h:50
int ZEXPORT c4_gzread(gzFile file, voidp buf, unsigned len)
Definition: gzio.c:405

References Buffer, BufferLoad, BufferPtr, c4_gzread(), StdThreadCheck::Check(), CStdFileBufSize, hFile, hgzFile, and thread_check.

Referenced by Advance(), and Read().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Open()

bool CStdFile::Open ( const char *  szFileName,
bool  fCompressed = false 
)

Definition at line 95 of file CStdFile.cpp.

96 {
97  SCopy(szFilename,Name,_MAX_PATH);
98  thread_check.Set();
99  // Set modes
100  ModeWrite=false;
101  int flags = _O_BINARY|O_CLOEXEC|O_RDONLY;
102 #ifdef _WIN32
103  int mode = _S_IREAD|_S_IWRITE;
104  int fd = _wopen(GetWideChar(Name), flags, mode);
105 #else
106  mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
107  int fd = open(Name, flags, mode);
108 #endif
109  if(fd == -1) return false;
110  if (fCompressed)
111  {
112  if (!(hgzFile = c4_gzdopen(fd,"rb"))) { close(fd); return false; }
113  /* Reject uncompressed files */
114  if(c4_gzdirect(hgzFile))
115  {
117  hgzFile = nullptr;
118  return false;
119  }
120  }
121  else
122  {
123  if (!(hFile = fdopen(fd,"rb"))) return false;
124  }
125  // Reset buffer
126  ClearBuffer();
127  // Set status
128  Status=true;
129  return true;
130 }
int ZEXPORT c4_gzdirect(gzFile file)
Definition: gzio.c:919

References _MAX_PATH, _O_BINARY, c4_gzclose(), c4_gzdirect(), c4_gzdopen(), ClearBuffer(), GetWideChar(), hFile, hgzFile, ModeWrite, Name, O_CLOEXEC, SCopy(), StdThreadCheck::Set(), Status, and thread_check.

Referenced by GetFileCRC(), GetFileSHA1(), main(), and C4Playback::Open().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Read() [1/2]

bool CStdFile::Read ( void *  pBuffer,
size_t  iSize 
)
inlineoverridevirtual

Implements CStdStream.

Definition at line 60 of file CStdFile.h.

60 { return Read(pBuffer, iSize, nullptr); }
int iSize
Definition: TstC4NetIO.cpp:32
bool Read(void *pBuffer, size_t iSize) override
Definition: CStdFile.h:60

References iSize, and Read().

Referenced by C4Playback::Check(), GetFileCRC(), GetFileSHA1(), main(), C4Playback::NextSequentialChunk(), and Read().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Read() [2/2]

bool CStdFile::Read ( void *  pBuffer,
size_t  iSize,
size_t *  ipFSize 
)

Definition at line 187 of file CStdFile.cpp.

188 {
190  int transfer;
191  if (!pBuffer) return false;
192  if (ModeWrite) return false;
193  BYTE *bypBuffer= (BYTE*) pBuffer;
194  if (ipFSize) *ipFSize = 0;
195  while (iSize>0)
196  {
197  // Valid data in the buffer: Transfer as much as possible
198  if (BufferLoad>BufferPtr)
199  {
200  transfer=std::min<size_t>(BufferLoad-BufferPtr,iSize);
201  memmove(bypBuffer, Buffer+BufferPtr, transfer);
202  BufferPtr+=transfer;
203  bypBuffer+=transfer;
204  iSize-=transfer;
205  if (ipFSize) *ipFSize += transfer;
206  }
207  // Buffer empty: Load
208  else if (LoadBuffer()<=0) return false;
209  }
210  return true;
211 }
uint8_t BYTE

References Buffer, BufferLoad, BufferPtr, StdThreadCheck::Check(), iSize, LoadBuffer(), ModeWrite, and thread_check.

Here is the call graph for this function:

◆ Rewind()

bool CStdFile::Rewind ( )

Definition at line 275 of file CStdFile.cpp.

276 {
278  if (ModeWrite) return false;
279  ClearBuffer();
280  if (hFile) rewind(hFile);
282  return true;
283 }
int ZEXPORT c4_gzrewind(gzFile file)
Definition: gzio.c:868

References c4_gzrewind(), StdThreadCheck::Check(), ClearBuffer(), hFile, hgzFile, ModeWrite, and thread_check.

Here is the call graph for this function:

◆ SaveBuffer()

bool CStdFile::SaveBuffer ( )
protected

Definition at line 222 of file CStdFile.cpp.

223 {
225  int saved = 0;
226  if (hFile) saved=fwrite(Buffer,1,BufferLoad,hFile);
228  if (pMemory) { pMemory->Append(Buffer, BufferLoad); saved = BufferLoad; }
229  if (saved!=BufferLoad) return false;
230  BufferLoad=0;
231  return true;
232 }
void Append(const void *pnData, size_t inSize)
Definition: StdBuf.h:254
int ZEXPORT c4_gzwrite(gzFile file, voidpc buf, unsigned len)
Definition: gzio.c:570

References StdBuf::Append(), Buffer, BufferLoad, c4_gzwrite(), StdThreadCheck::Check(), hFile, hgzFile, pMemory, and thread_check.

Referenced by Close(), Flush(), and Write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Seek()

int CStdFile::Seek ( long int  offset,
int  whence 
)

Definition at line 308 of file CStdFile.cpp.

309 {
310  // seek in file by offset and stdio-style SEEK_* constants. Only implemented for uncompressed files.
311  assert(!hgzFile);
312  return fseek(hFile, offset, whence);
313 }

References hFile, and hgzFile.

◆ Tell()

long int CStdFile::Tell ( )

Definition at line 315 of file CStdFile.cpp.

316 {
317  // get current file pos. Only implemented for uncompressed files.
318  assert(!hgzFile);
319  return ftell(hFile);
320 }

References hFile, and hgzFile.

◆ Write()

bool CStdFile::Write ( const void *  pBuffer,
int  iSize 
)

Definition at line 240 of file CStdFile.cpp.

241 {
243  int transfer;
244  if (!pBuffer) return false;
245  if (!ModeWrite) return false;
246  const BYTE *bypBuffer= (const BYTE*) pBuffer;
247  while (iSize>0)
248  {
249  // Space in buffer: Transfer as much as possible
251  {
252  transfer=std::min(CStdFileBufSize-BufferLoad,iSize);
253  memcpy(Buffer+BufferLoad,bypBuffer,transfer);
254  BufferLoad+=transfer;
255  bypBuffer+=transfer;
256  iSize-=transfer;
257  }
258  // Buffer full: Save
259  else if (!SaveBuffer()) return false;
260  }
261  return true;
262 }

References Buffer, BufferLoad, StdThreadCheck::Check(), CStdFileBufSize, iSize, ModeWrite, SaveBuffer(), and thread_check.

Referenced by C4Playback::Check(), C4UpdatePackage::Execute(), C4Group::ExtractEntry(), LogSilent(), main(), C4UpdatePackage::MakeUpdate(), C4Record::Rec(), C4Group::Save(), C4PXSSystem::Save(), CSurface8::Save(), C4Record::Stop(), C4UpdatePackage::WriteLog(), and WriteString().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ WriteString()

bool CStdFile::WriteString ( const char *  szStr)

Definition at line 264 of file CStdFile.cpp.

265 {
267  BYTE nl[2]={0x0D,0x0A};
268  if (!szStr) return false;
269  int size=SLen(szStr);
270  if (!Write((const void*)szStr,size)) return false;
271  if (!Write(nl,2)) return false;
272  return true;
273 }
size_t SLen(const char *sptr)
Definition: Standard.h:74
bool Write(const void *pBuffer, int iSize)
Definition: CStdFile.cpp:240

References StdThreadCheck::Check(), SLen(), thread_check, and Write().

Referenced by C4ConfigGeneral::CreateSaveFolder(), C4TableGraph::DumpToFile(), and C4MessageInput::ProcessCommand().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ Buffer

BYTE CStdFile::Buffer[CStdFileBufSize]
protected

Definition at line 50 of file CStdFile.h.

Referenced by LoadBuffer(), Read(), SaveBuffer(), and Write().

◆ BufferLoad

int CStdFile::BufferLoad
protected

Definition at line 51 of file CStdFile.h.

Referenced by Advance(), ClearBuffer(), Close(), Default(), Flush(), LoadBuffer(), Read(), SaveBuffer(), and Write().

◆ BufferPtr

int CStdFile::BufferPtr
protected

Definition at line 51 of file CStdFile.h.

Referenced by Advance(), ClearBuffer(), Default(), LoadBuffer(), and Read().

◆ hFile

FILE* CStdFile::hFile
protected

◆ hgzFile

gzFile CStdFile::hgzFile
protected

◆ MemoryPtr

int CStdFile::MemoryPtr
protected

Definition at line 49 of file CStdFile.h.

Referenced by Close(), Create(), and Default().

◆ ModeWrite

bool CStdFile::ModeWrite
protected

Definition at line 52 of file CStdFile.h.

Referenced by Advance(), Append(), Close(), Create(), CStdFile(), Flush(), Open(), Read(), Rewind(), and Write().

◆ Name

char CStdFile::Name[_MAX_PATH_LEN]

Definition at line 44 of file CStdFile.h.

Referenced by Append(), Close(), Create(), CStdFile(), Default(), and Open().

◆ pMemory

StdBuf* CStdFile::pMemory
protected

Definition at line 48 of file CStdFile.h.

Referenced by Close(), Create(), CStdFile(), Default(), and SaveBuffer().

◆ Status

bool CStdFile::Status

Definition at line 43 of file CStdFile.h.

Referenced by Append(), Close(), Create(), CStdFile(), Default(), and Open().

◆ thread_check

StdThreadCheck CStdFile::thread_check
protected

The documentation for this class was generated from the following files: