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 <stdio.h>
24 #include "platform/StdSync.h" // for StdThreadCheck
25 #include <zlib.h> // for gzFile
26 
27 const int CStdFileBufSize = 4096;
28 
30 {
31 public:
32  virtual bool Read(void *pBuffer, size_t iSize) = 0;
33  virtual bool Advance(int iOffset) = 0;
34  // Get size. compatible with c4group!
35  virtual size_t AccessedEntrySize() const = 0;
36  virtual ~CStdStream() {}
37 };
38 
39 class CStdFile: public CStdStream
40 {
41 public:
42  CStdFile();
43  ~CStdFile();
44  bool Status;
45  char Name[_MAX_PATH+1];
46 protected:
47  FILE *hFile;
48  gzFile hgzFile;
50  int MemoryPtr;
53  bool ModeWrite;
54  StdThreadCheck thread_check; // thread check helper to make sure only the thread that opened the file is using it
55 public:
56  bool Create(const char *szFileName, bool fCompressed=false, bool fExecutable=false, bool fMemory=false);
57  bool Open(const char *szFileName, bool fCompressed=false);
58  bool Append(const char *szFilename, bool text=false); // append (uncompressed only)
59  bool Close(StdBuf **ppMemory = nullptr);
60  bool Default();
61  bool Read(void *pBuffer, size_t iSize) override { return Read(pBuffer, iSize, 0); }
62  bool Read(void *pBuffer, size_t iSize, size_t *ipFSize);
63  bool Write(const void *pBuffer, int iSize);
64  bool WriteString(const char *szStr);
65  bool Rewind();
66  bool Advance(int iOffset) override;
67  int Seek(long int offset, int whence); // seek in file by offset and stdio-style SEEK_* constants. Only implemented for uncompressed files.
68  long int Tell(); // get current file pos. Only implemented for uncompressed files.
69  bool IsOpen() const { return hFile || hgzFile; }
70  // flush contents to disk
71  inline bool Flush() { if (ModeWrite && BufferLoad) return SaveBuffer(); else return true; }
72  size_t AccessedEntrySize() const override;
73 protected:
74  void ClearBuffer();
75  int LoadBuffer();
76  bool SaveBuffer();
77 };
78 
79 int UncompressedFileSize(const char *szFileName);
80 bool GetFileCRC(const char *szFilename, uint32_t *pCRC32);
81 bool GetFileSHA1(const char *szFilename, BYTE *pSHA1);
82 
83 #endif // INC_CSTDFILE
bool Close(StdBuf **ppMemory=nullptr)
Definition: CStdFile.cpp:155
int BufferPtr
Definition: CStdFile.h:52
bool GetFileSHA1(const char *szFilename, BYTE *pSHA1)
Definition: CStdFile.cpp:385
Definition: StdBuf.h:37
bool Default()
Definition: CStdFile.cpp:178
int Seek(long int offset, int whence)
Definition: CStdFile.cpp:312
bool Create(const char *szFileName, bool fCompressed=false, bool fExecutable=false, bool fMemory=false)
Definition: CStdFile.cpp:53
virtual size_t AccessedEntrySize() const =0
FILE * hFile
Definition: CStdFile.h:47
const int CStdFileBufSize
Definition: CStdFile.h:27
bool Status
Definition: CStdFile.h:44
uint8_t BYTE
#define _MAX_PATH
~CStdFile()
Definition: CStdFile.cpp:48
int UncompressedFileSize(const char *szFileName)
Definition: CStdFile.cpp:326
CStdFile()
Definition: CStdFile.cpp:36
bool Write(const void *pBuffer, int iSize)
Definition: CStdFile.cpp:244
bool ModeWrite
Definition: CStdFile.h:53
bool IsOpen() const
Definition: CStdFile.h:69
bool Append(const char *szFilename, bool text=false)
Definition: CStdFile.cpp:136
virtual ~CStdStream()
Definition: CStdFile.h:36
size_t AccessedEntrySize() const override
Definition: CStdFile.cpp:351
StdThreadCheck thread_check
Definition: CStdFile.h:54
bool GetFileCRC(const char *szFilename, uint32_t *pCRC32)
Definition: CStdFile.cpp:359
bool Read(void *pBuffer, size_t iSize) override
Definition: CStdFile.h:61
int BufferLoad
Definition: CStdFile.h:52
bool Advance(int iOffset) override
Definition: CStdFile.cpp:289
bool Open(const char *szFileName, bool fCompressed=false)
Definition: CStdFile.cpp:99
BYTE Buffer[CStdFileBufSize]
Definition: CStdFile.h:51
bool Rewind()
Definition: CStdFile.cpp:279
bool SaveBuffer()
Definition: CStdFile.cpp:226
int MemoryPtr
Definition: CStdFile.h:50
virtual bool Advance(int iOffset)=0
long int Tell()
Definition: CStdFile.cpp:319
char Name[_MAX_PATH+1]
Definition: CStdFile.h:45
bool WriteString(const char *szStr)
Definition: CStdFile.cpp:268
virtual bool Read(void *pBuffer, size_t iSize)=0
int LoadBuffer()
Definition: CStdFile.cpp:217
bool Flush()
Definition: CStdFile.h:71
gzFile hgzFile
Definition: CStdFile.h:48
void ClearBuffer()
Definition: CStdFile.cpp:238
StdBuf * pMemory
Definition: CStdFile.h:49
int iSize
Definition: TstC4NetIO.cpp:35