2011-04-24 01:14:00 +00:00
|
|
|
/**
|
|
|
|
** Supermodel
|
|
|
|
** A Sega Model 3 Arcade Emulator.
|
2011-09-14 19:08:43 +00:00
|
|
|
** Copyright 2011 Bart Trzynadlowski, Nik Henson
|
2011-04-24 01:14:00 +00:00
|
|
|
**
|
|
|
|
** This file is part of Supermodel.
|
|
|
|
**
|
|
|
|
** Supermodel is free software: you can redistribute it and/or modify it under
|
|
|
|
** the terms of the GNU General Public License as published by the Free
|
|
|
|
** Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
** any later version.
|
|
|
|
**
|
|
|
|
** Supermodel is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
** more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License along
|
|
|
|
** with Supermodel. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BlockFile.h
|
|
|
|
*
|
|
|
|
* Header file for block format container file management.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_BLOCKFILE_H
|
|
|
|
#define INCLUDED_BLOCKFILE_H
|
|
|
|
|
2016-04-02 21:32:28 +00:00
|
|
|
#include <cstdint>
|
2017-03-28 22:28:46 +00:00
|
|
|
#include <string>
|
2011-04-24 01:14:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CBlockFile:
|
|
|
|
*
|
|
|
|
* Block format container file. The file format is a series of consecutive,
|
|
|
|
* variable-length blocks referenced by unique name strings.
|
|
|
|
*
|
|
|
|
* All strings (comments and names) will be truncated to 1024 bytes, not
|
|
|
|
* including the null terminator.
|
|
|
|
*
|
|
|
|
* Members do not generate any output messages.
|
|
|
|
*/
|
|
|
|
class CBlockFile
|
|
|
|
{
|
|
|
|
public:
|
2016-04-02 21:32:28 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-03-27 03:19:15 +00:00
|
|
|
unsigned Read(void *data, uint32_t numBytes);
|
2016-04-02 21:32:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-03-27 03:19:15 +00:00
|
|
|
bool FindBlock(const std::string &name);
|
2016-04-02 21:32:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-03-27 03:19:15 +00:00
|
|
|
void Write(const void *data, uint32_t numBytes);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write(str):
|
|
|
|
*
|
|
|
|
* Outputs string (including null terminator) at the current file pointer
|
|
|
|
* position. Updates the block header appropriately.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* str String to write.
|
|
|
|
*/
|
|
|
|
void Write(const std::string &str);
|
2011-04-24 01:14:00 +00:00
|
|
|
|
2016-04-02 21:32:28 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-03-27 03:19:15 +00:00
|
|
|
void NewBlock(const std::string &title, const std::string &comment);
|
2011-04-24 01:14:00 +00:00
|
|
|
|
2016-04-02 21:32:28 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-03-27 03:19:15 +00:00
|
|
|
bool Create(const std::string &file, const std::string &headerName, const std::string &comment);
|
2011-04-24 01:14:00 +00:00
|
|
|
|
2016-04-02 21:32:28 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-03-27 03:19:15 +00:00
|
|
|
bool Load(const std::string &file);
|
2011-04-24 01:14:00 +00:00
|
|
|
|
2016-04-02 21:32:28 +00:00
|
|
|
/*
|
|
|
|
* Close(void):
|
|
|
|
*
|
|
|
|
* Closes the file.
|
|
|
|
*/
|
|
|
|
void Close(void);
|
2011-04-24 01:14:00 +00:00
|
|
|
|
2016-04-02 21:32:28 +00:00
|
|
|
/*
|
|
|
|
* CBlockFile(void):
|
|
|
|
* ~CBlockFile(void):
|
|
|
|
*
|
|
|
|
* Constructor and destructor.
|
|
|
|
*/
|
|
|
|
CBlockFile(void);
|
|
|
|
~CBlockFile(void);
|
2011-04-24 01:14:00 +00:00
|
|
|
|
|
|
|
private:
|
2016-04-02 21:32:28 +00:00
|
|
|
// Helper functions
|
2017-03-27 03:19:15 +00:00
|
|
|
void ReadString(std::string *str, uint32_t length);
|
|
|
|
unsigned ReadBytes(void *data, uint32_t numBytes);
|
2016-04-02 21:32:28 +00:00
|
|
|
unsigned ReadDWord(uint32_t *data);
|
|
|
|
void UpdateBlockSize(void);
|
|
|
|
void WriteByte(uint8_t data);
|
|
|
|
void WriteDWord(uint32_t data);
|
2017-03-27 03:19:15 +00:00
|
|
|
void WriteBytes(const void *data, uint32_t numBytes);
|
|
|
|
void WriteBlockHeader(const std::string &name, const std::string &comment);
|
2011-04-24 01:14:00 +00:00
|
|
|
|
2016-04-02 21:32:28 +00:00
|
|
|
// File state data
|
|
|
|
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
|
2011-04-24 01:14:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-04-02 21:32:28 +00:00
|
|
|
#endif // INCLUDED_BLOCKFILE_H
|