Branch data Line data Source code
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 : : #include <stdio.h>
44 : :
45 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46 : : DEFINITIONS
47 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
48 : :
49 : : #define ID_STRINGUTILS "$Id: string_utilities.h,v 1.14 2010/08/21 17:13:47 jberndt Exp $"
50 : :
51 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 : : FORWARD DECLARATIONS
53 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54 : :
55 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 : : CLASS DOCUMENTATION
57 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58 : :
59 : :
60 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 : : CLASS DECLARATION
62 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
63 : :
64 : : #if !defined(BASE)
65 : : extern std::string& trim_left(std::string& str);
66 : : extern std::string& trim_right(std::string& str);
67 : : extern std::string& trim(std::string& str);
68 : : extern std::string& trim_all_space(std::string& str);
69 : : extern std::string& to_upper(std::string& str);
70 : : extern std::string& to_lower(std::string& str);
71 : : extern bool is_number(const std::string& str);
72 : : std::vector <std::string> split(std::string str, char d);
73 : : extern std::string to_string(int);
74 : : extern std::string replace(std::string str, const std::string& old, const std::string& newstr);
75 : : #else
76 : : #include <cctype>
77 : :
78 : : using namespace std;
79 : :
80 : 2718 : string& trim_left(string& str)
81 : : {
82 [ + + + + ]: 35822 : while (str.size() && isspace((unsigned char)str[0])) {
[ + + ]
83 : 15203 : str = str.erase(0,1);
84 : : }
85 : 2718 : return str;
86 : : }
87 : :
88 : 2718 : string& trim_right(string& str)
89 : : {
90 [ + + + + ]: 17160 : while (str.size() && isspace((unsigned char)str[str.size()-1])) {
[ + + ]
91 : 5872 : str = str.erase(str.size()-1,1);
92 : : }
93 : 2718 : return str;
94 : : }
95 : :
96 : 2724 : string& trim(string& str)
97 : : {
98 [ + + ]: 2724 : if (str.size() == 0) return str;
99 : 2718 : string temp_str = trim_right(str);
100 : 5442 : return str = trim_left(temp_str);
101 : : }
102 : :
103 : 0 : string& trim_all_space(string& str)
104 : : {
105 [ # # ]: 0 : for (size_t i=0; i<str.size(); i++) {
106 [ # # ]: 0 : if (isspace((unsigned char)str[i])) {
107 : 0 : str = str.erase(i,1);
108 : 0 : --i;
109 : : }
110 : : }
111 : 0 : return str;
112 : : }
113 : :
114 : 0 : string& to_upper(string& str)
115 : : {
116 [ # # ]: 0 : for (size_t i=0; i<str.size(); i++) str[i] = toupper(str[i]);
117 : 0 : return str;
118 : : }
119 : :
120 : 142 : string& to_lower(string& str)
121 : : {
122 [ + + ]: 183 : for (size_t i=0; i<str.size(); i++) str[i] = tolower(str[i]);
123 : 142 : return str;
124 : : }
125 : :
126 : 118 : bool is_number(const string& str)
127 : : {
128 : 118 : return (str.find_first_not_of("+-.0123456789Ee") == string::npos);
129 : : }
130 : :
131 : 918 : vector <string> split(string str, char d)
132 : : {
133 : 918 : vector <string> str_array;
134 : 918 : size_t index=0;
135 : 918 : string temp = "";
136 : :
137 : 918 : trim(str);
138 : 918 : index = str.find(d);
139 [ + + ]: 1826 : while (index != string::npos) {
140 : 1816 : temp = str.substr(0,index);
141 : 908 : trim(temp);
142 [ + + ]: 908 : if (temp.size() > 0) str_array.push_back(temp);
143 : 908 : str = str.erase(0,index+1);
144 : 908 : index = str.find(d);
145 : : }
146 [ + + ]: 918 : if (str.size() > 0) {
147 : 898 : temp = trim(str);
148 [ + - ]: 898 : if (temp.size() > 0) str_array.push_back(temp);
149 : : }
150 : :
151 : 918 : return str_array;
152 : : }
153 : :
154 : 24 : string to_string(int i)
155 : : {
156 : : char buffer[32];
157 : 24 : sprintf(buffer, "%d", i);
158 : 24 : return string(buffer);
159 : : }
160 : :
161 : 4 : string replace(string str, const string& oldstr, const string& newstr)
162 : : {
163 : : int old_idx;
164 : 4 : string temp;
165 : 4 : old_idx = str.find(oldstr);
166 [ + - ]: 4 : if (old_idx >= 0) {
167 : 4 : temp = str.replace(old_idx, 1, newstr);
168 : : }
169 : 4 : return temp;
170 : : }
171 : :
172 : : #endif
173 : :
174 : : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 : :
176 : : #endif
177 : :
|