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

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