OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
CStdFile.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* A handy wrapper class to gzio files */
19 
20 #ifndef INC_CSTDFILE
21 #define INC_CSTDFILE
22 
23 #include "platform/StdSync.h" // for StdThreadCheck
24 #include <zlib.h> // for gzFile
25 
26 const int CStdFileBufSize = 4096;
27 
29 {
30 public:
31  virtual bool Read(void *pBuffer, size_t iSize) = 0;
32  virtual bool Advance(int iOffset) = 0;
33  // Get size. compatible with c4group!
34  virtual size_t AccessedEntrySize() const = 0;
35  virtual ~CStdStream() = default;
36 };
37 
38 class CStdFile: public CStdStream
39 {
40 public:
41  CStdFile();
42  ~CStdFile() override;
43  bool Status;
44  char Name[_MAX_PATH+1];
45 protected:
46  FILE *hFile;
47  gzFile hgzFile;
49  int MemoryPtr;
52  bool ModeWrite;
53  StdThreadCheck thread_check; // thread check helper to make sure only the thread that opened the file is using it
54 public:
55  bool Create(const char *szFileName, bool fCompressed=false, bool fExecutable=false, bool fMemory=false);
56  bool Open(const char *szFileName, bool fCompressed=false);
57  bool Append(const char *szFilename, bool text=false); // append (uncompressed only)
58  bool Close(StdBuf **ppMemory = nullptr);
59  bool Default();
60  bool Read(void *pBuffer, size_t iSize) override { return Read(pBuffer, iSize, nullptr); }
61  bool Read(void *pBuffer, size_t iSize, size_t *ipFSize);
62  bool Write(const void *pBuffer, int iSize);
63  bool WriteString(const char *szStr);
64  bool Rewind();
65  bool Advance(int iOffset) override;
66  int Seek(long int offset, int whence); // seek in file by offset and stdio-style SEEK_* constants. Only implemented for uncompressed files.
67  long int Tell(); // get current file pos. Only implemented for uncompressed files.
68  bool IsOpen() const { return hFile || hgzFile; }
69  // flush contents to disk
70  inline bool Flush() { if (ModeWrite && BufferLoad) return SaveBuffer(); else return true; }
71  size_t AccessedEntrySize() const override;
72 protected:
73  void ClearBuffer();
74  int LoadBuffer();
75  bool SaveBuffer();
76 };
77 
78 int UncompressedFileSize(const char *szFileName);
79 bool GetFileCRC(const char *szFilename, uint32_t *pCRC32);
80 bool GetFileSHA1(const char *szFilename, BYTE *pSHA1);
81 
82 #endif // INC_CSTDFILE
bool Close(StdBuf **ppMemory=nullptr)
Definition: CStdFile.cpp:151
int BufferPtr
Definition: CStdFile.h:51
bool GetFileSHA1(const char *szFilename, BYTE *pSHA1)
Definition: CStdFile.cpp:381
Definition: StdBuf.h:29
bool Default()
Definition: CStdFile.cpp:174
int Seek(long int offset, int whence)
Definition: CStdFile.cpp:308
bool Create(const char *szFileName, bool fCompressed=false, bool fExecutable=false, bool fMemory=false)
Definition: CStdFile.cpp:49
virtual size_t AccessedEntrySize() const =0
FILE * hFile
Definition: CStdFile.h:46
const int CStdFileBufSize
Definition: CStdFile.h:26
bool Status
Definition: CStdFile.h:43
uint8_t BYTE
#define _MAX_PATH
int UncompressedFileSize(const char *szFileName)
Definition: CStdFile.cpp:322
CStdFile()
Definition: CStdFile.cpp:32
bool Write(const void *pBuffer, int iSize)
Definition: CStdFile.cpp:240
bool ModeWrite
Definition: CStdFile.h:52
bool IsOpen() const
Definition: CStdFile.h:68
bool Append(const char *szFilename, bool text=false)
Definition: CStdFile.cpp:132
size_t AccessedEntrySize() const override
Definition: CStdFile.cpp:347
StdThreadCheck thread_check
Definition: CStdFile.h:53
bool GetFileCRC(const char *szFilename, uint32_t *pCRC32)
Definition: CStdFile.cpp:355
virtual ~CStdStream()=default
bool Read(void *pBuffer, size_t iSize) override
Definition: CStdFile.h:60
int BufferLoad
Definition: CStdFile.h:51
bool Advance(int iOffset) override
Definition: CStdFile.cpp:285
~CStdFile() override
Definition: CStdFile.cpp:44
bool Open(const char *szFileName, bool fCompressed=false)
Definition: CStdFile.cpp:95
BYTE Buffer[CStdFileBufSize]
Definition: CStdFile.h:50
bool Rewind()
Definition: CStdFile.cpp:275
bool SaveBuffer()
Definition: CStdFile.cpp:222
int MemoryPtr
Definition: CStdFile.h:49
virtual bool Advance(int iOffset)=0
long int Tell()
Definition: CStdFile.cpp:315
char Name[_MAX_PATH+1]
Definition: CStdFile.h:44
bool WriteString(const char *szStr)
Definition: CStdFile.cpp:264
virtual bool Read(void *pBuffer, size_t iSize)=0
int LoadBuffer()
Definition: CStdFile.cpp:213
bool Flush()
Definition: CStdFile.h:70
gzFile hgzFile
Definition: CStdFile.h:47
void ClearBuffer()
Definition: CStdFile.cpp:234
StdBuf * pMemory
Definition: CStdFile.h:48
int iSize
Definition: TstC4NetIO.cpp:32