Author: Jon S. Berndt
Date started: 06/01/09
- ------------- Copyright (C) 2009 Jon S. Berndt (jsb@hal-pc.org) -------------
+ ------------- Copyright (C) 2009 Jon S. Berndt (jon@jsbsim.org) -------------
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
INCLUDES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
+#include <string>
#include <vector>
-#include <ctype.h>
+#include <stdio.h>
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
DEFINITIONS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
-#define ID_STRINGUTILS "$Id$"
+#define ID_STRINGUTILS "$Id: string_utilities.h,v 1.14 2010/08/21 17:13:47 jberndt Exp $"
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FORWARD DECLARATIONS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
-using namespace std;
-
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
CLASS DOCUMENTATION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
#if !defined(BASE)
- extern string& trim_left(string& str);
- extern string& trim_right(string& str);
- extern string& trim(string& str);
- extern string& to_upper(string& str);
- extern string& to_lower(string& str);
- extern bool is_number(string& str);
- vector <string> split(string str, char d);
+ 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);
+ extern bool is_number(const std::string& str);
+ std::vector <std::string> split(std::string str, char d);
+ extern std::string to_string(int);
+ extern std::string replace(std::string str, const std::string& old, const std::string& newstr);
#else
+ #include <cctype>
+
+ using namespace std;
string& trim_left(string& str)
{
- while ( !isgraph(str[0]) ) {
+ while (str.size() && isspace((unsigned char)str[0])) {
str = str.erase(0,1);
- if (str.size() == 0) break;
}
return str;
}
string& trim_right(string& str)
{
- while (!isgraph(str[str.size()-1])) {
+ while (str.size() && isspace((unsigned char)str[str.size()-1])) {
str = str.erase(str.size()-1,1);
- if (str.size() == 0) break;
}
return str;
}
return str = trim_left(temp_str);
}
+ string& trim_all_space(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;
+ }
+
string& to_upper(string& str)
{
- for (int i=0; i<str.size(); i++) str[i] = toupper(str[i]);
+ for (size_t i=0; i<str.size(); i++) str[i] = toupper(str[i]);
return str;
}
string& to_lower(string& str)
{
- for (int i=0; i<str.size(); i++) str[i] = tolower(str[i]);
+ for (size_t i=0; i<str.size(); i++) str[i] = tolower(str[i]);
return str;
}
- bool is_number(string& str)
+ bool is_number(const string& str)
{
return (str.find_first_not_of("+-.0123456789Ee") == string::npos);
}
vector <string> split(string str, char d)
{
vector <string> str_array;
- int index=0;
+ size_t index=0;
string temp = "";
trim(str);
return str_array;
}
+ string to_string(int i)
+ {
+ char buffer[32];
+ sprintf(buffer, "%d", i);
+ return string(buffer);
+ }
+
+ string replace(string str, const string& oldstr, const string& newstr)
+ {
+ int old_idx;
+ string temp;
+ old_idx = str.find(oldstr);
+ if (old_idx >= 0) {
+ temp = str.replace(old_idx, 1, newstr);
+ }
+ return temp;
+ }
+
#endif
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#endif
+