nesca/Utils.h

103 lines
3.1 KiB
C
Raw Normal View History

2015-03-05 14:29:05 +00:00
#ifndef UTILS_H
#define UTILS_H
#include <iostream>
#include <algorithm>
#include <qstring.h>
#include <vector>
2015-08-07 22:37:28 +00:00
#include <qdatetime.h>
2015-03-05 14:29:05 +00:00
2015-08-30 14:40:00 +00:00
#define STRSTR(buff, str) Utils::ustrstr(buff, str)
2015-03-07 17:31:48 +00:00
using namespace std;
2015-03-05 14:29:05 +00:00
template<typename charT>
struct my_equal {
2015-03-07 17:31:48 +00:00
my_equal( const locale loc ) : loc_(loc) {}
2015-03-05 14:29:05 +00:00
bool operator()(charT ch1, charT ch2) {
2015-03-07 17:31:48 +00:00
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
2015-03-10 14:35:50 +00:00
return tolower(ch1) == tolower(ch2);
2015-03-07 17:31:48 +00:00
#else
2015-03-10 14:35:50 +00:00
return tolower(ch1, loc_) == tolower(ch2, loc_);
2015-03-07 17:31:48 +00:00
#endif
2015-03-05 14:29:05 +00:00
}
private:
2015-03-07 17:31:48 +00:00
const locale& loc_;
2015-03-05 14:29:05 +00:00
};
class Utils {
2015-08-07 22:37:28 +00:00
private: static std::string startDate;
2015-08-08 20:31:07 +00:00
private: static std::string startTime;
2016-02-28 16:07:10 +00:00
private: static std::string currentTarget;
2015-03-05 14:29:05 +00:00
public:
2015-04-25 19:45:01 +00:00
static int isDigest(const std::string *buffer);
2015-03-05 14:29:05 +00:00
// find substring (case insensitive)
template<typename T> static int ustrstr(const T& str1,
2015-03-05 14:29:05 +00:00
const T& str2,
2015-03-07 17:31:48 +00:00
const locale& loc = locale()) {
2015-03-06 14:32:36 +00:00
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;
2015-04-18 13:05:35 +00:00
}
2015-03-06 14:32:36 +00:00
template<typename T> static int ustrstr(const T& str1,
2015-04-18 13:05:35 +00:00
const char* str2c,
const locale& loc = locale()) {
2015-03-10 14:35:50 +00:00
2015-04-18 13:05:35 +00:00
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;
}
2016-02-28 16:07:10 +00:00
template<typename T> static int ustrstr(T *str1,
2015-04-18 13:05:35 +00:00
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;
}
2015-03-10 14:35:50 +00:00
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!");
}
2015-03-06 14:32:36 +00:00
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);
2015-08-07 22:37:28 +00:00
static void saveStartDate();
2015-08-08 20:31:07 +00:00
static void saveStartTime();
2015-08-07 22:37:28 +00:00
static std::string getStartDate();
2015-08-08 20:31:07 +00:00
static std::string getStartTime();
2016-02-28 16:07:10 +00:00
static void setCurrentTarget(const std::string target);
static std::string getCurrentTarget();
2015-08-07 22:37:28 +00:00
static void emitScaryError();
2016-02-28 16:07:10 +00:00
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);
2015-03-05 14:29:05 +00:00
};
#endif // UTILS_H