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

Last change on this file since 64 was 64, checked in by Marek Kurdej, 11 years ago

Modified property: added svn:keywords=Id.

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