1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
/***************************************************************************
basestreaminterface.h - description
-------------------
begin : Sat Oct 14 2000
copyright : (C) 2000 by Martin Bickel
email : bickel@asc-hq.org
***************************************************************************/
/*! \file basestreaminterface.h
\brief The standard interface for ASC streams.
This is the parent class for all kinds of streams:
normal files, compressed files, files inside containers, memory buffer, ...
*/
/***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef basestreaminterface_h_included
#define basestreaminterface_h_included
#include <bitset>
#include <SDL_stdinc.h>
#include "global.h"
#include "ascstring.h"
/** The interface for all kinds of IO stream. Because ASC was originally
written in Pascal, the streams don't have any relation to the C++ streams */
class tnstream {
public:
tnstream ( );
//! the modes in which a stream can operate
enum IOMode { uninitialized, reading, writing, appending };
/** writes data to the stream
\param buf pointer to the buffer
\param size the number of bytes which are going to be written
*/
virtual void writedata ( const void* buf, int size ) = 0 ;
/** Reads data from the stream.
\param buf the buffer which the data is written to
\param size the number of bytes which should be read
\param excpt If true the operation will throw an exception if the desired number of bytes
cannot be read.
\returns the number of bytes which were read.
*/
virtual int readdata ( void* buf, int size, bool excpt = true ) = 0 ;
/** Reads a string. All bytes of the files are stored in the allocated memory until either a 0 Byte
is read or the maximum number of character have been reached. The string will always be 0 terminated
in both cases.
\param pc pointer to an uninitialized pointer which will contain the string.
The memory will be allocated using new char[length of string] .
\param maxlength The maximum number of bytes that will be read. If zero, the maximum length
is not limited.
*/
virtual void readpchar( char** pc, int maxlength = 0) ;
/** Reads a string. All bytes of the files are stored in the allocated memory until either a 0 Byte
or a newline character is read or the maximum number of character have been reached.
The string will always be 0 terminated. Carriage return bytes will be filtered out.
\param pc pointer to an uninitialized pointer which will contain the string.
The memory will be allocated using new char[length of string] .
\param maxlength The maximum number of bytes that will be read. If zero, the maximum length
is not limited.
*/
virtual void readpnchar( char** pc, int maxlength = 0) ;
/** Reads a string. Carriage Return character will be filtered.
\param s The ASCString which will contain the data read.
\param includeCR if true, the reading will stop only a 0 Bytes or the end of stream,
so all newline character will be part of the text string. Useful for binary files.
If false reading will stop and 0 Bytes and newline characters. The newline character will not be part of the resulting string.
\returns true if there is remaining data after the string, false if the reading was ended because the end of the stream was reached.
*/
virtual bool readTextString ( ASCString& s, bool includeCR = false );
/** Reads and returns a string. Carriage Return character will be filtered. If the very first read
operation fails because the end of the stream was already reached, a readafterend exception will
be thrown. If some data could be read before the end was reached, this data will be returned.
\param includeCR if true, the reading will stop only at 0 Bytes or the end of stream,
so all newline character will be part of the text string. Useful for binary files.
If false reading will stop at 0 Bytes and newline characters. The newline character will not be part of the resulting string.
*/
virtual ASCString readString ( bool includeCR = false );
//! write the C-String pc to the stream
virtual void writepchar( const char* pc) ;
/** writes the C++ String pc to the stream.
\param binary If true, all bytes (including 0 bytes) of the string will be written to the stream.
Note that a string containing one or more 0 bytes cannot be read with a single stream reading operation.
If false, writing will stop at the first 0 character of the string (or its end of course)
*/
virtual void writeString( const string& pc, bool binary = true ) ;
//! Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is performed, if necessary.
virtual int readInt ( void );
//! Reads a 16 bit unsigned Integer. In the stream little-endian byte order is used and a translation is performed, if necessary.
virtual int readWord ( void );
//! Reads a 8 bit unsigned Integer.
virtual Uint8 readUint8 ( void );
//! Read a 8 bit character
virtual char readCharacter ( void );
template<size_t N>
void writeBitset( const std::bitset<N>& bs ) {
writeInt(1);
writeInt(N);
for( int i = 0; i < N;++i)
writeUint8( bs.test(i));
}
template<size_t N>
void readBitset( std::bitset<N>& bs ) {
readInt(); // version
int n = readInt();
assert( n == N );
bs.reset();
for( int i = 0; i < N;++i)
if ( readUint8() )
bs.set( i );
}
//! Reads a flaot variable.
virtual float readFloat ( void );
//! Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is performed, if necessary.
virtual void writeInt ( int i );
virtual void writeInt ( unsigned int i );
void writeInt ( bool b );
#ifdef SIZE_T_not_identical_to_INT
//! Writes a size_t as a 32 bit signed Integer. If the value is larger, an error is issued.
void writeInt ( size_t i );
#endif
//! Writes a 16 bit unsigned Integer. In the stream little-endian byte order is used and a translation is performed, if necessary.
virtual void writeWord ( int w );
//! Writes a 8 bit unsigned Integer.
virtual void writeUint8 ( Uint8 c );
//! Writes a 8 bit character
virtual void writeCharacter ( char c );
//! Write a floating point variable
virtual void writeFloat ( float f );
//! Writes an image to the stream and compresses it using RLE
virtual void writerlepict ( const void* buf );
//! Writes an image to the stream
virtual void writeImage ( const void* buf, bool compress = true );
/** Writes an RLE compressed or uncompressed image from the stream.
\param pnter Pointer to a void* , which will contain the new image.
\param allocated If false, a sufficient amount of memory will be allocated for the image.
If true it is assumed that a sufficiently large amount of memory has already been allocated.
\param size Will contain the size of the image. Must not be NULL.
*/
virtual void readrlepict( void** pnter, bool allocated, int* size);
//! returns the name of the stream.
virtual ASCString getDeviceName();
/** returns the location of the stream. This may be a complete english sentence describing how the stream (usually a file) can be found.
Should only be used for informing the user. */
virtual ASCString getLocation();
//! returns the archive name if the stream is located inside an archive; or "" if not.
virtual ASCString getArchive();
//! Sets the stream pointer to a new location. An exception is thrown if the stream does not support seeking.
virtual void seek ( int newpos );
//! returns the size of the stream or -1 if the stream does not have a size
virtual int getSize ( void ) { return -1; };
virtual ~tnstream() {};
protected:
ASCString devicename;
};
#endif // basestreaminterface_h_included
|