summaryrefslogtreecommitdiffstats
path: root/src/input
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2022-10-11 16:38:40 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2022-10-11 16:38:40 +0200
commit5f037f3a15456cc4dc4dff55f30cf6144f41cb02 (patch)
treee06d15c8abeb2924793dd997b07892b253698d81 /src/input
parent589e2f0d174ca043ac17fe3ae06b7e5532542e9d (diff)
downloaddabmux-5f037f3a15456cc4dc4dff55f30cf6144f41cb02.tar.gz
dabmux-5f037f3a15456cc4dc4dff55f30cf6144f41cb02.tar.bz2
dabmux-5f037f3a15456cc4dc4dff55f30cf6144f41cb02.zip
load_entire_file: stop playing loop if file disappears
Diffstat (limited to 'src/input')
-rw-r--r--src/input/File.cpp20
1 files changed, 14 insertions, 6 deletions
diff --git a/src/input/File.cpp b/src/input/File.cpp
index 8f5d3e3..a4ef365 100644
--- a/src/input/File.cpp
+++ b/src/input/File.cpp
@@ -138,7 +138,11 @@ ssize_t FileBase::rewind()
ssize_t FileBase::load_entire_file()
{
+ // Clear the buffer if the file open fails, this allows user to stop transmission
+ // of the current data.
+ m_file_contents.clear();
m_file_contents_offset = 0;
+
// Read entire file in chunks of 4MiB
constexpr size_t blocksize = 4 * 1024 * 1024;
constexpr int flags = O_RDONLY | O_BINARY;
@@ -152,8 +156,6 @@ ssize_t FileBase::load_entire_file()
return -1;
}
- // Do not clear the buffer if the file open fails
- m_file_contents.clear();
ssize_t offset = 0;
ssize_t r = 0;
do {
@@ -243,15 +245,21 @@ ssize_t FileBase::readFromFile(uint8_t *buffer, size_t size)
size_t remain = size;
while (m_file_contents_offset + remain > m_file_contents.size()) {
- copy( m_file_contents.begin() + m_file_contents_offset,
- m_file_contents.end(), buffer);
+ copy( m_file_contents.cbegin() + m_file_contents_offset,
+ m_file_contents.cend(), buffer);
size_t copied = m_file_contents.size() - m_file_contents_offset;
remain -= copied;
rewind();
+
+ // In case rewind() fails
+ if (m_file_contents.size() == 0) {
+ memset(buffer, 0, size);
+ return size;
+ }
}
- copy( m_file_contents.begin() + m_file_contents_offset,
- m_file_contents.begin() + m_file_contents_offset + remain, buffer);
+ copy( m_file_contents.cbegin() + m_file_contents_offset,
+ m_file_contents.cbegin() + m_file_contents_offset + remain, buffer);
m_file_contents_offset += remain;
}
return size;