Debian Codegraph @

src/FDM/JSBSim/input_output/string_utilities.cpp

raw symbols SCIP
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: string_utilities.cpp
4 Author: Bertrand Coconnier / Sean McLeod
5 Date started: 12/28/22
6 Purpose: Utilities to manipulate strings.
7
8 ------------ Copyright (C) 2022 Bertrand Coconnier, Sean McLeod, -------------
9
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 2 of the License, or (at your option) any
13 later version.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 details.
19
20 You should have received a copy of the GNU Lesser General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc., 59
22 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24 Further information about the GNU Lesser General Public License can also be
25 found on the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29 String handling conveniences such as trim, is_number, split, etc.; these new
30 capabilities have been incorporated into the source code where the
31 string::find() functions were formerly used.
32
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 INCLUDES
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
36
37 #include <errno.h>
38 #include <iostream>
39 #include <sstream>
40 #include <stdio.h>
41 #ifdef __APPLE__
42 #include <xlocale.h>
43 #else
44 #include <locale.h>
45 #endif
46
47 #include "FGJSBBase.h"
48 #include "string_utilities.h"
49
50 #ifdef _WIN32
51 typedef _locale_t locale_t;
52 #define freelocale _free_locale
53 #define strtod_l _strtod_l
54 #endif
55
56 namespace JSBSim {
57 struct CNumericLocale
58 {
59 CNumericLocale()
60 {
61 #ifdef _WIN32
62 Locale = _create_locale(LC_NUMERIC, "C");
63 #else
64 Locale = newlocale(LC_NUMERIC_MASK, "C", 0);
65 #endif
66 }
67
68 ~CNumericLocale()
69 {
70 freelocale(Locale);
71 }
72
73 locale_t Locale;
74 };
75
76 /* A locale independent version of atof().
77 * Whatever is the current locale of the application, atof_locale_c() reads
78 * numbers assuming that the decimal point is the period (.)
79 */
80 double atof_locale_c(const std::string& input)
81 {
82 const char* first = input.c_str();
83
84 // Skip leading whitespaces
85 while (isspace(*first)) ++first;
86 //Ignoring the leading '+' sign
87 if (*first == '+') ++first;
88
89 CNumericLocale numeric_c;
90 errno = 0; // Reset the error code
91 double value = strtod_l(first, nullptr, numeric_c.Locale);
92
93 // Error management
94 std::stringstream s;
95
96 if (fabs(value) == HUGE_VAL && errno == ERANGE)
97 s << "This number is too large: " << input;
98 else if (fabs(value) == 0 && errno == EINVAL)
99 s << "Expecting numeric attribute value, but got: " << input;
100 else
101 return value;
102
103 std::cerr << s.str() << std::endl;
104 throw JSBSim::BaseException(s.str());
105 }
106
107
108 std::string& trim_left(std::string& str)
109 {
110 while (!str.empty() && isspace((unsigned char)str[0])) {
111 str = str.erase(0,1);
112 }
113 return str;
114 }
115
116 std::string& trim_right(std::string& str)
117 {
118 while (!str.empty() && isspace((unsigned char)str[str.size()-1])) {
119 str = str.erase(str.size()-1,1);
120 }
121 return str;
122 }
123
124 std::string& trim(std::string& str)
125 {
126 if (str.empty()) return str;
127 std::string temp_str = trim_right(str);
128 return str = trim_left(temp_str);
129 }
130
131 std::string& trim_all_space(std::string& str)
132 {
133 for (size_t i=0; i<str.size(); i++) {
134 if (isspace((unsigned char)str[i])) {
135 str = str.erase(i,1);
136 --i;
137 }
138 }
139 return str;
140 }
141
142 std::string& to_upper(std::string& str)
143 {
144 for (size_t i=0; i<str.size(); i++) str[i] = toupper(str[i]);
145 return str;
146 }
147
148 std::string& to_lower(std::string& str)
149 {
150 for (size_t i=0; i<str.size(); i++) str[i] = tolower(str[i]);
151 return str;
152 }
153
154 bool is_number(const std::string& str)
155 {
156 if (str.empty())
157 return false;
158 else
159 return (str.find_first_not_of("+-.0123456789Ee") == std::string::npos);
160 }
161
162 std::vector <std::string> split(std::string str, char d)
163 {
164 std::vector <std::string> str_array;
165 size_t index=0;
166 std::string temp = "";
167
168 trim(str);
169 index = str.find(d);
170 while (index != std::string::npos) {
171 temp = str.substr(0,index);
172 trim(temp);
173 if (!temp.empty()) str_array.push_back(temp);
174 str = str.erase(0,index+1);
175 index = str.find(d);
176 }
177 if (!str.empty()) {
178 temp = trim(str);
179 if (!temp.empty()) str_array.push_back(temp);
180 }
181
182 return str_array;
183 }
184
185 std::string replace(std::string str, const std::string& oldstr, const std::string& newstr)
186 {
187 std::string temp = str;
188 size_t old_idx = str.find(oldstr);
189 if (old_idx != std::string::npos) {
190 temp = str.replace(old_idx, 1, newstr);
191 }
192 return temp;
193 }
194 };