OTB  9.0.0
Orfeo Toolbox
otbStringUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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 Res LexicalCast(In const& in, std::string const& kind)
53 {
54  try
55  {
56  return boost::lexical_cast<Res>(in);
57  }
58  catch (boost::bad_lexical_cast&)
59  {
60  std::ostringstream oss;
61  oss << "Cannot decode '" << in << "' as this is not a valid value for '" << kind << "'";
62  throw std::runtime_error(oss.str());
63  }
64 }
65 
82 template <typename T>
83 void 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, std::string& key, T& value, const T& defValue, std::string const& errmsg,
118  const std::string delims = "=")
119 {
120 
121  typedef boost::iterator_range<std::string::const_iterator> BoostRangeIteratorType;
122  typedef std::list<BoostRangeIteratorType> ListType;
123 
124  ListType split;
125 
126  boost::split(split, str, boost::is_any_of(delims), boost::token_compress_on);
127 
128  typename ListType::iterator it = split.begin();
129  BoostRangeIteratorType kIt = boost::trim_copy((*it));
130  key.assign(kIt.begin(), kIt.end());
131  ++it;
132 
133  if (it != split.end())
134  {
135  value = LexicalCast<T>(boost::trim_copy(*it), errmsg);
136  ++it;
137  }
138  else
139  {
140  value = defValue;
141  }
142 }
143 } // end namespace Utils
144 } // end namespace otb
145 
146 #endif // otbStringUtils_h
otb::Utils::LexicalCast
Res LexicalCast(In const &in, std::string const &kind)
Definition: otbStringUtils.h:52
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
Utils
Utils operations.
otb::Utils::ConvertStringToVector
void ConvertStringToVector(std::string const &str, T &ret, std::string const &errmsg, const char *delims=" ")
Definition: otbStringUtils.h:83
otb::Utils::SplitStringToSingleKeyValue
void SplitStringToSingleKeyValue(const std::string &str, std::string &key, T &value, const T &defValue, std::string const &errmsg, const std::string delims="=")
Definition: otbStringUtils.h:117