home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 5036 / source.7z / x_data.h < prev    next >
Encoding:
C/C++ Source or Header  |  2012-01-01  |  5.0 KB  |  158 lines

  1. #ifndef __XENTAX_DATA_H
  2. #define __XENTAX_DATA_H
  3.  
  4. /*
  5. ** DATA TYPES
  6. */
  7.  
  8. typedef __int8  sint08;
  9. typedef __int16 sint16;
  10. typedef __int32 sint32;
  11. typedef __int64 sint64;
  12. typedef unsigned __int8  uint08;
  13. typedef unsigned __int16 uint16;
  14. typedef unsigned __int32 uint32;
  15. typedef unsigned __int64 uint64;
  16.  
  17. typedef float float32;
  18. typedef double float64;
  19.  
  20. /*
  21. ** DATA CONVERSION
  22. */
  23.  
  24. float32 float_16_to_32(unsigned short value);
  25. inline uint32 interpret_as_uint32(float32 x) { return *(reinterpret_cast<uint32*>(&x)); }
  26. inline uint64 interpret_as_uint64(float64 x) { return *(reinterpret_cast<uint64*>(&x)); }
  27. inline float32 interpret_as_float32(uint32 x) { return *(reinterpret_cast<float32*>(&x)); }
  28. inline float64 interpret_as_float64(uint64 x) { return *(reinterpret_cast<float64*>(&x)); }
  29.  
  30. /*
  31. ** BYTE ORDER FUNCTIONS
  32. ** (1) FOR SINGLE ITEMS
  33. ** (2) FOR ARRAYS
  34. */
  35.  
  36. template<class T>
  37. inline void reverse_byte_order(T* data)
  38. {
  39.  unsigned char* ptr = reinterpret_cast<unsigned char*>(data);
  40.  std::reverse(ptr, ptr + sizeof(T)); 
  41. }
  42.  
  43. template<class T>
  44. inline void reverse_byte_order(T* data, size_t elem)
  45. {
  46.  for(size_t i = 0; i < elem; i++) {
  47.      unsigned char* ptr = reinterpret_cast<unsigned char*>(&data[i]);
  48.      std::reverse(ptr, ptr + sizeof(T));
  49.     }
  50. }
  51.  
  52. /*
  53. ** READING FUNCTIONS (INTEGERS)
  54. ** (LE) LITTLE ENDIAN
  55. ** (BE) BIG ENDIAN
  56. */
  57.  
  58. sint08 LE_read_sint08(std::istream& ifile);
  59. uint08 LE_read_uint08(std::istream& ifile);
  60. sint16 LE_read_sint16(std::istream& ifile);
  61. uint16 LE_read_uint16(std::istream& ifile);
  62. sint32 LE_read_sint32(std::istream& ifile);
  63. uint32 LE_read_uint32(std::istream& ifile);
  64. sint64 LE_read_sint64(std::istream& ifile);
  65. uint64 LE_read_uint64(std::istream& ifile);
  66. sint08 LE_read_sint08(std::istream& ifile, unsigned int offset);
  67. uint08 LE_read_uint08(std::istream& ifile, unsigned int offset);
  68. sint16 LE_read_sint16(std::istream& ifile, unsigned int offset);
  69. uint16 LE_read_uint16(std::istream& ifile, unsigned int offset);
  70. sint32 LE_read_sint32(std::istream& ifile, unsigned int offset);
  71. uint32 LE_read_uint32(std::istream& ifile, unsigned int offset);
  72. sint64 LE_read_sint64(std::istream& ifile, unsigned int offset);
  73. uint64 LE_read_uint64(std::istream& ifile, unsigned int offset);
  74.  
  75. sint08 BE_read_sint08(std::istream& ifile);
  76. uint08 BE_read_uint08(std::istream& ifile);
  77. sint16 BE_read_sint16(std::istream& ifile);
  78. uint16 BE_read_uint16(std::istream& ifile);
  79. sint32 BE_read_sint32(std::istream& ifile);
  80. uint32 BE_read_uint32(std::istream& ifile);
  81. sint64 BE_read_sint64(std::istream& ifile);
  82. uint64 BE_read_uint64(std::istream& ifile);
  83. sint08 BE_read_sint08(std::istream& ifile, unsigned int offset);
  84. uint08 BE_read_uint08(std::istream& ifile, unsigned int offset);
  85. sint16 BE_read_sint16(std::istream& ifile, unsigned int offset);
  86. uint16 BE_read_uint16(std::istream& ifile, unsigned int offset);
  87. sint32 BE_read_sint32(std::istream& ifile, unsigned int offset);
  88. uint32 BE_read_uint32(std::istream& ifile, unsigned int offset);
  89. sint64 BE_read_sint64(std::istream& ifile, unsigned int offset);
  90. uint64 BE_read_uint64(std::istream& ifile, unsigned int offset);
  91.  
  92. /*
  93. ** READING FUNCTIONS (FLOATING-POINT)
  94. ** (LE) LITTLE ENDIAN
  95. ** (BE) BIG ENDIAN
  96. */
  97.  
  98. float32 LE_read_float16(std::istream& ifile);
  99. float32 LE_read_float32(std::istream& ifile);
  100. float64 LE_read_float64(std::istream& ifile);
  101. float32 LE_read_float16(std::istream& ifile, unsigned int offset);
  102. float32 LE_read_float32(std::istream& ifile, unsigned int offset);
  103. float64 LE_read_float64(std::istream& ifile, unsigned int offset);
  104.  
  105. float32 BE_read_float16(std::istream& ifile);
  106. float32 BE_read_float32(std::istream& ifile);
  107. float64 BE_read_float64(std::istream& ifile);
  108. float32 BE_read_float16(std::istream& ifile, unsigned int offset);
  109. float32 BE_read_float32(std::istream& ifile, unsigned int offset);
  110. float64 BE_read_float64(std::istream& ifile, unsigned int offset);
  111.  
  112. /*
  113. ** READING FUNCTIONS (ARRAYS)
  114. ** (LE) LITTLE ENDIAN
  115. ** (BE) BIG ENDIAN
  116. */
  117.  
  118. template<class T>
  119. inline bool LE_read_array(std::istream& ifile, T* data, size_t n)
  120. {
  121.  ifile.read((char*)&data[0], n*sizeof(T));
  122.  if(ifile.fail()) return false;
  123.  return true;
  124. }
  125.  
  126. template<class T>
  127. inline bool BE_read_array(std::istream& ifile, T* data, size_t n)
  128. {
  129.  ifile.read((char*)&data[0], n*sizeof(T));
  130.  if(ifile.fail()) return false;
  131.  reverse_byte_order(data, n);
  132.  return true;
  133. }
  134.  
  135. template<class T>
  136. inline bool LE_read_array(std::istream& ifile, unsigned int offset, T* data, unsigned int n)
  137. {
  138.  ifile.seekg(offset);
  139.  if(ifile.fail()) return false;
  140.  return LE_read_array(ifile, data, n);
  141. }
  142.  
  143. template<class T>
  144. inline bool BE_read_array(std::istream& ifile, unsigned int offset, T* data, unsigned int n)
  145. {
  146.  ifile.seekg(offset);
  147.  if(ifile.fail()) return false;
  148.  return BE_read_array(ifile, data, n);
  149. }
  150.  
  151. /*
  152. ** READING FUNCTIONS (STRINGS)
  153. */
  154. bool read_string(std::istream& ifile, char* data, size_t size);
  155. bool read_string(std::istream& ifile, char* data, size_t size, char delimiter);
  156.  
  157. #endif
  158.