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