diff options
Diffstat (limited to 'src/files.cpp')
| -rw-r--r-- | src/files.cpp | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/src/files.cpp b/src/files.cpp index b757811..20f2672 100644 --- a/src/files.cpp +++ b/src/files.cpp @@ -6,6 +6,7 @@ #include <cstdio> #include <cstring> #include <iostream> +#include <stdexcept> using std::cout, std::endl; @@ -124,14 +125,17 @@ namespace TehImage void Writer::writeByte(char toWrite) { - fwrite(&toWrite, sizeof(char), 1, file); + if(pos == WRITER_BUFFER_SIZE) + throw std::out_of_range("Writer buffer ran out single-byte!!!"); + buffer[pos++] = toWrite; } void Writer::writeBytes(char toWrite[], std::size_t len) { - // for(int i = 0; i < len; i++) - // cout << toWrite[i] << endl; - fwrite(toWrite, sizeof(char), len, file); + if(pos + len > WRITER_BUFFER_SIZE) + throw std::out_of_range("Writer buffer ran out multi-byte!!!"); + memcpy(&(buffer[pos]), toWrite, len); + pos += len; } void Writer::zeroBytes(std::size_t len) @@ -145,10 +149,21 @@ namespace TehImage void Writer::close() { if(ready) + { + flushBuffer(); fclose(file); + } ready = false; } + void Writer::flushBuffer() + { + if(!ready) + throw std::logic_error("Cannot flush buffer before opening file"); + fwrite(buffer, sizeof(char), pos, file); + pos = 0; + } + template <> void Writer::writeData(uint8_t toWrite) { writeByte(toWrite); |
