home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 5257 / source.7z / xentax.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2012-03-28  |  2.2 KB  |  81 lines

  1. #include "xentax.h"
  2.  
  3. real32 float_16_to_32(uint16 value)
  4. {
  5.  // sign/exponent/mantissa
  6.  const uint16 s = (value & 0x8000);
  7.  const uint16 e = (value & 0x7C00) >> 10;
  8.  const uint16 m = (value & 0x03FF);
  9.  
  10.  // ok
  11.  const real32 sgn = (s ? -1.0f : 1.0f);
  12.  if(e == 0) return sgn*(m == 0 ? 0.0f : std::pow(2.0f, -14.0f)*((real32)m/1024.0f));
  13.  if(e < 32) return sgn*std::pow(2.0f, (real32)e - 15.0f)*(1.0f + ((real32)m/1024.0f));
  14.  
  15.  // not ok!
  16.  if(m == 0) return std::numeric_limits<real32>::quiet_NaN();
  17.  return std::numeric_limits<real32>::quiet_NaN();
  18. }
  19.  
  20. bool read_string(std::istream& ifile, char* data, size_t size)
  21.  return read_string(ifile, data, size, (char)0);
  22. }
  23.  
  24. bool read_string(std::istream& ifile, char* data, size_t size, char delimiter)
  25. {
  26.  if(size == 0) return false;
  27.  for(size_t curr = 0; ; curr++) {
  28.      char c = (char)ifile.peek();
  29.      if(ifile.fail() || ifile.eof()) return false;
  30.      ifile.seekg(1, std::ios::cur);
  31.      if(ifile.fail()) return false;
  32.      if(c == delimiter) { data[curr] = '\0'; break; }
  33.      else data[curr] = c;     
  34.     }
  35.  return true;
  36. }
  37.  
  38. bool write_aligned_string_02(std::ofstream& ofile, const char* str)
  39. {
  40.  // length of string including null terminator (very important to include terminator!)
  41.  uint32 elem = strlen(str) + 1;
  42.  if(!elem) return false;
  43.  
  44.  // length of aligned string
  45.  uint32 size = align02(elem);
  46.  if(!size) return false;
  47.  
  48.  // copy string
  49.  boost::shared_array<char> data(new char[size]);
  50.  for(size_t i = 0; i < size; i++) data[i] = '\0';
  51.  memmove(data.get(), str, elem);
  52.  
  53.  // write string
  54.  ofile.write(data.get(), size);
  55.  if(ofile.fail()) return false;
  56.  
  57.  return true;
  58. }
  59.  
  60. bool write_aligned_string_04(std::ofstream& ofile, const char* str)
  61. {
  62.  // length of string including null terminator (very important to include terminator!)
  63.  uint32 elem = strlen(str) + 1;
  64.  if(!elem) return false;
  65.  
  66.  // length of aligned string
  67.  uint32 size = align04(elem);
  68.  if(!size) return false;
  69.  
  70.  // copy string
  71.  boost::shared_array<char> data(new char[size]);
  72.  for(size_t i = 0; i < size; i++) data[i] = '\0';
  73.  memmove(data.get(), str, elem);
  74.  
  75.  // write string
  76.  ofile.write(data.get(), size);
  77.  if(ofile.fail()) return false;
  78.  
  79.  return true;
  80. }