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

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

Added: some documentation and doc todo comments.

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