61 int32 numBytesRead = 0;
62 stream->
read (buffer, (int32)size, &numBytesRead);
69 int32 numBytesWritten = 0;
70 stream->
write ((
void*)buffer, (int32)size, &numBytesWritten);
71 return numBytesWritten;
78 stream->
seek (pos, mode, &result);
93FStreamSizeHolder::FStreamSizeHolder (
FStreamer &s)
94: stream (s), sizePos (-1)
100 sizePos = stream.
tell ();
101 stream.writeInt32 (0L);
110 int64 currentPos = stream.
tell ();
112 stream.
seek (sizePos, kSeekSet);
113 int32 size = int32 (currentPos - sizePos -
sizeof (int32));
114 stream.writeInt32 (size);
116 stream.
seek (currentPos, kSeekSet);
123 sizePos = stream.
tell ();
125 stream.readInt32 (size);
126 sizePos += size +
sizeof (int32);
134 stream.
seek (sizePos, kSeekSet);
140FStreamer::FStreamer (int16 _byteOrder)
141: byteOrder (_byteOrder)
146bool FStreamer::writeChar8 (char8 c)
148 return writeRaw ((
void*)&c,
sizeof (char8)) ==
sizeof (char8);
152bool FStreamer::readChar8 (char8& c)
154 return readRaw ((
void*)&c,
sizeof (char8)) ==
sizeof (char8);
158bool FStreamer::writeUChar8 (
unsigned char c)
160 return writeRaw ((
void*)&c,
sizeof (
unsigned char)) ==
sizeof (
unsigned char);
164bool FStreamer::readUChar8 (
unsigned char& c)
166 return readRaw ((
void*)&c,
sizeof (
unsigned char)) ==
sizeof (
unsigned char);
170bool FStreamer::writeChar16 (char16 c)
172 if (BYTEORDER != byteOrder)
174 return writeRaw ((
void*)&c,
sizeof (char16)) ==
sizeof (char16);
178bool FStreamer::readChar16 (char16& c)
180 if (
readRaw ((
void*)&c,
sizeof (char16)) ==
sizeof (char16))
182 if (BYTEORDER != byteOrder)
191bool FStreamer::writeInt8 (int8 c)
193 return writeRaw ((
void*)&c,
sizeof (int8)) ==
sizeof (int8);
197bool FStreamer::readInt8 (int8& c)
199 return readRaw ((
void*)&c,
sizeof (int8)) ==
sizeof (int8);
203bool FStreamer::writeInt8u (uint8 c)
205 return writeRaw ((
void*)&c,
sizeof (uint8)) ==
sizeof (uint8);
209bool FStreamer::readInt8u (uint8& c)
211 return readRaw ((
void*)&c,
sizeof (uint8)) ==
sizeof (uint8);
216bool FStreamer::writeInt16 (int16 i)
218 if (BYTEORDER != byteOrder)
220 return writeRaw ((
void*)&i,
sizeof (int16)) ==
sizeof (int16);
224bool FStreamer::readInt16 (int16& i)
226 if (
readRaw ((
void*)&i,
sizeof (int16)) ==
sizeof (int16))
228 if (BYTEORDER != byteOrder)
237bool FStreamer::writeInt16Array (
const int16* array, int32 count)
239 for (int32 i = 0; i <
count; i++)
241 if (!writeInt16 (array[i]))
248bool FStreamer::readInt16Array (int16* array, int32 count)
250 for (int32 i = 0; i <
count; i++)
252 if (!readInt16 (array[i]))
259bool FStreamer::writeInt16u (uint16 i)
261 if (BYTEORDER != byteOrder)
263 return writeRaw ((
void*)&i,
sizeof (uint16)) ==
sizeof (uint16);
267bool FStreamer::readInt16u (uint16& i)
269 if (
readRaw ((
void*)&i,
sizeof (uint16)) ==
sizeof (uint16))
271 if (BYTEORDER != byteOrder)
280bool FStreamer::writeInt16uArray (
const uint16* array, int32 count)
282 for (int32 i = 0; i <
count; i++)
284 if (!writeInt16u (array[i]))
291bool FStreamer::readInt16uArray (uint16* array, int32 count)
293 for (int32 i = 0; i <
count; i++)
295 if (!readInt16u (array[i]))
303bool FStreamer::writeInt32 (int32 i)
305 if (BYTEORDER != byteOrder)
307 return writeRaw ((
void*)&i,
sizeof (int32)) ==
sizeof (int32);
311bool FStreamer::readInt32 (int32& i)
313 if (
readRaw ((
void*)&i,
sizeof (int32)) ==
sizeof (int32))
315 if (BYTEORDER != byteOrder)
324bool FStreamer::writeInt32Array (
const int32* array, int32 count)
326 for (int32 i = 0; i <
count; i++)
328 if (!writeInt32 (array[i]))
335bool FStreamer::readInt32Array (int32* array, int32 count)
337 for (int32 i = 0; i <
count; i++)
339 if (!readInt32 (array[i]))
346bool FStreamer::writeInt32u (uint32 i)
348 if (BYTEORDER != byteOrder)
350 return writeRaw ((
void*)&i,
sizeof (uint32)) ==
sizeof (uint32);
354bool FStreamer::readInt32u (uint32& i)
356 if (
readRaw ((
void*)&i,
sizeof (uint32)) ==
sizeof (uint32))
358 if (BYTEORDER != byteOrder)
367bool FStreamer::writeInt32uArray (
const uint32* array, int32 count)
369 for (int32 i = 0; i <
count; i++)
371 if (!writeInt32u (array[i]))
378bool FStreamer::readInt32uArray (uint32* array, int32 count)
380 for (int32 i = 0; i <
count; i++)
382 if (!readInt32u (array[i]))
390bool FStreamer::writeInt64 (int64 i)
392 if (BYTEORDER != byteOrder)
394 return writeRaw ((
void*)&i,
sizeof (int64)) ==
sizeof (int64);
398bool FStreamer::readInt64 (int64& i)
400 if (
readRaw ((
void*)&i,
sizeof (int64)) ==
sizeof (int64))
402 if (BYTEORDER != byteOrder)
411bool FStreamer::writeInt64Array (
const int64* array, int32 count)
413 for (int32 i = 0; i <
count; i++)
415 if (!writeInt64 (array[i]))
422bool FStreamer::readInt64Array (int64* array, int32 count)
424 for (int32 i = 0; i <
count; i++)
426 if (!readInt64 (array[i]))
433bool FStreamer::writeInt64u (uint64 i)
435 if (BYTEORDER != byteOrder)
437 return writeRaw ((
void*)&i,
sizeof (uint64)) ==
sizeof (uint64);
441bool FStreamer::readInt64u (uint64& i)
443 if (
readRaw ((
void*)&i,
sizeof (uint64)) ==
sizeof (uint64))
445 if (BYTEORDER != byteOrder)
454bool FStreamer::writeInt64uArray (
const uint64* array, int32 count)
456 for (int32 i = 0; i <
count; i++)
458 if (!writeInt64u (array[i]))
465bool FStreamer::readInt64uArray (uint64* array, int32 count)
467 for (int32 i = 0; i <
count; i++)
469 if (!readInt64u (array[i]))
477bool FStreamer::writeFloat (
float f)
479 if (BYTEORDER != byteOrder)
481 return writeRaw ((
void*)&f,
sizeof (
float)) ==
sizeof (
float);
485bool FStreamer::readFloat (
float& f)
487 if (
readRaw ((
void*)&f,
sizeof (
float)) ==
sizeof (
float))
489 if (BYTEORDER != byteOrder)
498bool FStreamer::writeFloatArray (
const float* array, int32 count)
500 for (int32 i = 0; i <
count; i++)
502 if (!writeFloat (array[i]))
509bool FStreamer::readFloatArray (
float* array, int32 count)
511 for (int32 i = 0; i <
count; i++)
513 if (!readFloat (array[i]))
520bool FStreamer::writeDouble (
double d)
522 if (BYTEORDER != byteOrder)
528bool FStreamer::readDouble (
double& d)
530 if (
readRaw ((
void*)&d,
sizeof (
double)) ==
sizeof (
double))
532 if (BYTEORDER != byteOrder)
541bool FStreamer::writeDoubleArray (
const double* array, int32 count)
543 for (int32 i = 0; i <
count; i++)
545 if (!writeDouble (array[i]))
552bool FStreamer::readDoubleArray (
double* array, int32 count)
554 for (int32 i = 0; i <
count; i++)
556 if (!readDouble (array[i]))
566 bool res = readInt16 (v);
574 return writeInt16 ((int16)b);
580 TSize size =
strlen (ptr);
590 if (size < 1 || ptr ==
nullptr)
597 if (
readRaw ((
void*)&c,
sizeof (
char)) !=
sizeof (
char))
600 if (c ==
'\n' || c ==
'\0')
607 if (i > 0 && ptr[i - 1] ==
'\r')
632 if (
writeRaw (kBomUtf8, kBomUtf8Length) != kBomUtf8Length)
635 TSize size = str.
length () + 1;
654 if (
readRaw ((
void*)&c,
sizeof (
char)) !=
sizeof (
char))
661 char8* source = tmp.
str8 ();
665 if (
memcmp (source, kBomUtf8, kBomUtf8Length) == 0)
697 int32 length = (s) ? (int32)
strlen (s) + 1 : 0;
698 if (!writeInt32 (length))
702 return writeRaw (s,
sizeof (char8) * length) ==
static_cast<TSize
>(
sizeof (char8) * length);
710 return sizeof (int32) + (int32)
strlen (s) + 1;
717 if (!readInt32 (length))
724 char8* s = (length > 0) ? NEWVEC char8[length] :
nullptr;
726 readRaw (s, length *
sizeof (char8));
731bool FStreamer::skip (uint32 bytes)
736 if (readInt8 (tmp) ==
false)
743bool FStreamer::pad (uint32 bytes)
747 if (writeInt8 (0) ==
false)
bool put(uint8)
append value at end, grows Buffer if necessary
void setDelta(uint32 d)
define the block size by which the Buffer grows, see grow()
uint32 getFillSize() const
char16 * wcharPtr() const
conversion
char8 * str8() const
conversion
bool isAsciiString() const
Checks if all characters in string are in ascii range.
static int32 wideStringToMultiByte(char8 *dest, const char16 *source, int32 char8Count, uint32 destCodePage=kCP_Default)
If dest is zero, this returns the maximum number of bytes needed to convert source.
static int32 multiByteToWideString(char16 *dest, const char8 *source, int32 wcharCount, uint32 sourceCodePage=kCP_Default)
If dest is zero, this returns the maximum number of bytes needed to convert source.
virtual int32 length() const
Return length of string.
void beginWrite()
remembers position and writes 0
void endRead()
jump to end of chunk
int32 endWrite()
writes and returns size (since the start marker)
int32 beginRead()
returns size
Byteorder-aware base class for typed stream i/o.
char8 * readStr8()
read a string length and string text (The return string must be deleted when use is finished)
virtual TSize writeRaw(const void *, TSize)=0
Write one buffer of size.
TSize writeString8(const char8 *ptr, bool terminate=false)
a direct output function writing only one string (ascii 8bit)
int32 readStringUtf8(tchar *ptr, int32 maxSize)
read a UTF8 string
virtual int64 seek(int64, FSeekMode)=0
Set file position for stream.
TSize readString8(char8 *ptr, TSize size)
a direct input function reading only one string (ascii) (ended by a or \0 or eof)
virtual int64 tell()=0
Return current file position.
static int32 getStr8Size(const char8 *ptr)
returns the size of a saved string
bool writeStringUtf8(const tchar *ptr)
always terminated, converts to utf8 if non ascii characters are in string
bool writeBool(bool)
Write one boolean.
virtual TSize readRaw(void *, TSize)=0
Read one buffer of size.
bool writeStr8(const char8 *ptr)
write a string length (strlen) and string itself
bool readBool(bool &)
Read one bool.
virtual tresult PLUGIN_API tell(int64 *pos)=0
Gets current stream read-write position.
virtual tresult PLUGIN_API read(void *buffer, int32 numBytes, int32 *numBytesRead=nullptr)=0
Reads binary data from stream.
virtual tresult PLUGIN_API seek(int64 pos, int32 mode, int64 *result=nullptr)=0
Sets stream read-write position.
virtual tresult PLUGIN_API write(void *buffer, int32 numBytes, int32 *numBytesWritten=nullptr)=0
Writes binary data to stream.
int64 tell() SMTG_OVERRIDE
Return current file position.
TSize writeRaw(const void *, TSize) SMTG_OVERRIDE
Write one buffer of size.
TSize readRaw(void *, TSize) SMTG_OVERRIDE
Read one buffer of size.
int64 seek(int64, FSeekMode) SMTG_OVERRIDE
Set file position for stream.
IBStreamer(IBStream *stream, int16 byteOrder=BYTEORDER)
Constructor for a given IBSTream and a byteOrder.
const char8 * text8() const SMTG_OVERRIDE
Returns pointer to string of type char8.
@ kCP_Default
Default ANSI codepage.
#define SWAP_32(l)
Byte-order Conversion Macros.