nesca/Utils.h

41 lines
1.0 KiB
C
Raw Normal View History

2015-03-05 14:29:05 +00:00
#ifndef UTILS_H
#define UTILS_H
#include <iostream>
#include <algorithm>
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__)
return toupper(ch1) == toupper(ch2);
#else
return toupper(ch1, loc_) == toupper(ch2, loc_);
#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 {
public:
// find substring (case insensitive)
template<typename T> static int ci_find_substr(const T& str1,
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;
}
char * getProxy();
int getProxyPort();
2015-03-05 14:29:05 +00:00
};
#endif // UTILS_H