Whitespace, cstdint types

This commit is contained in:
Bart Trzynadlowski 2016-04-02 21:32:28 +00:00
parent 9c2aff72c8
commit c7ad13fb31
2 changed files with 263 additions and 262 deletions

View file

@ -37,101 +37,101 @@
void CBlockFile::ReadString(char *str, unsigned strLen, unsigned maxLen) void CBlockFile::ReadString(char *str, unsigned strLen, unsigned maxLen)
{ {
if (NULL == fp) if (NULL == fp)
return; return;
if (strLen>maxLen) if (strLen>maxLen)
strLen = maxLen; strLen = maxLen;
fread(str, sizeof(char), strLen, fp); fread(str, sizeof(char), strLen, fp);
str[strLen] = '\0'; str[strLen] = '\0';
} }
unsigned CBlockFile::ReadBytes(void *data, unsigned numBytes) unsigned CBlockFile::ReadBytes(void *data, unsigned numBytes)
{ {
if (NULL == fp) if (NULL == fp)
return 0; return 0;
return fread(data, sizeof(UINT8), numBytes, fp); return fread(data, sizeof(uint8_t), numBytes, fp);
} }
unsigned CBlockFile::ReadDWord(UINT32 *data) unsigned CBlockFile::ReadDWord(uint32_t *data)
{ {
if (NULL == fp) if (NULL == fp)
return 0; return 0;
fread(data, sizeof(UINT32), 1, fp); fread(data, sizeof(uint32_t), 1, fp);
return 4; return 4;
} }
void CBlockFile::UpdateBlockSize(void) void CBlockFile::UpdateBlockSize(void)
{ {
long int curPos; long int curPos;
unsigned newBlockSize; unsigned newBlockSize;
if (NULL == fp) if (NULL == fp)
return; return;
curPos = ftell(fp); // save current file position curPos = ftell(fp); // save current file position
fseek(fp, blockStartPos, SEEK_SET); fseek(fp, blockStartPos, SEEK_SET);
newBlockSize = curPos - blockStartPos; newBlockSize = curPos - blockStartPos;
fwrite(&newBlockSize, sizeof(UINT32), 1, fp); fwrite(&newBlockSize, sizeof(uint32_t), 1, fp);
fseek(fp, curPos, SEEK_SET); // go back fseek(fp, curPos, SEEK_SET); // go back
} }
void CBlockFile::WriteByte(UINT8 data) void CBlockFile::WriteByte(uint8_t data)
{ {
if (NULL == fp) if (NULL == fp)
return; return;
fwrite(&data, sizeof(UINT8), 1, fp); fwrite(&data, sizeof(uint8_t), 1, fp);
UpdateBlockSize(); UpdateBlockSize();
} }
void CBlockFile::WriteDWord(UINT32 data) void CBlockFile::WriteDWord(uint32_t data)
{ {
if (NULL == fp) if (NULL == fp)
return; return;
fwrite(&data, sizeof(UINT32), 1, fp); fwrite(&data, sizeof(uint32_t), 1, fp);
UpdateBlockSize(); UpdateBlockSize();
} }
void CBlockFile::WriteBytes(const void *data, unsigned numBytes) void CBlockFile::WriteBytes(const void *data, unsigned numBytes)
{ {
if (NULL == fp) if (NULL == fp)
return; return;
fwrite(data, sizeof(UINT8), numBytes, fp); fwrite(data, sizeof(uint8_t), numBytes, fp);
UpdateBlockSize(); UpdateBlockSize();
} }
void CBlockFile::WriteBlockHeader(const char *name, const char *comment) void CBlockFile::WriteBlockHeader(const char *name, const char *comment)
{ {
unsigned nameLen, commentLen; unsigned nameLen, commentLen;
const char nullComment[1] = {'\0'}; const char nullComment[1] = {'\0'};
if (NULL == fp) if (NULL == fp)
return; return;
if (comment == NULL) if (comment == NULL)
comment = nullComment; comment = nullComment;
nameLen = strlen(name); nameLen = strlen(name);
commentLen = strlen(comment); commentLen = strlen(comment);
if (nameLen > 1024) if (nameLen > 1024)
nameLen = 1024; nameLen = 1024;
if (commentLen > 1024) if (commentLen > 1024)
commentLen = 1024; commentLen = 1024;
// Record current block starting position // Record current block starting position
blockStartPos = ftell(fp); blockStartPos = ftell(fp);
// Write the total block length field // Write the total block length field
WriteDWord(0); // will be automatically updated as we write the file WriteDWord(0); // will be automatically updated as we write the file
// Write name and comment lengths // Write name and comment lengths
WriteDWord(nameLen+1); WriteDWord(nameLen+1);
WriteDWord(commentLen+1); WriteDWord(commentLen+1);
WriteBytes(name, nameLen); WriteBytes(name, nameLen);
WriteByte(0); WriteByte(0);
WriteBytes(comment, commentLen); WriteBytes(comment, commentLen);
WriteByte(0); WriteByte(0);
// Record the start of the current data section // Record the start of the current data section
dataStartPos = ftell(fp); dataStartPos = ftell(fp);
} }
@ -142,117 +142,117 @@ void CBlockFile::WriteBlockHeader(const char *name, const char *comment)
Block Format Block Format
------------ ------------
blockLength (UINT32) Total length of block in bytes. blockLength (uint32_t) Total length of block in bytes.
nameLength (UINT32) Length of name field including terminating 0 (up to nameLength (uint32_t) Length of name field including terminating 0 (up to
1025). 1025).
commentLength (UINT32) Same as above, but for comment string. commentLength (uint32_t) Same as above, but for comment string.
name ... Name string (null-terminated, up to 1025 bytes). name ... Name string (null-terminated, up to 1025 bytes).
comment ... Comment string (same as above). comment ... Comment string (same as above).
data ... Raw data (blockLength - total header size). data ... Raw data (blockLength - total header size).
******************************************************************************/ ******************************************************************************/
unsigned CBlockFile::Read(void *data, unsigned numBytes) unsigned CBlockFile::Read(void *data, unsigned numBytes)
{ {
if (mode == 'r') if (mode == 'r')
return ReadBytes(data, numBytes); return ReadBytes(data, numBytes);
return 0; return 0;
} }
void CBlockFile::Write(const void *data, unsigned numBytes) void CBlockFile::Write(const void *data, unsigned numBytes)
{ {
if (mode == 'w') if (mode == 'w')
WriteBytes(data, numBytes); WriteBytes(data, numBytes);
} }
void CBlockFile::NewBlock(const char *name, const char *comment) void CBlockFile::NewBlock(const char *name, const char *comment)
{ {
if (mode == 'w') if (mode == 'w')
WriteBlockHeader(name, comment); WriteBlockHeader(name, comment);
} }
bool CBlockFile::FindBlock(const char *name) bool CBlockFile::FindBlock(const char *name)
{ {
long int curPos = 0; long int curPos = 0;
unsigned blockLen, nameLen, commentLen; unsigned blockLen, nameLen, commentLen;
if (mode != 'r') if (mode != 'r')
return FAIL; return FAIL;
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
while (curPos < fileSize) while (curPos < fileSize)
{ {
blockStartPos = curPos; blockStartPos = curPos;
// Read header // Read header
curPos += ReadDWord(&blockLen); curPos += ReadDWord(&blockLen);
curPos += ReadDWord(&nameLen); curPos += ReadDWord(&nameLen);
curPos += ReadDWord(&commentLen); curPos += ReadDWord(&commentLen);
ReadString(strBuf,nameLen,1025); ReadString(strBuf,nameLen,1025);
// Is this the block we want? // Is this the block we want?
if (!strcmp(strBuf,name)) if (!strcmp(strBuf,name))
{ {
fseek(fp, blockStartPos+12+nameLen+commentLen, SEEK_SET); // move to beginning of data fseek(fp, blockStartPos+12+nameLen+commentLen, SEEK_SET); // move to beginning of data
dataStartPos = ftell(fp); dataStartPos = ftell(fp);
return OKAY; return OKAY;
} }
// Move to next block // Move to next block
fseek(fp, blockStartPos+blockLen, SEEK_SET); fseek(fp, blockStartPos+blockLen, SEEK_SET);
curPos = blockStartPos+blockLen; curPos = blockStartPos+blockLen;
if (blockLen == 0) // this would never advance if (blockLen == 0) // this would never advance
break; break;
} }
return FAIL; return FAIL;
} }
bool CBlockFile::Create(const char *file, const char *headerName, const char *comment) bool CBlockFile::Create(const char *file, const char *headerName, const char *comment)
{ {
fp = fopen(file, "wb"); fp = fopen(file, "wb");
if (NULL == fp) if (NULL == fp)
return FAIL; return FAIL;
mode = 'w'; mode = 'w';
WriteBlockHeader(headerName, comment); WriteBlockHeader(headerName, comment);
return OKAY; return OKAY;
} }
bool CBlockFile::Load(const char *file) bool CBlockFile::Load(const char *file)
{ {
fp = fopen(file, "rb"); fp = fopen(file, "rb");
if (NULL == fp) if (NULL == fp)
return FAIL; return FAIL;
mode = 'r'; mode = 'r';
// TODO: is this a valid block file? // TODO: is this a valid block file?
// Get the file size // Get the file size
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
fileSize = ftell(fp); fileSize = ftell(fp);
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
return OKAY; return OKAY;
} }
void CBlockFile::Close(void) void CBlockFile::Close(void)
{ {
if (fp != NULL) if (fp != NULL)
fclose(fp); fclose(fp);
fp = NULL; fp = NULL;
mode = 0; mode = 0;
} }
CBlockFile::CBlockFile(void) CBlockFile::CBlockFile(void)
{ {
fp = NULL; fp = NULL;
mode = 0; // neither reading nor writing (do nothing) mode = 0; // neither reading nor writing (do nothing)
} }
CBlockFile::~CBlockFile(void) CBlockFile::~CBlockFile(void)
{ {
if (fp != NULL) // in case user forgot if (fp != NULL) // in case user forgot
fclose(fp); fclose(fp);
fp = NULL; fp = NULL;
mode = 0; mode = 0;
} }

