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