summaryrefslogtreecommitdiffstats
path: root/src/input
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-12-20 11:18:43 +0100
commit2441cc2dfff9aa0fe995ada8be0222ca37c7882c (patch)
tree2a9230c02f623aa4e6b8c01f9a5cb607b5825680 /src/input
parent3b9939c2528502e78b8433a80e0fa0736e278569 (diff)
downloaddabmux-2441cc2dfff9aa0fe995ada8be0222ca37c7882c.tar.gz
dabmux-2441cc2dfff9aa0fe995ada8be0222ca37c7882c.tar.bz2
dabmux-2441cc2dfff9aa0fe995ada8be0222ca37c7882c.zip
Simplify nonblocking File input
(cherry picked from commit 1ce18aa61b14ae5201d70cdf07be62183e18de67)
Diffstat (limited to 'src/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);