Ginga  0.13.6
 All Classes Namespaces Functions Variables
SystemCompat.h
1 /******************************************************************************
2 Este arquivo eh parte da implementacao do ambiente declarativo do middleware
3 Ginga (Ginga-NCL).
4 
5 Direitos Autorais Reservados (c) 1989-2007 PUC-Rio/Laboratorio TeleMidia
6 
7 Este programa eh software livre; voce pode redistribui-lo e/ou modificah-lo sob
8 os termos da Licenca Publica Geral GNU versao 2 conforme publicada pela Free
9 Software Foundation.
10 
11 Este programa eh distribuido na expectativa de que seja util, porem, SEM
12 NENHUMA GARANTIA; nem mesmo a garantia implicita de COMERCIABILIDADE OU
13 ADEQUACAO A UMA FINALIDADE ESPECIFICA. Consulte a Licenca Publica Geral do
14 GNU versao 2 para mais detalhes.
15 
16 Voce deve ter recebido uma copia da Licenca Publica Geral do GNU versao 2 junto
17 com este programa; se nao, escreva para a Free Software Foundation, Inc., no
18 endereco 59 Temple Street, Suite 330, Boston, MA 02111-1307 USA.
19 
20 Para maiores informacoes:
21 ncl @ telemidia.puc-rio.br
22 http://www.ncl.org.br
23 http://www.ginga.org.br
24 http://www.telemidia.puc-rio.br
25 ******************************************************************************
26 This file is part of the declarative environment of middleware Ginga (Ginga-NCL)
27 
28 Copyright: 1989-2007 PUC-RIO/LABORATORIO TELEMIDIA, All Rights Reserved.
29 
30 This program is free software; you can redistribute it and/or modify it under
31 the terms of the GNU General Public License version 2 as published by
32 the Free Software Foundation.
33 
34 This program is distributed in the hope that it will be useful, but WITHOUT ANY
35 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
36 PARTICULAR PURPOSE. See the GNU General Public License version 2 for more
37 details.
38 
39 You should have received a copy of the GNU General Public License version 2
40 along with this program; if not, write to the Free Software
41 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
42 
43 For further information contact:
44 ncl @ telemidia.puc-rio.br
45 http://www.ncl.org.br
46 http://www.ginga.org.br
47 http://www.telemidia.puc-rio.br
48 *******************************************************************************/
49 
50 #ifndef SystemCompat_H_
51 #define SystemCompat_H_
52 
53 #include "config.h"
54 
55 #ifdef _WIN32
56 
57 #if ENABLE_MEM_LEAK_DETECTION
58 #ifndef _CRTDBG_MAP_ALLOC
59 #define _CRTDBG_MAP_ALLOC
60 #endif //_CRTDBG_MAP_ALLOC
61 
62 #define _ATL_DISABLE_NOTHROW_NEW
63 
64 #ifndef DBG_NEW
65 #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
66 #define new DBG_NEW
67 #endif //DBG_NEW
68 
69 #include <stdlib.h>
70 #include <crtdbg.h>
71 #endif //ENABLE_MEM_LEAK_DETECTION
72 #endif //_WIN32
73 
74 extern "C" {
75 #include <dirent.h>
76 #if HAVE_ZIP
77  #include <zip.h>
78 #endif
79 // For Linux, Mac OS Snow Leopard (10.6) and Win32
80 #include <dlfcn.h>
81 #include <errno.h>
82 #include <fcntl.h>
83 #include <string.h>
84 #include <assert.h>
85 #include <stdio.h>
86 
87 #ifdef _WIN32
88 #ifndef isnan
89 #define isnan(x) ((x) != (x))
90 #endif
91 
92 #ifndef snprintf
93 #define snprintf sprintf_s
94 #endif
95 
96  #include <sys/timeb.h>
97  #include <sys/types.h>
98  #include <time.h>
99  #include <direct.h>
100  #include <io.h>
101  #include <windows.h>
102  #include <winbase.h>
103  #include <winsock2.h>
104  #ifdef WINSTRUCTS
105  #include <Ws2tcpip.h>
106  #endif
107  #pragma comment(lib,"ws2_32.lib")
108 
109  #ifdef BUILD_DLL
110  #define COMP_API __declspec(dllexport)
111  #else
112  #define COMP_API __declspec(dllimport)
113  #endif //BUILD_DLL
114 
115 #else // For Linux and Mac OS Snow Leopard (10.6)
116  #if (defined __APPLE__ || defined HAVE_MACH_SL_H ) // For Mac OS Snow Leopard (10.6)
117  #include <mach/mach.h>
118  #include <mach/mach_host.h>
119  #include <mach/mach_types.h>
120  #include <mach/vm_statistics.h>
121  #include <sys/sysctl.h>
122  #include <sys/types.h>
123  #elif (HAVE_SYS_SYSINFO_H) // For Linux only
124  #include <sys/sysinfo.h>
125  #endif
126 
127  #if (defined HAVE_SYS_SOCKET_H || defined HAVE_IF_H)
128  #ifdef STDC_HEADERS
129  #include <stdlib.h>
130  #include <stddef.h>
131  #else
132  #ifdef HAVE_STDLIB_H
133  #include <stdlib.h>
134  #endif
135  #endif
136  #endif
137 
138  #include <sys/param.h>
139  #include <unistd.h>
140  #include <sys/resource.h>
141  #include <signal.h>
142  #include <sys/utsname.h>
143 
144  #include <sys/types.h>
145  #include <sys/stat.h>
146  #include <sys/ioctl.h>
147  #include <sys/time.h>
148  #include <netinet/in.h>
149  #include <arpa/inet.h>
150  #include <netdb.h>
151  #include <net/if.h>
152  #ifdef HAVE_SYS_SOCKET_H
153  #include <sys/socket.h>
154  #endif
155 
156  #ifdef HAVE_IF_H
157  #include <net/if.h>
158  #endif
159 
160  #define COMP_API
161 #endif
162 }
163 
164 #include "util/functions.h"
165 using namespace ::br::pucrio::telemidia::util;
166 
167 #include <iostream>
168 #include <string>
169 using namespace std;
170 
171 #include <stdint.h>
172 
173 #ifndef INT64_C
174 #define INT64_C(c) (c ## LL)
175 #endif //INT64_C
176 
177 #ifndef UINT64_C
178 #define UINT64_C(c) (c ## ULL)
179 #endif //UINT64_C
180 
181 #ifndef __INT64_C
182 #define __INT64_C int64_t
183 #endif //__INT64_C
184 
185 #ifndef PRIx64
186 #define PRIx64 "llx"
187 #endif
188 
189 #if defined(_WIN32) && !defined(__MINGW32__)
190 
191 /* clock_gettime() deps begin */
192 static const int CLOCK_REALTIME = 0;
193 static const int CLOCK_MONOTONIC = 1;
194 static const int CLOCK_PROCESS_CPUTIME_ID = 2;
195 static const int CLOCK_THREAD_CPUTIME_ID = 3;
196 
197 #define DELTA_EPOCH_IN_SEC INT64_C(11644473600)
198 #define DELTA_EPOCH_IN_USEC INT64_C(11644473600000000)
199 #define DELTA_EPOCH_IN_100NS INT64_C(116444736000000000)
200 #define DELTA_EPOCH_IN_NS INT64_C(11644473600000000000)
201 
202 #define POW10_2 INT64_C(100)
203 #define POW10_3 INT64_C(1000)
204 #define POW10_4 INT64_C(10000)
205 #define POW10_6 INT64_C(1000000)
206 #define POW10_7 INT64_C(10000000)
207 #define POW10_9 INT64_C(1000000000)
208 /* clock_gettime() deps end */
209 
210 struct timezone
211 {
212  int tz_minuteswest; /* minutes W of Greenwich */
213  int tz_dsttime; /* type of dst correction */
214 };
215 
216 typedef HANDLE PipeDescriptor;
217 
218 #else
219 typedef int PipeDescriptor;
220 #endif
221 
222 // If access modes (F_OK, X_OK, W_OK or R_OK) is not defined we must define
223 // then. This will usually occur on Windows.
224 #ifndef F_OK
225 #define F_OK 0
226 #endif
227 
228 #ifndef X_OK
229 #define X_OK 1
230 #endif
231 
232 #ifndef W_OK
233 #define W_OK 2
234 #endif
235 
236 #ifndef R_OK
237 #define R_OK 4
238 #endif
239 
240 namespace br {
241 namespace pucrio {
242 namespace telemidia {
243 namespace ginga {
244 namespace core {
245 namespace system {
246 namespace compat {
247  class SystemCompat {
248  private:
249  static string filesPref;
250  static string ctxFilesPref;
251  static string installPref;
252  static string userCurrentPath;
253  static string gingaCurrentPath;
254  static string pathD;
255  static string iUriD;
256  static string fUriD;
257  static string gingaPrefix;
258  static bool initialized;
259  static void* cmInstance;
260 
261  static void checkValues();
262  static void initializeGingaPrefix();
263  static void initializeGingaPath();
264  static void initializeUserCurrentPath();
265  static void initializeGingaConfigFile();
266 
267  static void sigpipeHandler(int x) throw(const char*);
268 
269  public:
270  /********
271  * URIs *
272  ********/
273  static string getGingaPrefix();
274  static string updatePath(string dir);
275  static string updatePath(string dir, string separator);
276  static bool isXmlStr(string location);
277  static bool checkUriPrefix(string uri);
278  static bool isAbsolutePath(string path);
279 
280  static string getIUriD();
281  static string getFUriD();
282 
283  static string getPath(string filename);
284  static string convertRelativePath(string relPath);
285 
286  static string getGingaBinPath();
287  static string getUserCurrentPath();
288 
289  /*
290  * defines a new base directory to ginga context files
291  * use it carefully
292  */
293  static void setGingaContextPrefix(string newBaseDir);
294 
295  /*
296  * get base directory to ginga context files
297  */
298  static string getGingaContextPrefix();
299 
300  /*
301  * updates the URL with specific system delimiter
302  * and append with ginga config files prefix
303  */
304  static string appendGingaFilesPrefix(string relUrl);
305 
306  /*
307  * updates the URL with specific system delimiter
308  * and append with ginga config install prefix
309  */
310  static string appendGingaInstallPrefix(string relUrl);
311 
312 
313  /******************************
314  * Handling Dynamic libraries *
315  ******************************/
316  static void* getComponentManagerInstance();
317  static void setComponentManagerInstance(void* cmInstance);
318 
319  static string appendLibExt(string libName);
320 
321  static void* loadComponent(
322  string libName, void** llib, string symName);
323 
324  static bool releaseComponent(void* component);
325 
326  /****************
327  * SIG Handlers *
328  ****************/
329  static void initializeSigpipeHandler();
330 
331 
332  /*****************
333  * Embedded Info *
334  *****************/
335  static string getOperatingSystem();
336  static float getClockSpeed();
337  static float getMemorySize();
338 
339 
340  /**********************
341  * Specific Functions *
342  **********************/
343  static void strError (int err, char *buf, size_t size);
344  static int changeDir (const char *path);
345  static void makeDir(const char* dirName, unsigned int mode);
346  static void uSleep(unsigned int microseconds);
347  static string getTemporaryDir();
348 
349  static void gingaProcessExit(short status);
350 
351  static const short LOG_NULL = 0;
352  static const short LOG_STDO = 1;
353  static const short LOG_FILE = 2;
354  static void setLogTo(short logType, string sufix="");
355 
356  /******************
357  * Time functions *
358  ******************/
359  static int gettimeofday(struct timeval *tv, struct timezone *tz);
360  static int getUserClock(struct timeval* usrClk);
361  static int clockGetTime(int clockType, struct timespec* tv);
362 
363 
364  /******************
365  * Math functions *
366  ******************/
367  static int rint (double x);
368 
369 
370  /******************
371  * Pipe Functions *
372  ******************/
373  static string checkPipeName(string pipeName);
374 
375  private:
376  static void checkPipeDescriptor(PipeDescriptor pd);
377 
378  public:
379  static bool createPipe(string pipeName, PipeDescriptor* pd);
380  static bool openPipe(string pipeName, PipeDescriptor* pd);
381  static void closePipe(PipeDescriptor pd);
382  static int readPipe(PipeDescriptor pd, char* buffer, int buffSize);
383  static int writePipe(PipeDescriptor pd, char* data, int dataSize);
384 
385  /*****************
386  * Zip Functions *
387  *****************/
388  static int zip_directory(const string &zipfile_path, const string &directory_path, const string &iUriD); // REFACTORING
389  static int zipwalker(void* zipfile, string initdir, string dirpath, string iUriD);
390  static int unzip_file(const char *zipname, const char *filedir);
391  static bool getZipError(struct zip* file, string* strError);
392 
393  /**********************
394  * MemCheck Functions *
395  **********************/
396  static bool initMemCheck();
397  static void memCheckPoint();
398  static bool finishMemCheck();
399  };
400 }
401 }
402 }
403 }
404 }
405 }
406 }
407 
408 #endif /*SystemCompat_H_*/