Post: [C++] Stream Read/Write Class
07-25-2013, 11:07 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Hello guys! I wrote this class to make it easier to read/write to data in memory for various purposes. It's all in native and doesn't require any additional (or managed) headers. Pretty much cross platform, otherwise it's not hard to convert.

stream.h - You must login or register to view this content.

stream.cpp - You must login or register to view this content.

Include both stream.h and stream.cpp to your project. Then in the class you're working in.
Include the stream files with the following:
    #include "stream.h"


Then To Use the class with ease!
    
int main(int argc, char** args)
{
u64 fileLength = 0;
get_size( args[1], &fileLength );
u8* fileData = new u8[fileLength];
read_file( args[1], fileData, fileLength );

//Add Here
Stream s( fileData, fileLength, LittleEndian );
s.ReadInt32();
s.ReadInt16();
s.ReadInt64();
s.ReadCString();
s.Read( 1000 ); //read 1000 bytes
s.ReadByte();

s.writeInt32(3);
...
...
}


This class supports ALL data type just get the address with &. You can set any length, but it will overflow if length is over the actual allocated memory space. It supports both LittleEndian and BigEndian, supports reading and writing of int16, int32, int64, cstring, buffers, and a single byte.

I am writing a part soon where you can read a struct, or an easier way of doing this is by s.ReadBytes(sizeof( struct));


Thanks and let me know what you think!

Update:
Could do this for a faster/less allocated code
    
u8* Stream::ReadBytes(int count)
{
u64 lpos = this->position;
this->AdvPosition(count);
return &this->buffer[lpos];
}
Last edited by Jakes625 ; 07-27-2013 at 11:36 AM.

The following user thanked Jakes625 for this useful post:

OmGRhys-x
07-27-2013, 08:18 AM #2
OmGRhys-x
Are you high?
Originally posted by Jakes625 View Post
Hello guys! I wrote this class to make it easier to read/write to data in memory for various purposes. It's all in native and doesn't require any additional (or managed) headers. Pretty much cross platform, otherwise it's not hard to convert.

stream.h - You must login or register to view this content.

stream.cpp - You must login or register to view this content.

Include both stream.h and stream.cpp to your project. Then in the class you're working in.
Include the stream files with the following:
    #include "stream.h"


Then To Use the class with ease!
    
int main(int argc, char** args)
{
u64 fileLength = 0;
get_size( args[1], &fileLength );
u8* fileData = new u8[fileLength];
read_file( args[1], fileData, fileLength );

//Add Here
Stream s( fileData, fileLength, LittleEndian );
s.ReadInt32();
s.ReadInt16();
s.ReadInt64();
s.ReadCString();
s.Read( 1000 ); //read 1000 bytes
s.ReadByte();

s.writeInt32(3);
...
...
}


This class supports ALL data type just get the address with &. You can set any length, but it will overflow if length is over the actual allocated memory space. It supports both LittleEndian and BigEndian, supports reading and writing of int16, int32, int64, cstring, buffers, and a single byte.

I am writing a part soon where you can read a struct, or an easier way of doing this is by s.ReadBytes(sizeof( struct));


Thanks and let me know what you think!

Update:
Could do this for a faster/less allocated code
    
u8* Stream::ReadBytes(int count)
{
this->AdvPosition(count);
return &this->buffer[this->position];
}




Nice bb Smile

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo