1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * BlkFile.h
|
---|
4 | * Copyright (C) 2003 UNESCO
|
---|
5 | *
|
---|
6 | * A component of the Greenstone digital library software
|
---|
7 | * from the New Zealand Digital Library Project at the
|
---|
8 | * University of Waikato, New Zealand.
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | *********************************************************************/
|
---|
25 |
|
---|
26 | //////////////////////////////////////////////////////////////////////
|
---|
27 | // BLKFILE.H
|
---|
28 | //
|
---|
29 | // Class for accessing a file per blocks of blksize bytes, blocks
|
---|
30 | // are sequential from 1 to nblock.
|
---|
31 | //
|
---|
32 | //////////////////////////////////////////////////////////////////////
|
---|
33 |
|
---|
34 | #ifndef _BLKFILE_H_
|
---|
35 | #define _BLKFILE_H_
|
---|
36 | #include "CacheMan.h"
|
---|
37 | #include "File.h"
|
---|
38 |
|
---|
39 | //////////////////////////
|
---|
40 | // Block I/O file class
|
---|
41 | //
|
---|
42 |
|
---|
43 |
|
---|
44 |
|
---|
45 | class BlkFile : public CFileBase
|
---|
46 | {
|
---|
47 | public:
|
---|
48 | enum { blk_size = 512 };
|
---|
49 | protected:
|
---|
50 | unsigned blkSize_; // Block Size
|
---|
51 | CacheManager cache_;
|
---|
52 | public:
|
---|
53 | BlkFile(int bs=BlkFile::blk_size);
|
---|
54 | BlkFile(const _TCHAR *fname, int bs=BlkFile::blk_size);
|
---|
55 | virtual FileError Close();
|
---|
56 | virtual ~BlkFile();
|
---|
57 | virtual void ReadBlk(void* d, long b);
|
---|
58 | virtual void WriteBlk(void* d, long b);
|
---|
59 | virtual void ReadBlk(void* d);
|
---|
60 | virtual void WriteBlk(void* d);
|
---|
61 |
|
---|
62 | void SetBlockSize(int blockSize) { blkSize_ = blockSize; }
|
---|
63 | int GetBlockSize() { return blkSize_; }
|
---|
64 | };
|
---|
65 |
|
---|
66 | ////////////////////////////////////////
|
---|
67 |
|
---|
68 | inline BlkFile::BlkFile(int bs) :
|
---|
69 | // Makes a Blocked File object that isn't connected to a file
|
---|
70 | // Block size is bs
|
---|
71 | CFileBase(), blkSize_(bs)
|
---|
72 | {
|
---|
73 | cache_.Create(bs, 40);
|
---|
74 | cache_.Connect(this);
|
---|
75 | // Nothing more to do
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | ////////////////////////////////////////
|
---|
80 |
|
---|
81 | inline BlkFile::BlkFile(const _TCHAR *fname, int bs) :
|
---|
82 | // Makes a Blocked File object connected to a file
|
---|
83 | // Block size is bs
|
---|
84 | CFileBase(), blkSize_(bs), cache_(bs, 20)
|
---|
85 | {
|
---|
86 | CFileBase::Open(fname, FileSystem::FILE_READWRITE);
|
---|
87 | cache_.Create(bs, 40);
|
---|
88 | cache_.Connect(this);
|
---|
89 | // Nothing more to do
|
---|
90 | }
|
---|
91 |
|
---|
92 | ////////////////////////////////////////
|
---|
93 |
|
---|
94 | inline BlkFile::~BlkFile()
|
---|
95 | // Destructor for BlkFile object
|
---|
96 | {
|
---|
97 | cache_.Disconnect();
|
---|
98 | // Nothing more to do
|
---|
99 | }
|
---|
100 |
|
---|
101 | ////////////////////////////////////////////
|
---|
102 |
|
---|
103 |
|
---|
104 | inline FileError BlkFile::Close()
|
---|
105 | {
|
---|
106 | if (IsOpen())
|
---|
107 | cache_.Invalidate();
|
---|
108 | return CFileBase::Close();
|
---|
109 | }
|
---|
110 | ////////////////////////////////////////
|
---|
111 |
|
---|
112 | inline void BlkFile::ReadBlk(void* d, long b)
|
---|
113 | // Reads block number b into d, b is in range [1:NBLOCK]
|
---|
114 | {
|
---|
115 | ASSERT(d!=NULL && b>0);
|
---|
116 | //TRACE("\nBlkFile::ReadBlk -- %s Read Block %ld", file_name, b);
|
---|
117 | //CFileBase::Fetch(d, blkSize_, (b-1)*blkSize_);
|
---|
118 | cache_.Read((b-1)*blkSize_, d);
|
---|
119 | }
|
---|
120 |
|
---|
121 | ////////////////////////////////////////
|
---|
122 |
|
---|
123 | inline void BlkFile::WriteBlk(void* d, long b)
|
---|
124 | // Writes blksize bytes from d at block number b
|
---|
125 | {
|
---|
126 | ASSERT(d!=NULL && b>0);
|
---|
127 | //TRACE("\nBlkFile::WriteBlk -- %s Write Block %ld", file_name, b);
|
---|
128 | //CFileBase::Store(d, blkSize_, (b-1)*blkSize_);
|
---|
129 | cache_.Write((b-1)*blkSize_, d);
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | ////////////////////////////////////////
|
---|
134 |
|
---|
135 | inline void BlkFile::ReadBlk(void* d)
|
---|
136 | // Reads block at current address into d
|
---|
137 | {
|
---|
138 | ASSERT(d!=NULL);
|
---|
139 | //CFileBase::Fetch(d, blkSize_);
|
---|
140 | long addr = CFileBase::FilePosition();
|
---|
141 | cache_.Read(addr, d);
|
---|
142 | }
|
---|
143 |
|
---|
144 | ////////////////////////////////////////
|
---|
145 |
|
---|
146 | inline void BlkFile::WriteBlk(void* d)
|
---|
147 | // Writes blkSize_ bytes from d at current address
|
---|
148 | {
|
---|
149 | ASSERT(d!=NULL);
|
---|
150 | //CFileBase::Store(d, blkSize_);
|
---|
151 | long addr = CFileBase::FilePosition();
|
---|
152 | cache_.Write(addr, d);
|
---|
153 | }
|
---|
154 |
|
---|
155 | #endif
|
---|