forked from pantyusha/nesca
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Utils.h
102 lines (84 loc) · 3.15 KB
/
Utils.h
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
#ifndef UTILS_H
#define UTILS_H
#include <iostream>
#include <algorithm>
#include <qstring.h>
#include <vector>
#include <qdatetime.h>
#define STRSTR(buff, str) Utils::ustrstr(buff, str)
using namespace std;
template<typename charT>
struct my_equal {
my_equal( const locale loc ) : loc_(loc) {}
bool operator()(charT ch1, charT ch2) {
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
return tolower(ch1) == tolower(ch2);
#else
return tolower(ch1, loc_) == tolower(ch2, loc_);
#endif
}
private:
const locale& loc_;
};
class Utils {
private: static std::string startDate;
private: static std::string startTime;
private: static std::string currentTarget;
public:
static int isDigest(const std::string *buffer);
// find substring (case insensitive)
template<typename T> static int ustrstr(const T& str1,
const T& str2,
const locale& loc = locale()) {
auto it = std::search(str1.begin(), str1.end(), str2.begin(), str2.end(),
my_equal<typename T::value_type>(loc));
if(it != str1.end()) return it - str1.begin();
else return -1;
}
template<typename T> static int ustrstr(const T& str1,
const char* str2c,
const locale& loc = locale()) {
std::string str2 = std::string(str2c);
auto it = std::search(str1.begin(), str1.end(), str2.begin(), str2.end(),
my_equal<typename T::value_type>(loc));
if (it != str1.end()) return it - str1.begin();
else return -1;
}
template<typename T> static int ustrstr(T *str1,
const char* str2c,
const locale& loc = locale()) {
std::string str2 = std::string(str2c);
auto it = std::search(str1->begin(), str1->end(), str2.begin(), str2.end(),
my_equal<typename T::value_type>(loc));
if (it != str1->end()) return it - str1->begin();
else return -1;
}
static QString GetNSErrorDefinition(const char *str, const char *elem){
const char *temp = strstr(str, elem);
if (temp != NULL)
{
char definition[128] = { 0 };
const char *firstComma = strstr(temp + strlen(elem) + 1, "\"");
const char *lastComma = strstr(firstComma + 1, "\"");
int sz = lastComma - firstComma - 1;
strncpy(definition, firstComma + 1, (sz < 128 ? sz : 128));
return QString(definition);
}
else return QString("No definition found!");
}
char * getProxy();
int getProxyPort();
static std::string getStrValue(const std::string &data, const std::string &delim1, const std::string &delim2);
static std::vector<std::string> splitToStrVector(const std::string &s, char delim);
static std::vector<int> splitToIntVector(const std::string &s, char delim);
static void saveStartDate();
static void saveStartTime();
static std::string getStartDate();
static std::string getStartTime();
static void setCurrentTarget(const std::string target);
static std::string getCurrentTarget();
static void emitScaryError();
static int addBARow(QString str1, QString str2, QString str3, int rowIndex);
static std::string getHeaderValue(std::string *buff, const std::string headerValue, const std::string outputName);
};
#endif // UTILS_H