Added functions to safely read and write bools

This commit is contained in:
Bart Trzynadlowski 2017-09-24 20:49:31 +00:00
parent 1db2f6bb84
commit c4f8471d7a
2 changed files with 40 additions and 0 deletions

View file

@ -28,6 +28,7 @@
#include <cstdio>
#include <cstring>
#include <cstdint>
#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')

View file

@ -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):
*