Menu

[13a134]: / src / gfile.h  Maximize  Restore  History

Download this file

85 lines (67 with data), 2.2 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Copyright (c) 1999 Shane Hudson
* Copyright (C) 2014-2016 Fulvio Benini
* This file is part of Scid (Shane's Chess Information Database).
*
* Scid is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* Scid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scid. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SCID_GFILE_H
#define SCID_GFILE_H
#include "common.h"
class MFile;
// The GFile max block size is 128 kilobytes because IndexEntry::Length is a 17 bits uint
#define GF_BLOCKSIZE 131072
const char GFILE_SUFFIX [] = ".sg4";
class GFile
{
private:
fileNameT FileName;
MFile * Handle;
fileModeT FileMode;
uint Offset;
int NumBlocks;
int LastBlockSize;
struct gfBlockT
{
int blockNum;
bool dirty;
uint length;
byte data [GF_BLOCKSIZE];
} CurrentBlock;
public:
GFile() { Init(); }
~GFile();
errorT Create (const char * filename);
errorT Open (const char * filename, fileModeT fmode);
errorT Close ();
errorT flush();
errorT addGame(const byte* src, size_t length, uint& resOffset);
const byte* getGame(uint offset, uint length) {
int blockNum = (offset / GF_BLOCKSIZE);
int endBlockNum = (offset + length - 1) / GF_BLOCKSIZE;
if (endBlockNum != blockNum || blockNum >= NumBlocks) return 0;
if (CurrentBlock.blockNum != blockNum) {
if (Fetch (blockNum) != OK) return 0;
}
return CurrentBlock.data + (offset % GF_BLOCKSIZE);
}
private:
GFile(const GFile&);
GFile& operator=(const GFile&);
void Init ();
errorT Fetch(int blockNum);
};
#endif // SCID_GFLE_H
//////////////////////////////////////////////////////////////////////
// EOF: gfile.h
//////////////////////////////////////////////////////////////////////