aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2019-08-21 15:34:35 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2019-08-21 15:34:35 +0200
commit1ce18aa61b14ae5201d70cdf07be62183e18de67 (patch)
tree878c5c87503c0ae3731a9596f638a80dd5be00a0
parentbfb88439e160ed2e939c64455d430a28e9903d59 (diff)
downloaddabmux-1ce18aa61b14ae5201d70cdf07be62183e18de67.tar.gz
dabmux-1ce18aa61b14ae5201d70cdf07be62183e18de67.tar.bz2
dabmux-1ce18aa61b14ae5201d70cdf07be62183e18de67.zip
Simplify nonblocking File input
-rw-r--r--src/input/File.cpp37
1 files changed, 20 insertions, 17 deletions
diff --git a/src/input/File.cpp b/src/input/File.cpp
index 5d25f49..20036ae 100644
--- a/src/input/File.cpp
+++ b/src/input/File.cpp
@@ -2,7 +2,7 @@
Copyright (C) 2009 Her Majesty the Queen in Right of Canada (Communications
Research Center Canada)
- Copyright (C) 2018 Matthias P. Braendli
+ Copyright (C) 2019 Matthias P. Braendli
http://www.opendigitalradio.org
*/
@@ -105,12 +105,13 @@ int FileBase::rewind()
ssize_t FileBase::readFromFile(uint8_t* buffer, size_t size)
{
+ using namespace std;
+
ssize_t ret = 0;
if (m_nonblock) {
if (size > m_nonblock_buffer.size()) {
- size_t m_nonblock_buffer_len = m_nonblock_buffer.size();
- size_t required_len = size - m_nonblock_buffer_len;
- std::vector<uint8_t> buf(required_len);
+ const size_t required_len = size - m_nonblock_buffer.size();
+ vector<uint8_t> buf(required_len);
ret = read(m_fd, buf.data(), required_len);
/* If no process has the pipe open for writing, read() shall return 0
@@ -128,23 +129,25 @@ ssize_t FileBase::readFromFile(uint8_t* buffer, size_t size)
etiLog.level(alert) << "ERROR: Can't read file " << strerror(errno);
return -1;
}
-
- std::copy(m_nonblock_buffer.begin(), m_nonblock_buffer.end(), buffer);
- buffer += m_nonblock_buffer_len;
- m_nonblock_buffer.clear();
- std::copy(buf.begin(), buf.end(), buffer);
- return ret+m_nonblock_buffer_len;
+
+ // read() might read less data than requested
+ buf.resize(ret);
+
+ copy(buf.begin(), buf.end(), back_inserter(m_nonblock_buffer));
}
- else {
- std::copy(m_nonblock_buffer.begin(), m_nonblock_buffer.begin() + size, buffer);
- std::vector<uint8_t> remaining_buf;
- std::copy(m_nonblock_buffer.begin() + size, m_nonblock_buffer.end(),
- std::back_inserter(remaining_buf));
- m_nonblock_buffer = std::move(remaining_buf);
+ if (m_nonblock_buffer.size() >= size) {
+ copy(m_nonblock_buffer.begin(), m_nonblock_buffer.begin() + size, buffer);
+
+ vector<uint8_t> remaining_buf;
+ copy(m_nonblock_buffer.begin() + size, m_nonblock_buffer.end(), back_inserter(remaining_buf));
+ m_nonblock_buffer = move(remaining_buf);
+
return size;
}
- return 0;
+ else {
+ return 0;
+ }
}
else {
ret = read(m_fd, buffer, size);