Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the flag BASE from string_utilities.h #837

Merged
merged 6 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/FGJSBBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ HISTORY
INCLUDES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

#define BASE

#include "FGJSBBase.h"
#include "models/FGAtmosphere.h"

Expand Down
1 change: 1 addition & 0 deletions src/input_output/FGXMLElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
INCLUDES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

#include <iostream>
#include <sstream> // for assembling the error messages / what of exceptions.
#include <stdexcept> // using domain_error, invalid_argument, and length_error.
#include "FGXMLElement.h"
Expand Down
2 changes: 2 additions & 0 deletions src/input_output/FGfdmSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ INCLUDES
#include <unistd.h>
#endif
#include <iomanip>
#include <iostream>
#include <sstream>
#include <cstring>
#include "FGfdmSocket.h"

Expand Down
94 changes: 93 additions & 1 deletion src/input_output/string_utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ INCLUDES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

#include <errno.h>
#include <iostream>
#include <sstream>
#include <stdio.h>
#ifdef __APPLE__
#include <xlocale.h>
#else
#include <locale.h>
#endif
#include <sstream>

#include "FGJSBBase.h"
#include "string_utilities.h"
Expand All @@ -51,6 +53,7 @@ typedef _locale_t locale_t;
#define strtod_l _strtod_l
#endif

namespace JSBSim {
struct CNumericLocale
{
CNumericLocale()
Expand Down Expand Up @@ -100,3 +103,92 @@ double atof_locale_c(const std::string& input)
std::cerr << s.str() << std::endl;
throw JSBSim::BaseException(s.str());
}


std::string& trim_left(std::string& str)
{
while (!str.empty() && isspace((unsigned char)str[0])) {
str = str.erase(0,1);
}
return str;
}

std::string& trim_right(std::string& str)
{
while (!str.empty() && isspace((unsigned char)str[str.size()-1])) {
str = str.erase(str.size()-1,1);
}
return str;
}

std::string& trim(std::string& str)
{
if (str.empty()) return str;
std::string temp_str = trim_right(str);
return str = trim_left(temp_str);
}

std::string& trim_all_space(std::string& str)
{
for (size_t i=0; i<str.size(); i++) {
if (isspace((unsigned char)str[i])) {
str = str.erase(i,1);
--i;
}
}
return str;
}

std::string& to_upper(std::string& str)
{
for (size_t i=0; i<str.size(); i++) str[i] = toupper(str[i]);
return str;
}

std::string& to_lower(std::string& str)
{
for (size_t i=0; i<str.size(); i++) str[i] = tolower(str[i]);
return str;
}

bool is_number(const std::string& str)
{
if (str.empty())
return false;
else
return (str.find_first_not_of("+-.0123456789Ee") == std::string::npos);
}

std::vector <std::string> split(std::string str, char d)
{
std::vector <std::string> str_array;
size_t index=0;
std::string temp = "";

trim(str);
index = str.find(d);
while (index != std::string::npos) {
temp = str.substr(0,index);
trim(temp);
if (!temp.empty()) str_array.push_back(temp);
str = str.erase(0,index+1);
index = str.find(d);
}
if (!str.empty()) {
temp = trim(str);
if (!temp.empty()) str_array.push_back(temp);
}

return str_array;
}

std::string replace(std::string str, const std::string& oldstr, const std::string& newstr)
{
std::string temp = str;
size_t old_idx = str.find(oldstr);
if (old_idx != std::string::npos) {
temp = str.replace(old_idx, 1, newstr);
}
return temp;
}
};
119 changes: 11 additions & 108 deletions src/input_output/string_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,121 +39,24 @@ INCLUDES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <stdio.h>

#include "JSBSim_API.h"

/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
CLASS DECLARATION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

