Util.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006-2010 Jacek Sieka, arnetheduck on gmail point com
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #ifndef ADCHPP_UTIL_H
00020 #define ADCHPP_UTIL_H
00021 
00022 #include "common.h"
00023 
00024 namespace adchpp {
00025 
00027 template<class T1, class T2, class op = std::equal_to<T1> >
00028 class CompareFirst {
00029 public:
00030     CompareFirst(const T1& compareTo) : a(compareTo) { }
00031     bool operator()(const std::pair<T1, T2>& p) { return op()(p.first, a); }
00032 private:
00033     CompareFirst& operator=(const CompareFirst&);
00034     const T1& a;
00035 };
00036 
00038 template<class T1, class T2, class op = std::equal_to<T2> >
00039 class CompareSecond {
00040 public:
00041     CompareSecond(const T2& compareTo) : a(compareTo) { }
00042     bool operator()(const std::pair<T1, T2>& p) { return op()(p.second, a); }
00043 private:
00044     CompareSecond& operator=(const CompareSecond&);
00045     const T2& a;
00046 };
00047 
00048 struct DeleteFunction {
00049     template<typename T>
00050     void operator()(T* ptr) { delete ptr; }
00051 };
00052 
00057 template<typename T1>
00058 inline int compare(const T1& v1, const T1& v2) { return (v1 < v2) ? -1 : ((v1 == v2) ? 0 : 1); }
00059 
00060 class Flags {
00061     public:
00062         typedef size_t MaskType;
00063 
00064         Flags() : flags(0) { }
00065         Flags(const Flags& rhs) : flags(rhs.flags) { }
00066         Flags(MaskType f) : flags(f) { }
00067         MaskType getFlags() const { return flags; }
00068         bool isSet(MaskType aFlag) const { return (flags & aFlag) == aFlag; }
00069         bool isAnySet(MaskType aFlag) const { return (flags & aFlag) != 0; }
00070         void setFlag(MaskType aFlag) { flags |= aFlag; }
00071         void unsetFlag(MaskType aFlag) { flags &= ~aFlag; }
00072         Flags& operator=(const Flags& rhs) { flags = rhs.flags; return *this; }
00073     private:
00074         MaskType flags;
00075 };
00076 
00077 struct Stats {
00078     ADCHPP_DLL static size_t queueCalls;
00079     ADCHPP_DLL static int64_t queueBytes;
00080     ADCHPP_DLL static size_t sendCalls;
00081     ADCHPP_DLL static int64_t sendBytes;
00082     ADCHPP_DLL static int64_t recvCalls;
00083     ADCHPP_DLL static int64_t recvBytes;
00084     ADCHPP_DLL static time_t startTime;
00085 };
00086 
00087 class Util
00088 {
00089 public:
00090     enum Reason {
00091         REASON_BAD_STATE,
00092         REASON_CID_CHANGE,
00093         REASON_CID_TAKEN,
00094         REASON_FLOODING,
00095         REASON_HUB_FULL,
00096         REASON_INVALID_COMMAND_TYPE,
00097         REASON_INVALID_IP,
00098         REASON_INVALID_SID,
00099         REASON_LOGIN_TIMEOUT,
00100         REASON_MAX_COMMAND_SIZE,
00101         REASON_NICK_INVALID,
00102         REASON_NICK_TAKEN,
00103         REASON_NO_BASE_SUPPORT,
00104         REASON_NO_TIGR_SUPPORT,
00105         REASON_PID_MISSING,
00106         REASON_PID_CID_LENGTH,
00107         REASON_PID_CID_MISMATCH,
00108         REASON_PID_WITHOUT_CID,
00109         REASON_PLUGIN,
00110         REASON_WRITE_OVERFLOW,
00111         REASON_NO_BANDWIDTH,
00112         REASON_INVALID_DESCRIPTION,
00113         REASON_WRITE_TIMEOUT,
00114         REASON_LAST,
00115     };
00116 
00117     ADCHPP_DLL static size_t reasons[REASON_LAST];
00118 
00119     ADCHPP_DLL static std::string emptyString;
00120     static std::wstring emptyStringW;
00121 
00122     ADCHPP_DLL static void initialize(const std::string& configPath);
00123     ADCHPP_DLL static std::string getOsVersion();
00124     ADCHPP_DLL static void decodeUrl(const std::string& aUrl, std::string& aServer, short& aPort, std::string& aFile);
00125     ADCHPP_DLL static std::string formatTime(const std::string& msg, time_t t = time(NULL));
00126 
00127     static const std::string& getCfgPath() { return cfgPath; }
00128     static void setCfgPath(const std::string& path) { cfgPath = path; }
00129 
00130     ADCHPP_DLL static std::string getAppPath();
00131     ADCHPP_DLL static std::string getAppName();
00132 
00133 #ifndef _WIN32
00134     ADCHPP_DLL static void setApp(const std::string& app);
00135     static std::string appPath;
00136     static std::string appName;
00137 
00138 #endif
00139 
00140     ADCHPP_DLL static std::string translateError(int aError);
00141 
00142     static std::string formatBytes(const std::string& aString) { return formatBytes(toInt64(aString)); }
00143 
00144     ADCHPP_DLL static std::string getShortTimeString();
00145     ADCHPP_DLL static std::string getTimeString();
00146 
00147     ADCHPP_DLL static std::string formatBytes(int64_t aBytes);
00148 
00149     ADCHPP_DLL static void tokenize(StringList& lst, const std::string& str, char sep, std::string::size_type j = 0);
00150 
00151     static std::string formatSeconds(int64_t aSec) {
00152         char buf[64];
00153         sprintf(buf, "%01d:%02d:%02d:%02d", (int)(aSec / (24*60*60)), (int)((aSec / (60*60)) % 24), (int)((aSec / 60) % 60), (int)(aSec % 60));
00154         return buf;
00155     }
00156 
00157     static bool toBool(const std::string& aString) { return toBool(aString.c_str()); }
00158     static int toInt(const std::string& aString) { return toInt(aString.c_str()); }
00159     static double toDouble(const std::string& aString) { return toDouble(aString.c_str()); }
00160     static float toFloat(const std::string& aString) { return toFloat(aString.c_str()); }
00161     static int64_t toInt64(const std::string& aString) { return toInt64(aString.c_str()); }
00162 
00163     static bool toBool(const char* aString) { return toInt(aString) > 0; }
00164     static int toInt(const char* aString) { return ::atoi(aString); }
00165     static double toDouble(const char* aString) { return ::atof(aString); }
00166     static float toFloat(const char* aString) { return (float)::atof(aString); }
00167     static int64_t toInt64(const char* aString) {
00168 #ifdef _MSC_VER
00169         return _atoi64(aString);
00170 #else
00171         return atoll(aString);
00172 #endif
00173     }
00174 
00175     static std::string toString(bool val) {
00176         return val ? "1" : "0";
00177     }
00178 
00179     static std::string toString(short val) {
00180         char buf[8];
00181         sprintf(buf, "%d", (int)val);
00182         return buf;
00183     }
00184     static std::string toString(unsigned short val) {
00185         char buf[8];
00186         sprintf(buf, "%u", (unsigned int)val);
00187         return buf;
00188     }
00189     static std::string toString(int val) {
00190         char buf[16];
00191         sprintf(buf, "%d", val);
00192         return buf;
00193     }
00194     static std::string toString(unsigned int val) {
00195         char buf[16];
00196         sprintf(buf, "%u", val);
00197         return buf;
00198     }
00199     static std::string toString(long val) {
00200         char buf[32];
00201         sprintf(buf, "%ld", val);
00202         return buf;
00203     }
00204     static std::string toString(unsigned long val) {
00205         char buf[32];
00206         sprintf(buf, "%lu", val);
00207         return buf;
00208     }
00209     static std::string toString(long long val) {
00210         char buf[32];
00211 #if defined(_MSC_VER) || defined(__MINGW32__)
00212         sprintf(buf, "%I64d", val);
00213 #else
00214         sprintf(buf, "%lld", val);
00215 #endif
00216         return buf;
00217     }
00218     static std::string toString(unsigned long long val) {
00219         char buf[32];
00220 #if defined(_MSC_VER) || defined(__MINGW32__)
00221         sprintf(buf, "%I64u", val);
00222 #else
00223         sprintf(buf, "%llu", val);
00224 #endif
00225         return buf;
00226     }
00227 
00228     static std::string toString(double val, int maxDec = 2) {
00229         char buf[32];
00230         sprintf(buf, "%.*f", maxDec, val);
00231         return buf;
00232     }
00233 
00234     static const std::string& toString(const std::string& aString) {
00235         return aString;
00236     }
00237 
00239     ADCHPP_DLL static std::string getLocalIp();
00240 
00241     ADCHPP_DLL static uint32_t rand();
00242     static uint32_t rand(uint32_t high) { return rand() % high; }
00243     static uint32_t rand(uint32_t low, uint32_t high) { return rand(high-low) + low; }
00244     static double randd() { return ((double)rand()) / ((double)0xffffffff); }
00245 
00246     ADCHPP_DLL static bool isPrivateIp(std::string const& ip);
00247     ADCHPP_DLL static bool validateCharset(std::string const& field, int p);
00248 private:
00249     ADCHPP_DLL static std::string cfgPath;
00250 };
00251 
00252 }
00253 
00254 #endif // UTIL_H
Generated on Sat Nov 27 23:37:53 2010 for adchpp by  doxygen 1.6.3