source: pacpusframework/branches/0.0.x/include/Pacpus/kernel/DbiteFile.h

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

Minor: added pacpus namespace in Win32ShMem.
Ignored dll-interface warnings in MSVC (unimportant because concerns STL): C4251, C4275.

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