00001
00002 #ifndef MUtilsH
00003 #define MUtilsH
00004
00005 #include <string>
00006 #include <stdexcept>
00007 #include <sstream>
00008 #include <stdio.h>
00009
00010 #ifdef WIN32
00011 typedef unsigned long DWORD;
00012 #define TIME DWORD
00013 #define GETTIME(a) a = GetTickCount()
00014 #define RESETTIME(a) a = 0
00015 #define TIMEZERO 0
00016 #else
00017 #define TIME struct timeval
00018 #define GETTIME(a) gettimeofday(&(a),NULL)
00019 #define RESETTIME(a) a.tv_sec = a.tv_usec = 0
00020 #define TIMEZERO {0,0}
00021 #endif
00022
00023
00024 class BadConversion : public std::runtime_error {
00025 public:
00026 BadConversion(const std::string& s) : std::runtime_error(s) {}
00027 };
00028
00029
00030 namespace MUtils {
00031
00032
00033 class MFilePtr {
00034
00035 FILE* p;
00036
00037 public:
00038
00039 MFilePtr(const char* n, const char *a) { p = fopen(n,a); }
00040 MFilePtr(FILE* pp) { p = pp; }
00041 ~MFilePtr(){if(p) fclose(p);}
00042
00043 operator FILE*() {return p;}
00044
00045 };
00046
00047
00048 class MCharPtr {
00049
00050 char* p;
00051
00052 public:
00053
00054 MCharPtr(char* n) { p = n; }
00055 ~MCharPtr(){if(p) delete [] p;}
00056
00057 operator char*() {return p;}
00058
00059 };
00060
00061
00062 class MDate {
00063 public:
00064 class baddate : public std::exception {
00065 public:
00066 baddate(const std::string& m) {Msg=m;}
00067 const char* what() const {return Msg.c_str();}
00068 private:
00069 std::string Msg;
00070 };
00071
00072 MDate() {}
00073 ~MDate() {}
00074
00075 void Set(const std::string& d) {
00076
00077 Date = d;
00078 }
00079 MDate& operator =(const MDate& d) {Set((std::string)d);return *this;}
00080 operator std::string() const {return Date;}
00081 const char* c_str() const {return Date.c_str();}
00082 std::string Format(int f=0) const;
00083 private:
00084 std::string Date;
00085 };
00086
00087
00088 bool operator <(const MDate& a, const MDate& b);
00089 bool operator ==(const MDate& a, const MDate& b);
00090 bool operator !=(const MDate& a, const MDate& b);
00091 std::ostream& operator <<(std::ostream& o, const MDate& m);
00092
00093
00094 MDate Now();
00095 void Replace(std::string& str, const std::string& from, const std::string& to);
00096 std::string ToSQL(const std::string& token);
00097 template<class OutIt>
00098 void Split(const std::string& s, const std::string& sep, OutIt dest);
00099 std::string Tolower(std::string str);
00100 std::string int2string(int i);
00101 std::string long2string(unsigned long i);
00102 int string2int(const std::string& s);
00103
00105 unsigned long TimeInterval(const TIME& a, const TIME& b);
00106
00107
00115 std::string MyNow(int t);
00116
00117 std::string GetAppFileName();
00118 void Back2Slash(std::string& str);
00119 void Slash2Back(std::string& str);
00120 std::string ExtractFilePath(const std::string& str);
00121 std::string ExtractFileName(const std::string& str);
00122 std::string ExtractFileNameWithoutExt(const std::string& str);
00123 std::string ExtractFileExt(const std::string& str);
00124 bool FileExists(const std::string& str);
00125 unsigned __int64 FileSize(const std::string& fn);
00126 MDate FileDate(const std::string& str);
00127 bool RenameFile(const std::string& from,const std::string& to);
00128 bool DeleteFile(const std::string& str);
00129 bool IsPrefix(const std::string& prefix, const std::string& str);
00130 }
00131 #endif
00132