FlightGear next
string_utilities.h
Go to the documentation of this file.
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
26HISTORY
27--------------------------------------------------------------------------------
2806/01/09 JSB Created
29
30%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31SENTRY
32%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
33
34#ifndef STRINGUTILS_H
35#define STRINGUTILS_H
36
37/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38INCLUDES
39%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40
41#include <string>
42#include <sstream>
43#include <iostream>
44#include <vector>
45#include <stdio.h>
46
47/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48CLASS DECLARATION
49%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51#if !defined(BASE)
52 extern std::string& trim_left(std::string& str);
53 extern std::string& trim_right(std::string& str);
54 extern std::string& trim(std::string& str);
55 extern std::string& trim_all_space(std::string& str);
56 extern std::string& to_upper(std::string& str);
57 extern std::string& to_lower(std::string& str);
58 extern bool is_number(const std::string& str);
59 std::vector <std::string> split(std::string str, char d);
60
61 extern std::string replace(std::string str, const std::string& old, const std::string& newstr);
62#else
63 #include <cctype>
64
65 std::string& trim_left(std::string& str)
66 {
67 while (!str.empty() && isspace((unsigned char)str[0])) {
68 str = str.erase(0,1);
69 }
70 return str;
71 }
72
73 std::string& trim_right(std::string& str)
74 {
75 while (!str.empty() && isspace((unsigned char)str[str.size()-1])) {
76 str = str.erase(str.size()-1,1);
77 }
78 return str;
79 }
80
81 std::string& trim(std::string& str)
82 {
83 if (str.empty()) return str;
84 std::string temp_str = trim_right(str);
85 return str = trim_left(temp_str);
86 }
87
88 std::string& trim_all_space(std::string& str)
89 {
90 for (size_t i=0; i<str.size(); i++) {
91 if (isspace((unsigned char)str[i])) {
92 str = str.erase(i,1);
93 --i;
94 }
95 }
96 return str;
97 }
98
99 std::string& to_upper(std::string& str)
100 {
101 for (size_t i=0; i<str.size(); i++) str[i] = toupper(str[i]);
102 return str;
103 }
104
105 std::string& to_lower(std::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 std::string& str)
112 {
113 if (str.empty())
114 return false;
115 else
116 return (str.find_first_not_of("+-.0123456789Ee") == std::string::npos);
117 }
118
119 std::vector <std::string> split(std::string str, char d)
120 {
121 std::vector <std::string> str_array;
122 size_t index=0;
123 std::string temp = "";
124
125 trim(str);
126 index = str.find(d);
127 while (index != std::string::npos) {
128 temp = str.substr(0,index);
129 trim(temp);
130 if (!temp.empty()) str_array.push_back(temp);
131 str = str.erase(0,index+1);
132 index = str.find(d);
133 }
134 if (!str.empty()) {
135 temp = trim(str);
136 if (!temp.empty()) str_array.push_back(temp);
137 }
138
139 return str_array;
140 }
141
142 std::string replace(std::string str, const std::string& oldstr, const std::string& newstr)
143 {
144 std::string temp = str;
145 size_t old_idx = str.find(oldstr);
146 if (old_idx != std::string::npos) {
147 temp = str.replace(old_idx, 1, newstr);
148 }
149 return temp;
150 }
151
152#endif
153
154//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155
156#endif
#define i(x)
std::string & to_upper(std::string &str)
bool is_number(const std::string &str)
std::string replace(std::string str, const std::string &old, const std::string &newstr)
std::vector< std::string > split(std::string str, char d)
std::string & to_lower(std::string &str)
std::string & trim(std::string &str)
std::string & trim_left(std::string &str)
std::string & trim_right(std::string &str)
std::string & trim_all_space(std::string &str)