00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_LETTER_INT_LETTER_HXX
00018 # define VCSN_ALGEBRA_IMPLEMENTATION_LETTER_INT_LETTER_HXX
00019 
00020 # include <sstream>
00021 # include <limits.h>
00022 
00023 # include <vaucanson/algebra/implementation/letter/int_letter.hh>
00024 
00025 namespace vcsn {
00026 
00027   namespace algebra {
00028 
00029     template <>
00030     struct letter_traits<int>
00031     {
00032       
00033       typedef misc::false_t is_char_letter;
00034 
00035       enum
00036       {
00037         
00038         
00039         cardinal = UINT_MAX
00040       };
00041 
00042       LETTER_DEFAULT(open_par, "(")
00043       LETTER_DEFAULT(close_par, ")")
00044       LETTER_DEFAULT(plus, "+")
00045       LETTER_DEFAULT(times, ".")
00046       LETTER_DEFAULT(star, "*")
00047       LETTER_DEFAULT(epsilon, "e")
00048       LETTER_DEFAULT(zero, "z")
00049       LETTER_DEFAULT(open_weight, "{")
00050       LETTER_DEFAULT(close_weight, "}")
00051       LETTER_DEFAULT(space, " ")
00052 
00053       static
00054       int
00055       literal_to_letter(const std::string& str)
00056       {
00057         std::stringstream sstr(str);
00058         int ret;
00059         sstr >> ret;
00060         return ret;
00061       }
00062 
00063       static
00064       std::string
00065       letter_to_literal(const int& c)
00066       {
00067         std::stringstream sstr;
00068         sstr << c;
00069         return sstr.str();
00070       }
00071 
00072       // An int is "simple" with dimension 1.
00073       static std::string kind() { return "simple"; }
00074       static int dim() { return 1; }
00075 
00076     };
00077 
00078     template <typename S, typename CharContainer>
00079     bool op_parse(const algebra::FreeMonoidBase<S>& set,
00080                   std::basic_string<int>& v,
00081                   const std::string& s,
00082                   typename std::string::const_iterator& i,
00083                   const CharContainer&)
00084     {
00085       typename std::string::const_iterator j = i;
00086       typename std::string::const_iterator k;
00087       typename std::string::const_iterator back;
00088 
00089       while (i != s.end()) {
00090         std::string out;
00091         back = i;
00092 
00093         while ((i != s.end()) && (((*i >= '0') && (*i <= '9'))) || (*i == '#'))
00094           if (*i == '#') {
00095             ++i;
00096             break;
00097           }
00098           else {
00099             out += *i;
00100             ++i;
00101           }
00102 
00103         int value;
00104         std::istringstream is(out);
00105         is >> value;
00106         if (!set.alphabet().contains(value)) {
00107           i = back;
00108           break ;
00109         }
00110         v.push_back(value);
00111       }
00112       return (i != j);
00113     }
00114 
00115   } // ! algebra
00116 
00117 } // ! vcsn
00118 
00119 namespace std {
00120 
00121   inline
00122   ostream& operator<<(ostream& o, basic_string<int> s)
00123   {
00124     basic_string<int>::const_iterator i;
00125     basic_string<int>::const_iterator j;
00126 
00127     for (i = s.begin(); i != s.end(); ) {
00128       o << *i;
00129       i++;
00130       if (i != s.end())
00131         o << "#";
00132     }
00133 
00134     return o;
00135   }
00136 
00137 } // ! std
00138 
00139 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_LETTER_INT_LETTER_HXX