JSBSIM_API bool is_number(const std::string& str);
namespace JSBSim {
JSBSIM_API double atof_locale_c(const std::string& input);

#if !defined(BASE)
extern std::string& trim_left(std::string& str);
extern std::string& trim_right(std::string& str);
extern std::string& trim(std::string& str);
extern std::string& trim_all_space(std::string& str);
extern std::string& to_upper(std::string& str);
extern std::string& to_lower(std::string& str);
std::vector <std::string> split(std::string str, char d);

extern std::string replace(std::string str, const std::string& old, const std::string& newstr);
#else
#include <cctype>

std::string& trim_left(std::string& str)
{
while (!str.empty() && isspace((unsigned char)str[0])) {
str = str.erase(0,1);
}
return str;
}

std::string& trim_right(std::string& str)
{
while (!str.empty() && isspace((unsigned char)str[str.size()-1])) {
str = str.erase(str.size()-1,1);
}
return str;
}

std::string& trim(std::string& str)
{
if (str.empty()) return str;
std::string temp_str = trim_right(str);
return str = trim_left(temp_str);
}

std::string& trim_all_space(std::string& str)
{
for (size_t i=0; i<str.size(); i++) {
if (isspace((unsigned char)str[i])) {
str = str.erase(i,1);
--i;
}
}
return str;
}

std::string& to_upper(std::string& str)
{
for (size_t i=0; i<str.size(); i++) str[i] = toupper(str[i]);
return str;
}

std::string& to_lower(std::string& str)
{
for (size_t i=0; i<str.size(); i++) str[i] = tolower(str[i]);
return str;
}

bool is_number(const std::string& str)
{
if (str.empty())
return false;
else
return (str.find_first_not_of("+-.0123456789Ee") == std::string::npos);
}

std::vector <std::string> split(std::string str, char d)
{
std::vector <std::string> str_array;
size_t index=0;
std::string temp = "";

trim(str);
index = str.find(d);
while (index != std::string::npos) {
temp = str.substr(0,index);
trim(temp);
if (!temp.empty()) str_array.push_back(temp);
str = str.erase(0,index+1);
index = str.find(d);
}
if (!str.empty()) {
temp = trim(str);
if (!temp.empty()) str_array.push_back(temp);
}

return str_array;
}

std::string replace(std::string str, const std::string& oldstr, const std::string& newstr)
{
std::string temp = str;
size_t old_idx = str.find(oldstr);
if (old_idx != std::string::npos) {
temp = str.replace(old_idx, 1, newstr);
}
return temp;
}

#endif
JSBSIM_API std::string& trim_left(std::string& str);
JSBSIM_API std::string& trim_right(std::string& str);
JSBSIM_API std::string& trim(std::string& str);
JSBSIM_API std::string& trim_all_space(std::string& str);
JSBSIM_API std::string& to_upper(std::string& str);
JSBSIM_API std::string& to_lower(std::string& str);
JSBSIM_API bool is_number(const std::string& str);
JSBSIM_API std::vector <std::string> split(std::string str, char d);
JSBSIM_API std::string replace(std::string str, const std::string& old, const std::string& newstr);
};

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Expand Down
1 change: 1 addition & 0 deletions src/simgear/xml/easyxml.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ INCLUDES

#include <fstream>
#include <iostream>
#include <sstream>

#include "FGJSBBase.h"

Expand Down
10 changes: 5 additions & 5 deletions tests/unit_tests/StringUtilitiesTest.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <string>
#include <cxxtest/TestSuite.h>
#define BASE
#include <input_output/string_utilities.h>
#undef BASE
#include "FGJSBBase.h"
#include <input_output/string_utilities.h>

using namespace JSBSim;

class StringUtilitiesTest : public CxxTest::TestSuite
{
Expand Down Expand Up @@ -95,8 +95,8 @@ class StringUtilitiesTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(atof_locale_c("1E-999"), 0.0);
TS_ASSERT_EQUALS(atof_locale_c("-1E-999"), 0.0);
TS_ASSERT_EQUALS(atof_locale_c("0.0"), 0.0);
TS_ASSERT_THROWS(atof_locale_c("1E+999"), JSBSim::BaseException&);
TS_ASSERT_THROWS(atof_locale_c("-1E+999"), JSBSim::BaseException&);
TS_ASSERT_THROWS(atof_locale_c("1E+999"), BaseException&);
TS_ASSERT_THROWS(atof_locale_c("-1E+999"), BaseException&);
}

private:
Expand Down