home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 5036 / source.7z / x_stream.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2012-01-31  |  28.7 KB  |  1,071 lines

  1. #include "xentax.h"
  2.  
  3. const binary_stream::size_type binary_stream::npos = std::numeric_limits<size_type>::max();
  4.  
  5. binary_stream::binary_stream()
  6. {
  7.  elem = 0;
  8.  position = 0;
  9. }
  10.  
  11. binary_stream::binary_stream(const binary_stream& bs)
  12. {
  13.  data = bs.data;
  14.  elem = bs.elem;
  15.  position = bs.position;
  16. }
  17.  
  18. binary_stream::binary_stream(const boost::shared_array<char>& stream, size_type n)
  19. {
  20.  data = stream;
  21.  elem = n;
  22.  position = 0;
  23. }
  24.  
  25. binary_stream::~binary_stream()
  26. {
  27. }
  28.  
  29. binary_stream& binary_stream::operator =(const binary_stream& bs)
  30. {
  31.  if(this == &bs) return *this;
  32.  data = bs.data;
  33.  elem = bs.elem;
  34.  position = bs.position;
  35.  return *this;
  36. }
  37.  
  38. void binary_stream::seek(size_type pos)
  39. {
  40.  if(can_seek(pos)) position = pos;
  41.  else position = npos;
  42. }
  43.  
  44. void binary_stream::seek_reverse(size_type pos)
  45. {
  46.  if(can_seek(pos)) position = elem - pos;
  47.  else position = npos;
  48. }
  49.  
  50. void binary_stream::move(diff_type pos)
  51. {
  52.  if(pos == 0) return;
  53.  else if(pos > 0) seek(position + pos);
  54.  else if(pos < 0) {
  55.     size_type off = (size_type)std::abs(pos);
  56.     if(!(position < off)) position -= off;
  57.     else position = npos;
  58.    }
  59. }
  60.  
  61. void binary_stream::move(diff_type pos, size_type from)
  62. {
  63.  if(fail()) return;
  64.  seek(from);
  65.  if(good()) move(pos);
  66. }
  67.  
  68. void binary_stream::reset(void)
  69. {
  70.  position = 0;
  71. }
  72.  
  73. binary_stream::size_type binary_stream::search(const char* pattern, size_type p_len, size_t from)
  74. {
  75.  // initialize bad-character shifts
  76.  boost::shared_array<unsigned char> table(new unsigned char[256]);
  77.  for(size_t i = 0; i < 256; i++) table[i] = p_len;
  78.  
  79.  // bad-character shifts
  80.  const unsigned char* p_str = reinterpret_cast<const unsigned char*>(pattern);
  81.  for(size_t i = 0; i < p_len - 1; i++) table[p_str[i]] = p_len - i - 1;
  82.  
  83.  // convert source string
  84.  const unsigned char* s_str = reinterpret_cast<const unsigned char*>(data.get());
  85.  size_type s_len = elem;
  86.  
  87.  // search for p_str in s_str
  88.  size_t j = from;
  89.  while(j <= (s_len - p_len)) {
  90.        unsigned char c = s_str[j + p_len - 1];
  91.        if(p_str[p_len - 1] == c && memcmp(p_str, s_str + j, p_len - 1) == 0) return j;
  92.        j += table[c];
  93.       }
  94.  
  95.  return npos;
  96. }
  97.  
  98. void binary_stream::read(char* ptr, size_type n)
  99. {
  100.  if(fail() || !ptr || !n) return;
  101.  size_type newpos = position + n;
  102.  if(!can_move(n)) { position = npos; return; }
  103.  memmove(ptr, c_pos(), n);
  104.  position = newpos;
  105. }
  106.  
  107. void binary_stream::read_reverse(char* ptr, size_type n)
  108. {
  109.  read(ptr, n);
  110.  if(good()) std::reverse(ptr, ptr + n);
  111. }
  112.  
  113. std::string binary_stream::read_string(void)
  114. {
  115.  std::string retval;
  116.  for(;;) {
  117.      char c;
  118.      read(&c, 1);
  119.      if(fail()) break;
  120.      retval += c;
  121.      if(c == '\0') break;
  122.     }
  123.  return retval;
  124. }
  125.  
  126. sint08 binary_stream::LE_read_sint08(void)
  127. {
  128.  typedef sint08 T;
  129.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  130.  size_type newpos = position + sizeof(T);
  131.  if(!can_seek(newpos)) { position = npos; return T(); }
  132.  T r = *reinterpret_cast<const T*>(c_pos());
  133.  position = newpos;
  134.  return r;
  135. }
  136.  
  137. uint08 binary_stream::LE_read_uint08(void)
  138. {
  139.  typedef uint08 T;
  140.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  141.  size_type newpos = position + sizeof(T);
  142.  if(!can_seek(newpos)) { position = npos; return T(); }
  143.  T r = *reinterpret_cast<const T*>(c_pos());
  144.  position = newpos;
  145.  return r;
  146. }
  147.  
  148. sint16 binary_stream::LE_read_sint16(void)
  149. {
  150.  typedef sint16 T;
  151.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  152.  size_type newpos = position + sizeof(T);
  153.  if(!can_seek(newpos)) { position = npos; return T(); }
  154.  T r = *reinterpret_cast<const T*>(c_pos());
  155.  position = newpos;
  156.  return r;
  157. }
  158.  
  159. uint16 binary_stream::LE_read_uint16(void)
  160. {
  161.  typedef uint16 T;
  162.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  163.  size_type newpos = position + sizeof(T);
  164.  if(!can_seek(newpos)) { position = npos; return T(); }
  165.  T r = *reinterpret_cast<const T*>(c_pos());
  166.  position = newpos;
  167.  return r;
  168. }
  169.  
  170. sint32 binary_stream::LE_read_sint32(void)
  171. {
  172.  typedef sint32 T;
  173.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  174.  size_type newpos = position + sizeof(T);
  175.  if(!can_seek(newpos)) { position = npos; return T(); }
  176.  T r = *reinterpret_cast<const T*>(c_pos());
  177.  position = newpos;
  178.  return r;
  179. }
  180.  
  181. uint32 binary_stream::LE_read_uint32(void)
  182. {
  183.  typedef uint32 T;
  184.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  185.  size_type newpos = position + sizeof(T);
  186.  if(!can_seek(newpos)) { position = npos; return T(); }
  187.  T r = *reinterpret_cast<const T*>(c_pos());
  188.  position = newpos;
  189.  return r;
  190. }
  191.  
  192. sint64 binary_stream::LE_read_sint64(void)
  193. {
  194.  typedef sint64 T;
  195.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  196.  size_type newpos = position + sizeof(T);
  197.  if(!can_seek(newpos)) { position = npos; return T(); }
  198.  T r = *reinterpret_cast<const T*>(c_pos());
  199.  position = newpos;
  200.  return r;
  201. }
  202.  
  203. uint64 binary_stream::LE_read_uint64(void)
  204. {
  205.  typedef uint64 T;
  206.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  207.  size_type newpos = position + sizeof(T);
  208.  if(!can_seek(newpos)) { position = npos; return T(); }
  209.  T r = *reinterpret_cast<const T*>(c_pos());
  210.  position = newpos;
  211.  return r;
  212. }
  213.  
  214. sint08 binary_stream::LE_read_sint08(size_type pos)
  215. {
  216.  typedef sint08 T;
  217.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  218.  size_type newpos = pos + sizeof(T);
  219.  if(can_seek(newpos)) return *reinterpret_cast<const T*>(c_str(pos));
  220.  position = npos;
  221.  return T();
  222. }
  223.  
  224. uint08 binary_stream::LE_read_uint08(size_type pos)
  225. {
  226.  typedef uint08 T;
  227.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  228.  size_type newpos = pos + sizeof(T);
  229.  if(can_seek(newpos)) return *reinterpret_cast<const T*>(c_str(pos));
  230.  position = npos;
  231.  return T();
  232. }
  233.  
  234. sint16 binary_stream::LE_read_sint16(size_type pos)
  235. {
  236.  typedef sint16 T;
  237.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  238.  size_type newpos = pos + sizeof(T);
  239.  if(can_seek(newpos)) return *reinterpret_cast<const T*>(c_str(pos));
  240.  position = npos;
  241.  return T();
  242. }
  243.  
  244. uint16 binary_stream::LE_read_uint16(size_type pos)
  245. {
  246.  typedef uint16 T;
  247.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  248.  size_type newpos = pos + sizeof(T);
  249.  if(can_seek(newpos)) return *reinterpret_cast<const T*>(c_str(pos));
  250.  position = npos;
  251.  return T();
  252. }
  253.  
  254. sint32 binary_stream::LE_read_sint32(size_type pos)
  255. {
  256.  typedef sint32 T;
  257.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  258.  size_type newpos = pos + sizeof(T);
  259.  if(can_seek(newpos)) return *reinterpret_cast<const T*>(c_str(pos));
  260.  position = npos;
  261.  return T();
  262. }
  263.  
  264. uint32 binary_stream::LE_read_uint32(size_type pos)
  265. {
  266.  typedef uint32 T;
  267.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  268.  size_type newpos = pos + sizeof(T);
  269.  if(can_seek(newpos)) return *reinterpret_cast<const T*>(c_str(pos));
  270.  position = npos;
  271.  return T();
  272. }
  273.  
  274. sint64 binary_stream::LE_read_sint64(size_type pos)
  275. {
  276.  typedef sint64 T;
  277.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  278.  size_type newpos = pos + sizeof(T);
  279.  if(can_seek(newpos)) return *reinterpret_cast<const T*>(c_str(pos));
  280.  position = npos;
  281.  return T();
  282. }
  283.  
  284. uint64 binary_stream::LE_read_uint64(size_type pos)
  285. {
  286.  typedef uint64 T;
  287.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  288.  size_type newpos = pos + sizeof(T);
  289.  if(can_seek(newpos)) return *reinterpret_cast<const T*>(c_str(pos));
  290.  position = npos;
  291.  return T();
  292. }
  293.  
  294. sint08 binary_stream::BE_read_sint08(void)
  295. {
  296.  return LE_read_sint08();
  297. }
  298.  
  299. uint08 binary_stream::BE_read_uint08(void)
  300. {
  301.  return LE_read_uint08();
  302. }
  303.  
  304. sint16 binary_stream::BE_read_sint16(void)
  305. {
  306.  typedef sint16 T;
  307.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  308.  size_type newpos = position + sizeof(T);
  309.  if(!can_seek(newpos)) { position = npos; return T(); }
  310.  T r = *reinterpret_cast<const T*>(c_pos());
  311.  position = newpos;
  312.  reverse_byte_order(&r);
  313.  return r;
  314. }
  315.  
  316. uint16 binary_stream::BE_read_uint16(void)
  317. {
  318.  typedef uint16 T;
  319.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  320.  size_type newpos = position + sizeof(T);
  321.  if(!can_seek(newpos)) { position = npos; return T(); }
  322.  T r = *reinterpret_cast<const T*>(c_pos());
  323.  position = newpos;
  324.  reverse_byte_order(&r);
  325.  return r;
  326. }
  327.  
  328. sint32 binary_stream::BE_read_sint32(void)
  329. {
  330.  typedef sint32 T;
  331.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  332.  size_type newpos = position + sizeof(T);
  333.  if(!can_seek(newpos)) { position = npos; return T(); }
  334.  T r = *reinterpret_cast<const T*>(c_pos());
  335.  position = newpos;
  336.  reverse_byte_order(&r);
  337.  return r;
  338. }
  339.  
  340. uint32 binary_stream::BE_read_uint32(void)
  341. {
  342.  typedef uint32 T;
  343.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  344.  size_type newpos = position + sizeof(T);
  345.  if(!can_seek(newpos)) { position = npos; return T(); }
  346.  T r = *reinterpret_cast<const T*>(c_pos());
  347.  position = newpos;
  348.  reverse_byte_order(&r);
  349.  return r;
  350. }
  351.  
  352. sint64 binary_stream::BE_read_sint64(void)
  353. {
  354.  typedef sint64 T;
  355.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  356.  size_type newpos = position + sizeof(T);
  357.  if(!can_seek(newpos)) { position = npos; return T(); }
  358.  T r = *reinterpret_cast<const T*>(c_pos());
  359.  position = newpos;
  360.  reverse_byte_order(&r);
  361.  return r;
  362. }
  363.  
  364. uint64 binary_stream::BE_read_uint64(void)
  365. {
  366.  typedef uint64 T;
  367.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  368.  size_type newpos = position + sizeof(T);
  369.  if(!can_seek(newpos)) { position = npos; return T(); }
  370.  T r = *reinterpret_cast<const T*>(c_pos());
  371.  position = newpos;
  372.  reverse_byte_order(&r);
  373.  return r;
  374. }
  375.  
  376. sint08 binary_stream::BE_read_sint08(size_type pos)
  377. {
  378.  return LE_read_sint08(pos);
  379. }
  380.  
  381. uint08 binary_stream::BE_read_uint08(size_type pos)
  382. {
  383.  return LE_read_uint08(pos);
  384. }
  385.  
  386. sint16 binary_stream::BE_read_sint16(size_type pos)
  387. {
  388.  typedef sint16 T;
  389.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  390.  size_type newpos = pos + sizeof(T);
  391.  if(!can_seek(newpos)) { position = npos; return T(); }
  392.  T r = *reinterpret_cast<const T*>(c_str(pos));
  393.  reverse_byte_order(&r);
  394.  return r;
  395. }
  396.  
  397. uint16 binary_stream::BE_read_uint16(size_type pos)
  398. {
  399.  typedef uint16 T;
  400.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  401.  size_type newpos = pos + sizeof(T);
  402.  if(!can_seek(newpos)) { position = npos; return T(); }
  403.  T r = *reinterpret_cast<const T*>(c_str(pos));
  404.  reverse_byte_order(&r);
  405.  return r;
  406. }
  407.  
  408. sint32 binary_stream::BE_read_sint32(size_type pos)
  409. {
  410.  typedef sint32 T;
  411.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  412.  size_type newpos = pos + sizeof(T);
  413.  if(!can_seek(newpos)) { position = npos; return T(); }
  414.  T r = *reinterpret_cast<const T*>(c_str(pos));
  415.  reverse_byte_order(&r);
  416.  return r;
  417. }
  418.  
  419. uint32 binary_stream::BE_read_uint32(size_type pos)
  420. {
  421.  typedef uint32 T;
  422.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  423.  size_type newpos = pos + sizeof(T);
  424.  if(!can_seek(newpos)) { position = npos; return T(); }
  425.  T r = *reinterpret_cast<const T*>(c_str(pos));
  426.  reverse_byte_order(&r);
  427.  return r;
  428. }
  429.  
  430. sint64 binary_stream::BE_read_sint64(size_type pos)
  431. {
  432.  typedef sint64 T;
  433.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  434.  size_type newpos = pos + sizeof(T);
  435.  if(!can_seek(newpos)) { position = npos; return T(); }
  436.  T r = *reinterpret_cast<const T*>(c_str(pos));
  437.  reverse_byte_order(&r);
  438.  return r;
  439. }
  440.  
  441. uint64 binary_stream::BE_read_uint64(size_type pos)
  442. {
  443.  typedef uint64 T;
  444.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  445.  size_type newpos = pos + sizeof(T);
  446.  if(!can_seek(newpos)) { position = npos; return T(); }
  447.  T r = *reinterpret_cast<const T*>(c_str(pos));
  448.  reverse_byte_order(&r);
  449.  return r;
  450. }
  451.  
  452. float32 binary_stream::LE_read_float16(void)
  453. {
  454.  typedef uint16 T; typedef float32 R;
  455.  if(fail() || !(position < (npos - sizeof(T)))) return R();
  456.  size_type oldpos = position;
  457.  size_type newpos = position + sizeof(T);
  458.  if(!can_seek(newpos)) { position = npos; return R(); }
  459.  position = newpos;
  460.  T r = *reinterpret_cast<const T*>(c_str(oldpos));
  461.  return float_16_to_32(r);
  462. }
  463.  
  464. float32 binary_stream::LE_read_float32(void)
  465. {
  466.  typedef float32 T;
  467.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  468.  size_type oldpos = position;
  469.  size_type newpos = position + sizeof(T);
  470.  if(!can_seek(newpos)) { position = npos; return T(); }
  471.  position = newpos;
  472.  return *reinterpret_cast<const T*>(c_str(oldpos));
  473. }
  474.  
  475. float64 binary_stream::LE_read_float64(void)
  476. {
  477.  typedef float64 T;
  478.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  479.  size_type oldpos = position;
  480.  size_type newpos = position + sizeof(T);
  481.  if(!can_seek(newpos)) { position = npos; return T(); }
  482.  position = newpos;
  483.  return *reinterpret_cast<const T*>(c_str(oldpos));
  484. }
  485.  
  486. float32 binary_stream::LE_read_float16(size_type pos)
  487. {
  488.  typedef uint16 T; typedef float32 R;
  489.  if(fail() || !(pos < (npos - sizeof(T)))) return R();
  490.  size_type newpos = pos + sizeof(T);
  491.  if(can_seek(newpos)) return float_16_to_32(*reinterpret_cast<const T*>(c_str(pos)));
  492.  position = npos;
  493.  return R();
  494. }
  495.  
  496. float32 binary_stream::LE_read_float32(size_type pos)
  497. {
  498.  typedef float32 T;
  499.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  500.  size_type newpos = pos + sizeof(T);
  501.  if(can_seek(newpos)) return *reinterpret_cast<const T*>(c_str(pos));
  502.  position = npos;
  503.  return T();
  504. }
  505.  
  506. float64 binary_stream::LE_read_float64(size_type pos)
  507. {
  508.  typedef float64 T;
  509.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  510.  size_type newpos = pos + sizeof(T);
  511.  if(can_seek(newpos)) return *reinterpret_cast<const T*>(c_str(pos));
  512.  position = npos;
  513.  return T();
  514. }
  515.  
  516. float32 binary_stream::BE_read_float16(void)
  517. {
  518.  typedef uint16 T; typedef float32 R;
  519.  if(fail() || !(position < (npos - sizeof(T)))) return R();
  520.  size_type oldpos = position;
  521.  size_type newpos = position + sizeof(T);
  522.  if(!can_seek(newpos)) { position = npos; return R(); }
  523.  position = newpos;
  524.  T r = *reinterpret_cast<const T*>(c_str(oldpos));
  525.  reverse_byte_order(&r);
  526.  return float_16_to_32(r);
  527. }
  528.  
  529. float32 binary_stream::BE_read_float32(void)
  530. {
  531.  typedef float32 T;
  532.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  533.  size_type oldpos = position;
  534.  size_type newpos = position + sizeof(T);
  535.  if(!can_seek(newpos)) { position = npos; return T(); }
  536.  position = newpos;
  537.  T r = *reinterpret_cast<const T*>(c_str(oldpos));
  538.  reverse_byte_order(&r);
  539.  return r;
  540. }
  541.  
  542. float64 binary_stream::BE_read_float64(void)
  543. {
  544.  typedef float64 T;
  545.  if(fail() || !(position < (npos - sizeof(T)))) return T();
  546.  size_type oldpos = position;
  547.  size_type newpos = position + sizeof(T);
  548.  if(!can_seek(newpos)) { position = npos; return T(); }
  549.  position = newpos;
  550.  T r = *reinterpret_cast<const T*>(c_str(oldpos));
  551.  reverse_byte_order(&r);
  552.  return r;
  553. }
  554.  
  555. float32 binary_stream::BE_read_float16(size_type pos)
  556. {
  557.  typedef uint16 T; typedef float32 R;
  558.  if(fail() || !(pos < (npos - sizeof(T)))) return R();
  559.  size_type newpos = pos + sizeof(T);
  560.  if(!can_seek(newpos)) { position = npos; return R(); }
  561.  T r = *reinterpret_cast<const T*>(c_str(pos));
  562.  reverse_byte_order(&r);
  563.  return float_16_to_32(r);
  564. }
  565.  
  566. float32 binary_stream::BE_read_float32(size_type pos)
  567. {
  568.  typedef float32 T;
  569.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  570.  size_type newpos = pos + sizeof(T);
  571.  if(!can_seek(newpos)) { position = npos; return T(); }
  572.  T r = *reinterpret_cast<const T*>(c_str(pos));
  573.  reverse_byte_order(&r);
  574.  return r;
  575. }
  576.  
  577. float64 binary_stream::BE_read_float64(size_type pos)
  578. {
  579.  typedef float64 T;
  580.  if(fail() || !(pos < (npos - sizeof(T)))) return T();
  581.  size_type newpos = pos + sizeof(T);
  582.  if(!can_seek(newpos)) { position = npos; return T(); }
  583.  T r = *reinterpret_cast<const T*>(c_str(pos));
  584.  reverse_byte_order(&r);
  585.  return r;
  586. }
  587.  
  588. void binary_stream::LE_read(sint08* ptr, size_type n)
  589. {
  590.  typedef sint08 T;
  591.  if(fail() || !ptr || !n) return;
  592.  size_type offset = n*sizeof(T);
  593.  size_type newpos = position + offset;
  594.  if(!can_seek(newpos)) { position = npos; return; }
  595.  memmove(ptr, c_pos(), offset);
  596.  position = newpos;
  597. }
  598.  
  599. void binary_stream::LE_read(sint16* ptr, size_type n)
  600. {
  601.  typedef sint16 T;
  602.  if(fail() || !ptr || !n) return;
  603.  size_type offset = n*sizeof(T);
  604.  size_type newpos = position + offset;
  605.  if(!can_seek(newpos)) { position = npos; return; }
  606.  memmove(ptr, c_pos(), offset);
  607.  position = newpos;
  608. }
  609.  
  610. void binary_stream::LE_read(sint32* ptr, size_type n)
  611. {
  612.  typedef sint32 T;
  613.  if(fail() || !ptr || !n) return;
  614.  size_type offset = n*sizeof(T);
  615.  size_type newpos = position + offset;
  616.  if(!can_seek(newpos)) { position = npos; return; }
  617.  memmove(ptr, c_pos(), offset);
  618.  position = newpos;
  619. }
  620.  
  621. void binary_stream::LE_read(sint64* ptr, size_type n)
  622. {
  623.  typedef sint64 T;
  624.  if(fail() || !ptr || !n) return;
  625.  size_type offset = n*sizeof(T);
  626.  size_type newpos = position + offset;
  627.  if(!can_seek(newpos)) { position = npos; return; }
  628.  memmove(ptr, c_pos(), offset);
  629.  position = newpos;
  630. }
  631.  
  632. void binary_stream::LE_read(sint08* ptr, size_type n, size_type pos)
  633. {
  634.  typedef sint08 T;
  635.  if(fail() || !ptr || !n) return;
  636.  size_type offset = n*sizeof(T);
  637.  size_type newpos = position + offset;
  638.  if(!can_move(newpos)) { position = npos; return; }
  639.  memmove(ptr, c_str(pos), offset);
  640. }
  641.  
  642. void binary_stream::LE_read(sint16* ptr, size_type n, size_type pos)
  643. {
  644.  typedef sint16 T;
  645.  if(fail() || !ptr || !n) return;
  646.  size_type offset = n*sizeof(T);
  647.  size_type newpos = position + offset;
  648.  if(!can_move(newpos)) { position = npos; return; }
  649.  memmove(ptr, c_str(pos), offset);
  650. }
  651.  
  652. void binary_stream::LE_read(sint32* ptr, size_type n, size_type pos)
  653. {
  654.  typedef sint32 T;
  655.  if(fail() || !ptr || !n) return;
  656.  size_type offset = n*sizeof(T);
  657.  size_type newpos = position + offset;
  658.  if(!can_move(newpos)) { position = npos; return; }
  659.  memmove(ptr, c_str(pos), offset);
  660. }
  661.  
  662. void binary_stream::LE_read(sint64* ptr, size_type n, size_type pos)
  663. {
  664.  typedef sint64 T;
  665.  if(fail() || !ptr || !n) return;
  666.  size_type offset = n*sizeof(T);
  667.  size_type newpos = position + offset;
  668.  if(!can_move(newpos)) { position = npos; return; }
  669.  memmove(ptr, c_str(pos), offset);
  670. }
  671.  
  672. void binary_stream::BE_read(sint08* ptr, size_type n)
  673. {
  674. }
  675.  
  676. void binary_stream::BE_read(sint16* ptr, size_type n)
  677. {
  678. }
  679.  
  680. void binary_stream::BE_read(sint32* ptr, size_type n)
  681. {
  682. }
  683.  
  684. void binary_stream::BE_read(sint64* ptr, size_type n)
  685. {
  686. }
  687.  
  688. void binary_stream::BE_read(sint08* ptr, size_type n, size_type pos)
  689. {
  690. }
  691.  
  692. void binary_stream::BE_read(sint16* ptr, size_type n, size_type pos)
  693. {
  694. }
  695.  
  696. void binary_stream::BE_read(sint32* ptr, size_type n, size_type pos)
  697. {
  698. }
  699.  
  700. void binary_stream::BE_read(sint64* ptr, size_type n, size_type pos)
  701. {
  702. }
  703.  
  704. void binary_stream::LE_read(uint08* ptr, size_type n)
  705. {
  706.  typedef uint08 T;
  707.  if(fail() || !ptr || !n) return;
  708.  size_type offset = n*sizeof(T);
  709.  size_type newpos = position + offset;
  710.  if(!can_seek(newpos)) { position = npos; return; }
  711.  memmove(ptr, c_pos(), offset);
  712.  position = newpos;
  713. }
  714.  
  715. void binary_stream::LE_read(uint16* ptr, size_type n)
  716. {
  717.  typedef uint16 T;
  718.  if(fail() || !ptr || !n) return;
  719.  size_type offset = n*sizeof(T);
  720.  size_type newpos = position + offset;
  721.  if(!can_seek(newpos)) { position = npos; return; }
  722.  memmove(ptr, c_pos(), offset);
  723.  position = newpos;
  724. }
  725.  
  726. void binary_stream::LE_read(uint32* ptr, size_type n)
  727. {
  728.  typedef uint32 T;
  729.  if(fail() || !ptr || !n) return;
  730.  size_type offset = n*sizeof(T);
  731.  size_type newpos = position + offset;
  732.  if(!can_seek(newpos)) { position = npos; return; }
  733.  memmove(ptr, c_pos(), offset);
  734.  position = newpos;
  735. }
  736.  
  737. void binary_stream::LE_read(uint64* ptr, size_type n)
  738. {
  739.  typedef uint64 T;
  740.  if(fail() || !ptr || !n) return;
  741.  size_type offset = n*sizeof(T);
  742.  size_type newpos = position + offset;
  743.  if(!can_seek(newpos)) { position = npos; return; }
  744.  memmove(ptr, c_pos(), offset);
  745.  position = newpos;
  746. }
  747.  
  748. void binary_stream::LE_read(uint08* ptr, size_type n, size_type pos)
  749. {
  750.  // validate arguments
  751.  typedef uint08 T;
  752.  if(fail() || !ptr || !n) return;
  753.  
  754.  // compute offset
  755.  size_type offset = n*sizeof(T);
  756.  if(!(position < npos - offset)) { position = npos; return; }
  757.  
  758.  // compute newpos
  759.  size_type newpos = position + offset;
  760.  if(!can_seek(newpos)) { position = npos; return; }
  761.  
  762.  // copy data from current position
  763.  memmove(ptr, c_pos(), offset);
  764.  position = newpos;
  765. }
  766.  
  767. void binary_stream::LE_read(uint16* ptr, size_type n, size_type pos)
  768. {
  769.  typedef uint16 T;
  770.  if(fail() || !ptr || !n) return;
  771.  size_type offset = n*sizeof(T);
  772.  if(!(position < npos - offset)) { position = npos; return; }
  773.  size_type newpos = position + offset;
  774.  if(!can_seek(newpos)) { position = npos; return; }
  775.  memmove(ptr, c_pos(), offset);
  776.  position = newpos;
  777. }
  778.  
  779. void binary_stream::LE_read(uint32* ptr, size_type n, size_type pos)
  780. {
  781.  typedef uint32 T;
  782.  if(fail() || !ptr || !n) return;
  783.  size_type offset = n*sizeof(T);
  784.  if(!(position < npos - offset)) { position = npos; return; }
  785.  size_type newpos = position + offset;
  786.  if(!can_seek(newpos)) { position = npos; return; }
  787.  memmove(ptr, c_pos(), offset);
  788.  position = newpos;
  789. }
  790.  
  791. void binary_stream::LE_read(uint64* ptr, size_type n, size_type pos)
  792. {
  793.  typedef uint64 T;
  794.  if(fail() || !ptr || !n) return;
  795.  size_type offset = n*sizeof(T);
  796.  if(!(position < npos - offset)) { position = npos; return; }
  797.  size_type newpos = position + offset;
  798.  if(!can_seek(newpos)) { position = npos; return; }
  799.  memmove(ptr, c_pos(), offset);
  800.  position = newpos;
  801. }
  802.  
  803. void binary_stream::BE_read(uint08* ptr, size_type n)
  804. {
  805.  LE_read(ptr, n);
  806.  if(good()) reverse_byte_order(ptr, n);
  807. }
  808.  
  809. void binary_stream::BE_read(uint16* ptr, size_type n)
  810. {
  811.  LE_read(ptr, n);
  812.  if(good()) reverse_byte_order(ptr, n);
  813. }
  814.  
  815. void binary_stream::BE_read(uint32* ptr, size_type n)
  816. {
  817.  LE_read(ptr, n);
  818.  if(good()) reverse_byte_order(ptr, n);
  819. }
  820.  
  821. void binary_stream::BE_read(uint64* ptr, size_type n)
  822. {
  823.  LE_read(ptr, n);
  824.  if(good()) reverse_byte_order(ptr, n);
  825. }
  826.  
  827. void binary_stream::BE_read(uint08* ptr, size_type n, size_type pos)
  828. {
  829.  LE_read(ptr, n, pos);
  830.  if(good()) reverse_byte_order(ptr, n);
  831. }
  832.  
  833. void binary_stream::BE_read(uint16* ptr, size_type n, size_type pos)
  834. {
  835.  LE_read(ptr, n, pos);
  836.  if(good()) reverse_byte_order(ptr, n);
  837. }
  838.  
  839. void binary_stream::BE_read(uint32* ptr, size_type n, size_type pos)
  840. {
  841.  LE_read(ptr, n, pos);
  842.  if(good()) reverse_byte_order(ptr, n);
  843. }
  844.  
  845. void binary_stream::BE_read(uint64* ptr, size_type n, size_type pos)
  846. {
  847.  LE_read(ptr, n, pos);
  848.  if(good()) reverse_byte_order(ptr, n);
  849. }
  850.  
  851. void binary_stream::LE_read(float32* ptr, size_type n)
  852. {
  853.  // validate arguments
  854.  typedef float32 T;
  855.  if(fail() || !ptr || !n) return;
  856.  
  857.  // compute offset
  858.  size_type offset = n*sizeof(T);
  859.  if(!(position < npos - offset)) { position = npos; return; }
  860.  
  861.  // compute newpos
  862.  size_type newpos = position + offset;
  863.  if(!can_seek(newpos)) { position = npos; return; }
  864.  
  865.  // copy data from current position
  866.  memmove(ptr, c_pos(), offset);
  867.  position = newpos;
  868. }
  869.  
  870. void binary_stream::LE_read(float64* ptr, size_type n)
  871. {
  872.  typedef float64 T;
  873.  if(fail() || !ptr || !n) return;
  874.  size_type offset = n*sizeof(T);
  875.  if(!(position < npos - offset)) { position = npos; return; }
  876.  size_type newpos = position + offset;
  877.  if(!can_seek(newpos)) { position = npos; return; }
  878.  memmove(ptr, c_pos(), offset);
  879.  position = newpos;
  880. }
  881.  
  882. void binary_stream::LE_read(float32* ptr, size_type n, size_type pos)
  883. {
  884.  // validate arguments
  885.  typedef float32 T;
  886.  if(fail() || !ptr || !n) return;
  887.  
  888.  // compute offset
  889.  size_type offset = n*sizeof(T);
  890.  if(!(pos < npos - offset)) { position = npos; return; }
  891.  
  892.  // compute newpos
  893.  size_type newpos = pos + offset;
  894.  if(!can_seek(newpos)) { position = npos; return; }
  895.  
  896.  // copy data from selected position
  897.  memmove(ptr, c_str(pos), offset);
  898. }
  899.  
  900. void binary_stream::LE_read(float64* ptr, size_type n, size_type pos)
  901. {
  902.  typedef float64 T;
  903.  if(fail() || !ptr || !n) return;
  904.  size_type offset = n*sizeof(T);
  905.  if(!(pos < npos - offset)) { position = npos; return; }
  906.  size_type newpos = pos + offset;
  907.  if(!can_seek(newpos)) { position = npos; return; }
  908.  memmove(ptr, c_str(pos), offset);
  909. }
  910.  
  911. void binary_stream::BE_read(float32* ptr, size_type n)
  912. {
  913.  LE_read(ptr, n);
  914.  if(good()) reverse_byte_order(ptr, n);
  915. }
  916.  
  917. void binary_stream::BE_read(float64* ptr, size_type n)
  918. {
  919.  LE_read(ptr, n);
  920.  if(good()) reverse_byte_order(ptr, n);
  921. }
  922.  
  923. void binary_stream::BE_read(float32* ptr, size_type n, size_type pos)
  924. {
  925.  LE_read(ptr, n, pos);
  926.  if(good()) reverse_byte_order(ptr, n);
  927. }
  928.  
  929. void binary_stream::BE_read(float64* ptr, size_type n, size_type pos)
  930. {
  931.  LE_read(ptr, n, pos);
  932.  if(good()) reverse_byte_order(ptr, n);
  933. }
  934.  
  935. void binary_stream::LE_read_float16(float32* ptr, size_type n)
  936. {
  937.  // validate arguments
  938.  size_type typesize = sizeof(uint16);
  939.  if(fail() || !ptr || !n) return;
  940.  
  941.  // compute offset
  942.  size_type offset = n*typesize;
  943.  if(!(position < npos - offset)) { position = npos; return; }
  944.  
  945.  // compute newpos
  946.  size_type newpos = position + offset;
  947.  if(!can_seek(newpos)) { position = npos; return; }
  948.  
  949.  // copy data from current position
  950.  for(size_type i = 0; i < n; i++) ptr[i] = float_16_to_32(*reinterpret_cast<const uint16*>(c_str(position + i*typesize)));
  951.  position = newpos;
  952. }
  953.  
  954. void binary_stream::LE_read_float16(float64* ptr, size_type n)
  955. {
  956.  size_type typesize = sizeof(uint16);
  957.  if(fail() || !ptr || !n) return;
  958.  size_type offset = n*typesize;
  959.  if(!(position < npos - offset)) { position = npos; return; }
  960.  size_type newpos = position + offset;
  961.  if(!can_seek(newpos)) { position = npos; return; }
  962.  for(size_type i = 0; i < n; i++) ptr[i] = float_16_to_32(*reinterpret_cast<const uint16*>(c_str(position + i*typesize)));
  963.  position = newpos;
  964. }
  965.  
  966. void binary_stream::LE_read_float16(float32* ptr, size_type n, size_type pos)
  967. {
  968.  // validate arguments
  969.  size_type typesize = sizeof(uint16);
  970.  if(fail() || !ptr || !n) return;
  971.  
  972.  // compute offset
  973.  size_type offset = n*typesize;
  974.  if(!(pos < npos - offset)) { position = npos; return; }
  975.  
  976.  // compute newpos
  977.  size_type newpos = pos + offset;
  978.  if(!can_seek(newpos)) { position = npos; return; }
  979.  
  980.  // copy data from selected position
  981.  for(size_type i = 0; i < n; i++) ptr[i] = float_16_to_32(*reinterpret_cast<const uint16*>(c_str(pos + i*typesize)));
  982. }
  983.  
  984. void binary_stream::LE_read_float16(float64* ptr, size_type n, size_type pos)
  985. {
  986.  size_type typesize = sizeof(uint16);
  987.  if(fail() || !ptr || !n) return;
  988.  size_type offset = n*typesize;
  989.  if(!(pos < npos - offset)) { position = npos; return; }
  990.  size_type newpos = pos + offset;
  991.  if(!can_seek(newpos)) { position = npos; return; }
  992.  for(size_type i = 0; i < n; i++) ptr[i] = float_16_to_32(*reinterpret_cast<const uint16*>(c_str(pos + i*typesize)));
  993. }
  994.  
  995. void binary_stream::BE_read_float16(float32* ptr, size_type n)
  996. {
  997.  // validate arguments
  998.  size_type typesize = sizeof(uint16);
  999.  if(fail() || !ptr || !n) return;
  1000.  
  1001.  // compute offset
  1002.  size_type offset = n*typesize;
  1003.  if(!(position < npos - offset)) { position = npos; return; }
  1004.  
  1005.  // compute newpos
  1006.  size_type newpos = position + offset;
  1007.  if(!can_seek(newpos)) { position = npos; return; }
  1008.  
  1009.  // copy data from current position
  1010.  for(size_type i = 0; i < n; i++) {
  1011.      uint16 temp = *reinterpret_cast<const uint16*>(c_str(position + i*typesize));
  1012.      reverse_byte_order(&temp);
  1013.      ptr[i] = float_16_to_32(temp);
  1014.     }
  1015.  
  1016.  // move to newpos
  1017.  position = newpos;
  1018. }
  1019.  
  1020. void binary_stream::BE_read_float16(float64* ptr, size_type n)
  1021. {
  1022.  size_type typesize = sizeof(uint16);
  1023.  if(fail() || !ptr || !n) return;
  1024.  size_type offset = n*typesize;
  1025.  if(!(position < npos - offset)) { position = npos; return; }
  1026.  size_type newpos = position + offset;
  1027.  if(!can_seek(newpos)) { position = npos; return; }
  1028.  for(size_type i = 0; i < n; i++) {
  1029.      uint16 temp = *reinterpret_cast<const uint16*>(c_str(position + i*typesize));
  1030.      reverse_byte_order(&temp);
  1031.      ptr[i] = float_16_to_32(temp);
  1032.     }
  1033.  position = newpos;
  1034. }
  1035.  
  1036. void binary_stream::BE_read_float16(float32* ptr, size_type n, size_type pos)
  1037. {
  1038.  // validate arguments
  1039.  size_type typesize = sizeof(uint16);
  1040.  if(fail() || !ptr || !n) return;
  1041.  
  1042.  // compute offset
  1043.  size_type offset = n*typesize;
  1044.  if(!(pos < npos - offset)) { position = npos; return; }
  1045.  
  1046.  // compute newpos
  1047.  size_type newpos = pos + offset;
  1048.  if(!can_seek(newpos)) { position = npos; return; }
  1049.  
  1050.  // copy data from selected position
  1051.  for(size_type i = 0; i < n; i++) {
  1052.      uint16 temp = *reinterpret_cast<const uint16*>(c_str(pos + i*typesize));
  1053.      reverse_byte_order(&temp);
  1054.      ptr[i] = float_16_to_32(temp);
  1055.     }
  1056. }
  1057.  
  1058. void binary_stream::BE_read_float16(float64* ptr, size_type n, size_type pos)
  1059. {
  1060.  size_type typesize = sizeof(uint16);
  1061.  if(fail() || !ptr || !n) return;
  1062.  size_type offset = n*typesize;
  1063.  if(!(pos < npos - offset)) { position = npos; return; }
  1064.  size_type newpos = pos + offset;
  1065.  if(!can_seek(newpos)) { position = npos; return; }
  1066.  for(size_type i = 0; i < n; i++) {
  1067.      uint16 temp = *reinterpret_cast<const uint16*>(c_str(pos + i*typesize));
  1068.      reverse_byte_order(&temp);
  1069.      ptr[i] = float_16_to_32(temp);
  1070.     }
  1071. }