SNAP Library 2.1, User Reference  2013-09-25 10:47:25
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
os.h
Go to the documentation of this file.
00001 #include "bd.h"
00002 
00003 #ifdef GLib_WIN
00004 
00005 // Com
00006 class TCom{
00007 public:
00008   TCom(){
00009     /*EAssertR(::CoInitialize(NULL)==S_OK, "COM initialization failed.");*/}
00010   ~TCom(){/*CoUninitialize();*/}
00011 };
00012 
00014 // System-Processes
00015 class TSysProc{
00016 public:
00017   static void Sleep(const uint& MSecs);
00018   static TStr GetExeFNm();
00019   static void SetLowPriority();
00020   static bool ExeProc(const TStr& ExeFNm, TStr& ParamStr);
00021 };
00022 
00024 // System-Memory-Status
00025 ClassTP(TSysMemStat, PSysMemStat)//{
00026 private:
00027   MEMORYSTATUSEX MemStat;
00028 public:
00029   TSysMemStat(){Refresh();}
00030   ~TSysMemStat(){}
00031   TSysMemStat(TSIn&){Fail;}
00032   static TPt<TSysMemStat> Load(TSIn&){Fail; return NULL;}
00033   void Save(TSOut&){Fail;}
00034 
00035   TSysMemStat& operator=(const TSysMemStat&){Fail; return *this;}
00036 
00037   void Refresh(){
00038     MemStat.dwLength=sizeof(MEMORYSTATUSEX);
00039     GlobalMemoryStatusEx(&MemStat);}
00040 
00041   uint64 GetLoad(){ // percent of memory in use
00042     return uint64(MemStat.dwMemoryLoad);}
00043   uint64 GetTotalPhys(){ // bytes of physical memory
00044     return uint64(MemStat.ullTotalPhys);}
00045   uint64 GetAvailPhys(){ // free physical memory bytes
00046     return uint64(MemStat.ullAvailPhys);}
00047   uint64 GetTotalPageFile(){ // bytes of paging file
00048     return uint64(MemStat.ullTotalPageFile);}
00049   uint64 GetAvailPageFile(){ // free bytes of paging file
00050     return uint64(MemStat.ullAvailPageFile);}
00051   uint64 GetTotalVirtual(){ // user bytes of address space
00052     return uint64(MemStat.ullTotalVirtual);}
00053   uint64 GetAvailVirtual(){ // free user bytes
00054     return uint64(MemStat.ullAvailVirtual);}
00055 
00056   TStr GetLoadStr();
00057   TStr GetUsageStr();
00058   TStr GetInfoStr();
00059   TStr GetStr();
00060 
00061   static bool Is32Bit(){return sizeof(char*)==4;}
00062   static bool Is64Bit(){return sizeof(char*)==8;}
00063 };
00064 
00066 // System-Console
00067 ClassTP(TSysConsole, PSysConsole)//{
00068 private:
00069   bool Ok;
00070   HANDLE hStdOut;
00071 public:
00072   TSysConsole();
00073   static PSysConsole New(){return PSysConsole(new TSysConsole());}
00074   ~TSysConsole();
00075   TSysConsole(TSIn&){Fail;}
00076   static PSysConsole Load(TSIn&){Fail; return NULL;}
00077   void Save(TSOut&){Fail;}
00078 
00079   TSysConsole& operator=(const TSysConsole&){Fail; return *this;}
00080 
00081   void Put(const TStr& Str);
00082   void PutLn(const TStr& Str){
00083     Put(Str); Put("\r\n");}
00084 
00085   static void OpenFile(const TStr& FNm){
00086     ShellExecute(0, "open", FNm.CStr(), "", "", SW_SHOWNORMAL);}
00087 };
00088 
00090 // System-Console-Notifier
00091 class TSysConsoleNotify: public TNotify{
00092 private:
00093   PSysConsole SysConsole;
00094 public:
00095   TSysConsoleNotify(const PSysConsole& _SysConsole):
00096     SysConsole(_SysConsole){}
00097   static PNotify New(const PSysConsole& _SysConsole){
00098     return PNotify(new TSysConsoleNotify(_SysConsole));}
00099 
00100   void OnNotify(const TNotifyType& Type, const TStr& MsgStr);
00101   void OnStatus(const TStr& MsgStr);
00102 };
00103 
00105 // System-Messages
00106 //class TSysMsg{
00107 //public:
00108 //  static void Loop();
00109 //  static void Quit();
00110 //};
00111 
00113 // System-Time
00114 class TSysTm{
00115 public:
00116   static TTm GetCurUniTm();
00117   static TTm GetCurLocTm();
00118   static uint64 GetCurUniMSecs();
00119   static uint64 GetCurLocMSecs();
00120   static uint64 GetMSecsFromTm(const TTm& Tm);
00121   static TTm GetTmFromMSecs(const uint64& MSecs);
00122   static uint GetMSecsFromOsStart();
00123 
00124   static TTm GetLocTmFromUniTm(const TTm& Tm);
00125   static TTm GetUniTmFromLocTm(const TTm& Tm);
00126 
00127   static uint64 GetProcessMSecs();
00128   static uint64 GetThreadMSecs();
00129 
00130   static uint64 GetPerfTimerFq();
00131   static uint64 GetPerfTimerTicks();
00132 };
00133 
00135 // System-Strings
00136 class TSysStr{
00137 public:
00138   static TStr GetCmLn();
00139   static TStr GetMsgStr(const DWORD& MsgCd);
00140   static TStr GetLastMsgStr(){return GetMsgStr(GetLastError());}
00141   static char* GetLastMsgCStr();
00142 
00143   static BSTR NewBStr(const TStr& Str){
00144     return SysAllocStringByteLen(Str.CStr(), Str.Len());}
00145   static TStr DelBStr(BSTR& BStr){
00146     TStr Str=(char*)BStr; SysFreeString(BStr); BStr=NULL; return Str;}
00147 };
00148 
00150 // Registry-Key
00151 ClassTP(TRegKey, PRegKey)//{
00152 private:
00153   bool Ok;
00154   HKEY hKey;
00155   void UndefCopyAssgin(TRegKey);
00156 private:
00157   TRegKey(): Ok(false), hKey(0){}
00158   TRegKey(const bool& _Ok, const HKEY& _hKey): Ok(_Ok), hKey(_hKey){}
00159   ~TRegKey(){if (Ok){RegCloseKey(hKey);}}
00160 
00161   HKEY GetHandle() const {return hKey;}
00162 public:
00163   static PRegKey GetKey(const PRegKey& Key, const TStr& SubKeyNm);
00164   static TStr GetVal(const PRegKey& Key, const TStr& SubKeyNm, const TStr& ValNm);
00165   static PRegKey GetClassesRootKey(){return new TRegKey(true, HKEY_CLASSES_ROOT);}
00166   static PRegKey GetCurrentUserKey(){return new TRegKey(true, HKEY_CURRENT_USER);}
00167   static PRegKey GetLocalMachineKey(){return new TRegKey(true, HKEY_LOCAL_MACHINE);}
00168   static PRegKey GetUsersKey(){return new TRegKey(true, HKEY_USERS);}
00169 
00170   bool IsOk() const {return Ok;}
00171   void GetKeyNmV(TStrV& KeyNmV) const;
00172   void GetValV(TStrKdV& ValNmStrKdV) const;
00173 };
00174 
00176 // Program StdIn and StdOut redirection using pipes
00177 class TStdIOPipe {
00178 private:
00179   HANDLE ChildStdinRd, ChildStdinWrDup;
00180   HANDLE ChildStdoutWr, ChildStdoutRdDup;
00181   void CreateProc(const TStr& Cmd);
00182 private:
00183   UndefDefaultCopyAssign(TStdIOPipe);
00184 public:
00185   TStdIOPipe(const TStr& CmdToExe);
00186   ~TStdIOPipe();
00187 
00188   int Write(const char* Bf) { return Write(Bf, (int) strlen(Bf)); }
00189   int Write(const char* Bf, const int& BfLen);
00190   int Read(char *Bf, const int& BfMxLen);
00191 };
00192 
00193 #elif defined(GLib_UNIX)
00194 
00196 // Compatibility functions
00197 
00198 int GetModuleFileName(void *hModule, char *Bf, int MxBfL);
00199 int GetCurrentDirectory(const int MxBfL, char *Bf);
00200 int CreateDirectory(const char *FNm, void *useless);
00201 int RemoveDirectory(const char *FNm);
00202 uint64 Epoch2Ft(time_t Epoch);
00203 time_t Ft2Epoch(uint64 Ft);
00204 
00206 // System-Processes
00207 class TSysProc{
00208 public:
00209   static int Sleep(const uint& MSecs);
00210   static TStr GetExeFNm();
00211   static void SetLowPriority();
00212   static bool ExeProc(const TStr& ExeFNm, TStr& ParamStr);
00213 };
00214 
00216 // System-Messages
00217 class TSysMsg{
00218 public:
00219   static void Loop();
00220   static void Quit();
00221 };
00222 
00224 // System-Time
00225 class TSysTm{
00226 public:
00227   static TTm GetCurUniTm();
00228   static TTm GetCurLocTm();
00229   static uint64 GetCurUniMSecs();
00230   static uint64 GetCurLocMSecs();
00231   static uint64 GetMSecsFromTm(const TTm& Tm);
00232   static TTm GetTmFromMSecs(const uint64& MSecs);
00233   static uint GetMSecsFromOsStart();
00234 
00235   static TTm GetLocTmFromUniTm(const TTm& Tm);
00236   static TTm GetUniTmFromLocTm(const TTm& Tm);
00237 
00238   static uint64 GetProcessMSecs();
00239   static uint64 GetThreadMSecs();
00240 
00241   static uint64 GetPerfTimerFq();
00242   static uint64 GetPerfTimerTicks();
00243 };
00244 
00246 // Program StdIn and StdOut redirection using pipes
00247 // J: not yet ported to Linux
00248 class TStdIOPipe {
00249 private:
00250   int ChildStdinRd, ChildStdinWrDup;
00251   int ChildStdoutWr, ChildStdoutRdDup;
00252   void CreateProc(const TStr& Cmd);
00253 private:
00254   UndefDefaultCopyAssign(TStdIOPipe);
00255 public:
00256   TStdIOPipe(const TStr& CmdToExe);
00257   ~TStdIOPipe();
00258 
00259   int Write(const char* Bf) { return Write(Bf, (int) strlen(Bf)); }
00260   int Write(const char* Bf, const int& BfLen);
00261   int Read(char *Bf, const int& BfMxLen);
00262 };
00263 
00264 #endif