source: pacpusframework/branches/2.0-beta1/include/Pacpus/kernel/DbiteFile.h@ 139

Last change on this file since 139 was 89, checked in by morasjul, 12 years ago

PACPUS 2.0 Beta deployed in new branch

Major changes:
-Add communication interface between components
-Add examples for communications interface (TestComponents)
-Move to Qt5 support

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