Scid  4.6.5
mfile.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////
2 //
3 // FILE: mfile.h
4 // MFile class
5 //
6 // Part of: Scid (Shane's Chess Information Database)
7 // Version: 2.0
8 //
9 // Notice: Copyright (c) 2000 Shane Hudson. All rights reserved.
10 //
11 // Author: Shane Hudson (sgh@users.sourceforge.net)
12 //
13 //////////////////////////////////////////////////////////////////////
14 
15 
16 // An MFile is a file that can be a regular file, or memory-only with
17 // no actual file on any device.
18 // In addition, an MFile can decode its contents from a GZip (.gz) file
19 // and will hopefully in future also be able to extract the contents of
20 // all files in a Zip file, as if they were in one large plain file.
21 
22 
23 #ifndef SCID_MFILE_H
24 #define SCID_MFILE_H
25 
26 #include "common.h"
27 #include <stdio.h>
28 
29 #ifdef ZLIB
30  #include <zlib.h>
31  inline bool gzable() { return true; }
32 #else
33  // If the zlib compression library is NOT used, create dummy inline
34  // functions to replace those used in zlib, which saves wrapping every
35  // zlib function call with #ifndef conditions.
36  inline bool gzable() { return false; }
37  typedef void * gzFile;
38  inline gzFile gzopen (const char*, const char*) { return 0; }
39  inline int gzputc (gzFile, int c) { return c; }
40  inline int gzgetc (gzFile) { return -1; }
41  inline int gzread (gzFile, unsigned char*, int) { return 0; }
42  inline int gzeof (gzFile) { return 1; }
43  inline int gzseek (gzFile, int, int) { return 0; }
44  inline int gzclose (gzFile) { return 0; }
45 #endif
46 
47 
48 enum mfileT {
50 };
51 
52 class MFile
53 {
54  private:
55  static const int SEQREADBUFSIZE = 64 * 1024;
56  FILE * Handle; // For regular files.
57  gzFile GzHandle; // For Gzip files.
58  fileModeT FileMode;
59  mfileT Type;
60  char * FileName;
61  uint Capacity;
62  uint Location;
63  uint SeqReadLocation;
64  byte * Data;
65  byte * CurrentPtr;
66  // The next few fields are used to improve I/O speed on Gzip files, by
67  // avoiding doing a gzgetc() every character, since the zlib file gzio.c
68  // simply does a (relatively slow) gzread() for each gzgetc().
69  byte * GzBuffer;
70  int GzBuffer_Avail;
71  byte * GzBuffer_Current;
72 
73  void Init();
74  void Extend();
75  int FillGzBuffer();
76 
77  public:
78  MFile() { Init(); }
79  ~MFile() {
80  if (Handle != NULL) fclose (Handle);
81  if (GzHandle != NULL) gzclose (GzHandle);
82  if (Data != NULL) delete[] Data;
83  if (GzBuffer != NULL) delete[] GzBuffer;
84  if (FileName != NULL) delete[] FileName;
85  }
86 
87  errorT Create (const char * name, fileModeT fmode);
88  errorT Open (const char * name, fileModeT fmode);
89  errorT Seek (uint position);
90  errorT Flush ();
91  inline bool EndOfFile();
92 
93  errorT WriteNBytes (const char * str, uint length);
94  errorT ReadNBytes (char * str, uint length);
95  inline errorT WriteOneByte (byte value);
96  errorT WriteTwoBytes (uint value);
97  errorT WriteThreeBytes (uint value);
98  errorT WriteFourBytes (uint value);
99  inline int ReadOneByte ();
100  uint ReadTwoBytes ();
101  uint ReadThreeBytes ();
102  uint ReadFourBytes ();
103 
104  const char * GetFileName () { return (FileName == NULL ? "" : FileName); }
105 };
106 
107 inline bool
109 {
110  switch (Type) {
111  case MFILE_MEMORY:
112  case MFILE_SEQREAD:
113  return (Location >= Capacity);
114  case MFILE_REGULAR:
115  return feof(Handle);
116  case MFILE_GZIP:
117  if (GzBuffer_Avail > 0) { return 0; }
118  return gzeof(GzHandle);
119  default:
120  return false;
121  }
122 }
123 
124 inline errorT
126 {
127  ASSERT (FileMode != FMODE_ReadOnly);
128  if (Type == MFILE_MEMORY) {
129  if (Location >= Capacity) { Extend(); }
130  *CurrentPtr++ = value;
131  Location++;
132  return OK;
133  }
134  if (Type == MFILE_SEQREAD) Seek(Location);
135  Location++;
136  if (Type == MFILE_GZIP) {
137  return (gzputc(GzHandle, value) == EOF) ? ERROR_FileWrite : OK;
138  }
139  return (putc(value, Handle) == EOF) ? ERROR_FileWrite : OK;
140 }
141 
142 inline int
144 {
145  ASSERT (FileMode != FMODE_WriteOnly);
146  if (Type == MFILE_MEMORY || Type == MFILE_SEQREAD) {
147  if (Location >= Capacity) { return EOF; }
148  if (Location == SeqReadLocation && Type == MFILE_SEQREAD) {
149  SeqReadLocation += fread(Data, 1, SEQREADBUFSIZE, Handle);
150  CurrentPtr = Data;
151  }
152  byte value = *CurrentPtr;
153  Location++;
154  CurrentPtr++;
155  return (int) value;
156  }
157  Location++;
158  if (Type == MFILE_GZIP) {
159  if (GzBuffer_Avail <= 0) {
160  return FillGzBuffer();
161  }
162  GzBuffer_Avail--;
163  int retval = *GzBuffer_Current;
164  GzBuffer_Current++;
165  return retval;
166  }
167  return getc(Handle);
168 }
169 
170 #endif // SCID_MFILE_H
171 
unsigned char byte
Definition: common.h:97
uint ReadTwoBytes()
Definition: mfile.cpp:212
Definition: mfile.h:52
uint ReadThreeBytes()
Definition: mfile.cpp:232
const errorT OK
Definition: error.h:23
void * gzFile
Definition: mfile.h:37
const errorT ERROR_FileWrite
Definition: error.h:32
#define ASSERT(f)
Definition: common.h:67
int gzseek(gzFile, int, int)
Definition: mfile.h:43
errorT Flush()
Definition: mfile.cpp:88
names
Definition: tablebase.tcl:260
int gzputc(gzFile, int c)
Definition: mfile.h:39
errorT ReadNBytes(char *str, uint length)
Definition: mfile.cpp:190
errorT WriteTwoBytes(uint value)
Definition: mfile.cpp:204
errorT Seek(uint position)
Definition: mfile.cpp:57
gzFile gzopen(const char *, const char *)
Definition: mfile.h:38
int gzread(gzFile, unsigned char *, int)
Definition: mfile.h:41
uint ReadFourBytes()
Definition: mfile.cpp:256
errorT Open(const char *name, fileModeT fmode)
Definition: mfile.cpp:95
errorT WriteFourBytes(uint value)
Definition: mfile.cpp:245
int gzclose(gzFile)
Definition: mfile.h:44
uint32_t uint
Definition: common.h:99
bool gzable()
Definition: mfile.h:36
int gzeof(gzFile)
Definition: mfile.h:42
unsigned short errorT
Definition: error.h:20
int ReadOneByte()
Definition: mfile.h:143
errorT WriteOneByte(byte value)
Definition: mfile.h:125
int gzgetc(gzFile)
Definition: mfile.h:40
MFile()
Definition: mfile.h:78
const char * GetFileName()
Definition: mfile.h:104
errorT Create(const char *name, fileModeT fmode)
Definition: mfile.cpp:137
mfileT
Definition: mfile.h:48
bool EndOfFile()
Definition: mfile.h:108
fileModeT
Definition: common.h:144
errorT WriteThreeBytes(uint value)
Definition: mfile.cpp:223
errorT WriteNBytes(const char *str, uint length)
Definition: mfile.cpp:155
~MFile()
Definition: mfile.h:79