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