7 #ifndef H_POTTU_TDRFILE
8 #define H_POTTU_TDRFILE
18 #include <fmt/format.h>
39 TDRFile(
const std::string &path ) : _path(path) {}
54 FILE *ftmp = fopen( _path.c_str(),
"rb" );
56 std::string errmsg = fmt::format(
"TDRFile::open(): Couldn't open \'{}\'. Reason: {}",
57 _path, std::strerror(errno) );
58 throw std::runtime_error( errmsg );
60 off_t p = fseek( ftmp, 0, SEEK_END );
62 std::string errmsg( std::strerror(errno) );
64 throw std::runtime_error( errmsg );
66 _filesize = ftell( ftmp );
69 _f.open( _path,
"rb" );
71 _findWordOrderingAndBlocksize();
109 throw std::runtime_error(
"Cannot read. File is not open." );
112 int ret = _f.
read( (
char *)buf, _blocksize );
113 if( ret == 0 && _f.isEof() )
115 else if( ret ==
int(_blocksize) ) {
118 return _lastheader.dataLen;
120 throw std::runtime_error(
"Immature end of file or failure in reading" );
123 const GZFile &getGZFile() const noexcept {
return _f; }
125 bool isOpen() const noexcept {
return _f.
isOpen(); }
126 bool isEof() const noexcept {
return _f.isEof(); }
128 bool isZipped() const noexcept {
return _gzipped; }
129 bool isWordSwapRequired() const noexcept {
return _wordSwapRequired; }
130 uint32_t getBlocksize() const noexcept {
return _blocksize; }
131 int64_t getFilesize() const noexcept {
return _filesize; }
133 const std::string &getPath() const noexcept {
return _path; }
135 const ebyedataheader_t &getLastHeader() const noexcept {
return _lastheader; }
140 void _findWordOrderingAndBlocksize() {
143 int readCount = _f.
read( buf, 24 );
144 if( readCount != 24 )
145 throw std::runtime_error(
"Failed to read the header." );
147 if( !header0.isValid() ) {
148 std::cerr << header0 <<
"\n";
149 throw std::runtime_error(
"Header is not valid" );
152 std::vector<dataitem_t> tmpdata;
153 tmpdata.resize( header0.dataLen / 8 );
154 int bytesToRead = (header0.dataLen / 8) * 8;
155 readCount = _f.
read( (
char *)tmpdata.data(), bytesToRead );
156 if( readCount != bytesToRead )
157 throw std::runtime_error(
"Failed to read the first block of data." );
162 unsigned noswap_rectypes[4] = {0};
163 unsigned swap_rectypes[4] = {0};
164 for(
const auto &item : tmpdata ) {
165 ++noswap_rectypes[
static_cast<uint32_t
>( item.getType() ) ];
166 ++swap_rectypes[
static_cast<uint32_t
>( item.asSwapped().getType() ) ];
174 _wordSwapRequired = swap_rectypes[2] + swap_rectypes[3] > noswap_rectypes[2] + noswap_rectypes[3];
176 uint32_t testblocksize = 1024;
177 while( testblocksize < (1<<20) ) {
179 if( testblocksize < header0.dataLen )
183 _f.
seek( testblocksize );
185 readCount = _f.
read( buf, 24 );
186 if( readCount != 24 )
187 throw std::runtime_error(
"Failed to read the header while guessing the blocksize." );
194 if( testblocksize >= (1<<20) )
195 throw std::runtime_error(
"Failed to find the blocksize." );
197 _blocksize = testblocksize;
202 _blockbuf.resize( _blocksize );
207 uint32_t _blocksize{0};
208 int64_t _filesize{-1};
210 bool _wordSwapRequired{
false};
212 uint32_t _currentblock{0};
214 ebyedataheader_t _lastheader;
215 std::vector<uint8_t> _blockbuf;
Definition: GZFile.hpp:21
int read(char *buf, unsigned int size)
Reads data from the file to a buffer.
Definition: GZFile.hpp:87
bool isOpen() const noexcept
Checks if file is open.
Definition: GZFile.hpp:112
void seek(size_t position)
Seeks the file position.
Definition: GZFile.hpp:102
bool isZipped() const noexcept
Tells whether the file is zipped.
Definition: GZFile.hpp:118
Readonly access to a tdr file with some helpers.
Definition: TDRFile.hpp:31
void close()
Closes the file.
Definition: TDRFile.hpp:81
void open()
Opens the file and sets file information.
Definition: TDRFile.hpp:49
size_t readBlock(uint8_t *buf)
Reads next block.
Definition: TDRFile.hpp:91
TDRFile(const std::string &path)
Sets the filename.
Definition: TDRFile.hpp:39
Defines the raw low level dataitems used in tdr datastreams and files.
Definition: mainpage.dox:6