]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/JSBSim/input_output/string_utilities.h
PAtch by Andreas Gaeb to eliminate NaN's in the location code
[flightgear.git] / src / FDM / JSBSim / input_output / string_utilities.h
index bc3a152a9e105df37b06c890254c61862156cab8..b55073f8b910e1504380587ef1988403bdfb9b0f 100644 (file)
@@ -40,12 +40,13 @@ INCLUDES
 
 #include <string>
 #include <vector>
+#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
@@ -64,18 +65,21 @@ CLASS DECLARATION
   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 <ctype.h>
+  #include <cctype>
 
   using namespace std;
 
   string& trim_left(string& str)
   {
-    while (str.size() && !isgraph(str[0])) {
+    while (str.size() && isspace((unsigned char)str[0])) {
       str = str.erase(0,1);
     }
     return str;
@@ -83,7 +87,7 @@ CLASS DECLARATION
 
   string& trim_right(string& str)
   {
-    while (str.size() && !isgraph(str[str.size()-1])) {
+    while (str.size() && isspace((unsigned char)str[str.size()-1])) {
       str = str.erase(str.size()-1,1);
     }
     return str;
@@ -96,6 +100,17 @@ CLASS DECLARATION
     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 (size_t i=0; i<str.size(); i++) str[i] = toupper(str[i]);
@@ -136,6 +151,24 @@ CLASS DECLARATION
     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
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%