home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / TextCompiler.h < prev    next >
C/C++ Source or Header  |  2005-11-14  |  3KB  |  139 lines

  1. #ifndef __TEXTCOMPILER_H__
  2. #define __TEXTCOMPILER_H__
  3.  
  4. namespace TextCompiler
  5. {
  6.     // Write an indirect value
  7.     template <typename type> inline void WriteValue(type const * const ptr, idFile *out, bool byteSwap=false) 
  8.     { 
  9.         if(out != NULL)
  10.         {
  11.             if(!byteSwap)
  12.             {
  13.                 out->Write(ptr, sizeof(type));
  14.             }
  15.             else
  16.             {
  17.                 byte const * const p = (byte *)ptr;
  18.  
  19.                 for(int i=sizeof(type)-1;i>=0;i--)
  20.                 {
  21.                     out->Write(&(p[i]), 1);
  22.                 }
  23.             }
  24.  
  25.         }
  26.     }
  27.  
  28.     // Write a direct value
  29.     template <typename type> inline void WriteValue(type const val, idFile *out, bool byteSwap=false) 
  30.     { 
  31.         if(out != NULL)
  32.         {
  33.             if(!byteSwap)
  34.             {
  35.                 out->Write(&val, sizeof(type));
  36.             }
  37.             else
  38.             {
  39.                 byte const * const p = (byte *)&val;
  40.  
  41.                 for(int i=sizeof(type)-1;i>=0;i--)
  42.                 {
  43.                     out->Write(&(p[i]), 1);
  44.                 }
  45.             }
  46.          }
  47.     }
  48.  
  49.     template <typename type> inline type ReadValue(idFile *in) 
  50.     { 
  51.         type ret; 
  52.         
  53.         in->Read(&ret, sizeof(type));
  54.         return ret;
  55.     }
  56.  
  57.     // specialization write for idStr's
  58.     template <> inline void WriteValue(idStr const * const ptr, idFile *out, bool byteSwap) 
  59.     { 
  60.         if(out != NULL)
  61.         {
  62.             // if less than 32, then we don't need a trailing null
  63.             // this is because 5 bits can be used to represent string length in the token header
  64.             // zero length strings end up needing to be null terminated
  65.             if((ptr->Length() < 32) && (ptr->Length() != 0))
  66.             {
  67.                 out->Write(ptr->c_str(), ptr->Length());
  68.             }
  69.             else
  70.             {
  71.                 out->Write(ptr->c_str(), ptr->Length()+1);
  72.             }
  73.         }
  74.     }
  75.  
  76.     // specialization read for idStr's
  77.     template <> inline idStr ReadValue(idFile *in) 
  78.     { 
  79.         char c;
  80.         idStr str;
  81.  
  82.         in->Read(&c, 1);
  83.         while(c != '\0')
  84.         {
  85.             str.Append(c);
  86.             in->Read(&c, 1);
  87.         }
  88.  
  89.         str.Append(c);
  90.  
  91.         return str;
  92.     }
  93.  
  94.     template <typename type> inline void WriteArray(type const * const ptr, unsigned int count, idFile *out)
  95.     {
  96.         WriteValue<unsigned int>(&count, out);
  97.         for(unsigned int i=0;i<count; i++)
  98.         {
  99.             WriteValue<type>(&(ptr[i]), out);
  100.         }
  101.     }
  102.  
  103.     template <typename type> inline type *ReadArray(idFile *in, unsigned int *count=NULL) 
  104.     { 
  105.         unsigned int len; 
  106.         len = ReadValue<unsigned int>(in);
  107.         type *buffer = (type *)malloc(len*sizeof(type)); 
  108.         type *ptr = buffer;
  109.         for(unsigned int i=0;i<len;i++)
  110.         {
  111.             *ptr = ReadValue<type>(in);
  112.             ptr++;
  113.         }
  114.         if(count != NULL)
  115.             *count = len;
  116.         return buffer;
  117.     }
  118.  
  119.     template <typename type> inline void WriteIdList(idList<type> const * const ptr, idFile *out)
  120.     {
  121.         WriteArray<type>(ptr->Ptr(), ptr->Num(), out);
  122.     }
  123.  
  124.     template <typename type> inline idList<type> ReadIdList(idFile *in)
  125.     {
  126.         idList<type> ret;
  127.         ret.Clear();
  128.         unsigned int count;
  129.         idAutoPtr<type> array(ReadArray<type>(in, &count));
  130.  
  131.         for(unsigned int i=0;i<count;i++)
  132.             ret.Append(array[i]);
  133.  
  134.         return ret;
  135.     }
  136. };
  137.  
  138. #endif
  139.