escaper.hxx

Go to the documentation of this file.
00001 // escaper.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2004 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_MISC_ESCAPER_HXX
00018 # define VCSN_MISC_ESCAPER_HXX
00019 
00028 # include <string>
00029 # include <sstream>
00030 
00031 # include <vaucanson/misc/escaper.hh>
00032 # include <vaucanson/tools/usual_escaped_characters.hh>
00033 
00034 namespace utility
00035 {
00036 
00038   inline
00039   int
00040   escaped()
00041   {
00042     static const int idx = std::ios::xalloc();
00043     return idx;
00044   }
00045 
00046   /*--------.
00047   | escaper |
00048   `--------*/
00049 
00050   template <class T>
00051   escaper<T>::escaper(const T& w) : w_ (w)
00052   {
00053   }
00054 
00055   template <class T>
00056   std::ostream&
00057   escaper<T>::operator () (std::ostream& ostr) const
00058   {
00059     std::ostringstream          o;
00060     o << w_;
00061     std::string                 w = o.str();
00062     const std::set<char>&       e = getesc(ostr);
00063     for (std::string::const_iterator i = w.begin(); i != w.end(); ++i)
00064       if (e.find(*i) != e.end())
00065         ostr << "\\" << *i;
00066       else
00067         ostr << *i;
00068     return ostr;
00069   }
00070 
00071   /*-------------.
00072   | make_escaper |
00073   `-------------*/
00074 
00075   template <class T>
00076   escaper<T>
00077   make_escaper(const T& w)
00078   {
00079     return escaper<T> (w);
00080   }
00081 
00082   /*-------.
00083   | setesc |
00084   `-------*/
00085 
00086   inline
00087   setesc::setesc(const std::set<char>& s) : s_ (s)
00088   {
00089   }
00090 
00091   inline
00092   std::ostream&
00093   setesc::operator () (std::ostream& ostr) const
00094   {
00095     typedef std::set<char>      esc_set;
00096     const int                   idx = escaped();
00097 
00098     if (not ostr.pword(idx))
00099       ostr.register_callback(pword_delete<esc_set>, idx);
00100     else
00101       delete static_cast<esc_set*> (ostr.pword(idx));
00102     ostr.pword(idx) = new esc_set (s_);
00103     return ostr;
00104   }
00105 
00106   /*-------.
00107   | getesc |
00108   `-------*/
00109 
00110   inline
00111   std::set<char>& getesc(std::ostream& ostr)
00112   {
00113     const int idx = escaped();
00114 
00115     if (not ostr.pword(idx))
00116       ostr << setesc (vcsn::tools::usual_escaped_characters());
00117     return *static_cast<std::set<char>*> (ostr.pword(idx));
00118   }
00119 
00120 } // End of namespace utility.
00121 
00122 #endif // ! VCSN_MISC_ESCAPER_HXX

Generated on Fri Jul 28 12:18:31 2006 for Vaucanson by  doxygen 1.4.6