From abec4838d2536812a4fe382334a1c9f500f7728b Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 17 Dec 2013 17:41:53 +0100 Subject: updates in new file input --- src/inputs/InputFile.cpp | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'src/inputs/InputFile.cpp') diff --git a/src/inputs/InputFile.cpp b/src/inputs/InputFile.cpp index 3701584..58c3ada 100644 --- a/src/inputs/InputFile.cpp +++ b/src/inputs/InputFile.cpp @@ -25,7 +25,7 @@ along with CRC-DabMux. If not, see . */ -#include "inputs/InputFile.h" +#include "inputs/Input.h" #include #include @@ -50,6 +50,44 @@ int InputFile::Open() return 0; } +/** + * This function replace the read function by trying many times a reading. + * It tries to read until all bytes are read. Very useful when reading from a + * pipe because sometimes 2 pass is necessary to read all bytes. + * @param file File descriptor. + * @param data Address of the buffer to write data into. + * @param size Number of bytes to read. + * @param tries Max number of tries to read. + * @return Same as read function: + * Nb of bytes read. + * -1 if error. + */ +long InputFile::ReadData(void* data, size_t size, unsigned int tries) +{ + size_t result; + size_t offset = 0; + if (size == 0) return 0; + if (tries == 0) return 0; + result = read(file, data, size); + if (result == -1) { + if (errno == EAGAIN) { + return ReadData(data, size, tries - 1); + } + return -1; + } + offset = result; + size -= offset; + data = (char*)data + offset; + result = ReadData(data, size, tries - 1); + if (result == -1) { + return -1; + } + offset += result; + return offset; +} + + + int InputFile::Rewind() { return lseek(this->file, 0, SEEK_SET); -- cgit v1.2.3