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

Last change on this file since 45 was 31, checked in by sgosseli, 12 years ago

Huge commit: use the new includes style in all the files, add the license header in all the headers, and in some cpp.

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