dinkumware.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #ifndef BOOST_ARCHIVE_DINKUMWARE_HPP
  2. #define BOOST_ARCHIVE_DINKUMWARE_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // dinkumware.hpp:
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. // this file adds a couple of things that are missing from the dinkumware
  15. // implementation of the standard library.
  16. #include <iterator>
  17. #include <string>
  18. #include <boost/config.hpp>
  19. #include <boost/cstdint.hpp>
  20. namespace std {
  21. // define i/o operators for 64 bit integers
  22. template<class CharType>
  23. basic_ostream<CharType> &
  24. operator<<(basic_ostream<CharType> & os, boost::uint64_t t){
  25. // octal rendering of 64 bit number would be 22 octets + eos
  26. CharType d[23];
  27. unsigned int radix;
  28. if(os.flags() & (int)std::ios_base::hex)
  29. radix = 16;
  30. else
  31. if(os.flags() & (int)std::ios_base::oct)
  32. radix = 8;
  33. else
  34. //if(s.flags() & (int)std::ios_base::dec)
  35. radix = 10;
  36. unsigned int i = 0;
  37. do{
  38. unsigned int j = t % radix;
  39. d[i++] = j + ((j < 10) ? '0' : ('a' - 10));
  40. t /= radix;
  41. }
  42. while(t > 0);
  43. d[i--] = '\0';
  44. // reverse digits
  45. unsigned int j = 0;
  46. while(j < i){
  47. CharType k = d[i];
  48. d[i] = d[j];
  49. d[j] = k;
  50. --i;++j;
  51. }
  52. os << d;
  53. return os;
  54. }
  55. template<class CharType>
  56. basic_ostream<CharType> &
  57. operator<<(basic_ostream<CharType> &os, boost::int64_t t){
  58. if(0 <= t){
  59. os << static_cast<boost::uint64_t>(t);
  60. }
  61. else{
  62. os.put('-');
  63. os << -t;
  64. }
  65. return os;
  66. }
  67. template<class CharType>
  68. basic_istream<CharType> &
  69. operator>>(basic_istream<CharType> &is, boost::int64_t & t){
  70. CharType d;
  71. do{
  72. d = is.get();
  73. }
  74. while(::isspace(d));
  75. bool negative = (d == '-');
  76. if(negative)
  77. d = is.get();
  78. unsigned int radix;
  79. if(is.flags() & (int)std::ios_base::hex)
  80. radix = 16;
  81. else
  82. if(is.flags() & (int)std::ios_base::oct)
  83. radix = 8;
  84. else
  85. //if(s.flags() & (int)std::ios_base::dec)
  86. radix = 10;
  87. t = 0;
  88. do{
  89. if('0' <= d && d <= '9')
  90. t = t * radix + (d - '0');
  91. else
  92. if('a' <= d && d <= 'f')
  93. t = t * radix + (d - 'a' + 10);
  94. else
  95. break;
  96. d = is.get();
  97. }
  98. while(!is.fail());
  99. // restore the delimiter
  100. is.putback(d);
  101. is.clear();
  102. if(negative)
  103. t = -t;
  104. return is;
  105. }
  106. template<class CharType>
  107. basic_istream<CharType> &
  108. operator>>(basic_istream<CharType> &is, boost::uint64_t & t){
  109. boost::int64_t it;
  110. is >> it;
  111. t = it;
  112. return is;
  113. }
  114. template<>
  115. class back_insert_iterator<basic_string<char> > : public
  116. iterator<output_iterator_tag, char>
  117. {
  118. public:
  119. typedef basic_string<char> container_type;
  120. typedef container_type::reference reference;
  121. explicit back_insert_iterator(container_type & s)
  122. : container(& s)
  123. {} // construct with container
  124. back_insert_iterator<container_type> & operator=(
  125. container_type::const_reference Val_
  126. ){ // push value into container
  127. //container->push_back(Val_);
  128. *container += Val_;
  129. return (*this);
  130. }
  131. back_insert_iterator<container_type> & operator*(){
  132. return (*this);
  133. }
  134. back_insert_iterator<container_type> & operator++(){
  135. // pretend to preincrement
  136. return (*this);
  137. }
  138. back_insert_iterator<container_type> operator++(int){
  139. // pretend to postincrement
  140. return (*this);
  141. }
  142. protected:
  143. container_type *container; // pointer to container
  144. };
  145. template<char>
  146. inline back_insert_iterator<basic_string<char> > back_inserter(
  147. basic_string<char> & s
  148. ){
  149. return (std::back_insert_iterator<basic_string<char> >(s));
  150. }
  151. template<>
  152. class back_insert_iterator<basic_string<wchar_t> > : public
  153. iterator<output_iterator_tag, wchar_t>
  154. {
  155. public:
  156. typedef basic_string<wchar_t> container_type;
  157. typedef container_type::reference reference;
  158. explicit back_insert_iterator(container_type & s)
  159. : container(& s)
  160. {} // construct with container
  161. back_insert_iterator<container_type> & operator=(
  162. container_type::const_reference Val_
  163. ){ // push value into container
  164. //container->push_back(Val_);
  165. *container += Val_;
  166. return (*this);
  167. }
  168. back_insert_iterator<container_type> & operator*(){
  169. return (*this);
  170. }
  171. back_insert_iterator<container_type> & operator++(){
  172. // pretend to preincrement
  173. return (*this);
  174. }
  175. back_insert_iterator<container_type> operator++(int){
  176. // pretend to postincrement
  177. return (*this);
  178. }
  179. protected:
  180. container_type *container; // pointer to container
  181. };
  182. template<wchar_t>
  183. inline back_insert_iterator<basic_string<wchar_t> > back_inserter(
  184. basic_string<wchar_t> & s
  185. ){
  186. return (std::back_insert_iterator<basic_string<wchar_t> >(s));
  187. }
  188. } // namespace std
  189. #endif //BOOST_ARCHIVE_DINKUMWARE_HPP