View file

@ -28,6 +28,7 @@
#ifndef INCLUDED_BLOCKFILE_H #ifndef INCLUDED_BLOCKFILE_H
#define INCLUDED_BLOCKFILE_H #define INCLUDED_BLOCKFILE_H
#include <cstdint>
/* /*
* CBlockFile: * CBlockFile:
@ -43,128 +44,128 @@
class CBlockFile class CBlockFile
{ {
public: public:
/* /*
* Read(data, numBytes): * Read(data, numBytes):
* *
* Reads data from the current file position. * Reads data from the current file position.
* *
* Parameters: * Parameters:
* data Buffer to read to. * data Buffer to read to.
* numBytes Number of bytes to read. * numBytes Number of bytes to read.
* *
* Returns: * Returns:
* Number of bytes read. If not the same as numBytes, an error * Number of bytes read. If not the same as numBytes, an error
* occurred. * occurred.
*/ */
unsigned Read(void *data, unsigned numBytes); unsigned Read(void *data, unsigned numBytes);
/* /*
* FindBlock(name): * FindBlock(name):
* *
* Scans the file for a block with the given name string. When it is found, * 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. * the file pointer is set to the beginning of the data region.
* *
* Parameters: * Parameters:
* name Name of block to locate. * name Name of block to locate.
* *
* Returns: * Returns:
* OKAY if found, FAIL if unable to locate. * OKAY if found, FAIL if unable to locate.
*/ */
bool FindBlock(const char *name); bool FindBlock(const char *name);
/* /*
* Write(data, numBytes): * Write(data, numBytes):
* *
* Outputs data at the current file pointer position. Updates the block * Outputs data at the current file pointer position. Updates the block
* header appropriately. * header appropriately.
* *
* Parameters: * Parameters:
* data Data to write. * data Data to write.
* numBytes Number of bytes to write. * numBytes Number of bytes to write.
*/ */
void Write(const void *data, unsigned numBytes); void Write(const void *data, unsigned numBytes);
/* /*
* NewBlock(name, comment): * NewBlock(name, comment):
* *
* Begins a new block. Writes the block header and sets the file pointer to * Begins a new block. Writes the block header and sets the file pointer to
* the beginning of its data area. * the beginning of its data area.
* *
* Parameters: * Parameters:
* name Block name. Must be unique and not NULL. * name Block name. Must be unique and not NULL.
* comment Comment string to embed in the block header. * comment Comment string to embed in the block header.
*/ */
void NewBlock(const char *title, const char *comment); void NewBlock(const char *title, const char *comment);
/* /*
* Create(file, headerName, comment): * Create(file, headerName, comment):
* *
* Opens a block file for writing and creates the header block. This * Opens a block file for writing and creates the header block. This
* function must be called before attempting to write data. Otherwise, all * function must be called before attempting to write data. Otherwise, all
* write commands will be silently ignored. Read commands will be ignored * write commands will be silently ignored. Read commands will be ignored
* and will always return 0's. * and will always return 0's.
* *
* Parameters: * Parameters:
* file File path. * file File path.
* headerName Block name for header. Must be unique and not NULL. * headerName Block name for header. Must be unique and not NULL.
* comment Comment string that will be embedded into file header. * comment Comment string that will be embedded into file header.
* *
* Returns: * Returns:
* OKAY if successfully opened, otherwise FAIL. * OKAY if successfully opened, otherwise FAIL.
*/ */
bool Create(const char *file, const char *headerName, const char *comment); bool Create(const char *file, const char *headerName, const char *comment);
/* /*
* Load(file): * Load(file):
* *
* Open a block file file for reading. * Open a block file file for reading.
* *
* Parameters: * Parameters:
* file File path. * file File path.
* *
* Returns: * Returns:
* OKAY if successfully opened and confirmed to be a valid Supermodel * OKAY if successfully opened and confirmed to be a valid Supermodel
* block file, otherwise FAIL. If the file could not be opened, all * block file, otherwise FAIL. If the file could not be opened, all
* subsequent operations will be silently ignored (reads will return * subsequent operations will be silently ignored (reads will return
* 0's). Write commands will be ignored. * 0's). Write commands will be ignored.
*/ */
bool Load(const char *file); bool Load(const char *file);
/* /*
* Close(void): * Close(void):
* *
* Closes the file. * Closes the file.
*/ */
void Close(void); void Close(void);
/* /*
* CBlockFile(void): * CBlockFile(void):
* ~CBlockFile(void): * ~CBlockFile(void):
* *
* Constructor and destructor. * Constructor and destructor.
*/ */
CBlockFile(void); CBlockFile(void);
~CBlockFile(void); ~CBlockFile(void);
private: private:
// Helper functions // Helper functions
void ReadString(char *str, unsigned strLen, unsigned maxLen); void ReadString(char *str, unsigned strLen, unsigned maxLen);
unsigned ReadBytes(void *data, unsigned numBytes); unsigned ReadBytes(void *data, unsigned numBytes);
unsigned ReadDWord(UINT32 *data); unsigned ReadDWord(uint32_t *data);
void UpdateBlockSize(void); void UpdateBlockSize(void);
void WriteByte(UINT8 data); void WriteByte(uint8_t data);
void WriteDWord(UINT32 data); void WriteDWord(uint32_t data);
void WriteBytes(const void *data, unsigned numBytes); void WriteBytes(const void *data, unsigned numBytes);
void WriteBlockHeader(const char *name, const char *comment); void WriteBlockHeader(const char *name, const char *comment);
// File state data // File state data
char strBuf[1026]; // buffers up to a 1024-character string, its terminator, and an extra terminator (just in case) char strBuf[1026]; // buffers up to a 1024-character string, its terminator, and an extra terminator (just in case)
FILE *fp; FILE *fp;
int mode; // 'r' for read, 'w' for write int mode; // 'r' for read, 'w' for write
long int fileSize; // size of file in bytes long int fileSize; // size of file in bytes
long int blockStartPos; // points to beginning of current block (or file) header long int blockStartPos; // points to beginning of current block (or file) header
long int dataStartPos; // points to beginning of current block's data section long int dataStartPos; // points to beginning of current block's data section
}; };
#endif // INCLUDED_BLOCKFILE_H #endif // INCLUDED_BLOCKFILE_H