SNAP Library 6.0, User Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
os.h
Go to the documentation of this file.
1 #include "bd.h"
2 
3 #ifdef GLib_WIN
4 // Com
6 class TCom{
7 public:
8  TCom(){
9  /*EAssertR(::CoInitialize(NULL)==S_OK, "COM initialization failed.");*/}
10  ~TCom(){/*CoUninitialize();*/}
11 };
12 
14 // System-Processes
15 class TSysProc{
16 public:
17  static void Sleep(const uint& MSecs);
18  static TStr GetExeFNm();
19  static void SetLowPriority();
20  static bool ExeProc(const TStr& ExeFNm, TStr& ParamStr);
21 };
22 
24 // System-Memory-Status
25 ClassTP(TSysMemStat, PSysMemStat)//{
26 private:
27  MEMORYSTATUSEX MemStat;
28 public:
29  TSysMemStat(){Refresh();}
30  ~TSysMemStat(){}
31  TSysMemStat(TSIn&){Fail;}
32  static TPt<TSysMemStat> Load(TSIn&){Fail; return NULL;}
33  void Save(TSOut&){Fail;}
34 
35  TSysMemStat& operator=(const TSysMemStat&){Fail; return *this;}
36 
37  void Refresh(){
38  MemStat.dwLength=sizeof(MEMORYSTATUSEX);
39  GlobalMemoryStatusEx(&MemStat);}
40 
41  uint64 GetLoad(){ // percent of memory in use
42  return uint64(MemStat.dwMemoryLoad);}
43  uint64 GetTotalPhys(){ // bytes of physical memory
44  return uint64(MemStat.ullTotalPhys);}
45  uint64 GetAvailPhys(){ // free physical memory bytes
46  return uint64(MemStat.ullAvailPhys);}
47  uint64 GetTotalPageFile(){ // bytes of paging file
48  return uint64(MemStat.ullTotalPageFile);}
49  uint64 GetAvailPageFile(){ // free bytes of paging file
50  return uint64(MemStat.ullAvailPageFile);}
51  uint64 GetTotalVirtual(){ // user bytes of address space
52  return uint64(MemStat.ullTotalVirtual);}
53  uint64 GetAvailVirtual(){ // free user bytes
54  return uint64(MemStat.ullAvailVirtual);}
55 
56  TStr GetLoadStr();
57  TStr GetUsageStr();
58  TStr GetInfoStr();
59  TStr GetStr();
60 
61  static bool Is32Bit(){return sizeof(char*)==4;}
62  static bool Is64Bit(){return sizeof(char*)==8;}
63 };
64 
66 // System-Console
67 ClassTP(TSysConsole, PSysConsole)//{
68 private:
69  bool Ok;
70  HANDLE hStdOut;
71 public:
72  TSysConsole();
73  static PSysConsole New(){return PSysConsole(new TSysConsole());}
74  ~TSysConsole();
75  TSysConsole(TSIn&){Fail;}
76  static PSysConsole Load(TSIn&){Fail; return NULL;}
77  void Save(TSOut&){Fail;}
78 
79  TSysConsole& operator=(const TSysConsole&){Fail; return *this;}
80 
81  void Put(const TStr& Str);
82  void PutLn(const TStr& Str){
83  Put(Str); Put("\r\n");}
84 
85  static void OpenFile(const TStr& FNm){
86  ShellExecute(0, "open", FNm.CStr(), "", "", SW_SHOWNORMAL);}
87 };
88 
90 // System-Console-Notifier
91 class TSysConsoleNotify: public TNotify{
92 private:
93  PSysConsole SysConsole;
94 public:
95  TSysConsoleNotify(const PSysConsole& _SysConsole):
96  SysConsole(_SysConsole){}
97  static PNotify New(const PSysConsole& _SysConsole){
98  return PNotify(new TSysConsoleNotify(_SysConsole));}
99 
100  void OnNotify(const TNotifyType& Type, const TStr& MsgStr);
101  void OnStatus(const TStr& MsgStr);
102 };
103 
105 // System-Messages
106 //class TSysMsg{
107 //public:
108 // static void Loop();
109 // static void Quit();
110 //};
111 
113 // System-Time
114 class TSysTm{
115 public:
116  static TTm GetCurUniTm();
117  static TTm GetCurLocTm();
118  static uint64 GetCurUniMSecs();
119  static uint64 GetCurLocMSecs();
120  static uint64 GetMSecsFromTm(const TTm& Tm);
121  static TTm GetTmFromMSecs(const uint64& MSecs);
122  static uint GetMSecsFromOsStart();
123 
124  static TTm GetLocTmFromUniTm(const TTm& Tm);
125  static TTm GetUniTmFromLocTm(const TTm& Tm);
126 
127  static uint64 GetProcessMSecs();
128  static uint64 GetThreadMSecs();
129 
130  static uint64 GetPerfTimerFq();
131  static uint64 GetPerfTimerTicks();
132 };
133 
135 // System-Strings
136 class TSysStr{
137 public:
138  static TStr GetCmLn();
139  static TStr GetMsgStr(const DWORD& MsgCd);
140  static TStr GetLastMsgStr(){return GetMsgStr(GetLastError());}
141  static char* GetLastMsgCStr();
142 
143  static BSTR NewBStr(const TStr& Str){
144  return SysAllocStringByteLen(Str.CStr(), Str.Len());}
145  static TStr DelBStr(BSTR& BStr){
146  TStr Str=(char*)BStr; SysFreeString(BStr); BStr=NULL; return Str;}
147 };
148 
150 // Registry-Key
151 ClassTP(TRegKey, PRegKey)//{
152 private:
153  bool Ok;
154  HKEY hKey;
155  void UndefCopyAssgin(TRegKey);
156 private:
157  TRegKey(): Ok(false), hKey(0){}
158  TRegKey(const bool& _Ok, const HKEY& _hKey): Ok(_Ok), hKey(_hKey){}
159  ~TRegKey(){if (Ok){RegCloseKey(hKey);}}
160 
161  HKEY GetHandle() const {return hKey;}
162 public:
163  static PRegKey GetKey(const PRegKey& Key, const TStr& SubKeyNm);
164  static TStr GetVal(const PRegKey& Key, const TStr& SubKeyNm, const TStr& ValNm);
165  static PRegKey GetClassesRootKey(){return new TRegKey(true, HKEY_CLASSES_ROOT);}
166  static PRegKey GetCurrentUserKey(){return new TRegKey(true, HKEY_CURRENT_USER);}
167  static PRegKey GetLocalMachineKey(){return new TRegKey(true, HKEY_LOCAL_MACHINE);}
168  static PRegKey GetUsersKey(){return new TRegKey(true, HKEY_USERS);}
169 
170  bool IsOk() const {return Ok;}
171  void GetKeyNmV(TStrV& KeyNmV) const;
172  void GetValV(TStrKdV& ValNmStrKdV) const;
173 };
174 
176 // Program StdIn and StdOut redirection using pipes
177 class TStdIOPipe {
178 private:
179  HANDLE ChildStdinRd, ChildStdinWrDup;
180  HANDLE ChildStdoutWr, ChildStdoutRdDup;
181  void CreateProc(const TStr& Cmd);
182 private:
183  UndefDefaultCopyAssign(TStdIOPipe);
184 public:
185  TStdIOPipe(const TStr& CmdToExe);
186  ~TStdIOPipe();
187 
188  int Write(const char* Bf) { return Write(Bf, (int) strlen(Bf)); }
189  int Write(const char* Bf, const int& BfLen);
190  int Read(char *Bf, const int& BfMxLen);
191 };
192 
193 #elif defined(GLib_UNIX)
194 
196 // Compatibility functions
197 
198 int GetModuleFileName(void *hModule, char *Bf, int MxBfL);
199 int GetCurrentDirectory(const int MxBfL, char *Bf);
200 int CreateDirectory(const char *FNm, void *useless);
201 int RemoveDirectory(const char *FNm);
202 uint64 Epoch2Ft(time_t Epoch);
203 time_t Ft2Epoch(uint64 Ft);
204 
206 // System-Processes
207 class TSysProc{
208 public:
209  static int Sleep(const uint& MSecs);
210  static TStr GetExeFNm();
211  static void SetLowPriority();
212  static bool ExeProc(const TStr& ExeFNm, TStr& ParamStr);
213 };
214 
216 // System-Messages
217 class TSysMsg{
218 public:
219  static void Loop();
220  static void Quit();
221 };
222 
224 // System-Time
225 class TSysTm{
226 public:
227  static TTm GetCurUniTm();
228  static TTm GetCurLocTm();
229  static uint64 GetCurUniMSecs();
230  static uint64 GetCurLocMSecs();
231  static uint64 GetMSecsFromTm(const TTm& Tm);
232  static TTm GetTmFromMSecs(const uint64& MSecs);
233  static uint GetMSecsFromOsStart();
234 
235  static TTm GetLocTmFromUniTm(const TTm& Tm);
236  static TTm GetUniTmFromLocTm(const TTm& Tm);
237 
238  static uint64 GetProcessMSecs();
239  static uint64 GetThreadMSecs();
240 
241  static uint64 GetPerfTimerFq();
242  static uint64 GetPerfTimerTicks();
243 };
244 
246 // Program StdIn and StdOut redirection using pipes
247 // J: not yet ported to Linux
248 class TStdIOPipe {
249 private:
250  int ChildStdinRd, ChildStdinWrDup;
251  int ChildStdoutWr, ChildStdoutRdDup;
252  void CreateProc(const TStr& Cmd);
253 private:
254  UndefDefaultCopyAssign(TStdIOPipe);
255 public:
256  TStdIOPipe(const TStr& CmdToExe);
257  ~TStdIOPipe();
258 
259  int Write(const char* Bf) { return Write(Bf, (int) strlen(Bf)); }
260  int Write(const char* Bf, const int& BfLen);
261  int Read(char *Bf, const int& BfMxLen);
262 };
263 
264 #endif
int Len() const
Definition: dt.h:490
#define UndefDefaultCopyAssign(TNm)
Definition: bd.h:203
unsigned int uint
Definition: bd.h:11
#define Fail
Definition: bd.h:238
TPt< TNotify > PNotify
Definition: ut.h:30
Definition: fl.h:58
#define ClassTP(TNm, PNm)
Definition: bd.h:126
unsigned long long uint64
Definition: bd.h:38
Definition: ut.h:30
virtual void OnNotify(const TNotifyType &, const TStr &)
Definition: ut.h:38
Definition: fl.h:128
Definition: tm.h:213
Definition: dt.h:412
enum TNotifyType_ TNotifyType
virtual void OnStatus(const TStr &)
Definition: ut.h:39
Definition: bd.h:196
char * CStr()
Definition: dt.h:479