1// mk4io.h -- 2// $Id: mk4io.h 1230 2007-03-09 15:58:53Z jcw $ 3// This is part of Metakit, the homepage is http://www.equi4.com/metakit.html 4 5/** @file 6 * Declaration of the file stream and strategy classes. 7 */ 8 9#ifndef __MK4IO_H__ 10#define __MK4IO_H__ 11 12#include <stdio.h> 13 14///////////////////////////////////////////////////////////////////////////// 15/// A file stream can be used to serialize using the stdio library. 16 17class c4_FileStream: public c4_Stream { 18 public: 19 c4_FileStream(FILE *stream_, bool owned_ = false); 20 virtual ~c4_FileStream(); 21 22 virtual int Read(void *buffer_, int length_); 23 virtual bool Write(const void *buffer_, int length_); 24 25 FILE *_stream; 26 bool _owned; 27}; 28 29///////////////////////////////////////////////////////////////////////////// 30/// A file strategy encapsulates code dealing with all file I/O. 31 32class c4_FileStrategy: public c4_Strategy { 33 public: 34 /// Construct a new strategy object 35 c4_FileStrategy(FILE *file_ = 0); 36 virtual ~c4_FileStrategy(); 37 38 /// True if we can do I/O with this object 39 virtual bool IsValid()const; 40 /// Open a data file by name 41 virtual bool DataOpen(const char *fileName_, int mode_); 42 /// Read a number of bytes 43 virtual int DataRead(t4_i32 pos_, void *buffer_, int length_); 44 /// Write a number of bytes, return true if successful 45 virtual void DataWrite(t4_i32 pos_, const void *buffer_, int length_); 46 /// Flush and truncate file 47 virtual void DataCommit(t4_i32 newSize_); 48 /// Support for memory-mapped files 49 virtual void ResetFileMapping(); 50 /// Report total size of the datafile 51 virtual t4_i32 FileSize(); 52 /// Return a good value to use as fresh generation counter 53 virtual t4_i32 FreshGeneration(); 54 55 protected: 56 /// Pointer to file object 57 FILE *_file; 58 /// Pointer to same file object, if it must be deleted at end 59 FILE *_cleanup; 60}; 61 62///////////////////////////////////////////////////////////////////////////// 63 64#endif // __MK4IO_H__ 65