Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class StringByteStream
- {
- private string output;
- private uint position;
- StringByteStream()
- {
- output = "";
- position = 0;
- }
- StringByteStream(string stream)
- {
- output = stream;
- position = 0;
- }
- StringByteStream(string stream, uint head)
- {
- output = stream;
- position = head;
- }
- ///////////////////////////////
- //+ Append
- ///////////////////////////////
- void append(uint8 data)
- {
- output += data;
- }
- void append(string data)
- {
- append( uint32(data.length()) );
- output += data;
- }
- void append(int16 data)
- {
- //Get writing position & create buffer string
- uint pos = output.length();
- string vals = "12";
- //Write into the buffer
- vals[0] = ( (data ) & 255); vals[1] = ( (data>>8 ) & 255);
- //Append the buffer
- output += vals;
- }
- void append(uint16 data)
- {
- //Get writing position & create buffer string
- uint pos = output.length();
- string vals = "12";
- //Write into the buffer
- vals[0] = ( (data ) & 255); vals[1] = ( (data>>8 ) & 255);
- //Append the buffer
- output += vals;
- }
- void append(int32 data)
- {
- //Get writing position & create buffer string
- uint pos = output.length();
- string vals = "1234";
- //Write into the buffer
- vals[0] = ( (data ) & 255); vals[1] = ( (data>>8 ) & 255);
- vals[2] = ( (data>>16) & 255); vals[3] = ( (data>>24) & 255);
- //Append the buffer
- output += vals;
- }
- void append(uint32 data)
- {
- //Get writing position & create buffer string
- uint pos = output.length();
- string vals = "1234";
- //Write into the buffer
- vals[0] = ( (data ) & 255); vals[1] = ( (data>>8 ) & 255);
- vals[2] = ( (data>>16) & 255); vals[3] = ( (data>>24) & 255);
- //Append the buffer
- output += vals;
- }
- void append(uint64 data)
- {
- //Get writing position & create buffer string
- uint pos = output.length();
- string vals = "12345678";
- //Write into the buffer
- vals[0] = ( (data ) & 255); vals[1] = ( (data>>8 ) & 255);
- vals[2] = ( (data>>16) & 255); vals[3] = ( (data>>24) & 255);
- vals[4] = ( (data>>32) & 255); vals[5] = ( (data>>40) & 255);
- vals[6] = ( (data>>48) & 255); vals[7] = ( (data>>56) & 255);
- //Append the buffer
- output += vals;
- }
- void append(int64 data)
- {
- //Get writing position & create buffer string
- uint pos = output.length();
- string vals = "12345678";
- //Write into the buffer
- vals[0] = ( (data ) & 255); vals[1] = ( (data>>8 ) & 255);
- vals[2] = ( (data>>16) & 255); vals[3] = ( (data>>24) & 255);
- vals[4] = ( (data>>32) & 255); vals[5] = ( (data>>40) & 255);
- vals[6] = ( (data>>48) & 255); vals[7] = ( (data>>56) & 255);
- //Append the buffer
- output += vals;
- }
- void append(float data)
- {
- int32 num = int32(data);
- int32 dec = int32(data * 1000000.0f) - num * 1000000; //Max digits is 6 for float.
- AddDebugMessage("F: " + dec,false);
- append(num); append(int32(dec));
- }
- void append(double data)
- {
- int64 num = int64(data);
- int64 dec = int64(data * 1000000000.0) - num * 1000000000; //Max digits is 9
- append(num); append(int64(dec));
- }
- ///////////////////////////////
- //- Append
- ///////////////////////////////
- ///////////////////////////////
- //+ Write
- ///////////////////////////////
- //Makes sure there is <size> positions availible forward. If not, this function creates them.
- private void forwardAppend(uint size)
- {
- //Resize string if required
- int diff = (size + position - output.length());
- if(diff > 0) output.resize(output.length() + diff);
- /* AddDebugMessage("Len: "+output.length(),false); */
- }
- void write(uint8 data)
- {
- //Handle position at fringe of output / out of bounds
- if(position > output.length())
- {
- AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
- return;
- }
- else if(position == output.length()) output += "#";
- //Write the data & move position forward
- output[position] = data;
- position +=1;
- }
- void write(string data)
- {
- //OOB Test
- if(position > output.length())
- {
- AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
- return;
- }
- //Write string length
- uint strlength = data.length();
- write( uint32(strlength) );
- //Make sure there is enough room for char writing
- forwardAppend(strlength);
- //Write in the string data, moving position forward.
- for(uint i =0; i<data.length(); i++) { output[position] = data[i]; position++; }
- }
- void write(int16 data)
- {
- //OOB Test
- if(position > output.length())
- {
- AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
- return;
- }
- //Resize if required
- forwardAppend(2);
- //Write into the buffer
- output[position ]=(data & 255); output[position + 1]=((data>>8 ) & 255);
- position += 2;
- }
- void write(uint16 data)
- {
- //OOB Test
- if(position > output.length())
- {
- AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
- return;
- }
- //Resize if required
- forwardAppend(2);
- //Write into the buffer
- output[position]=(data & 255); output[position + 1]=((data>>8 ) & 255);
- position += 2;
- }
- void write(int32 data)
- {
- //OOB Test
- if(position > output.length())
- {
- AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
- return;
- }
- //Resize if required
- forwardAppend(4);
- //Write into the buffer
- output[position ]=((data ) & 255); output[position + 1]=((data>>8 ) & 255);
- output[position + 2]=((data>>16) & 255); output[position + 3]=((data>>24) & 255);
- position += 4;
- }
- void write(uint32 data)
- {
- //OOB Test
- if(position > output.length())
- {
- AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
- return;
- }
- //Resize if required
- forwardAppend(4);
- //Write into the buffer
- output[position ]=((data ) & 255); output[position + 1]=((data>>8 ) & 255);
- output[position + 2]=((data>>16) & 255); output[position + 3]=((data>>24) & 255);
- position += 4;
- }
- void write(int64 data)
- {
- //OOB Test
- if(position > output.length())
- {
- AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
- return;
- }
- //Resize if required
- forwardAppend(8);
- //Write into the buffer
- output[position ]=((data ) & 255); output[position + 1]=((data>>8 ) & 255);
- output[position + 2]=((data>>16) & 255); output[position + 3]=((data>>24) & 255);
- output[position + 4]=((data>>32) & 255); output[position + 5]=((data>>40) & 255);
- output[position + 6]=((data>>48) & 255); output[position + 7]=((data>>56) & 255);
- position += 8;
- }
- void write(uint64 data)
- {
- //OOB Test
- if(position > output.length())
- {
- AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
- return;
- }
- //Resize if required
- forwardAppend(8);
- //Write into the buffer
- output[position ]=((data ) & 255); output[position + 1]=((data>>8 ) & 255);
- output[position + 2]=((data>>16) & 255); output[position + 3]=((data>>24) & 255);
- output[position + 4]=((data>>32) & 255); output[position + 5]=((data>>40) & 255);
- output[position + 6]=((data>>48) & 255); output[position + 7]=((data>>56) & 255);
- position += 8;
- }
- void write(float data)
- {
- int32 num = int32(data);
- int32 dec = int32(data * 1000000.0f) - num * 1000000; //Max digits is 6 for float.
- write(num); write(int32(dec));
- }
- void write(double data)
- {
- int64 num = int64(data);
- int64 dec = int64(data * 1000000000.0) - num * 1000000000; //Max digits supported is 9
- write(num); write(int64(dec));
- }
- ///////////////////////////////
- //- Write
- ///////////////////////////////
- ///////////////////////////////
- //+ Read
- ///////////////////////////////
- uint8 readByte()
- {
- //OOB Test
- if(position >= output.length()) { AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false); return 0; }
- uint8 byte = output[position]; position++;
- return byte;
- }
- uint8[] readBytes(uint size)
- {
- //OOB Test
- uint8[] bytes = {};
- if( (position+size) > output.length()) { AddDebugMessage("Position out of bounds: " + (size + position) + " / " + output.length(),false); return bytes; }
- bytes.resize(size);
- for(uint i =0; i<size; i++) { bytes[i]=output[position]; position++; }
- return bytes;
- }
- uint8[] readToEnd()
- {
- uint8[] bytes = {};
- if(position >= output.length()) { AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false); return bytes; }
- bytes.resize(output.length()-position);
- for(uint i =0; position<bytes.length(); i++) { bytes[i]=output[position]; position++; }
- return bytes;
- }
- string readString()
- {
- //Read string length
- uint len = readUInt32();
- if(len==0) return "";
- //Check string length is valid. If not return empty string
- if( (position+len) > output.length()) { AddDebugMessage("String length invalid. End @ " + (len + position) + " / " + output.length(),false); return ""; }
- //Allocate string & read in data
- string data = ""; data.resize(len);
- for(uint i =0; i<data.length(); i++) { data[i] = output[position]; position++; }
- return data;
- }
- int16 readInt16()
- {
- uint8[] bytes = readBytes(2);
- if(bytes.length() == 2)
- return (int16(bytes[0]) ) + (int16(bytes[1])<<8 );
- else return 0;
- }
- int32 readInt32()
- {
- uint8[] bytes = readBytes(4);
- if(bytes.length() == 4)
- return (int32(bytes[0]) ) + (int32(bytes[1])<<8 ) + (int32(bytes[2])<<16) + (int32(bytes[3])<<24);
- else return 0;
- }
- int64 readInt64()
- {
- uint8[] bytes = readBytes(8);
- if(bytes.length() == 8)
- return (int32(bytes[0]) ) + (int32(bytes[1])<<8 ) + (int32(bytes[2])<<16) + (int32(bytes[3])<<24) +
- (int32(bytes[4])<<32) + (int32(bytes[5])<<40) + (int32(bytes[6])<<48) + (int32(bytes[7])<<56);
- else return 0;
- }
- uint16 readUInt16()
- {
- uint8[] bytes = readBytes(2);
- if(bytes.length() == 2)
- return (uint16(bytes[0]) ) + (uint16(bytes[1])<<8 );
- else return 0;
- }
- uint32 readUInt32()
- {
- uint8[] bytes = readBytes(4);
- if(bytes.length() == 4)
- return (uint32(bytes[0]) ) + (uint32(bytes[1])<<8 ) + (uint32(bytes[2])<<16) + (uint32(bytes[3])<<24);
- else return 0;
- }
- uint64 readUInt64()
- {
- uint8[] bytes = readBytes(8);
- if(bytes.length() == 8)
- return (uint32(bytes[0]) ) + (uint32(bytes[1])<<8 ) + (uint32(bytes[2])<<16) + (uint32(bytes[3])<<24) +
- (uint32(bytes[4])<<32) + (uint32(bytes[5])<<40) + (uint32(bytes[6])<<48) + (uint32(bytes[7])<<56);
- else return 0;
- }
- float readFloat()
- {
- float x = readInt32();
- return x + float(readInt32()) * 0.0000001f;
- }
- double readDouble()
- {
- double x = readInt64();
- return x + double(readInt64()) * 0.0000000001;
- }
- ///////////////////////////////
- //- Read
- ///////////////////////////////
- ///////////////////////////////
- //+ Stream Control
- ///////////////////////////////
- uint getPosition() { return position; }
- bool setPosition(uint newPosition)
- {
- if(newPosition > output.length()) return false;
- position = newPosition;
- return true;
- }
- bool endOfStream() { return position == output.length(); }
- string getStream() { return output; }
- uint getStreamLength() { return output.length(); }
- void setStreamSize(uint size) { output.resize(size); }
- void setStream(string stream) { output = stream; }
- ///////////////////////////////
- //- Stream Control
- ///////////////////////////////
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement