mirror of
https://github.com/RetroDECK/Supermodel.git
synced 2024-11-25 23:25:40 +00:00
Whitespace, cstdint types
This commit is contained in:
parent
9c2aff72c8
commit
c7ad13fb31
|
@ -37,102 +37,102 @@
|
|||
|
||||
void CBlockFile::ReadString(char *str, unsigned strLen, unsigned maxLen)
|
||||
{
|
||||
if (NULL == fp)
|
||||
return;
|
||||
if (strLen>maxLen)
|
||||
strLen = maxLen;
|
||||
fread(str, sizeof(char), strLen, fp);
|
||||
str[strLen] = '\0';
|
||||
if (NULL == fp)
|
||||
return;
|
||||
if (strLen>maxLen)
|
||||
strLen = maxLen;
|
||||
fread(str, sizeof(char), strLen, fp);
|
||||
str[strLen] = '\0';
|
||||
}
|
||||
|
||||
unsigned CBlockFile::ReadBytes(void *data, unsigned numBytes)
|
||||
{
|
||||
if (NULL == fp)
|
||||
return 0;
|
||||
return fread(data, sizeof(UINT8), numBytes, fp);
|
||||
if (NULL == fp)
|
||||
return 0;
|
||||
return fread(data, sizeof(uint8_t), numBytes, fp);
|
||||
}
|
||||
|
||||
unsigned CBlockFile::ReadDWord(UINT32 *data)
|
||||
unsigned CBlockFile::ReadDWord(uint32_t *data)
|
||||
{
|
||||
if (NULL == fp)
|
||||
return 0;
|
||||
fread(data, sizeof(UINT32), 1, fp);
|
||||
return 4;
|
||||
if (NULL == fp)
|
||||
return 0;
|
||||
fread(data, sizeof(uint32_t), 1, fp);
|
||||
return 4;
|
||||
}
|
||||
|
||||
|
||||
void CBlockFile::UpdateBlockSize(void)
|
||||
{
|
||||
long int curPos;
|
||||
unsigned newBlockSize;
|
||||
|
||||
if (NULL == fp)
|
||||
return;
|
||||
curPos = ftell(fp); // save current file position
|
||||
fseek(fp, blockStartPos, SEEK_SET);
|
||||
newBlockSize = curPos - blockStartPos;
|
||||
fwrite(&newBlockSize, sizeof(UINT32), 1, fp);
|
||||
fseek(fp, curPos, SEEK_SET); // go back
|
||||
long int curPos;
|
||||
unsigned newBlockSize;
|
||||
|
||||
if (NULL == fp)
|
||||
return;
|
||||
curPos = ftell(fp); // save current file position
|
||||
fseek(fp, blockStartPos, SEEK_SET);
|
||||
newBlockSize = curPos - blockStartPos;
|
||||
fwrite(&newBlockSize, sizeof(uint32_t), 1, fp);
|
||||
fseek(fp, curPos, SEEK_SET); // go back
|
||||
}
|
||||
|
||||
void CBlockFile::WriteByte(UINT8 data)
|
||||
void CBlockFile::WriteByte(uint8_t data)
|
||||
{
|
||||
if (NULL == fp)
|
||||
return;
|
||||
fwrite(&data, sizeof(UINT8), 1, fp);
|
||||
UpdateBlockSize();
|
||||
if (NULL == fp)
|
||||
return;
|
||||
fwrite(&data, sizeof(uint8_t), 1, fp);
|
||||
UpdateBlockSize();
|
||||
}
|
||||
|
||||
void CBlockFile::WriteDWord(UINT32 data)
|
||||
void CBlockFile::WriteDWord(uint32_t data)
|
||||
{
|
||||
if (NULL == fp)
|
||||
return;
|
||||
fwrite(&data, sizeof(UINT32), 1, fp);
|
||||
UpdateBlockSize();
|
||||
if (NULL == fp)
|
||||
return;
|
||||
fwrite(&data, sizeof(uint32_t), 1, fp);
|
||||
UpdateBlockSize();
|
||||
}
|
||||
|
||||
void CBlockFile::WriteBytes(const void *data, unsigned numBytes)
|
||||
{
|
||||
if (NULL == fp)
|
||||
return;
|
||||
fwrite(data, sizeof(UINT8), numBytes, fp);
|
||||
UpdateBlockSize();
|
||||
if (NULL == fp)
|
||||
return;
|
||||
fwrite(data, sizeof(uint8_t), numBytes, fp);
|
||||
UpdateBlockSize();
|
||||
}
|
||||
|
||||
void CBlockFile::WriteBlockHeader(const char *name, const char *comment)
|
||||
{
|
||||
unsigned nameLen, commentLen;
|
||||
const char nullComment[1] = {'\0'};
|
||||
unsigned nameLen, commentLen;
|
||||
const char nullComment[1] = {'\0'};
|
||||
|
||||
if (NULL == fp)
|
||||
return;
|
||||
|
||||
if (comment == NULL)
|
||||
comment = nullComment;
|
||||
|
||||
nameLen = strlen(name);
|
||||
commentLen = strlen(comment);
|
||||
if (nameLen > 1024)
|
||||
nameLen = 1024;
|
||||
if (commentLen > 1024)
|
||||
commentLen = 1024;
|
||||
if (NULL == fp)
|
||||
return;
|
||||
|
||||
if (comment == NULL)
|
||||
comment = nullComment;
|
||||
|
||||
nameLen = strlen(name);
|
||||
commentLen = strlen(comment);
|
||||
if (nameLen > 1024)
|
||||
nameLen = 1024;
|
||||
if (commentLen > 1024)
|
||||
commentLen = 1024;
|
||||
|
||||
// Record current block starting position
|
||||
blockStartPos = ftell(fp);
|
||||
// Record current block starting position
|
||||
blockStartPos = ftell(fp);
|
||||
|
||||
// Write the total block length field
|
||||
WriteDWord(0); // will be automatically updated as we write the file
|
||||
|
||||
// Write name and comment lengths
|
||||
WriteDWord(nameLen+1);
|
||||
WriteDWord(commentLen+1);
|
||||
WriteBytes(name, nameLen);
|
||||
WriteByte(0);
|
||||
WriteBytes(comment, commentLen);
|
||||
WriteByte(0);
|
||||
|
||||
// Record the start of the current data section
|
||||
dataStartPos = ftell(fp);
|
||||
}
|
||||
// Write the total block length field
|
||||
WriteDWord(0); // will be automatically updated as we write the file
|
||||
|
||||
// Write name and comment lengths
|
||||
WriteDWord(nameLen+1);
|
||||
WriteDWord(commentLen+1);
|
||||
WriteBytes(name, nameLen);
|
||||
WriteByte(0);
|
||||
WriteBytes(comment, commentLen);
|
||||
WriteByte(0);
|
||||
|
||||
// Record the start of the current data section
|
||||
dataStartPos = ftell(fp);
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
|
@ -142,117 +142,117 @@ void CBlockFile::WriteBlockHeader(const char *name, const char *comment)
|
|||
|
||||
Block Format
|
||||
------------
|
||||
blockLength (UINT32) Total length of block in bytes.
|
||||
nameLength (UINT32) Length of name field including terminating 0 (up to
|
||||
1025).
|
||||
commentLength (UINT32) Same as above, but for comment string.
|
||||
name ... Name string (null-terminated, up to 1025 bytes).
|
||||
comment ... Comment string (same as above).
|
||||
data ... Raw data (blockLength - total header size).
|
||||
blockLength (uint32_t) Total length of block in bytes.
|
||||
nameLength (uint32_t) Length of name field including terminating 0 (up to
|
||||
1025).
|
||||
commentLength (uint32_t) Same as above, but for comment string.
|
||||
name ... Name string (null-terminated, up to 1025 bytes).
|
||||
comment ... Comment string (same as above).
|
||||
data ... Raw data (blockLength - total header size).
|
||||
******************************************************************************/
|
||||
|
||||
unsigned CBlockFile::Read(void *data, unsigned numBytes)
|
||||
{
|
||||
if (mode == 'r')
|
||||
return ReadBytes(data, numBytes);
|
||||
return 0;
|
||||
if (mode == 'r')
|
||||
return ReadBytes(data, numBytes);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void CBlockFile::Write(const void *data, unsigned numBytes)
|
||||
{
|
||||
if (mode == 'w')
|
||||
WriteBytes(data, numBytes);
|
||||
if (mode == 'w')
|
||||
WriteBytes(data, numBytes);
|
||||
}
|
||||
|
||||
void CBlockFile::NewBlock(const char *name, const char *comment)
|
||||
{
|
||||
if (mode == 'w')
|
||||
WriteBlockHeader(name, comment);
|
||||
if (mode == 'w')
|
||||
WriteBlockHeader(name, comment);
|
||||
}
|
||||
|
||||
bool CBlockFile::FindBlock(const char *name)
|
||||
{
|
||||
long int curPos = 0;
|
||||
unsigned blockLen, nameLen, commentLen;
|
||||
|
||||
if (mode != 'r')
|
||||
return FAIL;
|
||||
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
while (curPos < fileSize)
|
||||
{
|
||||
blockStartPos = curPos;
|
||||
|
||||
// Read header
|
||||
curPos += ReadDWord(&blockLen);
|
||||
curPos += ReadDWord(&nameLen);
|
||||
curPos += ReadDWord(&commentLen);
|
||||
ReadString(strBuf,nameLen,1025);
|
||||
|
||||
// Is this the block we want?
|
||||
if (!strcmp(strBuf,name))
|
||||
{
|
||||
fseek(fp, blockStartPos+12+nameLen+commentLen, SEEK_SET); // move to beginning of data
|
||||
dataStartPos = ftell(fp);
|
||||
return OKAY;
|
||||
}
|
||||
|
||||
// Move to next block
|
||||
fseek(fp, blockStartPos+blockLen, SEEK_SET);
|
||||
curPos = blockStartPos+blockLen;
|
||||
if (blockLen == 0) // this would never advance
|
||||
break;
|
||||
}
|
||||
|
||||
return FAIL;
|
||||
long int curPos = 0;
|
||||
unsigned blockLen, nameLen, commentLen;
|
||||
|
||||
if (mode != 'r')
|
||||
return FAIL;
|
||||
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
while (curPos < fileSize)
|
||||
{
|
||||
blockStartPos = curPos;
|
||||
|
||||
// Read header
|
||||
curPos += ReadDWord(&blockLen);
|
||||
curPos += ReadDWord(&nameLen);
|
||||
curPos += ReadDWord(&commentLen);
|
||||
ReadString(strBuf,nameLen,1025);
|
||||
|
||||
// Is this the block we want?
|
||||
if (!strcmp(strBuf,name))
|
||||
{
|
||||
fseek(fp, blockStartPos+12+nameLen+commentLen, SEEK_SET); // move to beginning of data
|
||||
dataStartPos = ftell(fp);
|
||||
return OKAY;
|
||||
}
|
||||
|
||||
// Move to next block
|
||||
fseek(fp, blockStartPos+blockLen, SEEK_SET);
|
||||
curPos = blockStartPos+blockLen;
|
||||
if (blockLen == 0) // this would never advance
|
||||
break;
|
||||
}
|
||||
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
bool CBlockFile::Create(const char *file, const char *headerName, const char *comment)
|
||||
{
|
||||
fp = fopen(file, "wb");
|
||||
if (NULL == fp)
|
||||
return FAIL;
|
||||
mode = 'w';
|
||||
WriteBlockHeader(headerName, comment);
|
||||
return OKAY;
|
||||
fp = fopen(file, "wb");
|
||||
if (NULL == fp)
|
||||
return FAIL;
|
||||
mode = 'w';
|
||||
WriteBlockHeader(headerName, comment);
|
||||
return OKAY;
|
||||
}
|
||||
|
||||
|
||||
bool CBlockFile::Load(const char *file)
|
||||
{
|
||||
fp = fopen(file, "rb");
|
||||
if (NULL == fp)
|
||||
return FAIL;
|
||||
mode = 'r';
|
||||
|
||||
// TODO: is this a valid block file?
|
||||
|
||||
// Get the file size
|
||||
fseek(fp, 0, SEEK_END);
|
||||
fileSize = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
return OKAY;
|
||||
fp = fopen(file, "rb");
|
||||
if (NULL == fp)
|
||||
return FAIL;
|
||||
mode = 'r';
|
||||
|
||||
// TODO: is this a valid block file?
|
||||
|
||||
// Get the file size
|
||||
fseek(fp, 0, SEEK_END);
|
||||
fileSize = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
return OKAY;
|
||||
}
|
||||
|
||||
|
||||
void CBlockFile::Close(void)
|
||||
{
|
||||
if (fp != NULL)
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
mode = 0;
|
||||
if (fp != NULL)
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
mode = 0;
|
||||
}
|
||||
|
||||
CBlockFile::CBlockFile(void)
|
||||
{
|
||||
fp = NULL;
|
||||
mode = 0; // neither reading nor writing (do nothing)
|
||||
fp = NULL;
|
||||
mode = 0; // neither reading nor writing (do nothing)
|
||||
}
|
||||
|
||||
CBlockFile::~CBlockFile(void)
|
||||
{
|
||||
if (fp != NULL) // in case user forgot
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
mode = 0;
|
||||
if (fp != NULL) // in case user forgot
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
mode = 0;
|
||||
}
|
||||
|
|
229
Src/BlockFile.h
229
Src/BlockFile.h
|
@ -28,6 +28,7 @@
|
|||
#ifndef INCLUDED_BLOCKFILE_H
|
||||
#define INCLUDED_BLOCKFILE_H
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
/*
|
||||
* CBlockFile:
|
||||
|
@ -43,128 +44,128 @@
|
|||
class CBlockFile
|
||||
{
|
||||
public:
|
||||
/*
|
||||
* Read(data, numBytes):
|
||||
*
|
||||
* Reads data from the current file position.
|
||||
*
|
||||
* Parameters:
|
||||
* data Buffer to read to.
|
||||
* numBytes Number of bytes to read.
|
||||
*
|
||||
* Returns:
|
||||
* Number of bytes read. If not the same as numBytes, an error
|
||||
* occurred.
|
||||
*/
|
||||
unsigned Read(void *data, unsigned numBytes);
|
||||
|
||||
/*
|
||||
* FindBlock(name):
|
||||
*
|
||||
* Scans the file for a block with the given name string. When it is found,
|
||||
* the file pointer is set to the beginning of the data region.
|
||||
*
|
||||
* Parameters:
|
||||
* name Name of block to locate.
|
||||
*
|
||||
* Returns:
|
||||
* OKAY if found, FAIL if unable to locate.
|
||||
*/
|
||||
bool FindBlock(const char *name);
|
||||
|
||||
/*
|
||||
* Write(data, numBytes):
|
||||
*
|
||||
* Outputs data at the current file pointer position. Updates the block
|
||||
* header appropriately.
|
||||
*
|
||||
* Parameters:
|
||||
* data Data to write.
|
||||
* numBytes Number of bytes to write.
|
||||
*/
|
||||
void Write(const void *data, unsigned numBytes);
|
||||
/*
|
||||
* Read(data, numBytes):
|
||||
*
|
||||
* Reads data from the current file position.
|
||||
*
|
||||
* Parameters:
|
||||
* data Buffer to read to.
|
||||
* numBytes Number of bytes to read.
|
||||
*
|
||||
* Returns:
|
||||
* Number of bytes read. If not the same as numBytes, an error
|
||||
* occurred.
|
||||
*/
|
||||
unsigned Read(void *data, unsigned numBytes);
|
||||
|
||||
/*
|
||||
* FindBlock(name):
|
||||
*
|
||||
* Scans the file for a block with the given name string. When it is found,
|
||||
* the file pointer is set to the beginning of the data region.
|
||||
*
|
||||
* Parameters:
|
||||
* name Name of block to locate.
|
||||
*
|
||||
* Returns:
|
||||
* OKAY if found, FAIL if unable to locate.
|
||||
*/
|
||||
bool FindBlock(const char *name);
|
||||
|
||||
/*
|
||||
* Write(data, numBytes):
|
||||
*
|
||||
* Outputs data at the current file pointer position. Updates the block
|
||||
* header appropriately.
|
||||
*
|
||||
* Parameters:
|
||||
* data Data to write.
|
||||
* numBytes Number of bytes to write.
|
||||
*/
|
||||
void Write(const void *data, unsigned numBytes);
|
||||
|
||||
/*
|
||||
* NewBlock(name, comment):
|
||||
*
|
||||
* Begins a new block. Writes the block header and sets the file pointer to
|
||||
* the beginning of its data area.
|
||||
*
|
||||
* Parameters:
|
||||
* name Block name. Must be unique and not NULL.
|
||||
* comment Comment string to embed in the block header.
|
||||
*/
|
||||
void NewBlock(const char *title, const char *comment);
|
||||
/*
|
||||
* NewBlock(name, comment):
|
||||
*
|
||||
* Begins a new block. Writes the block header and sets the file pointer to
|
||||
* the beginning of its data area.
|
||||
*
|
||||
* Parameters:
|
||||
* name Block name. Must be unique and not NULL.
|
||||
* comment Comment string to embed in the block header.
|
||||
*/
|
||||
void NewBlock(const char *title, const char *comment);
|
||||
|
||||
/*
|
||||
* Create(file, headerName, comment):
|
||||
*
|
||||
* Opens a block file for writing and creates the header block. This
|
||||
* function must be called before attempting to write data. Otherwise, all
|
||||
* write commands will be silently ignored. Read commands will be ignored
|
||||
* and will always return 0's.
|
||||
*
|
||||
* Parameters:
|
||||
* file File path.
|
||||
* headerName Block name for header. Must be unique and not NULL.
|
||||
* comment Comment string that will be embedded into file header.
|
||||
*
|
||||
* Returns:
|
||||
* OKAY if successfully opened, otherwise FAIL.
|
||||
*/
|
||||
bool Create(const char *file, const char *headerName, const char *comment);
|
||||
/*
|
||||
* Create(file, headerName, comment):
|
||||
*
|
||||
* Opens a block file for writing and creates the header block. This
|
||||
* function must be called before attempting to write data. Otherwise, all
|
||||
* write commands will be silently ignored. Read commands will be ignored
|
||||
* and will always return 0's.
|
||||
*
|
||||
* Parameters:
|
||||
* file File path.
|
||||
* headerName Block name for header. Must be unique and not NULL.
|
||||
* comment Comment string that will be embedded into file header.
|
||||
*
|
||||
* Returns:
|
||||
* OKAY if successfully opened, otherwise FAIL.
|
||||
*/
|
||||
bool Create(const char *file, const char *headerName, const char *comment);
|
||||
|
||||
/*
|
||||
* Load(file):
|
||||
*
|
||||
* Open a block file file for reading.
|
||||
*
|
||||
* Parameters:
|
||||
* file File path.
|
||||
*
|
||||
* Returns:
|
||||
* OKAY if successfully opened and confirmed to be a valid Supermodel
|
||||
* block file, otherwise FAIL. If the file could not be opened, all
|
||||
* subsequent operations will be silently ignored (reads will return
|
||||
* 0's). Write commands will be ignored.
|
||||
*/
|
||||
bool Load(const char *file);
|
||||
/*
|
||||
* Load(file):
|
||||
*
|
||||
* Open a block file file for reading.
|
||||
*
|
||||
* Parameters:
|
||||
* file File path.
|
||||
*
|
||||
* Returns:
|
||||
* OKAY if successfully opened and confirmed to be a valid Supermodel
|
||||
* block file, otherwise FAIL. If the file could not be opened, all
|
||||
* subsequent operations will be silently ignored (reads will return
|
||||
* 0's). Write commands will be ignored.
|
||||
*/
|
||||
bool Load(const char *file);
|
||||
|
||||
/*
|
||||
* Close(void):
|
||||
*
|
||||
* Closes the file.
|
||||
*/
|
||||
void Close(void);
|
||||
/*
|
||||
* Close(void):
|
||||
*
|
||||
* Closes the file.
|
||||
*/
|
||||
void Close(void);
|
||||
|
||||
/*
|
||||
* CBlockFile(void):
|
||||
* ~CBlockFile(void):
|
||||
*
|
||||
* Constructor and destructor.
|
||||
*/
|
||||
CBlockFile(void);
|
||||
~CBlockFile(void);
|
||||
/*
|
||||
* CBlockFile(void):
|
||||
* ~CBlockFile(void):
|
||||
*
|
||||
* Constructor and destructor.
|
||||
*/
|
||||
CBlockFile(void);
|
||||
~CBlockFile(void);
|
||||
|
||||
private:
|
||||
// Helper functions
|
||||
void ReadString(char *str, unsigned strLen, unsigned maxLen);
|
||||
unsigned ReadBytes(void *data, unsigned numBytes);
|
||||
unsigned ReadDWord(UINT32 *data);
|
||||
void UpdateBlockSize(void);
|
||||
void WriteByte(UINT8 data);
|
||||
void WriteDWord(UINT32 data);
|
||||
void WriteBytes(const void *data, unsigned numBytes);
|
||||
void WriteBlockHeader(const char *name, const char *comment);
|
||||
// Helper functions
|
||||
void ReadString(char *str, unsigned strLen, unsigned maxLen);
|
||||
unsigned ReadBytes(void *data, unsigned numBytes);
|
||||
unsigned ReadDWord(uint32_t *data);
|
||||
void UpdateBlockSize(void);
|
||||
void WriteByte(uint8_t data);
|
||||
void WriteDWord(uint32_t data);
|
||||
void WriteBytes(const void *data, unsigned numBytes);
|
||||
void WriteBlockHeader(const char *name, const char *comment);
|
||||
|
||||
// File state data
|
||||
char strBuf[1026]; // buffers up to a 1024-character string, its terminator, and an extra terminator (just in case)
|
||||
FILE *fp;
|
||||
int mode; // 'r' for read, 'w' for write
|
||||
long int fileSize; // size of file in bytes
|
||||
long int blockStartPos; // points to beginning of current block (or file) header
|
||||
long int dataStartPos; // points to beginning of current block's data section
|
||||
// File state data
|
||||
char strBuf[1026]; // buffers up to a 1024-character string, its terminator, and an extra terminator (just in case)
|
||||
FILE *fp;
|
||||
int mode; // 'r' for read, 'w' for write
|
||||
long int fileSize; // size of file in bytes
|
||||
long int blockStartPos; // points to beginning of current block (or file) header
|
||||
long int dataStartPos; // points to beginning of current block's data section
|
||||
};
|
||||
|
||||
|
||||
#endif // INCLUDED_BLOCKFILE_H
|
||||
#endif // INCLUDED_BLOCKFILE_H
|
||||
|
|
Loading…
Reference in a new issue