2012-06-07 17:19:29 +00:00
//
// KRDataBlock.h
// KREngine
//
2012-09-11 03:06:35 +00:00
// Copyright 2012 Kearwood Gilbert. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
2012-06-07 17:19:29 +00:00
//
# ifndef KREngine_KRDataBlock_h
# define KREngine_KRDataBlock_h
class KRDataBlock {
public :
KRDataBlock ( ) ;
~ KRDataBlock ( ) ;
2012-09-11 03:06:35 +00:00
// Encapsulate a pointer. Note - The pointer will not be free'ed
bool load ( void * data , size_t size ) ;
// Load a file into memory using mmap. The data pointer will be protected as read-only until append() or expand() is called
bool load ( const std : : string & path ) ;
// Save the data to a file, and switch to read-only mode. The data pointer will be replaced with a mmap'ed address of the file; the malloc'ed data will be freed
bool save ( const std : : string & path ) ;
2012-06-07 17:19:29 +00:00
2012-09-11 03:06:35 +00:00
// Append data to the end of the block, increasing the size of the block and making it read-write.
void append ( void * data , size_t size ) ;
2012-12-20 01:23:57 +00:00
// Append data to the end of the block, increasing the size of the block and making it read-write.
void append ( KRDataBlock & data ) ;
// Append string to the end of the block, increasing the size of the block and making it read-write. The null terminating character is included
void append ( const std : : string & s ) ;
2012-12-07 00:20:06 +00:00
// Expand or shrink the data block, and switch it to read-write mode. Note - this may result in a mmap'ed file being copied to malloc'ed ram and then closed
2012-09-11 03:06:35 +00:00
void expand ( size_t size ) ;
// Unload a file, releasing any mmap'ed file handles or malloc'ed ram that was in use
void unload ( ) ;
// Return a pointer to the start of the data block
void * getStart ( ) ;
// Return a pointer to the one byte after the end of the data block
void * getEnd ( ) ;
// Return the size of the data block. Use append() or expand() to make the data block larger
size_t getSize ( ) const ;
2013-01-04 20:31:39 +00:00
// Get the contents as a string
std : : string getString ( ) ;
2012-06-07 17:19:29 +00:00
private :
void * m_data ;
2012-09-11 03:06:35 +00:00
size_t m_data_size ;
// For memory mapped objects:
int m_fdPackFile ;
// For malloc'ed objects:
bool m_bMalloced ;
2012-06-07 17:19:29 +00:00
} ;
# endif