SCL  1.0
Standard Control Library : Control, dynamics, physics, and simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Groups Pages
strutil.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Roland Philippsen <roland dot philippsen at gmx dot net>
3  *
4  * BSD license:
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holder nor the names of
14  * contributors to this software may be used to endorse or promote
15  * products derived from this software without specific prior written
16  * permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS''
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDER OR THE CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
38 #ifndef SFL_STRUTIL_HPP
39 #define SFL_STRUTIL_HPP
40 
41 
42 #include <string>
43 #include <sstream>
44 
45 #ifdef W_QNX
46 extern "C" {
47 #include <stddef.h> // for qnx
48 }
49 #endif
50 
51 
52 namespace sclnet {
53 
54  template<typename displayable_t, typename prefix_t>
55  std::string displayString(displayable_t const & displayable, prefix_t const & prefix)
56  {
57  std::ostringstream result;
58  displayable.display(result, prefix);
59  return result.str();
60  }
61 
62 }
63 
64 
65 namespace sfl {
66 
68  template<typename Foo>
69  std::string to_string(const Foo & foo) {
70  std::ostringstream os;
71  os << foo;
72  return os.str();
73  }
74 
76  template<>
77  std::string to_string<bool>(const bool & flag);
78 
80  template<typename Foo>
81  bool string_to(const std::string & str, Foo & foo) {
82  Foo bar;
83  std::istringstream is(str);
84  if( ! (is >> bar))
85  return false;
86  foo = bar;
87  return true;
88  }
89 
95  template<>
96  bool string_to<bool>(const std::string & str, bool & foo);
97 
102  template<typename Foo>
103  bool string_to_bool(const std::string & str, Foo & foo) {
104  bool bar;
105  if( ! string_to(str, bar))
106  return false;
107  foo = bar ? 1 : 0;
108  return true;
109  }
110 
111 
136  bool splitstring(std::string const & input, char separator,
137  std::string & head, std::string & tail);
138 
139 
144  template<typename tokenlist_t>
145  size_t tokenize(std::string const & input, char separator, tokenlist_t & output) {
146  std::string head;
147  std::string tail(input);
148  while (splitstring(tail, separator, head, tail))
149  output.push_back(head);
150  output.push_back(head);
151  return output.size();
152  }
153 
154 
159  template<typename tokenlist_t, typename value_t>
160  bool token_to(tokenlist_t const & tokenlist, size_t index, value_t & value) {
161  if (index >= tokenlist.size())
162  return false;
163  return string_to(tokenlist[index], value);
164  }
165 
166 }
167 
168 #endif // SFL_STRUTIL_HPP