1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * File.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 | // File.h -- File Base class
|
---|
28 | //
|
---|
29 | //////////////////////////////////////////////////////////////////////////
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 | #ifndef __FILE_BASE__
|
---|
34 | #define __FILE_BASE__
|
---|
35 |
|
---|
36 | #include "FileSystem.h"
|
---|
37 | # define __CPP_EXCEPTIONS__ 1
|
---|
38 |
|
---|
39 | // CRC-32 checksum used to detect bit errors
|
---|
40 | typedef unsigned long Checksum;
|
---|
41 |
|
---|
42 |
|
---|
43 | // Constants for file operations and stream position
|
---|
44 | const int MaxNameLength = 512; // Max length of names buffers
|
---|
45 | const FileSystem::FAU_t StartOfFile = (FileSystem::FAU_t)0; // First byte in the file
|
---|
46 | const FileSystem::FAU_t CURRADDR = (FileSystem::FAU_t)-1;// Indicates current location
|
---|
47 | const isis::uint32_t CheckWord = 0xfefefefe; // Default synchronization word
|
---|
48 | const isis::int32_t FSListCorrupt = (FileSystem::FAU_t)-1;// Free space list is corrupt
|
---|
49 |
|
---|
50 |
|
---|
51 | //------------------
|
---|
52 | // File error codes
|
---|
53 | //------------------
|
---|
54 | enum FileError {
|
---|
55 | FILE_NO_ERROR = 0, // No errors reported
|
---|
56 | FILE_CHECKSUM_ERROR, // Checksum Error
|
---|
57 | FILE_CLOSE_ERROR, // Error closing file
|
---|
58 | FILE_CORRUPT, // File corrupted
|
---|
59 | FILE_CREATION_ERROR, // Error creating file
|
---|
60 | FILE_EXISTS, // File already exists
|
---|
61 | FILE_NOT_OPEN_ERROR, // Trying to use a closed file
|
---|
62 | FILE_NOT_READY, // File not ready (failed or closed file)
|
---|
63 | FILE_NOT_WRITEABLE, // Could not write to file
|
---|
64 | FILE_OPEN_ERROR, // Error opening file
|
---|
65 | FILE_POSITION_ERROR, // Cannot obtain the current file position
|
---|
66 | FILE_READ_ERROR, // Error reading file
|
---|
67 | FILE_SEEK_ERROR, // Error seeking in file
|
---|
68 | FILE_WRITE_ERROR, // Error writing to file
|
---|
69 |
|
---|
70 | FMGR_NO_DATABASE_OPEN, // No database open
|
---|
71 | FMGR_NO_FILE_EXISTS, // No such file exists
|
---|
72 | FMGR_NO_OBJECTS_EXIST, // No objects exist
|
---|
73 | FMGR_NULL_PTR, // Accessing a null pointer
|
---|
74 | FMGR_OBJECT_EXISTS, // Object already exists
|
---|
75 | FMGR_OPEN_FILE_REFERENCE, // Another object is referencing this file
|
---|
76 | FMGR_OVERFLOW, // Math overflow
|
---|
77 | FMGR_PARSE_ERROR, // Parse error
|
---|
78 | FMGR_PATH_ERROR, // Invalid path
|
---|
79 | FMGR_READ_ONLY_FILE, // Trying to write to read-only file
|
---|
80 | FMGR_STACK_EMPTY, // Stack empty
|
---|
81 | FMGR_STACK_FULL, // Stack full
|
---|
82 | FMGR_SYNC_ERROR, // Synchronization Error
|
---|
83 | FMGR_UNDERFLOW, // Math under-flow
|
---|
84 | FMGR_WRONG_FILE_TYPE, // Wrong file type
|
---|
85 |
|
---|
86 | // Persistent lock error codes
|
---|
87 | FMGR_INVALID_LOCK_TYPE, // Invalid lock type specified
|
---|
88 | FMGR_FILELOCK_ACCESS_ERROR, // The file lock cannot be accessed
|
---|
89 | FMGR_FILELOCK_ERROR, // Error locking the file
|
---|
90 | FMGR_RECORDLOCK_ACCESS_ERROR, // The record lock cannot be accessed
|
---|
91 | FMGR_RECORDLOCK_ERROR // Error locking a record};
|
---|
92 |
|
---|
93 |
|
---|
94 | };
|
---|
95 |
|
---|
96 |
|
---|
97 |
|
---|
98 |
|
---|
99 | class DLL_CODE_API CFileBase
|
---|
100 | {
|
---|
101 | protected:
|
---|
102 | // File I/O operation codes
|
---|
103 | enum IO_Op {
|
---|
104 | IO_READ, // A read was performed
|
---|
105 | IO_WRITE, // A write operation was performed
|
---|
106 | IO_REWIND, // A rewind operation was performed
|
---|
107 | IO_NO_OPERATION, // No operation was performed
|
---|
108 | IO_SEEK // A seek operation was preformed
|
---|
109 | };
|
---|
110 |
|
---|
111 | _TCHAR file_name[MaxNameLength]; // Open file name
|
---|
112 | FileSystem::FPTR* fp; // Stream file handle
|
---|
113 | IO_Op last_operation; // Last I/O operation preformed
|
---|
114 | FileError file_error; // Last reported file error
|
---|
115 |
|
---|
116 | // File status members
|
---|
117 | int is_ok; // Used to signal a fatal error condition
|
---|
118 | int is_open; // True if the file is open
|
---|
119 | int ready_for_reading; // True if the file is ready for reading
|
---|
120 | int ready_for_writing; // True if the file is ready for writing
|
---|
121 |
|
---|
122 |
|
---|
123 | public:
|
---|
124 | CFileBase();
|
---|
125 | virtual ~CFileBase();
|
---|
126 |
|
---|
127 | public: // File functions
|
---|
128 | virtual FileError Create(const TCHAR *fname);
|
---|
129 | virtual FileError Open(const TCHAR *fname, FileSystem::AccessMode mode = FileSystem::FILE_READWRITE);
|
---|
130 | virtual FileError Close();
|
---|
131 |
|
---|
132 | FileError Seek(FileSystem::FAU_t offset, FileSystem::SeekMode mode = FileSystem::FILE_SEEK_BEG);
|
---|
133 |
|
---|
134 | FileError Fetch(void* buf, isis::uint32_t nBytes, FileSystem::FAU_t file_addres = CURRADDR);
|
---|
135 | FileError Store(const void* buf, isis::uint32_t nBytes, FileSystem::FAU_t file_address = CURRADDR,
|
---|
136 | int flush_flag = 1, int bit_test = 1);
|
---|
137 |
|
---|
138 | FileSystem::FAU_t SeekTo(FileSystem::FAU_t file_address);
|
---|
139 | FileSystem::StreamPos FilePosition();
|
---|
140 |
|
---|
141 | FileError Flush();
|
---|
142 |
|
---|
143 | _TCHAR *FileName() { return file_name; }
|
---|
144 |
|
---|
145 | public: // 32-bit CRC checksum routines
|
---|
146 | isis::uint32_t CalcChecksum(isis::uint32_t bytes, FileSystem::FAU_t file_address, int mem_alloc = 1);
|
---|
147 |
|
---|
148 | public: // File Status functions
|
---|
149 | int IsOK() { return ((is_ok == 1) && (is_open == 1)); }
|
---|
150 | int IsOK() const { return ((is_ok == 1) && (is_open == 1)); }
|
---|
151 | int IsOpen() { return ((is_ok == 1) && (is_open == 1)); }
|
---|
152 | int IsOpen() const { return ((is_ok == 1) && (is_open == 1)); }
|
---|
153 | int ReadyForReading() { return ready_for_reading == 1; }
|
---|
154 | int ReadyForReading() const { return ready_for_reading == 1; }
|
---|
155 | int ReadyForWriting() { return ready_for_writing == 1; }
|
---|
156 | int ReadyForWriting() const { return ready_for_writing == 1; }
|
---|
157 |
|
---|
158 | public: // Exception handling functions
|
---|
159 | FileError GetFileError() { return file_error; }
|
---|
160 | FileError GetFileError() const { return file_error; }
|
---|
161 | void SetFileError(FileError err) { file_error = err; }
|
---|
162 | void ResetFileError() { file_error = FILE_NO_ERROR; }
|
---|
163 | const TCHAR *FileExceptionMessage();
|
---|
164 |
|
---|
165 | public: // General purpose file utilities
|
---|
166 | static int Exists(const TCHAR *fname);
|
---|
167 | static FileSystem::FAU_t FileSize(const TCHAR *fname);
|
---|
168 | static int CanOpenForWriting(const TCHAR *fname);
|
---|
169 | static int CanOpenReadOnly(const TCHAR *fname);
|
---|
170 |
|
---|
171 | };
|
---|
172 |
|
---|
173 |
|
---|
174 | #ifdef __CPP_EXCEPTIONS__
|
---|
175 | // Class declarations for exceptions representing program errors.
|
---|
176 | // This implementation is provided for use C++'s built-in exception
|
---|
177 | // handling routines.
|
---|
178 |
|
---|
179 | class CFileBaseException
|
---|
180 | {
|
---|
181 | int error_;
|
---|
182 | public:
|
---|
183 | CFileBaseException(int error) { error_ = error; }
|
---|
184 | };
|
---|
185 |
|
---|
186 | #endif // __CPP_EXCEPTIONS__
|
---|
187 |
|
---|
188 | #endif // __FILE_BASE__
|
---|