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

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