format_implementation.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // -*- C++ -*-
  2. // Boost general library format ---------------------------
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. // (C) Samuel Krempp 2001
  5. // krempp@crans.ens-cachan.fr
  6. // Permission to copy, use, modify, sell and
  7. // distribute this software is granted provided this copyright notice appears
  8. // in all copies. This software is provided "as is" without express or implied
  9. // warranty, and with no claim as to its suitability for any purpose.
  10. // ideas taken from Rüdiger Loos's format class
  11. // and Karl Nelson's ofstream
  12. // ----------------------------------------------------------------------------
  13. // format_implementation.hpp Implementation of the basic_format class
  14. // ----------------------------------------------------------------------------
  15. #ifndef BOOST_FORMAT_IMPLEMENTATION_HPP
  16. #define BOOST_FORMAT_IMPLEMENTATION_HPP
  17. #include <boost/throw_exception.hpp>
  18. #include <boost/assert.hpp>
  19. #include <boost/format.hpp>
  20. namespace boost {
  21. // -------- format:: -------------------------------------------
  22. basic_format::basic_format(const char* str)
  23. : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
  24. items_(), oss_(), exceptions_(io::all_error_bits)
  25. {
  26. state0_.set_by_stream(oss_);
  27. string_t emptyStr;
  28. if( !str) str = emptyStr.c_str();
  29. parse( str );
  30. }
  31. #ifndef BOOST_NO_STD_LOCALE
  32. basic_format::basic_format(const char* str, const std::locale & loc)
  33. : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
  34. items_(), oss_(), exceptions_(io::all_error_bits)
  35. {
  36. oss_.imbue( loc );
  37. state0_.set_by_stream(oss_);
  38. string_t emptyStr;
  39. if( !str) str = emptyStr.c_str();
  40. parse( str );
  41. }
  42. basic_format::basic_format(const string_t& s, const std::locale & loc)
  43. : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
  44. items_(), oss_(), exceptions_(io::all_error_bits)
  45. {
  46. oss_.imbue( loc );
  47. state0_.set_by_stream(oss_);
  48. parse(s);
  49. }
  50. #endif //BOOST_NO_STD_LOCALE
  51. basic_format::basic_format(const string_t& s)
  52. : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
  53. items_(), oss_(), exceptions_(io::all_error_bits)
  54. {
  55. state0_.set_by_stream(oss_);
  56. parse(s);
  57. }
  58. basic_format:: basic_format(const basic_format& x)
  59. : style_(x.style_), cur_arg_(x.cur_arg_), num_args_(x.num_args_), dumped_(false),
  60. items_(x.items_), prefix_(x.prefix_), bound_(x.bound_),
  61. oss_(), // <- we obviously can't copy x.oss_
  62. state0_(x.state0_), exceptions_(x.exceptions_)
  63. {
  64. state0_.apply_on(oss_);
  65. }
  66. basic_format& basic_format::operator= (const basic_format& x)
  67. {
  68. if(this == &x)
  69. return *this;
  70. state0_ = x.state0_;
  71. state0_.apply_on(oss_);
  72. // plus all the other (trivial) assignments :
  73. exceptions_ = x.exceptions_;
  74. items_ = x.items_;
  75. prefix_ = x.prefix_;
  76. bound_=x.bound_;
  77. style_=x.style_;
  78. cur_arg_=x.cur_arg_;
  79. num_args_=x.num_args_;
  80. dumped_=x.dumped_;
  81. return *this;
  82. }
  83. unsigned char basic_format::exceptions() const
  84. {
  85. return exceptions_;
  86. }
  87. unsigned char basic_format::exceptions(unsigned char newexcept)
  88. {
  89. unsigned char swp = exceptions_;
  90. exceptions_ = newexcept;
  91. return swp;
  92. }
  93. basic_format& basic_format ::clear()
  94. // empty the string buffers (except bound arguments, see clear_binds() )
  95. // and make the format object ready for formatting a new set of arguments
  96. {
  97. BOOST_ASSERT( bound_.size()==0 || num_args_ == static_cast<int>(bound_.size()) );
  98. for(unsigned long i=0; i<items_.size(); ++i){
  99. items_[i].state_ = items_[i].ref_state_;
  100. // clear converted strings only if the corresponding argument is not bound :
  101. if( bound_.size()==0 || !bound_[ items_[i].argN_ ] ) items_[i].res_.resize(0);
  102. }
  103. cur_arg_=0; dumped_=false;
  104. // maybe first arg is bound:
  105. if(bound_.size() != 0)
  106. {
  107. while(cur_arg_ < num_args_ && bound_[cur_arg_] ) ++cur_arg_;
  108. }
  109. return *this;
  110. }
  111. basic_format& basic_format ::clear_binds()
  112. // cancel all bindings, and clear()
  113. {
  114. bound_.resize(0);
  115. clear();
  116. return *this;
  117. }
  118. basic_format& basic_format::clear_bind(int argN)
  119. // cancel the binding of ONE argument, and clear()
  120. {
  121. if(argN<1 || argN > num_args_ || bound_.size()==0 || !bound_[argN-1] )
  122. {
  123. if( exceptions() & io::out_of_range_bit )
  124. boost::throw_exception(io::out_of_range()); // arg not in range.
  125. else return *this;
  126. }
  127. bound_[argN-1]=false;
  128. clear();
  129. return *this;
  130. }
  131. std::string basic_format::str() const
  132. {
  133. dumped_=true;
  134. if(items_.size()==0)
  135. return prefix_;
  136. if( cur_arg_ < num_args_)
  137. if( exceptions() & io::too_few_args_bit )
  138. boost::throw_exception(io::too_few_args()); // not enough variables have been supplied !
  139. unsigned long sz = prefix_.size();
  140. unsigned long i;
  141. for(i=0; i < items_.size(); ++i)
  142. sz += items_[i].res_.size() + items_[i].appendix_.size();
  143. string_t res;
  144. res.reserve(sz);
  145. res += prefix_;
  146. for(i=0; i < items_.size(); ++i)
  147. {
  148. const format_item_t& item = items_[i];
  149. res += item.res_;
  150. if( item.argN_ == format_item_t::argN_tabulation)
  151. {
  152. BOOST_ASSERT( item.pad_scheme_ & format_item_t::tabulation);
  153. std::streamsize n = item.state_.width_ - res.size();
  154. if( n > 0 )
  155. res.append( n, item.state_.fill_ );
  156. }
  157. res += item.appendix_;
  158. }
  159. return res;
  160. }
  161. namespace io {
  162. namespace detail {
  163. template<class T>
  164. basic_format& bind_arg_body( basic_format& self,
  165. int argN,
  166. const T& val)
  167. // bind one argument to a fixed value
  168. // this is persistent over clear() calls, thus also over str() and <<
  169. {
  170. if(self.dumped_) self.clear(); // needed, because we will modify cur_arg_..
  171. if(argN<1 || argN > self.num_args_)
  172. {
  173. if( self.exceptions() & io::out_of_range_bit )
  174. boost::throw_exception(io::out_of_range()); // arg not in range.
  175. else return self;
  176. }
  177. if(self.bound_.size()==0)
  178. self.bound_.assign(self.num_args_,false);
  179. else
  180. BOOST_ASSERT( self.num_args_ == static_cast<signed int>(self.bound_.size()) );
  181. int o_cur_arg = self.cur_arg_;
  182. self.cur_arg_ = argN-1; // arrays begin at 0
  183. self.bound_[self.cur_arg_]=false; // if already set, we unset and re-sets..
  184. self.operator%(val); // put val at the right place, because cur_arg is set
  185. // Now re-position cur_arg before leaving :
  186. self.cur_arg_ = o_cur_arg;
  187. self.bound_[argN-1]=true;
  188. if(self.cur_arg_ == argN-1 )
  189. // hum, now this arg is bound, so move to next free arg
  190. {
  191. while(self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_]) ++self.cur_arg_;
  192. }
  193. // In any case, we either have all args, or are on a non-binded arg :
  194. BOOST_ASSERT( self.cur_arg_ >= self.num_args_ || ! self.bound_[self.cur_arg_]);
  195. return self;
  196. }
  197. template<class T>
  198. basic_format& modify_item_body( basic_format& self,
  199. int itemN,
  200. const T& manipulator)
  201. // applies a manipulator to the format_item describing a given directive.
  202. // this is a permanent change, clear or clear_binds won't cancel that.
  203. {
  204. if(itemN<1 || itemN >= static_cast<signed int>(self.items_.size() ))
  205. {
  206. if( self.exceptions() & io::out_of_range_bit )
  207. boost::throw_exception(io::out_of_range()); // item not in range.
  208. else return self;
  209. }
  210. self.items_[itemN-1].ref_state_.apply_manip( manipulator );
  211. self.items_[itemN-1].state_ = self.items_[itemN-1].ref_state_;
  212. return self;
  213. }
  214. } // namespace detail
  215. } // namespace io
  216. } // namespace boost
  217. #endif // BOOST_FORMAT_IMPLEMENTATION_HPP