mirror of
https://github.com/RetroDECK/Supermodel.git
synced 2024-11-25 23:25:40 +00:00
Added functions to safely read and write bools
This commit is contained in:
parent
1db2f6bb84
commit
c4f8471d7a
|
@ -28,6 +28,7 @@
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <cstdint>
|
||||||
#include "Supermodel.h"
|
#include "Supermodel.h"
|
||||||
|
|
||||||
|
|
||||||
|
@ -154,12 +155,27 @@ unsigned CBlockFile::Read(void *data, uint32_t numBytes)
|
||||||
return 0;
|
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)
|
void CBlockFile::Write(const void *data, uint32_t numBytes)
|
||||||
{
|
{
|
||||||
if (mode == 'w')
|
if (mode == 'w')
|
||||||
WriteBytes(data, numBytes);
|
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)
|
void CBlockFile::Write(const std::string &str)
|
||||||
{
|
{
|
||||||
if (mode == 'w')
|
if (mode == 'w')
|
||||||
|
|
|
@ -60,6 +60,19 @@ public:
|
||||||
*/
|
*/
|
||||||
unsigned Read(void *data, uint32_t numBytes);
|
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):
|
* FindBlock(name):
|
||||||
*
|
*
|
||||||
|
@ -74,6 +87,17 @@ public:
|
||||||
*/
|
*/
|
||||||
bool FindBlock(const std::string &name);
|
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):
|
* Write(data, numBytes):
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue