switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / Utility.ct
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Utility non-inline template implementation  */
30
31 //#include "Utility.ih"
32
33 // Custom includes
34 #include <sstream>
35 #include <limits>
36 #include <boost/format.hpp>
37 #include "Traits.hh"
38
39 #define prefix_
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43 // senf::console::ArgumentTraits< CharAsString<CharT> >
44
45 template <class CharT>
46 prefix_ void senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::
47 parse(ParseCommandInfo::TokensRange const & tokens, CharAsString<CharT> & out)
48 {
49     std::string v;
50     console::parse(tokens,v);
51     if (v.size() != 1)
52         throw SyntaxErrorException("Invalid size of character constant");
53     out.value = static_cast<CharT>(v[0]);
54 }
55
56 template <class CharT>
57 prefix_ std::string
58 senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::description()
59 {
60     return std::numeric_limits<CharT>::is_signed ? "char" : "uchar";
61 }
62
63 template <class CharT>
64 prefix_ std::string senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::
65 str(CharAsString<CharT> value)
66 {
67     return console::str(std::string(1,value.value));
68 }
69
70 template <class CharT>
71 prefix_ void senf::console::ReturnValueTraits< senf::console::CharAsString<CharT> >::
72 format(CharAsString<CharT> value, std::ostream & os)
73 {
74     console::format(std::string(1,value.value),os);
75 }
76
77 //-/////////////////////////////////////////////////////////////////////////////////////////////////
78 // senf::console::ArgumentTraits< senf::console::ValueRange<T> >
79
80 template <class T>
81 prefix_ void senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
82 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
83 {
84     if (tokens.size() != 1)
85         throw SyntaxErrorException("parameter syntax error");
86     std::string v (tokens.begin()[0].value());
87     std::string::size_type i (v.find(':'));
88     try {
89         if (i == std::string::npos)
90             out.low = out.high = boost::lexical_cast<T>(v);
91         else {
92             out.low = boost::lexical_cast<T>(v.substr(0,i));
93             out.high = boost::lexical_cast<T>(v.substr(i+1));
94         }
95     }
96     catch (std::bad_cast & ex) {
97         throw SyntaxErrorException("parameter syntax error");
98     }
99 }
100
101 template <class T>
102 prefix_ std::string senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
103 description()
104 {
105     return (boost::format("range<%s>") % ArgumentTraits<T>::description()).str();
106 }
107
108 template <class T>
109 prefix_ std::string senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
110 str(type const & value)
111 {
112     std::stringstream ss;
113     senf::console::format(value, ss);
114     return ss.str();
115 }
116
117 //-/////////////////////////////////////////////////////////////////////////////////////////////////
118 // senf::console::ReturnValueTraits< senf::console::ValueRange<T> >
119
120 template <class T>
121 prefix_ void senf::console::ReturnValueTraits< senf::console::ValueRange<T> >::
122 format(type const & value, std::ostream & os)
123 {
124     os << senf::console::str(value.low);
125     if (value.low != value.high)
126         os << ':' << senf::console::str(value.high);
127 }
128
129 //-/////////////////////////////////////////////////////////////////////////////////////////////////
130 // senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >
131
132 template <class Enum>
133 prefix_ void senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::
134 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
135 {
136     CheckedArgumentIteratorWrapper arg (tokens);
137     out.value = 0;
138     while (arg) {
139         Enum v;
140         console::parse( *(arg++), v);
141         out.value |= v;
142     }
143 }
144
145 template <class Enum>
146 prefix_ std::string
147 senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::description()
148 {
149     return ArgumentTraits<Enum>::description();
150 }
151
152 template <class Enum>
153 prefix_ std::string
154 senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::str(type const & value)
155 {
156     std::stringstream ss;
157     console::format(value, ss);
158     return ss.str();
159 }
160
161 //-/////////////////////////////////////////////////////////////////////////////////////////////////
162 // senf::console::ReturnValueTraits< senf::console::FlagCollection<Enum> >
163
164 template <class Enum>
165 prefix_ void senf::console::ReturnValueTraits< senf::console::FlagCollection<Enum> >::
166 format(type const & value, std::ostream & os)
167 {
168     unsigned n (0);
169     std::stringstream ss;
170     unsigned long flag (1);
171     for (unsigned bit (0); bit<sizeof(value.value)*CHAR_BIT; ++bit, flag<<=1) {
172         if (value.value & flag) {
173             if (n++) ss << " ";
174             console::format(static_cast<Enum>(flag), ss);
175         }
176     }
177     os << (n != 1 ? "(" + ss.str() + ")" : ss.str());
178 }
179
180 //-/////////////////////////////////////////////////////////////////////////////////////////////////
181 #undef prefix_
182
183 \f
184 // Local Variables:
185 // mode: c++
186 // fill-column: 100
187 // comment-column: 40
188 // c-file-style: "senf"
189 // indent-tabs-mode: nil
190 // ispell-local-dictionary: "american"
191 // compile-command: "scons -u test"
192 // End: