summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Denker <jsd@av8n.com>2012-07-31 17:47:39 -0700
committerJohn Denker <jsd@av8n.com>2012-07-31 17:47:39 -0700
commitefb6301f794cc272173fa09ad83293ad3aa79ad4 (patch)
tree54ef2f8c20d913877b8f33fb21b850ebec441b8a
parentc9652f33e6a4c1769cb9c54a54f2a6bd78e5f434 (diff)
smarter utils
-rw-r--r--tools/utils.c35
-rw-r--r--tools/utils.h9
2 files changed, 31 insertions, 13 deletions
diff --git a/tools/utils.c b/tools/utils.c
index 602a144..a7c2878 100644
--- a/tools/utils.c
+++ b/tools/utils.c
@@ -6,26 +6,28 @@
///// due to lack of interger abs()
///// and ambiguous (and inefficient) promotion
#include <ctype.h> /* for isalnum() */
+using namespace std;
+
// strip off the directory part of a path, leaving just
// the basic filename
-std::string basename(const std::string path){
+string basename(const string path){
size_t where = path.rfind("/");
- if (where != std::string::npos) return path.substr(1+where);
+ if (where != string::npos) return path.substr(1+where);
return path;
}
////////////////
// little utility to help with argument parsing:
//
-int prefix(const std::string shorter, const std::string longer){
+int prefix(const string shorter, const string longer){
return shorter == longer.substr(0, shorter.length());
}
///////////////
// print a time as (-)hh:mm:ss
//
-std::string time_out(const int _ttt){
+string time_out(const int _ttt){
using namespace std;
int ttt(abs(_ttt));
int sec(ttt % 60);
@@ -46,25 +48,34 @@ using namespace std;
return foo.str();
}
-std::string toLower(const std::string a){
- std::string rslt = a;
- std::string::iterator rr;
+string toLower(const string a){
+ string rslt = a;
+ string::iterator rr;
for (rr = rslt.begin(); rr != rslt.end(); rr++){
*rr = tolower(*rr);
}
return rslt;
}
-////////////////
-std::string ltrim(const std::string foo){
- size_t where = foo.find_first_not_of(" \t\r\n");
+string ltrim(const string foo, const string strip){
+ size_t where = foo.find_first_not_of(strip);
if (where == foo.npos) return foo;
return foo.substr(where);
}
-static const std::string Pure_Enough("+-_.,@%~");
+string rtrim(const string foo, const string strip){
+ size_t where = foo.find_last_not_of(strip);
+ if (where == foo.npos) return "";
+ return foo.substr(0, where+1);
+}
+
+string trim(const string foo, const string bar){
+ return ltrim(rtrim(foo, bar), bar);
+}
+
+static const string Pure_Enough("+-_.,@%~");
-std::string purify(const std::string arg){
+string purify(const string arg){
using namespace std;
string rslt(arg);
for (string::iterator ptr = rslt.begin();
diff --git a/tools/utils.h b/tools/utils.h
index cbcb795..2c531b8 100644
--- a/tools/utils.h
+++ b/tools/utils.h
@@ -3,5 +3,12 @@ int prefix(const std::string shorter, const std::string longer);
std::string time_out(const int _ttt);
std::string toLower(const std::string a);
-std::string ltrim(const std::string a);
std::string purify(const std::string arg);
+std::string ltrim(const std::string foo,
+ const std::string strip = " \t\r\n");
+
+std::string rtrim(const std::string foo,
+ const std::string strip = " \t\r\n");
+
+std::string trim(const std::string foo,
+ const std::string strip = " \t\r\n");