File indexing completed on 2025-01-26 04:47:04
0001 // mk4io.h -- 0002 // This is part of Metakit, the homepage is http://www.equi4.com/metakit/ 0003 0004 /** @file 0005 * Declaration of the file stream and strategy classes. 0006 */ 0007 0008 #pragma once 0009 0010 #include <cstdio> 0011 0012 ///////////////////////////////////////////////////////////////////////////// 0013 /// A file stream can be used to serialize using the stdio library. 0014 0015 class c4_FileStream : public c4_Stream 0016 { 0017 public: 0018 c4_FileStream(FILE *stream_, bool owned_ = false); 0019 ~c4_FileStream() override; 0020 0021 int Read(void *buffer_, int length_) override; 0022 bool Write(const void *buffer_, int length_) override; 0023 0024 FILE *_stream; 0025 bool _owned; 0026 }; 0027 0028 ///////////////////////////////////////////////////////////////////////////// 0029 /// A file strategy encapsulates code dealing with all file I/O. 0030 0031 class c4_FileStrategy : public c4_Strategy 0032 { 0033 public: 0034 /// Construct a new strategy object 0035 c4_FileStrategy(FILE *file_ = nullptr); 0036 ~c4_FileStrategy() override; 0037 0038 /// True if we can do I/O with this object 0039 bool IsValid() const override; 0040 /// Open a data file by name 0041 virtual bool DataOpen(const char *fileName_, int mode_); 0042 /// Read a number of bytes 0043 int DataRead(t4_i32 pos_, void *buffer_, int length_) override; 0044 /// Write a number of bytes, return true if successful 0045 void DataWrite(t4_i32 pos_, const void *buffer_, int length_) override; 0046 /// Flush and truncate file 0047 void DataCommit(t4_i32 newSize_) override; 0048 /// Support for memory-mapped files 0049 void ResetFileMapping() override; 0050 /// Report total size of the datafile 0051 t4_i32 FileSize() override; 0052 /// Return a good value to use as fresh generation counter 0053 t4_i32 FreshGeneration() override; 0054 0055 protected: 0056 /// Pointer to file object 0057 FILE *_file; 0058 /// Pointer to same file object, if it must be deleted at end 0059 FILE *_cleanup; 0060 }; 0061 0062 ///////////////////////////////////////////////////////////////////////////// 0063