]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/input_output/string_utilities.h
Fix for bug 1304 - crash loading XML route
[flightgear.git] / src / FDM / JSBSim / input_output / 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.18 2014/01/13 10:46:03 ehofman 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 // libc++ has these as built-ins for all C++ language versions
78 #if !defined(_LIBCPP_VERSION)
79   extern std::string to_string(int);
80   extern std::string to_string(double);
81   extern std::string to_string(float);
82 #endif
83
84   extern std::string replace(std::string str, const std::string& old, const std::string& newstr);
85 #else
86   #include <cctype>
87
88   using namespace std;
89
90   string& trim_left(string& str)
91   {
92     while (str.size() && isspace((unsigned char)str[0])) {
93       str = str.erase(0,1);
94     }
95     return str;
96   }
97
98   string& trim_right(string& str)
99   {
100     while (str.size() && isspace((unsigned char)str[str.size()-1])) {
101       str = str.erase(str.size()-1,1);
102     }
103     return str;
104   }
105
106   string& trim(string& str)
107   {
108     if (str.size() == 0) return str;
109     string temp_str = trim_right(str);
110     return str = trim_left(temp_str);
111   }
112
113   string& trim_all_space(string& str)
114   {
115     for (size_t i=0; i<str.size(); i++) {
116       if (isspace((unsigned char)str[i])) {
117         str = str.erase(i,1);
118         --i;
119       }
120     }
121     return str;
122   }
123
124   string& to_upper(string& str)
125   {
126     for (size_t i=0; i<str.size(); i++) str[i] = toupper(str[i]);
127     return str;
128   }
129
130   string& to_lower(string& str)
131   {
132     for (size_t i=0; i<str.size(); i++) str[i] = tolower(str[i]);
133     return str;
134   }
135
136   bool is_number(const string& str)
137   {
138     if (str.size())
139       return (str.find_first_not_of("+-.0123456789Ee") == string::npos);
140     else
141       return false;
142   }
143
144   vector <string> split(string str, char d)
145   {
146     vector <string> str_array;
147     size_t index=0;
148     string temp = "";
149
150     trim(str);
151     index = str.find(d);
152     while (index != string::npos) {
153       temp = str.substr(0,index);
154       trim(temp);
155       if (temp.size() > 0) str_array.push_back(temp);
156       str = str.erase(0,index+1);
157       index = str.find(d);
158     }
159     if (str.size() > 0) {
160       temp = trim(str);
161       if (temp.size() > 0) str_array.push_back(temp);
162     }
163
164     return str_array;
165   }
166 /* Comment out to_string functions when they are defined already - C++ 11 defines these */
167   string to_string(int i)
168   {
169     char buffer[32];
170     sprintf(buffer, "%d", i);
171     return string(buffer);
172   }
173
174   string to_string(float x)
175   {
176     std::ostringstream o;
177     if (!(o << x)) cerr << "Bad float to string conversion" << endl;
178     return o.str();
179   }
180
181   string to_string(double x)
182   {
183     std::ostringstream o;
184     if (!(o << x)) cerr << "Bad double to string conversion" << endl;
185     return o.str();
186   }
187
188   string replace(string str, const string& oldstr, const string& newstr)
189   {
190     int old_idx;
191     string temp;
192     old_idx = str.find(oldstr);
193     if (old_idx >= 0) {
194       temp = str.replace(old_idx, 1, newstr);
195     }
196     return temp;
197   }
198
199 #endif
200
201 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202
203 #endif
204