source: pacpusframework/trunk/include/Pacpus/kernel/DbiteFile.h@ 59

Last change on this file since 59 was 59, checked in by Marek Kurdej, 12 years ago

Major: FileLib is now a shared library.
Added: FileLibConfig.h to handle dllimport/dllexport on Windows.
Note: MSVC warning C4251 is a normal thing when using an STL as a member field in a DLL-exported class.

File size: 8.6 KB
Line 
1/**
2 *
3 * Distributed under the UTC Heudiascy Pacpus License, Version 1.0.
4 * Copyright (c) UTC Heudiasyc 2010 - 2013. All rights reserved.
5 *
6 * See the LICENSE file for more information or a copy at:
7 * http://www.hds.utc.fr/~kurdejma/LICENSE_1_0.txt
8 *
9 */
10
11#ifndef DEF_PACPUS_DBITEFILE_H
12#define DEF_PACPUS_DBITEFILE_H
13
14#include <Pacpus/kernel/cstdint.h>
15#include <Pacpus/kernel/FileLibConfig.h>
16#include <Pacpus/kernel/hdfile_header_t.h>
17#include <Pacpus/kernel/road_time.h>
18
19#include <fstream>
20#include <iosfwd>
21#include <string>
22
23namespace pacpus {
24
25struct FILELIB_API VariableDataSizeTag {};
26FILELIB_API extern VariableDataSizeTag VariableDataSize;
27
28struct FILELIB_API ReadModeTag {};
29FILELIB_API extern ReadModeTag ReadMode;
30
31struct FILELIB_API WriteModeTag {};
32FILELIB_API extern WriteModeTag WriteMode;
33
34struct FILELIB_API DiagnoseModeTag {};
35FILELIB_API extern DiagnoseModeTag DiagnoseMode;
36
37class FILELIB_API DbiteFile
38{
39public:
40 /// @todo Documentation
41 enum ReadDirection
42 {
43 ReadForward
44 , ReadBackward
45 };
46
47 /// @throws nothing
48 DbiteFile();
49 /// @throws nothing
50 ~DbiteFile();
51
52 /// Opens the file in read mode.
53 ///
54 /// @throws DbiteException on I/O error (read: cannot open the file)
55 /// @throws DbiteException on invalid file signature
56 void open(const std::string & path, ReadModeTag);
57
58 /// Opens the file in write mode using default settings.
59 /// It is necessary to set data type and data size afterwards.
60 ///
61 /// @throws DbiteException on I/O error (write: cannot write the header)
62 /// @see setRecordSize
63 /// @see setType
64 void open(const std::string & path, WriteModeTag);
65
66 /// Opens the file in write mode using given data type and data size.
67 ///
68 /// @code
69 /// DbiteFile dbt;
70 /// dbt.open(path, WriteMode, type, dataSize);
71 /// @endcode
72 /// is equivalent to
73 /// @code
74 /// DbiteFile dbt;
75 /// dbt.open(path, WriteMode)
76 /// dbt.setRecordSize(dataSize);
77 /// dbt.setType(type);
78 /// @endcode
79 ///
80 /// @throws DbiteException on I/O error (write: cannot write the header)
81 void open(const std::string & path, WriteModeTag, hdfile_header_t::DataTypeT type, hdfile_header_t::DataSizeT dataSize);
82
83 /// Opens the file in write mode using given data type and variable data size.
84 ///
85 /// @code
86 /// DbiteFile dbt;
87 /// dbt.open(path, WriteMode, type, VariableDataSize);
88 /// @endcode
89 /// is equivalent to
90 /// @code
91 /// DbiteFile dbt;
92 /// dbt.open(path, WriteMode)
93 /// dbt.setRecordSize(VariableDataSize);
94 /// dbt.setType(type);
95 /// @endcode
96 ///
97 /// @throws DbiteException on I/O error (write: cannot write the header)
98 void open(const std::string & path, WriteModeTag, hdfile_header_t::DataTypeT type, VariableDataSizeTag);
99
100 /// Tries to diagnose and repair the file.
101 /// @throws DbiteException on I/O error (write: cannot write the header)
102 //void open(const std::string & path, DiagnoseModeTag); // TODO
103
104 /// @throws nothing
105 void close();
106 /// @throws nothing
107 bool isOpen() const;
108
109 /// @throws DbiteException when file is not open
110 void goToFileBegin();
111 /// @throws DbiteException when file is not open
112 void goToFileEnd();
113 /// @throws DbiteException when file is not open
114 void goToDataBegin();
115
116 /// @throws nothing
117 const std::string & getPath() const;
118 /// @throws nothing
119 std::string getSignature() const;
120 /// @throws nothing
121 hdfile_header_t::DataTypeT getType() const;
122 /// @throws nothing
123 hdfile_header_t::VersionT getVersion() const;
124 /// @throws nothing
125 hdfile_header_t::DataOffsetT getDataOffset() const;
126 /// @throws nothing
127 hdfile_header_t::DataSizeT getRecordSize() const;
128 // FIXME: file size should be 64-bit long, have to change the hdfile_header_t structure as well
129 /// @throws nothing
130 hdfile_header_t::FileSizeT getFileSize() const;
131 int64_t getRealFileSize();
132 /// @throws nothing
133 road_time_t getTimeMin() const;
134 /// @throws nothing
135 road_time_t getTimeMax() const;
136 /// @throws nothing
137 hdfile_header_t::RecordCountT getRecordCount() const;
138
139 /// @throws nothing
140 void setType(hdfile_header_t::DataTypeT type);
141 /// @throws nothing
142 void setRecordSize(hdfile_header_t::DataSizeT recordSize);
143 /// @throws nothing
144 void setRecordSize(VariableDataSizeTag tag);
145 /// @throws nothing
146 void setTimeMin(road_time_t time);
147 /// @throws nothing
148 void setTimeMax(road_time_t time);
149 /// @throws nothing
150 void setRecordCount(hdfile_header_t::RecordCountT recourdCount);
151
152 /// prints header
153 /// @throws nothing
154 operator std::string();
155
156 /// @throws nothing
157 bool isVariableDataSize() const;
158
159 /// @throws DbiteException on I/O error
160 bool readRecord(size_t recordIndex, road_time_t & time, road_timerange_t & timeRange, char * data);
161 /// @throws DbiteException on I/O error
162 bool readRecord(size_t recordIndex, road_time_t & time, road_timerange_t & timeRange, char * data, size_t & dataSize);
163 /// @throws DbiteException on I/O error
164 bool readRecord(road_time_t & time, road_timerange_t & timeRange, char * data, const ReadDirection & direction = ReadForward);
165 /// @throws DbiteException on I/O error
166 bool readRecord(road_time_t & time, road_timerange_t & timeRange, char * data, size_t & dataSize, const ReadDirection & direction = ReadForward);
167 /// Reads only time and time range and moves the file position to the beginning of the next record
168 /// @throws DbiteException on I/O error
169 bool readRecord(road_time_t & time, road_timerange_t & timeRange, const ReadDirection & direction = ReadForward);
170 /// @throws DbiteException on I/O error
171 bool writeRecord(const road_time_t & time, const road_timerange_t & timeRange, const char * data, const size_t dataSize);
172 /// @throws DbiteException on I/O error
173 template <typename T>
174 bool writeRecord(const road_time_t & time, const road_timerange_t & timeRange, const T* data);
175
176 /// @throws nothing
177 bool isEndOfFile();
178
179private:
180 /// @throws nothing
181 void setPath(const std::string & path);
182 /// @throws nothing
183 void setVersion(hdfile_header_t::VersionT version);
184 /// @throws nothing
185 void setDataOffset(hdfile_header_t::DataOffsetT dataOffset);
186 /// @throws nothing
187 void setFileSize(hdfile_header_t::FileSizeT fileSize);
188
189 void initializeHeader();
190
191 bool isSignatureCorrect() const;
192 void setSignature();
193
194 const hdfile_header_t & getHeader() const;
195 hdfile_header_t & header();
196 void writeHeader();
197
198 void openWriteMode(const std::string & path);
199 void openWriteMode(const std::string & path, hdfile_header_t::DataTypeT type);
200 void closeDbite();
201 void closeDbite(ReadModeTag);
202 void closeDbite(WriteModeTag);
203 void readHeader();
204 void verifyHeader();
205
206 /// @throws DbiteException when file is not open
207 int64_t getReadPosition();
208 /// @throws DbiteException when file is not open
209 int64_t getWritePosition();
210 /// @throws DbiteException when file is not open
211 void setReadPosition(int64_t offset, std::ios_base::seekdir direction);
212 /// @throws DbiteException when file is not open
213 void setReadPosition(int64_t position);
214 /// @throws DbiteException when file is not open
215 void setWritePosition(int64_t offset, std::ios_base::seekdir direction);
216 /// @throws DbiteException when file is not open
217 void setWritePosition(int64_t position);
218
219 /// @throws DbiteException on I/O error
220 bool readRecordNoBufferCheck(road_time_t & time, road_timerange_t & timeRange, char * data, size_t & dataSize, const ReadDirection & direction);
221 /// @throws DbiteException on I/O error
222 bool readRecordForward(road_time_t & time, road_timerange_t & timeRange, char * data, size_t & dataSize);
223 /// @throws DbiteException on I/O error
224 bool readRecordBackward(road_time_t & time, road_timerange_t & timeRange, char * data, size_t & dataSize);
225
226 /// @throws DbiteException on I/O error
227 void read(std::fstream::char_type * s, std::streamsize n);
228 /// @throws DbiteException on I/O error
229 void write(const std::fstream::char_type * s, std::streamsize n);
230
231 /// @throws DbiteException when file is not open
232 void checkFileOpen();
233
234private:
235 std::fstream mFile;
236 hdfile_header_t mHeader;
237 bool mIsWriting;
238 std::string mPath;
239
240 bool mIsVariableRecordSize;
241};
242
243template <typename T>
244bool DbiteFile::writeRecord(const road_time_t & time, const road_timerange_t & timeRange, const T* data)
245{
246 return writeRecord(time, timeRange, (const char*) data, sizeof(T));
247}
248
249} // namespace pacpus
250
251#endif // DEF_PACPUS_DBITEFILE_H
Note: See TracBrowser for help on using the repository browser.