00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef APP_UTIL_H
00024 #define APP_UTIL_H
00025
00026 #include <libintl.h>
00027 #include <sys/time.h>
00028 #include <sys/types.h>
00029 #include <sstream>
00030 #include <locale>
00031 #include <string>
00032 #include <list>
00033 #include <map>
00034
00035
00036 namespace storage
00037 {
00038 using std::string;
00039 using std::list;
00040 using std::map;
00041
00042
00043 void createPath(const string& Path_Cv);
00044 bool checkNormalFile(const string& Path_Cv);
00045 bool checkDir(const string& Path_Cv);
00046 bool getStatMode(const string& Path_Cv, mode_t& val );
00047 bool setStatMode(const string& Path_Cv, mode_t val );
00048
00049 list<string> glob(const string& path, int flags);
00050
00051 struct StatVfs
00052 {
00053 unsigned long long sizeK;
00054 unsigned long long freeK;
00055 };
00056
00057 bool getStatVfs(const string& path, StatVfs&);
00058
00059 bool getMajorMinor(const string& device, unsigned long& major, unsigned long& minor);
00060
00061
00062 string extractNthWord(int Num_iv, const string& Line_Cv, bool GetRest_bi = false);
00063 std::list<string> splitString( const string& s, const string& delChars=" \t\n",
00064 bool multipleDelim=true, bool skipEmpty=true,
00065 const string& quotes="" );
00066 std::map<string,string> makeMap( const std::list<string>& l,
00067 const string& delim = "=",
00068 const string& removeSur = " \t\n" );
00069
00070 string udevAppendPart(const string&, unsigned num);
00071
00072 string udevEncode(const string&);
00073 string udevDecode(const string&);
00074
00075 map<string, string> getDirLinks(const string& dir);
00076
00077 bool mkdtemp(string& path);
00078
00079 bool readlink(const string& path, string& buf);
00080
00081
00082 class UdevMap
00083 {
00084 public:
00085
00086 UdevMap(const string& path);
00087
00088 typedef map<string, list<string>>::const_iterator const_iterator;
00089
00090 const_iterator begin() const { return data.begin(); }
00091 const_iterator end() const { return data.end(); }
00092
00093 const_iterator find(const string& nm) const { return data.find(nm); }
00094
00095 private:
00096
00097 map<string, list<string>> data;
00098
00099 };
00100
00101
00102 string normalizeDevice(const string& dev);
00103 list<string> normalizeDevices(const list<string>& devs);
00104 string undevDevice(const string& dev);
00105
00106 bool isNfsDev( const string& dev );
00107
00108 unsigned getMajorDevices(const char* driver);
00109
00110
00111 template<class StreamType>
00112 void classic(StreamType& stream)
00113 {
00114 stream.imbue(std::locale::classic());
00115 }
00116
00117
00118 enum LogLevel { DEBUG, MILESTONE, WARNING, ERROR };
00119
00120 void createLogger(const string& name, const string& logpath, const string& logfile);
00121
00122 bool testLogLevel(LogLevel level);
00123
00124 void prepareLogStream(std::ostringstream& stream);
00125
00126 std::ostringstream* logStreamOpen();
00127
00128 void logStreamClose(LogLevel level, const char* file, unsigned line,
00129 const char* func, std::ostringstream*);
00130
00131 #define y2deb(op) y2log_op(storage::DEBUG, __FILE__, __LINE__, __FUNCTION__, op)
00132 #define y2mil(op) y2log_op(storage::MILESTONE, __FILE__, __LINE__, __FUNCTION__, op)
00133 #define y2war(op) y2log_op(storage::WARNING, __FILE__, __LINE__, __FUNCTION__, op)
00134 #define y2err(op) y2log_op(storage::ERROR, __FILE__, __LINE__, __FUNCTION__, op)
00135
00136 #define y2log_op(level, file, line, func, op) \
00137 do { \
00138 if (storage::testLogLevel(level)) \
00139 { \
00140 std::ostringstream* __buf = storage::logStreamOpen(); \
00141 *__buf << op; \
00142 storage::logStreamClose(level, file, line, func, __buf); \
00143 } \
00144 } while (0)
00145
00146
00147 string hostname();
00148 string datetime();
00149
00150
00151 class StopWatch
00152 {
00153 public:
00154
00155 StopWatch();
00156
00157 friend std::ostream& operator<<(std::ostream& s, const StopWatch& sw);
00158
00159 protected:
00160
00161 struct timeval start_tv;
00162
00163 };
00164
00165
00166 struct Text
00167 {
00168 Text() : native(), text() {}
00169 Text(const string& native, const string& text) : native(native), text(text) {}
00170
00171 void clear();
00172
00173 const Text& operator+=(const Text& a);
00174
00175 string native;
00176 string text;
00177 };
00178
00179
00180 Text sformat(const Text& format, ...);
00181
00182
00183 Text _(const char* msgid);
00184 Text _(const char* msgid, const char* msgid_plural, unsigned long int n);
00185
00186
00187 extern const string app_ws;
00188
00189 }
00190
00191 #endif