]> git.lyx.org Git - lyx.git/blobdiff - boost/boost/format/internals.hpp
64-bit fix to boost::format.
[lyx.git] / boost / boost / format / internals.hpp
index 52448b731c2f6da09ba656d7354cfe3b831dba64..7690373b7b5d6c77b7add03d453b01b910af83a8 100644 (file)
 
 
 #include <string>
-#include <sstream>
+
+#include <boost/assert.hpp>
+#include <boost/format/outsstream.hpp>
+#include <boost/limits.hpp>
 
 namespace boost {
 namespace io {
 namespace detail {
 
 
-// --------------
-// set of params that define the format state of a stream
-
-template<class Ch, class Tr> 
-struct stream_format_state 
-{
-  typedef BOOST_IO_STD basic_ios<Ch, Tr>   basic_ios;
-
-  std::streamsize width_;
-  std::streamsize precision_;
-  Ch fill_; 
-  std::ios_base::fmtflags flags_;
-
-  stream_format_state()       : width_(-1), precision_(-1), fill_(0), flags_(std::ios_base::dec)  {}
-  stream_format_state(basic_ios& os)                  {set_by_stream(os); }
-
-  void apply_on(basic_ios & os) const;                //- applies format_state to the stream
-  template<class T> void apply_manip(T manipulator)   //- modifies state by applying manipulator.
-       { apply_manip_body<Ch, Tr, T>( *this, manipulator) ; }
-  void reset();                                       //- sets to default state.
-  void set_by_stream(const basic_ios& os);            //- sets to os's state.
-};  
-
-
-
-// --------------
-// format_item : stores all parameters that can be defined by directives in the format-string
-
-template<class Ch, class Tr>  
-struct format_item 
-{     
-  enum pad_values { zeropad = 1, spacepad =2, centered=4, tabulation = 8 };
-
-  enum arg_values { argN_no_posit   = -1, // non-positional directive. argN will be set later.
-                    argN_tabulation = -2, // tabulation directive. (no argument read) 
-                    argN_ignored    = -3  // ignored directive. (no argument read)
-  };
-  typedef BOOST_IO_STD basic_ios<Ch, Tr>              basic_ios;
-  typedef detail::stream_format_state<Ch, Tr>         stream_format_state;
-  typedef std::basic_string<Ch, Tr>           string_t;
-  typedef BOOST_IO_STD basic_ostringstream<Ch, Tr>    internal_stream_t;
-
-
-  int         argN_;           //- argument number (starts at 0,  eg : %1 => argN=0)
-                               //  negative values are used for items that don't process
-                               //  an argument
-  string_t    res_;            //- result of the formatting of this item
-  string_t    appendix_;       //- piece of string between this item and the next
-
-  stream_format_state ref_state_;// set by parsing the format_string, is only affected by modify_item
-  stream_format_state state_;  // always same as ref_state, _unless_ modified by manipulators 'group(..)'
-
-  // non-stream format-state parameters
-  signed int truncate_;        //- is >=0 for directives like %.5s (take 5 chars from the string)
-  unsigned int pad_scheme_;    //- several possible padding schemes can mix. see pad_values
-
-  format_item() : argN_(argN_no_posit), truncate_(-1), pad_scheme_(0)  {}
-
-  void compute_states();      // sets states  according to truncate and pad_scheme.
-}; 
-
-
-
-// -----------------------------------------------------------
-// Definitions
-// -----------------------------------------------------------
-
-// --- stream_format_state:: -------------------------------------------
-template<class Ch, class Tr> inline
-void stream_format_state<Ch,Tr> ::apply_on(basic_ios & os) const
-  // set the state of this stream according to our params
-{
-      if(width_ != -1)
-        os.width(width_);
-      if(precision_ != -1)
-        os.precision(precision_);
-      if(fill_ != 0)
-        os.fill(fill_);
-      os.flags(flags_);
-}
-
-template<class Ch, class Tr>      inline
-void stream_format_state<Ch,Tr> ::set_by_stream(const basic_ios& os) 
-  // set our params according to the state of this stream
-{
-      flags_ = os.flags();
-      width_ = os.width();
-      precision_ = os.precision();
-      fill_ = os.fill();
-}
-
-template<class Ch, class Tr, class T>  inline
-void apply_manip_body( stream_format_state<Ch, Tr>& self,
-                       T manipulator) 
-  // modify our params according to the manipulator
-{
-      BOOST_IO_STD basic_stringstream<Ch, Tr>  ss;
-      self.apply_on( ss );
-      ss << manipulator;
-      self.set_by_stream( ss );
-}
-
-template<class Ch, class Tr> inline
-void stream_format_state<Ch,Tr> ::reset() 
-  // set our params to standard's default state
-{
-      width_=-1; precision_=-1; fill_=0; 
-      flags_ = std::ios_base::dec; 
-}
-
-
-// --- format_items:: -------------------------------------------
-template<class Ch, class Tr> inline
-void format_item<Ch, Tr> ::compute_states() 
-  // reflect pad_scheme_   on  state_ and ref_state_ 
-  //   because some pad_schemes has complex consequences on several state params.
-{
-  if(pad_scheme_ & zeropad) 
-  {
-    if(ref_state_.flags_ & std::ios_base::left) 
+//----- stream_format_state --------------------------------------------------//
+
+//   set of params that define the format state of a stream
+    template<class Ch, class Tr> 
+    struct stream_format_state 
     {
-      pad_scheme_ = pad_scheme_ & (~zeropad); // ignore zeropad in left alignment
+        typedef BOOST_IO_STD basic_ios<Ch, Tr>   basic_ios;
+
+        stream_format_state(Ch fill)                 { reset(fill); }
+        stream_format_state(const basic_ios& os)     { set_by_stream(os); }
+
+        void reset(Ch fill);                     //- sets to default state.
+        void set_by_stream(const basic_ios& os); //- sets to os's state.
+        void apply_on(basic_ios & os) const;     //- applies format_state to the stream
+        template<class T> 
+        void apply_manip(T manipulator)          //- modifies state by applying manipulator
+            { apply_manip_body<Ch, Tr, T>( *this, manipulator) ; }
+
+        // --- data ---
+        std::streamsize width_;
+        std::streamsize precision_;
+        Ch fill_; 
+        std::ios_base::fmtflags flags_;
+        std::ios_base::iostate  rdstate_;
+        std::ios_base::iostate  exceptions_;
+    };  
+
+
+//----- format_item  ---------------------------------------------------------//
+
+//   stores all parameters that can be specified in format strings
+    template<class Ch, class Tr>  
+    struct format_item 
+    {     
+        enum pad_values { zeropad = 1, spacepad =2, centered=4, tabulation = 8 };
+                         // 1. if zeropad is set, all other bits are not, 
+                         // 2. if tabulation is set, all others are not.
+                         // centered and spacepad can be mixed freely.
+        enum arg_values { argN_no_posit   = -1, // non-positional directive. will set argN later
+                          argN_tabulation = -2, // tabulation directive. (no argument read) 
+                          argN_ignored    = -3  // ignored directive. (no argument read)
+        };
+        typedef BOOST_IO_STD basic_ios<Ch, Tr>              basic_ios;
+        typedef detail::stream_format_state<Ch, Tr>         stream_format_state;
+        typedef std::basic_string<Ch, Tr>                   string_t;
+
+        format_item(Ch fill) :argN_(argN_no_posit), fmtstate_(fill), 
+                              truncate_(max_streamsize()), pad_scheme_(0)  {}
+        void reset(Ch fill);
+        void compute_states(); // sets states  according to truncate and pad_scheme.
+
+        static std::streamsize max_streamsize() { 
+            return std::numeric_limits<std::streamsize>::max();
+        }
+
+        // --- data ---
+        int         argN_;  //- argument number (starts at 0,  eg : %1 => argN=0)
+                            //  negative values for items that don't process an argument
+        string_t    res_;      //- result of the formatting of this item
+        string_t    appendix_; //- piece of string between this item and the next
+
+        stream_format_state fmtstate_;// set by parsing, is only affected by modify_item
+
+        std::streamsize truncate_;    //- is set for directives like %.5s that ask truncation
+        unsigned int pad_scheme_;//- several possible padding schemes can mix. see pad_values
+    }; 
+
+
+
+//---- Definitions  ------------------------------------------------------------
+
+// ---   stream_format_state:: -------------------------------------------------
+    template<class Ch, class Tr>
+    void stream_format_state<Ch,Tr>:: apply_on(basic_ios & os) const {
+        // set the state of this stream according to our params
+        if(width_ != -1)
+            os.width(width_);
+        if(precision_ != -1)
+            os.precision(precision_);
+        if(fill_ != 0)
+            os.fill(fill_);
+        os.flags(flags_);
+        os.clear(rdstate_);
+        os.exceptions(exceptions_);
+    }
+
+    template<class Ch, class Tr>
+    void stream_format_state<Ch,Tr>:: set_by_stream(const basic_ios& os) {
+        // set our params according to the state of this stream
+        flags_ = os.flags();
+        width_ = os.width();
+        precision_ = os.precision();
+        fill_ = os.fill();
+        rdstate_ = os.rdstate();
+        exceptions_ = os.exceptions();
+    }
+
+
+    template<class Ch, class Tr, class T>
+    void apply_manip_body( stream_format_state<Ch, Tr>& self,
+                           T manipulator) {
+        // modify our params according to the manipulator
+        basic_outsstream<Ch, Tr>  ss;
+        self.apply_on( ss );
+        ss << manipulator;
+        self.set_by_stream( ss );
     }
-    else 
-    { 
-      ref_state_.fill_='0'; 
-      ref_state_.flags_ |= std::ios_base::internal;
+
+    template<class Ch, class Tr> inline
+    void stream_format_state<Ch,Tr>:: reset(Ch fill) {
+        // set our params to standard's default state.   cf ยง 27.4.4.1 of the C++ norm
+        width_=0; precision_=6; 
+        fill_=fill; // default is widen(' '), but we cant compute it without the locale
+        flags_ = std::ios_base::dec | std::ios_base::skipws; 
+        // the adjust_field part is left equal to 0, which means right.
+        exceptions_ = std::ios_base::goodbit;
+        rdstate_ = std::ios_base::goodbit;
+    }
+
+
+// ---   format_item:: --------------------------------------------------------
+
+    template<class Ch, class Tr> 
+    void format_item<Ch, Tr>:: 
+    reset(Ch fill) { 
+        argN_=argN_no_posit; truncate_ = max_streamsize(); pad_scheme_ =0; 
+        res_.resize(0); appendix_.resize(0);
+        fmtstate_.reset(fill);
+    }
+
+    template<class Ch, class Tr> 
+    void format_item<Ch, Tr>:: compute_states() {
+        // reflect pad_scheme_   on  fmt_state_
+        //   because some pad_schemes has complex consequences on several state params.
+        if(pad_scheme_ & zeropad) {
+            // ignore zeropad in left alignment :
+            if(fmtstate_.flags_ & std::ios_base::left) {
+              BOOST_ASSERT(!(fmtstate_.flags_ &(std::ios_base::adjustfield ^std::ios_base::left)));
+              // only left bit might be set. (not right, nor internal)
+              pad_scheme_ = pad_scheme_ & (~zeropad); 
+            }
+            else { 
+                pad_scheme_ &= ~spacepad; // printf ignores spacepad when zeropadding
+                fmtstate_.fill_='0'; 
+                fmtstate_.flags_ = (fmtstate_.flags_ & ~std::ios_base::adjustfield) 
+                    | std::ios_base::internal;
+                // removes all adjustfield bits, and adds internal.
+            }
+        }
+        if(pad_scheme_ & spacepad) {
+            if(fmtstate_.flags_ & std::ios_base::showpos)
+                pad_scheme_ &= ~spacepad;
+        }
     }
-  }
-  state_ = ref_state_;
-}
 
 
 } } } // namespaces boost :: io :: detail