1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Header: string_utilities.h
7 ------------- Copyright (C) 2009 Jon S. Berndt (jon@jsbsim.org) -------------
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
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
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.
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.
27 --------------------------------------------------------------------------------
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
48 #define ID_STRINGUTILS "$Id: string_utilities.h,v 1.13 2010/07/07 11:59:48 jberndt Exp $"
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
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& trim_all_space(std::string& str);
68 extern std::string& to_upper(std::string& str);
69 extern std::string& to_lower(std::string& str);
70 extern bool is_number(const std::string& str);
71 std::vector <std::string> split(std::string str, char d);
77 string& trim_left(string& str)
79 while (str.size() && isspace((unsigned char)str[0])) {
85 string& trim_right(string& str)
87 while (str.size() && isspace((unsigned char)str[str.size()-1])) {
88 str = str.erase(str.size()-1,1);
93 string& trim(string& str)
95 if (str.size() == 0) return str;
96 string temp_str = trim_right(str);
97 return str = trim_left(temp_str);
100 string& trim_all_space(string& str)
102 for (size_t i=0; i<str.size(); i++) {
103 if (isspace((unsigned char)str[i])) {
104 str = str.erase(i,1);
111 string& to_upper(string& str)
113 for (size_t i=0; i<str.size(); i++) str[i] = toupper(str[i]);
117 string& to_lower(string& str)
119 for (size_t i=0; i<str.size(); i++) str[i] = tolower(str[i]);
123 bool is_number(const string& str)
125 return (str.find_first_not_of("+-.0123456789Ee") == string::npos);
128 vector <string> split(string str, char d)
130 vector <string> str_array;
136 while (index != string::npos) {
137 temp = str.substr(0,index);
139 if (temp.size() > 0) str_array.push_back(temp);
140 str = str.erase(0,index+1);
143 if (str.size() > 0) {
145 if (temp.size() > 0) str_array.push_back(temp);
153 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%