aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2013-12-17 17:41:53 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2013-12-24 18:09:26 +0100
commitabec4838d2536812a4fe382334a1c9f500f7728b (patch)
tree42489c17d6fd8b8f5a1837a23ef18afa7256cded
parentbf16c5a22de3d2bab7db290e8cd8b031df7b7eb8 (diff)
downloaddabmux-abec4838d2536812a4fe382334a1c9f500f7728b.tar.gz
dabmux-abec4838d2536812a4fe382334a1c9f500f7728b.tar.bz2
dabmux-abec4838d2536812a4fe382334a1c9f500f7728b.zip
updates in new file input
-rw-r--r--src/inputs/Input.h5
-rw-r--r--src/inputs/InputFile.cpp40
-rw-r--r--src/inputs/InputTest.cpp2
3 files changed, 43 insertions, 4 deletions
diff --git a/src/inputs/Input.h b/src/inputs/Input.h
index 943b498..42c2ef7 100644
--- a/src/inputs/Input.h
+++ b/src/inputs/Input.h
@@ -51,7 +51,6 @@ class InputBase {
virtual int Close();
virtual int Clean(); // TODO destructor
- virtual int Rewind();
};
/********************************************/
@@ -69,7 +68,6 @@ class InputTest : InputBase {
int SetBitrate(int bitrate);
int Close();
int Clean();
- int Rewind();
private:
unsigned long counter;
@@ -91,6 +89,7 @@ class InputFile : InputBase {
int Close();
int Rewind();
protected:
+ long ReadData(void* data, size_t size, unsigned int tries);
std::string filename;
int file; // the file descriptor
};
@@ -119,3 +118,5 @@ class InputDabplusFile : InputFile {
#endif
+#endif
+
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);
diff --git a/src/inputs/InputTest.cpp b/src/inputs/InputTest.cpp
index becd8c7..4d2881e 100644
--- a/src/inputs/InputTest.cpp
+++ b/src/inputs/InputTest.cpp
@@ -26,7 +26,7 @@
along with CRC-DabMux. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "inputs/InputTest.h"
+#include "inputs/Input.h"
#include <string.h>
#ifdef _WIN32