switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / Traits.cci
1 // $Id$
2 //
3 // Copyright (C) 2008
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 Traits inline non-template implementation */
30
31 #include "Traits.ih"
32
33 // Custom includes
34 #include <boost/algorithm/string/predicate.hpp>
35
36 #define prefix_ inline
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40 // senf::console::ArgumentTraits<bool>
41
42 prefix_ void
43 senf::console::ArgumentTraits<bool>::parse(ParseCommandInfo::TokensRange const & tokens,
44                                            bool & out)
45 {
46     if (tokens.size() != 1)
47         throw SyntaxErrorException("argument syntax error");
48
49     if ( boost::istarts_with(std::string("true"), tokens.begin()->value())
50          || boost::istarts_with(std::string("enabled"), tokens.begin()->value())
51          || boost::istarts_with(std::string("yes"), tokens.begin()->value())
52          || boost::iequals(std::string("on"), tokens.begin()->value()) )
53         out = true;
54     else if (boost::istarts_with(std::string("false"), tokens.begin()->value())
55              || boost::istarts_with(std::string("disabled"), tokens.begin()->value())
56              || boost::istarts_with(std::string("no"), tokens.begin()->value())
57              || (boost::istarts_with(std::string("off"), tokens.begin()->value())
58                  && tokens.begin()->value().size() >= 2) )
59         out = false;
60     else {
61         int v (0);
62         senf::console::parse(tokens, v);
63         out = v;
64     }
65 }
66
67 prefix_ std::string senf::console::ArgumentTraits<bool>::description()
68 {
69     return "bool";
70 }
71
72 prefix_ std::string senf::console::ArgumentTraits<bool>::str(bool value)
73 {
74     return value ? "true" : "false";
75 }
76
77 //-/////////////////////////////////////////////////////////////////////////////////////////////////
78 // senf::console::ReturnValueTraits<bool>
79
80 prefix_ void senf::console::ReturnValueTraits<bool>::format(bool value, std::ostream & os)
81 {
82     formatTrueFalse(value, os);
83 }
84
85 //-/////////////////////////////////////////////////////////////////////////////////////////////////
86
87 prefix_ void senf::console::formatTrueFalse(bool value, std::ostream & os)
88 {
89     os << (value ? "true" : "false");
90 }
91
92 prefix_ void senf::console::formatYesNo(bool value, std::ostream & os)
93 {
94     os << (value ? "yes" : "no");
95 }
96
97 prefix_ void senf::console::formatEnabledDisabled(bool value, std::ostream & os)
98 {
99     os << (value ? "enabled" : "disabled");
100 }
101
102 prefix_ void senf::console::formatOnOff(bool value, std::ostream & os)
103 {
104     os << (value ? "on" : "off");
105 }
106
107 prefix_ void senf::console::formatOneZero(bool value, std::ostream & os)
108 {
109     os << (value ? "0" : "1");
110 }
111
112 //-/////////////////////////////////////////////////////////////////////////////////////////////////
113 #undef prefix_
114
115 \f
116 // Local Variables:
117 // mode: c++
118 // fill-column: 100
119 // comment-column: 40
120 // c-file-style: "senf"
121 // indent-tabs-mode: nil
122 // ispell-local-dictionary: "american"
123 // compile-command: "scons -u test"
124 // End: