summaryrefslogtreecommitdiff
path: root/tools/utils.h
blob: 428c7f543a3df21f0361021cb0643f329f652fc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef UTILS_H
#define UTILS_H
#include <string>
#include <list>
#include <iostream>

std::string noCR(const std::string bar);
int cmp_casefold(const std::string& a, const std::string& b);
std::string join(const std::string sep, const std::list<std::string> stuff);

/////////////////////////////////////////////////////////
// Case insensitive comparison of strings

class lessthan_foldcase{
public:
  bool operator() (const std::string& a, const std::string& b) const {
    size_t a_len = a.length();
    size_t b_len = b.length();

    size_t lim = a_len < b_len ? a_len : b_len;

    for (size_t i=0; i<lim; ++i)
    {
            char cha = toupper(a[i]);
            char chb = toupper(b[i]);

            if (cha < chb) return true;
            if (cha > chb) return false;
    }
    // here if one is an extension of the other
    if ( a_len < b_len ) return true;
    return false;
  }
};

std::string basename(const std::string path);
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 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");

typedef const char cc;
typedef cc* str;

class argParser{
public:
  std::list<str> argv;
  std::string current_arg;

  argParser(const int _argc, const str _argv[])
  {
    for (int ii=0; ii < _argc; ii++) {
      argv.push_back(_argv[ii]);
    }
  }
  std::string shift() {
    using namespace std;
    string rslt = argv.front();
    argv.pop_front();
    return rslt;
  }
  std::string next(){
    current_arg = shift();
    return current_arg;
  }
  size_t size() const {
    return argv.size();
  }
  int prefix(const std::string longer, const size_t required = 0){
    using namespace std;
    if (argv.size() < required) {
      if (required==1)
        cerr << "Option '" << current_arg
          << "' requires an argument." << endl;
      else
        cerr << "Option '" << current_arg
          << "' requires " << required << " arguments." << endl;
      throw int(1);
    }
    return current_arg == longer.substr(0, current_arg.length());
  }
};

std::string strError(const int);
std::string strError();
#endif