Advertisement
Guest User

Untitled

a guest
Jul 7th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. static byte *gpBuf;
  2. static size_t giSize;
  3. static size_t giRead;
  4. static bool giBadRead;
  5.  
  6. void BEGIN_READ( void *buf, int size )
  7. {
  8.     giRead = 0;
  9.     giBadRead = false;
  10.     giSize = size;
  11.     gpBuf = (byte*)buf;
  12. }
  13.  
  14. int READ_CHAR( void )
  15. {
  16.     int     c;
  17.    
  18.     if (giRead + 1 > giSize)
  19.     {
  20.         giBadRead = true;
  21.         return -1;
  22.     }
  23.        
  24.     c = (signed char)gpBuf[giRead];
  25.     giRead++;
  26.    
  27.     return c;
  28. }
  29.  
  30. int READ_BYTE( void )
  31. {
  32.     int     c;
  33.    
  34.     if (giRead+1 > giSize)
  35.     {
  36.         giBadRead = true;
  37.         return -1;
  38.     }
  39.        
  40.     c = (unsigned char)gpBuf[giRead];
  41.     giRead++;
  42.    
  43.     return c;
  44. }
  45.  
  46. int READ_SHORT( void )
  47. {
  48.     int     c;
  49.    
  50.     if (giRead+2 > giSize)
  51.     {
  52.         giBadRead = true;
  53.         return -1;
  54.     }
  55.        
  56.     c = (short)( gpBuf[giRead] + ( gpBuf[giRead+1] << 8 ) );
  57.    
  58.     giRead += 2;
  59.    
  60.     return c;
  61. }
  62.  
  63. int READ_WORD( void )
  64. {
  65.     return READ_SHORT();
  66. }
  67.  
  68.  
  69. int READ_LONG( void )
  70. {
  71.     int     c;
  72.    
  73.     if (giRead+4 > giSize)
  74.     {
  75.         giBadRead = true;
  76.         return -1;
  77.     }
  78.        
  79.     c = gpBuf[giRead] + (gpBuf[giRead + 1] << 8) + (gpBuf[giRead + 2] << 16) + (gpBuf[giRead + 3] << 24);
  80.    
  81.     giRead += 4;
  82.    
  83.     return c;
  84. }
  85.  
  86. float READ_FLOAT( void )
  87. {
  88.     union
  89.     {
  90.         byte    b[4];
  91.         float   f;
  92.         int     l;
  93.     } dat;
  94.    
  95.     dat.b[0] = gpBuf[giRead];
  96.     dat.b[1] = gpBuf[giRead+1];
  97.     dat.b[2] = gpBuf[giRead+2];
  98.     dat.b[3] = gpBuf[giRead+3];
  99.     giRead += 4;
  100.    
  101. //  dat.l = LittleLong (dat.l);
  102.  
  103.     return dat.f;  
  104. }
  105.  
  106. char* READ_STRING( void )
  107. {
  108.     static char     string[2048];
  109.     int             c;
  110.     size_t          l = 0;
  111.  
  112.     string[0] = 0;
  113.     do
  114.     {
  115.         if ( giRead+1 > giSize )
  116.             break; // no more characters
  117.  
  118.         c = READ_CHAR();
  119.         if (c == -1 || c == 0)
  120.             break;
  121.         string[l] = c;
  122.         l++;
  123.     } while (l < sizeof(string) - 1);
  124.    
  125.     string[l] = 0;
  126.    
  127.     return string;
  128. }
  129.  
  130. float READ_COORD( void )
  131. {
  132.     return (float)(READ_SHORT() * (1.0/8));
  133. }
  134.  
  135. float READ_ANGLE( void )
  136. {
  137.     return (float)(READ_CHAR() * (360.0/256));
  138. }
  139.  
  140. float READ_HIRESANGLE( void )
  141. {
  142.     return (float)(READ_SHORT() * (360.0/65536));
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement