diff options
Diffstat (limited to 'src/InputFileReader.cpp')
-rw-r--r-- | src/InputFileReader.cpp | 67 |
1 files changed, 34 insertions, 33 deletions
diff --git a/src/InputFileReader.cpp b/src/InputFileReader.cpp index 84f0be4..5e93477 100644 --- a/src/InputFileReader.cpp +++ b/src/InputFileReader.cpp @@ -3,7 +3,7 @@ Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyrigth (C) 2013 + Copyrigth (C) 2018 Matthias P. Braendli, matthias.braendli@mpb.li @@ -47,28 +47,29 @@ int InputFileReader::Open(std::string filename, bool loop) { filename_ = filename; loop_ = loop; - inputfile_ = fopen(filename_.c_str(), "r"); - if (inputfile_ == NULL) { + FILE* fd = fopen(filename_.c_str(), "r"); + if (fd == nullptr) { etiLog.level(error) << "Unable to open input file!"; perror(filename_.c_str()); return -1; } + inputfile_.reset(fd); return IdentifyType(); } int InputFileReader::Rewind() { - rewind(inputfile_); // Also clears the EOF flag + rewind(inputfile_.get()); // Also clears the EOF flag return IdentifyType(); } int InputFileReader::IdentifyType() { - EtiStreamType streamType = ETI_STREAM_TYPE_NONE; + EtiStreamType streamType = EtiStreamType::None; struct stat inputFileStat; - fstat(fileno(inputfile_), &inputFileStat); + fstat(fileno(inputfile_.get()), &inputFileStat); inputfilelength_ = inputFileStat.st_size; uint32_t sync; @@ -77,22 +78,22 @@ int InputFileReader::IdentifyType() char discard_buffer[6144]; - if (fread(&sync, sizeof(sync), 1, inputfile_) != 1) { + if (fread(&sync, sizeof(sync), 1, inputfile_.get()) != 1) { etiLog.level(error) << "Unable to read sync in input file!"; perror(filename_.c_str()); return -1; } if ((sync == 0x49c5f8ff) || (sync == 0xb63a07ff)) { - streamType = ETI_STREAM_TYPE_RAW; + streamType = EtiStreamType::Raw; if (inputfilelength_ > 0) { nbframes_ = inputfilelength_ / 6144; } else { nbframes_ = ~0; } - if (fseek(inputfile_, -sizeof(sync), SEEK_CUR) != 0) { + if (fseek(inputfile_.get(), -sizeof(sync), SEEK_CUR) != 0) { // if the seek fails, consume the rest of the frame - if (fread(discard_buffer, 6144 - sizeof(sync), 1, inputfile_) + if (fread(discard_buffer, 6144 - sizeof(sync), 1, inputfile_.get()) != 1) { etiLog.level(error) << "Unable to read from input file!"; perror(filename_.c_str()); @@ -104,7 +105,7 @@ int InputFileReader::IdentifyType() } nbFrames = sync; - if (fread(&frameSize, sizeof(frameSize), 1, inputfile_) != 1) { + if (fread(&frameSize, sizeof(frameSize), 1, inputfile_.get()) != 1) { etiLog.level(error) << "Unable to read frame size in input file!"; perror(filename_.c_str()); return -1; @@ -114,7 +115,7 @@ int InputFileReader::IdentifyType() sync |= ((uint32_t)frameSize) << 16; if ((sync == 0x49c5f8ff) || (sync == 0xb63a07ff)) { - streamType = ETI_STREAM_TYPE_STREAMED; + streamType = EtiStreamType::Streamed; frameSize = nbFrames & 0xffff; if (inputfilelength_ > 0) { nbframes_ = inputfilelength_ / (frameSize + 2); @@ -122,9 +123,9 @@ int InputFileReader::IdentifyType() else { nbframes_ = ~0; } - if (fseek(inputfile_, -6, SEEK_CUR) != 0) { + if (fseek(inputfile_.get(), -6, SEEK_CUR) != 0) { // if the seek fails, consume the rest of the frame - if (fread(discard_buffer, frameSize - 4, 1, inputfile_) + if (fread(discard_buffer, frameSize - 4, 1, inputfile_.get()) != 1) { etiLog.level(error) << "Unable to read from input file!"; perror(filename_.c_str()); @@ -135,16 +136,16 @@ int InputFileReader::IdentifyType() return 0; } - if (fread(&sync, sizeof(sync), 1, inputfile_) != 1) { + if (fread(&sync, sizeof(sync), 1, inputfile_.get()) != 1) { etiLog.level(error) << "Unable to read nb frame in input file!"; perror(filename_.c_str()); return -1; } if ((sync == 0x49c5f8ff) || (sync == 0xb63a07ff)) { - streamType = ETI_STREAM_TYPE_FRAMED; - if (fseek(inputfile_, -6, SEEK_CUR) != 0) { + streamType = EtiStreamType::Framed; + if (fseek(inputfile_.get(), -6, SEEK_CUR) != 0) { // if the seek fails, consume the rest of the frame - if (fread(discard_buffer, frameSize - 4, 1, inputfile_) + if (fread(discard_buffer, frameSize - 4, 1, inputfile_.get()) != 1) { etiLog.level(error) << "Unable to read from input file!"; perror(filename_.c_str()); @@ -160,21 +161,21 @@ int InputFileReader::IdentifyType() for (size_t i = 10; i < 6144 + 10; ++i) { sync >>= 8; sync &= 0xffffff; - if (fread((uint8_t*)&sync + 3, 1, 1, inputfile_) != 1) { + if (fread((uint8_t*)&sync + 3, 1, 1, inputfile_.get()) != 1) { etiLog.level(error) << "Unable to read from input file!"; perror(filename_.c_str()); return -1; } if ((sync == 0x49c5f8ff) || (sync == 0xb63a07ff)) { - streamType = ETI_STREAM_TYPE_RAW; + streamType = EtiStreamType::Raw; if (inputfilelength_ > 0) { nbframes_ = (inputfilelength_ - i) / 6144; } else { nbframes_ = ~0; } - if (fseek(inputfile_, -sizeof(sync), SEEK_CUR) != 0) { - if (fread(discard_buffer, 6144 - sizeof(sync), 1, inputfile_) + if (fseek(inputfile_.get(), -sizeof(sync), SEEK_CUR) != 0) { + if (fread(discard_buffer, 6144 - sizeof(sync), 1, inputfile_.get()) != 1) { etiLog.level(error) << "Unable to read from input file!"; perror(filename_.c_str()); @@ -190,17 +191,17 @@ int InputFileReader::IdentifyType() return -1; } -void InputFileReader::PrintInfo() +void InputFileReader::PrintInfo() const { fprintf(stderr, "Input file format: "); switch (streamtype_) { - case ETI_STREAM_TYPE_RAW: + case EtiStreamType::Raw: fprintf(stderr, "raw"); break; - case ETI_STREAM_TYPE_STREAMED: + case EtiStreamType::Streamed: fprintf(stderr, "streamed"); break; - case ETI_STREAM_TYPE_FRAMED: + case EtiStreamType::Framed: fprintf(stderr, "framed"); break; default: @@ -221,15 +222,15 @@ int InputFileReader::GetNextFrame(void* buffer) { uint16_t frameSize; - if (streamtype_ == ETI_STREAM_TYPE_RAW) { + if (streamtype_ == EtiStreamType::Raw) { frameSize = 6144; } else { - if (fread(&frameSize, sizeof(frameSize), 1, inputfile_) != 1) { + if (fread(&frameSize, sizeof(frameSize), 1, inputfile_.get()) != 1) { etiLog.level(error) << "Reached end of file."; if (loop_) { if (Rewind() == 0) { - if (fread(&frameSize, sizeof(frameSize), 1, inputfile_) != 1) { + if (fread(&frameSize, sizeof(frameSize), 1, inputfile_.get()) != 1) { PDEBUG("Error after rewinding file!\n"); etiLog.level(error) << "Error after rewinding file!"; return -1; @@ -252,15 +253,15 @@ int InputFileReader::GetNextFrame(void* buffer) } PDEBUG("Frame size: %u\n", frameSize); - size_t read_bytes = fread(buffer, 1, frameSize, inputfile_); + size_t read_bytes = fread(buffer, 1, frameSize, inputfile_.get()); if ( loop_ && - streamtype_ == ETI_STREAM_TYPE_RAW && //implies frameSize == 6144 - read_bytes == 0 && feof(inputfile_)) { + streamtype_ == EtiStreamType::Raw && //implies frameSize == 6144 + read_bytes == 0 && feof(inputfile_.get())) { // in case of an EOF from a RAW that we loop, rewind // otherwise, we won't tolerate it if (Rewind() == 0) { - read_bytes = fread(buffer, 1, frameSize, inputfile_); + read_bytes = fread(buffer, 1, frameSize, inputfile_.get()); } else { PDEBUG("Impossible to rewind file!\n"); |