JSBSim Flight Dynamics Model  1.0 (02 March 2017)
An Open Source Flight Dynamics and Control Software Library in C++
string_utilities.h
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 
3  Header: string_utilities.h
4  Author: Jon S. Berndt
5  Date started: 06/01/09
6 
7  ------------- Copyright (C) 2009 Jon S. Berndt (jon@jsbsim.org) -------------
8 
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17  details.
18 
19  You should have received a copy of the GNU Lesser General Public License along with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA 02111-1307, USA.
22 
23  Further information about the GNU Lesser General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25 
26 HISTORY
27 --------------------------------------------------------------------------------
28 06/01/09 JSB Created
29 
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31 SENTRY
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
33 
34 #ifndef STRINGUTILS_H
35 #define STRINGUTILS_H
36 
37 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 INCLUDES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 
41 #include <string>
42 #include <sstream>
43 #include <iostream>
44 #include <vector>
45 #include <stdio.h>
46 
47 
48 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 DEFINITIONS
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51 
52 #define ID_STRINGUTILS "$Id: string_utilities.h,v 1.24 2015/08/16 16:06:28 bcoconni Exp $"
53 
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 FORWARD DECLARATIONS
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 CLASS DOCUMENTATION
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
61 
62 
63 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 CLASS DECLARATION
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
66 
67 #if !defined(BASE)
68  extern std::string& trim_left(std::string& str);
69  extern std::string& trim_right(std::string& str);
70  extern std::string& trim(std::string& str);
71  extern std::string& trim_all_space(std::string& str);
72  extern std::string& to_upper(std::string& str);
73  extern std::string& to_lower(std::string& str);
74  extern bool is_number(const std::string& str);
75  std::vector <std::string> split(std::string str, char d);
76 
77  // These functions are built-ins for:
78  // * C++11
79  // * libc++ for all C++ language versions
80  // * Visual Studio for versions greater than 2010 (1700 -> MSVC++ 11.0 and VS 2012)
81 #if !defined(_LIBCPP_VERSION) && _MSC_VER < 1700 && __cplusplus < 201103L
82  extern std::string to_string(int);
83  extern std::string to_string(double);
84  extern std::string to_string(float);
85 #endif
86 
87  extern std::string replace(std::string str, const std::string& old, const std::string& newstr);
88 #else
89  #include <cctype>
90 
91  std::string& trim_left(std::string& str)
92  {
93  while (!str.empty() && isspace((unsigned char)str[0])) {
94  str = str.erase(0,1);
95  }
96  return str;
97  }
98 
99  std::string& trim_right(std::string& str)
100  {
101  while (!str.empty() && isspace((unsigned char)str[str.size()-1])) {
102  str = str.erase(str.size()-1,1);
103  }
104  return str;
105  }
106 
107  std::string& trim(std::string& str)
108  {
109  if (str.empty()) return str;
110  std::string temp_str = trim_right(str);
111  return str = trim_left(temp_str);
112  }
113 
114  std::string& trim_all_space(std::string& str)
115  {
116  for (size_t i=0; i<str.size(); i++) {
117  if (isspace((unsigned char)str[i])) {
118  str = str.erase(i,1);
119  --i;
120  }
121  }
122  return str;
123  }
124 
125  std::string& to_upper(std::string& str)
126  {
127  for (size_t i=0; i<str.size(); i++) str[i] = toupper(str[i]);
128  return str;
129  }
130 
131  std::string& to_lower(std::string& str)
132  {
133  for (size_t i=0; i<str.size(); i++) str[i] = tolower(str[i]);
134  return str;
135  }
136 
137  bool is_number(const std::string& str)
138  {
139  if (str.empty())
140  return false;
141  else
142  return (str.find_first_not_of("+-.0123456789Ee") == std::string::npos);
143  }
144 
145  std::vector <std::string> split(std::string str, char d)
146  {
147  std::vector <std::string> str_array;
148  size_t index=0;
149  std::string temp = "";
150 
151  trim(str);
152  index = str.find(d);
153  while (index != std::string::npos) {
154  temp = str.substr(0,index);
155  trim(temp);
156  if (!temp.empty()) str_array.push_back(temp);
157  str = str.erase(0,index+1);
158  index = str.find(d);
159  }
160  if (!str.empty()) {
161  temp = trim(str);
162  if (!temp.empty()) str_array.push_back(temp);
163  }
164 
165  return str_array;
166  }
167 
168  // These functions are built-ins for:
169  // * C++11
170  // * libc++ for all C++ language versions
171  // * Visual Studio for versions greater than 2010 (1700 -> MSVC++ 11.0 and VS 2012)
172 #if !defined(_LIBCPP_VERSION) && _MSC_VER < 1700 && __cplusplus < 201103L
173  std::string to_string(int i)
174  {
175  char buffer[32];
176  sprintf(buffer, "%d", i);
177  return std::string(buffer);
178  }
179 
180  std::string to_string(float x)
181  {
182  std::ostringstream o;
183  if (!(o << x)) std::cerr << "Bad float to string conversion" << std::endl;
184  return o.str();
185  }
186 
187  std::string to_string(double x)
188  {
189  std::ostringstream o;
190  if (!(o << x)) std::cerr << "Bad double to string conversion" << std::endl;
191  return o.str();
192  }
193 #endif
194 
195  std::string replace(std::string str, const std::string& oldstr, const std::string& newstr)
196  {
197  std::string temp = str;
198  size_t old_idx = str.find(oldstr);
199  if (old_idx != std::string::npos) {
200  temp = str.replace(old_idx, 1, newstr);
201  }
202  return temp;
203  }
204 
205 #endif
206 
207 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208 
209 #endif
210