]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/input_output/string_utilities.h
Merge branch 'topic/pu-crash'
[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 <vector>
43
44 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 DEFINITIONS
46 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
47
48 #define ID_STRINGUTILS "$Id$"
49
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 FORWARD DECLARATIONS
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 CLASS DOCUMENTATION
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57
58
59 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 CLASS DECLARATION
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
62
63 #if !defined(BASE)
64   extern std::string& trim_left(std::string& str);
65   extern std::string& trim_right(std::string& str);
66   extern std::string& trim(std::string& str);
67   extern std::string& to_upper(std::string& str);
68   extern std::string& to_lower(std::string& str);
69   extern bool is_number(const std::string& str);
70   std::vector <std::string> split(std::string str, char d);
71 #else
72   #include <ctype.h>
73
74   using namespace std;
75
76   string& trim_left(string& str)
77   {
78     while (str.size() && !isgraph(str[0])) {
79       str = str.erase(0,1);
80     }
81     return str;
82   }
83
84   string& trim_right(string& str)
85   {
86     while (str.size() && !isgraph(str[str.size()-1])) {
87       str = str.erase(str.size()-1,1);
88     }
89     return str;
90   }
91
92   string& trim(string& str)
93   {
94     if (str.size() == 0) return str;
95     string temp_str = trim_right(str);
96     return str = trim_left(temp_str);
97   }
98
99   string& to_upper(string& str)
100   {
101     for (size_t i=0; i<str.size(); i++) str[i] = toupper(str[i]);
102     return str;
103   }
104
105   string& to_lower(string& str)
106   {
107     for (size_t i=0; i<str.size(); i++) str[i] = tolower(str[i]);
108     return str;
109   }
110
111   bool is_number(const string& str)
112   {
113     return (str.find_first_not_of("+-.0123456789Ee") == string::npos);
114   }
115
116   vector <string> split(string str, char d)
117   {
118     vector <string> str_array;
119     size_t index=0;
120     string temp = "";
121
122     trim(str);
123     index = str.find(d);
124     while (index != string::npos) {
125       temp = str.substr(0,index);
126       trim(temp);
127       if (temp.size() > 0) str_array.push_back(temp);
128       str = str.erase(0,index+1);
129       index = str.find(d);
130     }
131     if (str.size() > 0) {
132       temp = trim(str);
133       if (temp.size() > 0) str_array.push_back(temp);
134     }
135
136     return str_array;
137   }
138
139 #endif
140
141 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142
143 #endif
144