summaryrefslogtreecommitdiff
path: root/tools/utils.c
diff options
context:
space:
mode:
authorJohn Denker <jsd@av8n.com>2012-07-29 23:50:11 (GMT)
committerJohn Denker <jsd@av8n.com>2012-07-29 23:50:11 (GMT)
commitd2564d25e802d1ee3230cf045c4940e836b5c6a2 (patch)
treeddc7c3feac95d01ccb859596c21f218b122060c5 /tools/utils.c
parent92280557b6bdde0b3e1d8fa93ba2fae494d5c355 (diff)
split ltgrey (and libltgrey) off from greylist;
put some utility functions into their own file.
Diffstat (limited to 'tools/utils.c')
-rw-r--r--tools/utils.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/tools/utils.c b/tools/utils.c
new file mode 100644
index 0000000..3ec6e4c
--- /dev/null
+++ b/tools/utils.c
@@ -0,0 +1,44 @@
+#include <string>
+#include <sstream>
+#include <iomanip>
+//#include <stdlib.h> /* for abs() */
+#include <cmath>
+
+// strip off the directory part of a path, leaving just
+// the basic filename
+std::string basename(const std::string path){
+ size_t where = path.rfind("/");
+ if (where != std::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){
+ return shorter == longer.substr(0, shorter.length());
+}
+
+///////////////
+// print a time as (-)hh:mm:ss
+//
+std::string time_out(const int _ttt){
+using namespace std;
+ int ttt(abs(_ttt));
+ int sec(ttt % 60);
+ int min((ttt / 60) % 60);
+ int hr(ttt / 3600);
+ stringstream foo;
+ int didsome(0);
+ if (_ttt < 0) foo << "-";
+ if (hr) {
+ foo << hr << ":";
+ didsome++;
+ }
+ if (didsome || min){
+ foo << setw(didsome?2:1) << setfill('0') << min << ":";
+ didsome++;
+ }
+ foo << setw(didsome?2:1) << setfill('0') << sec;
+ return foo.str();
+}