diff options
Diffstat (limited to 'src/inputs/InputFile.cpp')
| -rw-r--r-- | src/inputs/InputFile.cpp | 40 | 
1 files changed, 39 insertions, 1 deletions
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 <http://www.gnu.org/licenses/>.     */ -#include "inputs/InputFile.h" +#include "inputs/Input.h"  #include <string>  #include <cstdlib> @@ -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);  | 
