configBitstreamParser: fix CRLF vs LF: use fread with FILE (or stdin) instead of c++ stream

This commit is contained in:
Gwenhael Goavec-Merou 2021-05-26 17:56:21 +02:00
parent b28ae236a1
commit 0cae46b367
1 changed files with 9 additions and 10 deletions

View File

@ -17,21 +17,21 @@ ConfigBitstreamParser::ConfigBitstreamParser(const string &filename, int mode,
_bit_data(), _raw_data(), _hdr() _bit_data(), _raw_data(), _hdr()
{ {
if (!filename.empty()) { if (!filename.empty()) {
ifstream _fd(filename, ifstream::in | (ios_base::openmode)mode); FILE *_fd = fopen(filename.c_str(), "rb");
if (!_fd.is_open()) if (!_fd)
throw std::runtime_error("Error: fail to open " + _filename); throw std::runtime_error("Error: fail to open " + _filename);
_fd.seekg(0, _fd.end); fseek(_fd, 0, SEEK_END);
_file_size = _fd.tellg(); _file_size = ftell(_fd);
_fd.seekg(0, _fd.beg); fseek(_fd, 0, SEEK_SET);
_raw_data.resize(_file_size); _raw_data.resize(_file_size);
_bit_data.reserve(_file_size); _bit_data.reserve(_file_size);
_fd.read((char *)&_raw_data[0], sizeof(char) * _file_size); int ret = fread((char *)&_raw_data[0], sizeof(char), _file_size, _fd);
if (_fd.gcount() != _file_size) if (ret != _file_size)
throw std::runtime_error("Error: fail to read " + _filename); throw std::runtime_error("Error: fail to read " + _filename);
_fd.close(); fclose(_fd);
} else if (!isatty(fileno(stdin))) { } else if (!isatty(fileno(stdin))) {
_file_size = 0; _file_size = 0;
string tmp; string tmp;
@ -39,8 +39,7 @@ ConfigBitstreamParser::ConfigBitstreamParser(const string &filename, int mode,
size_t size; size_t size;
do { do {
cin.read((char *)&tmp[0], 4096); size = fread((char *)&tmp[0], sizeof(char), 4096, stdin);
size = cin.gcount();
_raw_data.append(tmp, 0, size); _raw_data.append(tmp, 0, size);
_file_size += size; _file_size += size;
} while (size > 0); } while (size > 0);