From c4f8471d7acb1dc31888519a5daf6fccf3192a9f Mon Sep 17 00:00:00 2001 From: Bart Trzynadlowski Date: Sun, 24 Sep 2017 20:49:31 +0000 Subject: [PATCH] Added functions to safely read and write bools --- Src/BlockFile.cpp | 16 ++++++++++++++++ Src/BlockFile.h | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/Src/BlockFile.cpp b/Src/BlockFile.cpp index 24b5c4e..023828a 100644 --- a/Src/BlockFile.cpp +++ b/Src/BlockFile.cpp @@ -28,6 +28,7 @@ #include #include +#include #include "Supermodel.h" @@ -154,12 +155,27 @@ unsigned CBlockFile::Read(void *data, uint32_t numBytes) return 0; } +unsigned CBlockFile::Read(bool *value) +{ + uint8_t byte; + unsigned numBytes = Read(&byte, sizeof(byte)); + if (numBytes > 0) + *value = byte != 0; + return numBytes; +} + void CBlockFile::Write(const void *data, uint32_t numBytes) { if (mode == 'w') WriteBytes(data, numBytes); } +void CBlockFile::Write(bool value) +{ + uint8_t byte = value ? 1 : 0; + Write(&byte, sizeof(byte)); +} + void CBlockFile::Write(const std::string &str) { if (mode == 'w') diff --git a/Src/BlockFile.h b/Src/BlockFile.h index b332e75..f3f7da6 100644 --- a/Src/BlockFile.h +++ b/Src/BlockFile.h @@ -60,6 +60,19 @@ public: */ unsigned Read(void *data, uint32_t numBytes); + /* + * Read(value): + * + * Reads a bool value from the current file position. + * + * Parameters: + * value Bool to read to. + * + * Returns: + * Number of bytes read. If not 1, an error occurred. + */ + unsigned Read(bool *value); + /* * FindBlock(name): * @@ -74,6 +87,17 @@ public: */ bool FindBlock(const std::string &name); + /* + * Write(value): + * + * Outputs a bool value at the current file pointer position as a byte with + * value either 0 or 1. Updates the block header appropriately. + * + * Parameters: + * value A boolean value to write. + */ + void Write(bool value); + /* * Write(data, numBytes): *