diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2013-12-17 17:41:53 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2013-12-24 18:09:26 +0100 |
commit | abec4838d2536812a4fe382334a1c9f500f7728b (patch) | |
tree | 42489c17d6fd8b8f5a1837a23ef18afa7256cded /src/inputs/InputFile.cpp | |
parent | bf16c5a22de3d2bab7db290e8cd8b031df7b7eb8 (diff) | |
download | dabmux-abec4838d2536812a4fe382334a1c9f500f7728b.tar.gz dabmux-abec4838d2536812a4fe382334a1c9f500f7728b.tar.bz2 dabmux-abec4838d2536812a4fe382334a1c9f500f7728b.zip |
updates in new file input
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); |