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

Last change on this file since 320 was 317, checked in by Marek Kurdej, 10 years ago

TestFileLib: minor fixes. Tests pass.

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