-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.cc
123 lines (110 loc) · 2.65 KB
/
utilities.cc
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <iterator>
#include <algorithm>
#include "utilities.h"
using namespace std;
std::string& ltrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
std::string& rtrim(std::string& s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
}
std::string& trim(std::string& s)
{
return ltrim(rtrim(s));
}
std::vector<std::string>& split(const std::string& s, char delim, std::vector<std::string>& elems)
{
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string& s, char delim)
{
std::vector<std::string> elems;
return split(s, delim, elems);
}
vector<string> string_split(const string& s)
{
vector<string> to_ret;
istringstream iss(s);
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter<vector<string> >(to_ret));
return to_ret;
}
std::string sday2iday(const std::string& s)
{
if(s == "sun")
return "1";
else if(s == "mon")
return "2";
else if(s == "tue")
return "3";
else if(s == "wed")
return "4";
else if(s == "thu")
return "5";
else if(s == "fri")
return "6";
else if(s == "sat")
return "7";
}
std::string smonth2imonth(const std::string& s)
{
if(s == "jan")
return "1";
else if(s == "feb")
return "2";
else if(s == "mar")
return "3";
else if(s == "apr")
return "4";
else if(s == "may")
return "5";
else if(s == "jun")
return "6";
else if(s == "jul")
return "7";
else if(s == "aug")
return "8";
else if(s == "sep")
return "9";
else if(s == "oct")
return "10";
else if(s == "nov")
return "11";
else if(s == "dec")
return "12";
}
bool isNumeric( const char* pszInput, int nNumberBase )
{
std::istringstream iss( pszInput );
if ( nNumberBase == 10 )
{
double dTestSink;
iss >> dTestSink;
}
else if ( nNumberBase == 8 || nNumberBase == 16 )
{
int nTestSink;
iss >> ( ( nNumberBase == 8 ) ? std::oct : std::hex ) >> nTestSink;
}
else
return false;
// was any input successfully consumed/converted?
if ( ! iss )
return false;
// was all the input successfully consumed/converted?
return ( iss.rdbuf()->in_avail() == 0 );
}