Advertisement
Apjjm

ATDD Scripting: R/W

Sep 12th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.57 KB | None | 0 0
  1. class StringByteStream
  2.  {
  3.     private string output;
  4.     private uint position;
  5.    
  6.     StringByteStream()
  7.     {
  8.         output = "";
  9.         position = 0;
  10.     }
  11.    
  12.     StringByteStream(string stream)
  13.     {
  14.         output = stream;
  15.         position = 0;
  16.     }
  17.    
  18.     StringByteStream(string stream, uint head)
  19.     {
  20.         output = stream;
  21.         position = head;
  22.     }
  23.    
  24. ///////////////////////////////
  25. //+ Append
  26. ///////////////////////////////
  27.     void append(uint8 data)
  28.     {
  29.         output += data;
  30.     }
  31.    
  32.     void append(string data)
  33.     {
  34.         append( uint32(data.length()) );
  35.         output += data;
  36.     }
  37.    
  38.     void append(int16 data)
  39.     {
  40.         //Get writing position & create buffer string
  41.         uint pos = output.length();
  42.         string vals = "12";
  43.        
  44.         //Write into the buffer
  45.         vals[0] = ( (data    ) & 255); vals[1] = ( (data>>8 ) & 255);
  46.        
  47.         //Append the buffer
  48.         output += vals;
  49.     }
  50.    
  51.     void append(uint16 data)
  52.     {
  53.         //Get writing position & create buffer string
  54.         uint pos = output.length();
  55.         string vals = "12";
  56.        
  57.         //Write into the buffer
  58.         vals[0] = ( (data    ) & 255); vals[1] = ( (data>>8 ) & 255);
  59.        
  60.         //Append the buffer
  61.         output += vals;
  62.     }
  63.    
  64.     void append(int32 data)
  65.     {
  66.         //Get writing position & create buffer string
  67.         uint pos = output.length();
  68.         string vals = "1234";
  69.        
  70.         //Write into the buffer
  71.         vals[0] = ( (data    ) & 255); vals[1] = ( (data>>8 ) & 255);
  72.         vals[2] = ( (data>>16) & 255); vals[3] = ( (data>>24) & 255);
  73.        
  74.         //Append the buffer
  75.         output += vals;
  76.     }
  77.    
  78.     void append(uint32 data)
  79.     {
  80.         //Get writing position & create buffer string
  81.         uint pos = output.length();
  82.         string vals = "1234";
  83.        
  84.         //Write into the buffer
  85.         vals[0] = ( (data    ) & 255); vals[1] = ( (data>>8 ) & 255);
  86.         vals[2] = ( (data>>16) & 255); vals[3] = ( (data>>24) & 255);
  87.        
  88.         //Append the buffer
  89.         output += vals;
  90.     }
  91.    
  92.     void append(uint64 data)
  93.     {
  94.         //Get writing position & create buffer string
  95.         uint pos = output.length();
  96.         string vals = "12345678";
  97.        
  98.         //Write into the buffer
  99.         vals[0] = ( (data    ) & 255); vals[1] = ( (data>>8 ) & 255);
  100.         vals[2] = ( (data>>16) & 255); vals[3] = ( (data>>24) & 255);
  101.         vals[4] = ( (data>>32) & 255); vals[5] = ( (data>>40) & 255);
  102.         vals[6] = ( (data>>48) & 255); vals[7] = ( (data>>56) & 255);
  103.         //Append the buffer
  104.         output += vals;
  105.     }
  106.    
  107.     void append(int64 data)
  108.     {
  109.         //Get writing position & create buffer string
  110.         uint pos = output.length();
  111.         string vals = "12345678";
  112.        
  113.         //Write into the buffer
  114.         vals[0] = ( (data    ) & 255); vals[1] = ( (data>>8 ) & 255);
  115.         vals[2] = ( (data>>16) & 255); vals[3] = ( (data>>24) & 255);
  116.         vals[4] = ( (data>>32) & 255); vals[5] = ( (data>>40) & 255);
  117.         vals[6] = ( (data>>48) & 255); vals[7] = ( (data>>56) & 255);
  118.        
  119.         //Append the buffer
  120.         output += vals;
  121.     }
  122.    
  123.     void append(float data)
  124.     {
  125.         int32 num = int32(data);
  126.         int32 dec = int32(data * 1000000.0f) - num * 1000000; //Max digits is 6 for float.
  127.         AddDebugMessage("F: " + dec,false);
  128.         append(num); append(int32(dec));
  129.     }
  130.    
  131.     void append(double data)
  132.     {
  133.         int64 num = int64(data);
  134.         int64 dec = int64(data * 1000000000.0) - num * 1000000000; //Max digits is 9
  135.         append(num); append(int64(dec));
  136.     }
  137.  
  138. ///////////////////////////////
  139. //- Append
  140. ///////////////////////////////
  141. ///////////////////////////////
  142. //+ Write
  143. ///////////////////////////////
  144.  
  145.     //Makes sure there is <size> positions availible forward. If not, this function creates them.
  146.     private void forwardAppend(uint size)
  147.     {
  148.         //Resize string if required
  149.         int diff = (size + position - output.length());
  150.         if(diff > 0) output.resize(output.length() + diff);
  151.         /* AddDebugMessage("Len: "+output.length(),false); */
  152.     }
  153.  
  154.     void write(uint8 data)
  155.     {
  156.         //Handle position at fringe of output / out of bounds
  157.         if(position > output.length())
  158.          {
  159.             AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
  160.             return;
  161.          }
  162.         else if(position == output.length()) output += "#";
  163.  
  164.        
  165.         //Write the data & move position forward
  166.         output[position] = data;
  167.         position +=1;
  168.     }
  169.    
  170.     void write(string data)
  171.     {
  172.         //OOB Test
  173.         if(position > output.length())
  174.          {
  175.             AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
  176.             return;
  177.          }
  178.          
  179.         //Write string length
  180.         uint strlength = data.length();
  181.         write( uint32(strlength) );
  182.        
  183.         //Make sure there is enough room for char writing
  184.         forwardAppend(strlength);
  185.        
  186.         //Write in the string data, moving position forward.
  187.         for(uint i =0; i<data.length(); i++)  { output[position] = data[i]; position++; }
  188.     }
  189.    
  190.     void write(int16 data)
  191.     {
  192.         //OOB Test
  193.         if(position > output.length())
  194.          {
  195.             AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
  196.             return;
  197.          }
  198.          
  199.         //Resize if required
  200.         forwardAppend(2);
  201.        
  202.         //Write into the buffer
  203.         output[position    ]=(data & 255); output[position + 1]=((data>>8 ) & 255);
  204.         position += 2;
  205.     }
  206.    
  207.     void write(uint16 data)
  208.     {
  209.         //OOB Test
  210.         if(position > output.length())
  211.          {
  212.             AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
  213.             return;
  214.          }
  215.          
  216.         //Resize if required
  217.         forwardAppend(2);
  218.        
  219.         //Write into the buffer
  220.         output[position]=(data & 255); output[position + 1]=((data>>8 ) & 255);
  221.         position += 2;
  222.     }
  223.    
  224.     void write(int32 data)
  225.     {
  226.         //OOB Test
  227.         if(position > output.length())
  228.          {
  229.             AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
  230.             return;
  231.          }
  232.          
  233.         //Resize if required
  234.         forwardAppend(4);
  235.        
  236.         //Write into the buffer
  237.         output[position    ]=((data    ) & 255); output[position + 1]=((data>>8 ) & 255);
  238.         output[position + 2]=((data>>16) & 255); output[position + 3]=((data>>24) & 255);
  239.         position += 4;
  240.     }
  241.    
  242.     void write(uint32 data)
  243.     {
  244.         //OOB Test
  245.         if(position > output.length())
  246.          {
  247.             AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
  248.             return;
  249.          }
  250.          
  251.         //Resize if required
  252.         forwardAppend(4);
  253.        
  254.         //Write into the buffer
  255.         output[position    ]=((data    ) & 255); output[position + 1]=((data>>8 ) & 255);
  256.         output[position + 2]=((data>>16) & 255); output[position + 3]=((data>>24) & 255);
  257.         position += 4;
  258.     }
  259.    
  260.     void write(int64 data)
  261.     {
  262.         //OOB Test
  263.         if(position > output.length())
  264.          {
  265.             AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
  266.             return;
  267.          }
  268.          
  269.         //Resize if required
  270.         forwardAppend(8);
  271.        
  272.         //Write into the buffer
  273.         output[position    ]=((data    ) & 255); output[position + 1]=((data>>8 ) & 255);
  274.         output[position + 2]=((data>>16) & 255); output[position + 3]=((data>>24) & 255);
  275.         output[position + 4]=((data>>32) & 255); output[position + 5]=((data>>40) & 255);
  276.         output[position + 6]=((data>>48) & 255); output[position + 7]=((data>>56) & 255);
  277.         position += 8;
  278.     }
  279.    
  280.     void write(uint64 data)
  281.     {
  282.         //OOB Test
  283.         if(position > output.length())
  284.          {
  285.             AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false);
  286.             return;
  287.          }
  288.          
  289.         //Resize if required
  290.         forwardAppend(8);
  291.        
  292.         //Write into the buffer
  293.         output[position    ]=((data    ) & 255); output[position + 1]=((data>>8 ) & 255);
  294.         output[position + 2]=((data>>16) & 255); output[position + 3]=((data>>24) & 255);
  295.         output[position + 4]=((data>>32) & 255); output[position + 5]=((data>>40) & 255);
  296.         output[position + 6]=((data>>48) & 255); output[position + 7]=((data>>56) & 255);
  297.         position += 8;
  298.     }
  299.    
  300.     void write(float data)
  301.     {
  302.         int32 num = int32(data);
  303.         int32 dec = int32(data * 1000000.0f) - num * 1000000; //Max digits is 6 for float.
  304.         write(num); write(int32(dec));
  305.     }
  306.    
  307.     void write(double data)
  308.     {
  309.         int64 num = int64(data);
  310.         int64 dec = int64(data * 1000000000.0) - num * 1000000000; //Max digits supported is 9
  311.         write(num); write(int64(dec));
  312.     }
  313.    
  314. ///////////////////////////////
  315. //- Write
  316. ///////////////////////////////
  317. ///////////////////////////////
  318. //+ Read
  319. ///////////////////////////////
  320.  
  321. uint8 readByte()
  322.  {
  323.     //OOB Test
  324.     if(position >= output.length()) { AddDebugMessage("Position out of bounds: " + position + " / " + output.length(),false); return 0; }
  325.     uint8 byte = output[position]; position++;
  326.     return byte;
  327.  }
  328.  
  329. uint8[] readBytes(uint size)
  330.  {
  331.     //OOB Test
  332.     uint8[] bytes = {};
  333.     if( (position+size) > output.length()) { AddDebugMessage("Position out of bounds: " + (size + position) + " / " + output.length(),false); return bytes; }
  334.     bytes.resize(size);
  335.     for(uint i =0; i<size; i++) { bytes[i]=output[position]; position++; }
  336.     return bytes;
  337.  }
  338.  
  339. uint8[] readToEnd()
  340.  {
  341.     uint8[] bytes = {};
  342.     if(position >= output.length()) { AddDebugMessage("Position out of bounds: " + position  + " / " + output.length(),false); return bytes; }
  343.     bytes.resize(output.length()-position);
  344.     for(uint i =0; position<bytes.length(); i++) { bytes[i]=output[position]; position++; }
  345.     return bytes;
  346.  }
  347.  
  348. string readString()
  349.  {
  350.     //Read string length
  351.     uint len = readUInt32();
  352.     if(len==0) return "";
  353.     //Check string length is valid. If not return empty string
  354.     if( (position+len) > output.length()) { AddDebugMessage("String length invalid. End @ " + (len + position)  + " / " + output.length(),false); return ""; }
  355.    
  356.     //Allocate string & read in data
  357.     string data = ""; data.resize(len);
  358.     for(uint i =0; i<data.length(); i++) { data[i] = output[position]; position++; }
  359.    
  360.     return data;
  361.  }
  362.  
  363. int16 readInt16()
  364.  {
  365.     uint8[] bytes = readBytes(2);
  366.     if(bytes.length() == 2)
  367.         return (int16(bytes[0])    ) + (int16(bytes[1])<<8 );
  368.     else return 0;
  369.  }
  370.  
  371. int32 readInt32()
  372.  {
  373.     uint8[] bytes = readBytes(4);
  374.     if(bytes.length() == 4)
  375.         return (int32(bytes[0])    ) + (int32(bytes[1])<<8 ) + (int32(bytes[2])<<16) + (int32(bytes[3])<<24);
  376.     else return 0;
  377.  }
  378.  
  379. int64 readInt64()
  380.  {
  381.     uint8[] bytes = readBytes(8);
  382.     if(bytes.length() == 8)
  383.         return (int32(bytes[0])    ) + (int32(bytes[1])<<8 ) + (int32(bytes[2])<<16) + (int32(bytes[3])<<24) +
  384.                (int32(bytes[4])<<32) + (int32(bytes[5])<<40) + (int32(bytes[6])<<48) + (int32(bytes[7])<<56);
  385.     else return 0;
  386.  }
  387.  
  388. uint16 readUInt16()
  389.  {
  390.     uint8[] bytes = readBytes(2);
  391.     if(bytes.length() == 2)
  392.         return (uint16(bytes[0])    ) + (uint16(bytes[1])<<8 );
  393.     else return 0;
  394.  }
  395.  
  396. uint32 readUInt32()
  397.  {
  398.     uint8[] bytes = readBytes(4);
  399.     if(bytes.length() == 4)
  400.         return (uint32(bytes[0])    ) + (uint32(bytes[1])<<8 ) + (uint32(bytes[2])<<16) + (uint32(bytes[3])<<24);
  401.     else return 0;
  402.  }
  403.  
  404. uint64 readUInt64()
  405.  {
  406.     uint8[] bytes = readBytes(8);
  407.     if(bytes.length() == 8)
  408.         return (uint32(bytes[0])    ) + (uint32(bytes[1])<<8 ) + (uint32(bytes[2])<<16) + (uint32(bytes[3])<<24) +
  409.                (uint32(bytes[4])<<32) + (uint32(bytes[5])<<40) + (uint32(bytes[6])<<48) + (uint32(bytes[7])<<56);
  410.     else return 0;
  411.  }
  412.  
  413. float readFloat()
  414.  {
  415.     float x = readInt32();
  416.     return x + float(readInt32()) * 0.0000001f;
  417.  }
  418.  
  419. double readDouble()
  420.  {
  421.     double x = readInt64();
  422.     return x + double(readInt64()) * 0.0000000001;
  423.  }
  424. ///////////////////////////////
  425. //- Read
  426. ///////////////////////////////
  427. ///////////////////////////////
  428. //+ Stream Control
  429. ///////////////////////////////
  430.     uint getPosition() { return position; }
  431.     bool setPosition(uint newPosition)
  432.     {
  433.         if(newPosition > output.length()) return false;
  434.         position = newPosition;
  435.         return true;
  436.     }
  437.  
  438.     bool endOfStream() { return position == output.length(); }
  439.    
  440.     string getStream() { return output; }
  441.    
  442.     uint getStreamLength() { return output.length(); }
  443.  
  444.     void setStreamSize(uint size) { output.resize(size); }
  445.    
  446.     void setStream(string stream) { output = stream; }
  447. ///////////////////////////////
  448. //- Stream Control
  449. ///////////////////////////////
  450.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement