| 1 | /* |
| 2 | * LegacyClonk |
| 3 | * |
| 4 | * Copyright (c) 1998-2000, Matthes Bender (RedWolf Design) |
| 5 | * Copyright (c) 2017-2020, The LegacyClonk Team and contributors |
| 6 | * |
| 7 | * Distributed under the terms of the ISC license; see accompanying file |
| 8 | * "COPYING" for details. |
| 9 | * |
| 10 | * "Clonk" is a registered trademark of Matthes Bender, used with permission. |
| 11 | * See accompanying file "TRADEMARK" for details. |
| 12 | * |
| 13 | * To redistribute this file separately, substitute the full license texts |
| 14 | * for the above references. |
| 15 | */ |
| 16 | |
| 17 | /* A handy wrapper class to gzio files */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include <StdFile.h> |
| 22 | #include <StdGzCompressedFile.h> |
| 23 | |
| 24 | #include <memory> |
| 25 | #include <cstdio> |
| 26 | |
| 27 | constexpr unsigned int CStdFileBufSize = 4096; |
| 28 | |
| 29 | class CStdFile |
| 30 | { |
| 31 | public: |
| 32 | CStdFile(); |
| 33 | ~CStdFile(); |
| 34 | bool Status; |
| 35 | char Name[_MAX_PATH + 1]; |
| 36 | |
| 37 | protected: |
| 38 | FILE *hFile; |
| 39 | std::shared_ptr<StdGzCompressedFile::Read> readCompressedFile; |
| 40 | std::shared_ptr<StdGzCompressedFile::Write> writeCompressedFile; |
| 41 | uint8_t Buffer[CStdFileBufSize]; |
| 42 | size_t BufferLoad, BufferPtr; |
| 43 | bool ModeWrite; |
| 44 | |
| 45 | public: |
| 46 | bool Create(const char *szFileName, bool fCompressed = false, bool fExecutable = false, bool exclusive = false); |
| 47 | bool Open(const char *szFileName, bool fCompressed = false); |
| 48 | bool Append(const char *szFilename); // append (uncompressed only) |
| 49 | bool Close(); |
| 50 | bool Default(); |
| 51 | bool Read(void *pBuffer, size_t iSize) { return Read(pBuffer, iSize, ipFSize: nullptr); } |
| 52 | bool Read(void *pBuffer, size_t iSize, size_t *ipFSize); |
| 53 | bool Write(const void *pBuffer, size_t iSize); |
| 54 | bool WriteString(const char *szStr); |
| 55 | bool Rewind(); |
| 56 | bool Advance(size_t iOffset); |
| 57 | // Single line commands |
| 58 | bool Load(const char *szFileName, uint8_t **lpbpBuf, |
| 59 | size_t *ipSize = nullptr, int iAppendZeros = 0, |
| 60 | bool fCompressed = false); |
| 61 | bool Save(const char *szFileName, const uint8_t *bpBuf, |
| 62 | size_t iSize, |
| 63 | bool fCompressed = false, |
| 64 | bool executable = false, |
| 65 | bool exclusive = false); |
| 66 | // flush contents to disk |
| 67 | inline bool Flush() { if (ModeWrite && BufferLoad) return SaveBuffer(); else return true; } |
| 68 | size_t AccessedEntrySize(); |
| 69 | |
| 70 | protected: |
| 71 | void ClearBuffer(); |
| 72 | size_t LoadBuffer(); |
| 73 | bool SaveBuffer(); |
| 74 | }; |
| 75 | |
| 76 | size_t UncompressedFileSize(const char *szFileName); |
| 77 | |