dinkumware.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. //#endif
  115. template<>
  116. class back_insert_iterator<basic_string<char> > : public
  117. iterator<output_iterator_tag, char>
  118. {
  119. public:
  120. typedef basic_string<char> container_type;
  121. typedef container_type::reference reference;
  122. explicit back_insert_iterator(container_type & s)
  123. : container(& s)
  124. {} // construct with container
  125. back_insert_iterator<container_type> & operator=(
  126. container_type::const_reference Val_
  127. ){ // push value into container
  128. //container->push_back(Val_);
  129. *container += Val_;
  130. return (*this);
  131. }
  132. back_insert_iterator<container_type> & operator*(){
  133. return (*this);
  134. }
  135. back_insert_iterator<container_type> & operator++(){
  136. // pretend to preincrement
  137. return (*this);
  138. }
  139. back_insert_iterator<container_type> operator++(int){
  140. // pretend to postincrement
  141. return (*this);
  142. }
  143. protected:
  144. container_type *container; // pointer to container
  145. };
  146. template<char>
  147. inline back_insert_iterator<basic_string<char> > back_inserter(
  148. basic_string<char> & s
  149. ){
  150. return (std::back_insert_iterator<basic_string<char> >(s));
  151. }
  152. template<>
  153. class back_insert_iterator<basic_string<wchar_t> > : public
  154. iterator<output_iterator_tag, wchar_t>
  155. {
  156. public:
  157. typedef basic_string<wchar_t> container_type;
  158. typedef container_type::reference reference;
  159. explicit back_insert_iterator(container_type & s)
  160. : container(& s)
  161. {} // construct with container
  162. back_insert_iterator<container_type> & operator=(
  163. container_type::const_reference Val_
  164. ){ // push value into container
  165. //container->push_back(Val_);
  166. *container += Val_;
  167. return (*this);
  168. }
  169. back_insert_iterator<container_type> & operator*(){
  170. return (*this);
  171. }
  172. back_insert_iterator<container_type> & operator++(){
  173. // pretend to preincrement
  174. return (*this);
  175. }
  176. back_insert_iterator<container_type> operator++(int){
  177. // pretend to postincrement
  178. return (*this);
  179. }
  180. protected:
  181. container_type *container; // pointer to container
  182. };
  183. template<wchar_t>
  184. inline back_insert_iterator<basic_string<wchar_t> > back_inserter(
  185. basic_string<wchar_t> & s
  186. ){
  187. return (std::back_insert_iterator<basic_string<wchar_t> >(s));
  188. }
  189. } // namespace std
  190. #endif //BOOST_ARCHIVE_DINKUMWARE_HPP