OTB  6.7.0
Orfeo Toolbox
otbStringUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbStringUtils_h
22 #define otbStringUtils_h
23 
24 
25 #include <string>
26 #include <vector>
27 #include <limits>
28 #include <stdexcept>
29 
30 #if defined(__GNUC__) || defined(__clang__)
31 #pragma GCC diagnostic push
32 #pragma GCC diagnostic ignored "-Wshadow"
33 #pragma GCC diagnostic ignored "-Wunused-parameter"
34 #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
35 #include <boost/lexical_cast.hpp>
36 #include <boost/algorithm/string.hpp>
37 #include <boost/algorithm/string/predicate.hpp>
38 #include <boost/utility/enable_if.hpp>
39 #pragma GCC diagnostic pop
40 #else
41 #include <boost/lexical_cast.hpp>
42 #include <boost/algorithm/string.hpp>
43 #include <boost/algorithm/string/predicate.hpp>
44 #include <boost/utility/enable_if.hpp>
45 #endif
46 
47 namespace otb
48 {
49 namespace Utils
50 {
51 template <typename Res, typename In >
52 inline
53 Res LexicalCast(In const& in, std::string const& kind) {
54  try
55  {
56  return boost::lexical_cast<Res>(in);
57  }
58  catch (boost::bad_lexical_cast &) {
59  std::ostringstream oss;
60  oss << "Cannot decode '" << in << "' as this is not a valid value for '" << kind << "'";
61  throw std::runtime_error(oss.str());
62  }
63 }
64 
81 template<typename T>
82 void
83 ConvertStringToVector(std::string const &str, T& ret, std::string const& errmsg, const char * delims=" ")
84 {
85  typedef std::vector<boost::iterator_range<std::string::const_iterator> > ListType;
86 
87  ListType split;
88 
89  boost::split(split, str, boost::is_any_of(delims));
90 
91  for(size_t i = 0; i < split.size(); i++)
92  {
93  typename T::value_type value = LexicalCast<typename T::value_type> (split[i], errmsg);
94  ret.push_back(value);
95  }
96 }
97 
116 template<typename T>
117 void SplitStringToSingleKeyValue(const std::string& str,
118  std::string& key, T& value, const T& defValue,
119  std::string const& errmsg, const std::string delims="=")
120 {
121 
122  typedef boost::iterator_range<std::string::const_iterator> BoostRangeIteratorType;
123  typedef std::list< BoostRangeIteratorType > ListType;
124 
125  ListType split;
126 
127  boost::split( split, str, boost::is_any_of(delims), boost::token_compress_on );
128 
129  typename ListType::iterator it = split.begin();
130  BoostRangeIteratorType kIt = boost::trim_copy((*it));
131  key.assign( kIt.begin(), kIt.end());
132  ++it;
133 
134  if( it != split.end())
135  {
136  value = LexicalCast<T>(boost::trim_copy(*it), errmsg);
137  ++it;
138  }
139  else
140  {
141  value = defValue;
142  }
143 }
144 } // end namespace Utils
145 } // end namespace otb
146 
147 #endif //otbStringUtils_h
void ConvertStringToVector(std::string const &str, T &ret, std::string const &errmsg, const char *delims=" ")
void SplitStringToSingleKeyValue(const std::string &str, std::string &key, T &value, const T &defValue, std::string const &errmsg, const std::string delims="=")
Res LexicalCast(In const &in, std::string const &kind)
Utils operations.