gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
OS.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 // This file contains a bunch of functions that depend on OS-dependent
7 // features and/or system calls
8 
9 // these are available on all OSes
10 #include <chrono>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <signal.h>
16 #include <time.h>
17 #include <math.h>
18 #include "GmshConfig.h"
19 #include "StringUtils.h"
20 #include "Context.h"
21 
22 #if defined(HAVE_ZIPPER)
23 #include <iostream>
24 #include <fstream>
25 #include "zipper.h"
26 #include "unzipper.h"
27 #endif
28 
29 #if defined(__APPLE__)
30 #include <sys/sysctl.h>
31 #include <mach-o/dyld.h>
32 #endif
33 
34 #if defined(__linux__) && !defined(BUILD_ANDROID)
35 #include <sys/sysinfo.h>
36 #endif
37 
38 #if !defined(WIN32) || defined(__CYGWIN__)
39 #include <unistd.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 #endif
43 
44 #if defined(WIN32)
45 #include <windows.h>
46 #include <process.h>
47 #include <psapi.h>
48 #include <io.h>
49 #include <direct.h>
50 #include <fcntl.h>
51 #include <iostream>
52 #include <fstream>
53 #include <sys/timeb.h>
54 #include <wchar.h>
55 #endif
56 
57 #include "GmshMessage.h"
58 
59 #if defined(WIN32) && !defined(__CYGWIN__)
60 
61 // Unicode utility routines borrowed from FLTK
62 
63 static unsigned int utf8decode(const char *p, const char *end, int *len)
64 {
65  static unsigned short cp1252[32] = {
66  0x20ac, 0x0081, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021,
67  0x02c6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008d, 0x017d, 0x008f,
68  0x0090, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
69  0x02dc, 0x2122, 0x0161, 0x203a, 0x0153, 0x009d, 0x017e, 0x0178};
70  unsigned char c = *(unsigned char *)p;
71  if(c < 0x80) {
72  if(len) *len = 1;
73  return c;
74  }
75  else if(c < 0xa0) {
76  if(len) *len = 1;
77  return cp1252[c - 0x80];
78  }
79  else if(c < 0xc2) {
80  goto FAIL;
81  }
82  if((end && p + 1 >= end) || (p[1] & 0xc0) != 0x80) goto FAIL;
83  if(c < 0xe0) {
84  if(len) *len = 2;
85  return ((p[0] & 0x1f) << 6) + ((p[1] & 0x3f));
86  }
87  else if(c == 0xe0) {
88  if(((unsigned char *)p)[1] < 0xa0) goto FAIL;
89  goto UTF8_3;
90  }
91  else if(c < 0xf0) {
92  UTF8_3:
93  if((end && p + 2 >= end) || (p[2] & 0xc0) != 0x80) goto FAIL;
94  if(len) *len = 3;
95  return ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6) + ((p[2] & 0x3f));
96  }
97  else if(c == 0xf0) {
98  if(((unsigned char *)p)[1] < 0x90) goto FAIL;
99  goto UTF8_4;
100  }
101  else if(c < 0xf4) {
102  UTF8_4:
103  if((end && p + 3 >= end) || (p[2] & 0xc0) != 0x80 || (p[3] & 0xc0) != 0x80)
104  goto FAIL;
105  if(len) *len = 4;
106  return ((p[0] & 0x07) << 18) + ((p[1] & 0x3f) << 12) +
107  ((p[2] & 0x3f) << 6) + ((p[3] & 0x3f));
108  }
109  else if(c == 0xf4) {
110  if(((unsigned char *)p)[1] > 0x8f) goto FAIL; // after 0x10ffff
111  goto UTF8_4;
112  }
113  else {
114  FAIL:
115  if(len) *len = 1;
116  return c;
117  }
118 }
119 
120 static unsigned int utf8toUtf16(const char *src, unsigned int srclen,
121  unsigned short *dst, unsigned int dstlen)
122 {
123  const char *p = src;
124  const char *e = src + srclen;
125  unsigned int count = 0;
126  if(dstlen)
127  for(;;) {
128  if(p >= e) {
129  dst[count] = 0;
130  return count;
131  }
132  if(!(*p & 0x80)) { // ascii
133  dst[count] = *p++;
134  }
135  else {
136  int len;
137  unsigned int ucs = utf8decode(p, e, &len);
138  p += len;
139  if(ucs < 0x10000) {
140  dst[count] = ucs;
141  }
142  else {
143  // make a surrogate pair:
144  if(count + 2 >= dstlen) {
145  dst[count] = 0;
146  count += 2;
147  break;
148  }
149  dst[count] = (((ucs - 0x10000u) >> 10) & 0x3ff) | 0xd800;
150  dst[++count] = (ucs & 0x3ff) | 0xdc00;
151  }
152  }
153  if(++count == dstlen) {
154  dst[count - 1] = 0;
155  break;
156  }
157  }
158  // we filled dst, measure the rest:
159  while(p < e) {
160  if(!(*p & 0x80))
161  p++;
162  else {
163  int len;
164  unsigned int ucs = utf8decode(p, e, &len);
165  p += len;
166  if(ucs >= 0x10000) ++count;
167  }
168  ++count;
169  }
170  return count;
171 }
172 
173 static unsigned int utf8FromUtf16(char *dst, unsigned int dstlen,
174  const wchar_t *src, unsigned int srclen)
175 {
176  unsigned int i = 0;
177  unsigned int count = 0;
178  if(dstlen) {
179  for(;;) {
180  unsigned int ucs;
181  if(i >= srclen) {
182  dst[count] = 0;
183  return count;
184  }
185  ucs = src[i++];
186  if(ucs < 0x80U) {
187  dst[count++] = ucs;
188  if(count >= dstlen) {
189  dst[count - 1] = 0;
190  break;
191  }
192  }
193  else if(ucs < 0x800U) { /* 2 bytes */
194  if(count + 2 >= dstlen) {
195  dst[count] = 0;
196  count += 2;
197  break;
198  }
199  dst[count++] = 0xc0 | (ucs >> 6);
200  dst[count++] = 0x80 | (ucs & 0x3F);
201  }
202  else if(ucs >= 0xd800 && ucs <= 0xdbff && i < srclen &&
203  src[i] >= 0xdc00 && src[i] <= 0xdfff) {
204  /* surrogate pair */
205  unsigned int ucs2 = src[i++];
206  ucs = 0x10000U + ((ucs & 0x3ff) << 10) + (ucs2 & 0x3ff);
207  /* all surrogate pairs turn into 4-byte utf8 */
208  if(count + 4 >= dstlen) {
209  dst[count] = 0;
210  count += 4;
211  break;
212  }
213  dst[count++] = 0xf0 | (ucs >> 18);
214  dst[count++] = 0x80 | ((ucs >> 12) & 0x3F);
215  dst[count++] = 0x80 | ((ucs >> 6) & 0x3F);
216  dst[count++] = 0x80 | (ucs & 0x3F);
217  }
218  else {
219  /* all others are 3 bytes: */
220  if(count + 3 >= dstlen) {
221  dst[count] = 0;
222  count += 3;
223  break;
224  }
225  dst[count++] = 0xe0 | (ucs >> 12);
226  dst[count++] = 0x80 | ((ucs >> 6) & 0x3F);
227  dst[count++] = 0x80 | (ucs & 0x3F);
228  }
229  }
230  }
231  /* we filled dst, measure the rest: */
232  while(i < srclen) {
233  unsigned int ucs = src[i++];
234  if(ucs < 0x80U) {
235  count++;
236  }
237  else if(ucs < 0x800U) { /* 2 bytes */
238  count += 2;
239  }
240  else if(ucs >= 0xd800 && ucs <= 0xdbff && i < srclen - 1 &&
241  src[i + 1] >= 0xdc00 && src[i + 1] <= 0xdfff) {
242  /* surrogate pair */
243  ++i;
244  count += 4;
245  }
246  else {
247  count += 3;
248  }
249  }
250  return count;
251 }
252 
253 static wchar_t *wbuf[3] = {nullptr, nullptr, nullptr};
254 
255 static void setwbuf(int i, const char *f)
256 {
257  // all strings in Gmsh are supposed to be UTF8-encoded, which is natively
258  // supported by Mac and Linux. Windows does not support UTF-8, but UTF-16
259  // (through wchar_t), so we need to convert.
260  if(i < 0 || i > 2) {
261  Msg::Error("Wrong buffer index in setwbuf");
262  return;
263  }
264  size_t l = strlen(f);
265  unsigned int wn = utf8toUtf16(f, (unsigned int)l, nullptr, 0) + 1;
266  wbuf[i] = (wchar_t *)realloc(wbuf[i], sizeof(wchar_t) * wn);
267  wn = utf8toUtf16(f, (unsigned)l, (unsigned short *)wbuf[i], wn);
268  wbuf[i][wn] = 0;
269 }
270 
271 #endif
272 
273 FILE *Fopen(const char *f, const char *mode)
274 {
275 #if defined(WIN32) && !defined(__CYGWIN__)
276  setwbuf(0, f);
277  setwbuf(1, mode);
278  return _wfopen(wbuf[0], wbuf[1]);
279 #else
280  return fopen(f, mode);
281 #endif
282 }
283 
284 std::string GetEnvironmentVar(const std::string &var)
285 {
286 #if defined(WIN32) && !defined(__CYGWIN__)
287  setwbuf(0, var.c_str());
288  const wchar_t *wtmp = _wgetenv(wbuf[0]);
289  if(!wtmp) return "";
290  char tmp[MAX_PATH];
291  utf8FromUtf16(tmp, MAX_PATH, wtmp, wcslen(wtmp));
292  // Don't accept top dir or anything partially expanded like
293  // Settings\%USERPROFILE%, etc.
294  if(!strcmp(tmp, "/") || strstr(tmp, "%") || strstr(tmp, "$"))
295  return "";
296  else
297  return std::string(tmp);
298 #else
299  const char *tmp = getenv(var.c_str());
300  if(!tmp) return "";
301  return std::string(tmp);
302 #endif
303 }
304 
305 void SetEnvironmentVar(const std::string &var, const std::string &val)
306 {
307 #if defined(WIN32) && !defined(__CYGWIN__)
308  setwbuf(0, (var + "=" + val).c_str());
309  _wputenv(wbuf[0]);
310 #else
311  setenv(var.c_str(), val.c_str(), 1);
312 #endif
313 }
314 
315 void SleepInSeconds(double s)
316 {
317 #if defined(WIN32) && !defined(__CYGWIN__)
318  Sleep((long)(1.e3 * s));
319 #else
320  usleep((long)(1.e6 * s));
321 #endif
322 }
323 
324 static void GetResources(double *s, long *mem)
325 {
326 #if defined(WIN32) && !defined(__CYGWIN__)
327  FILETIME creation, exit, kernel, user;
328  if(GetProcessTimes(GetCurrentProcess(), &creation, &exit, &kernel, &user)) {
329  *s = 1.e-7 * 4294967296. * (double)user.dwHighDateTime +
330  1.e-7 * (double)user.dwLowDateTime;
331  }
332  PROCESS_MEMORY_COUNTERS info;
333  GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
334  *mem = (long)info.PeakWorkingSetSize;
335 #else
336  static struct rusage r;
337  getrusage(RUSAGE_SELF, &r);
338  *s = (double)r.ru_utime.tv_sec + 1.e-6 * (double)r.ru_utime.tv_usec;
339 #if defined(__APPLE__)
340  *mem = (long)r.ru_maxrss;
341 #else
342  *mem = (long)(r.ru_maxrss * 1024L);
343 #endif
344 #endif
345 }
346 
348 {
349 #if !defined(WIN32) || defined(__CYGWIN__)
350  static struct rlimit r;
351 
352  getrlimit(RLIMIT_STACK, &r);
353 
354  // Try to get at least 16 MB of stack. Running with too small a stack
355  // can cause crashes in the recursive calls (e.g. for tet
356  // classification in 3D Delaunay)
357  if(r.rlim_cur < 16 * 1024 * 1024) {
358  Msg::Info("Increasing process stack size (%d kB < 16 MB)",
359  r.rlim_cur / 1024);
360  r.rlim_cur = r.rlim_max;
361  setrlimit(RLIMIT_STACK, &r);
362  }
363 #endif
364 }
365 
366 double Cpu()
367 {
368  long mem = 0;
369  double s = 0.;
370  GetResources(&s, &mem);
371  return s;
372 }
373 
374 double TotalRam()
375 {
376  double ram = 0;
377 #if defined(__APPLE__)
378  int name[] = {CTL_HW, HW_MEMSIZE};
379  int64_t value;
380  size_t len = sizeof(value);
381  if(sysctl(name, 2, &value, &len, nullptr, 0) != -1)
382  ram = value / (1024 * 1024);
383 #elif defined(WIN32)
384  MEMORYSTATUSEX status;
385  status.dwLength = sizeof(status);
386  GlobalMemoryStatusEx(&status);
387  ram = status.ullTotalPhys / ((double)1024 * 1024);
388 #elif defined(BUILD_ANDROID)
389  ram = 1024;
390 #elif defined(__linux__)
391  struct sysinfo infos;
392  if(sysinfo(&infos) != -1)
393  ram =
394  infos.totalram * (unsigned long)infos.mem_unit / ((double)1024 * 1024);
395 #endif
396  return ram;
397 }
398 
399 double TimeOfDay()
400 {
401  //auto t = std::chrono::system_clock::now();
402  auto t = std::chrono::steady_clock::now(); // guaranteed monotonic
403  return std::chrono::duration<double>(t.time_since_epoch()).count();
404 }
405 
407 {
408  long mem = 0;
409  double s = 0.;
410  GetResources(&s, &mem);
411  return mem;
412 }
413 
415 {
416 #if defined(WIN32) && !defined(__CYGWIN__)
417  return _getpid();
418 #else
419  return getpid();
420 #endif
421 }
422 
424 {
425  std::string name = "";
426 #if defined(WIN32) && !defined(__CYGWIN__)
427  wchar_t src[MAX_PATH];
428  unsigned long size = GetModuleFileNameW(nullptr, src, MAX_PATH);
429  if(size) {
430  char dst[MAX_PATH];
431  utf8FromUtf16(dst, MAX_PATH, src, size);
432  name = std::string(dst);
433  }
434 #elif defined(__APPLE__)
435  char path[PATH_MAX];
436  uint32_t size = sizeof(path);
437  if(_NSGetExecutablePath(path, &size) == 0) {
438  char real[PATH_MAX];
439  if(realpath(path, real)) {
440  name = std::string(real);
441  }
442  }
443 #elif defined(__linux__)
444  char path[4096];
445  int n = readlink("/proc/self/exe", path, sizeof(path));
446  if(n > 0 && n < (int)sizeof(path)) {
447  path[n] = '\0';
448  name = std::string(path);
449  }
450 #endif
451  return name;
452 }
453 
454 std::string GetAbsolutePath(const std::string &fileName)
455 {
456 #if defined(WIN32) && !defined(__CYGWIN__)
457  setwbuf(0, fileName.c_str());
458  wchar_t path[MAX_PATH];
459  unsigned long size = GetFullPathNameW(wbuf[0], MAX_PATH, path, nullptr);
460  if(size) {
461  char dst[MAX_PATH];
462  utf8FromUtf16(dst, MAX_PATH, path, size);
463  return std::string(dst);
464  }
465 #else
466  char path[4096];
467  if(realpath(fileName.c_str(), path)) return path;
468 #endif
469  return fileName;
470 }
471 
472 std::string GetHostName()
473 {
474  char host[256];
475  gethostname(host, sizeof(host));
476  return std::string(host);
477 }
478 
479 int UnlinkFile(const std::string &fileName)
480 {
481 #if defined(WIN32) && !defined(__CYGWIN__)
482  setwbuf(0, fileName.c_str());
483  return _wunlink(wbuf[0]);
484 #else
485  return unlink(fileName.c_str());
486 #endif
487 }
488 
489 int StatFile(const std::string &fileName)
490 {
491 #if defined(WIN32) && !defined(__CYGWIN__)
492  struct _stat buf;
493  setwbuf(0, fileName.c_str());
494  int ret = _wstat(wbuf[0], &buf);
495 #else
496  struct stat buf;
497  int ret = stat(fileName.c_str(), &buf);
498 #endif
499  return ret;
500 }
501 
502 int CreateSingleDir(const std::string &dirName)
503 {
504 #if defined(WIN32) && !defined(__CYGWIN__)
505  setwbuf(0, dirName.c_str());
506  if(_wmkdir(wbuf[0])) return 0;
507 #else
508  if(mkdir(dirName.c_str(), 0777)) return 0;
509 #endif
510  return 1;
511 }
512 
513 void CreatePath(const std::string &fullPath)
514 {
515  size_t lastp = fullPath.find_last_of('/'); // TODO: handle backslash for win?
516  if(lastp == std::string::npos) return;
517  std::string dirname = std::string(fullPath, 0, lastp);
518  size_t cur = 0;
519  while(cur != std::string::npos) {
520  cur = dirname.find('/', cur + 1);
521  CreateSingleDir(dirname.substr(0, cur));
522  }
523 }
524 
525 int KillProcess(int pid)
526 {
527 #if defined(WIN32) && !defined(__CYGWIN__)
528  HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
529  if(!TerminateProcess(hProc, 0)) {
530  CloseHandle(hProc);
531  return 0;
532  }
533 #else
534  if(kill(pid, 9)) return 0;
535 #endif
536  return 1;
537 }
538 
539 int SystemCallExe(const std::string &exe, const std::string &argsOrCommand,
540  bool blocking)
541 {
542  // do we try to run a .py script, .m script or an .exe?
543  std::vector<std::string> split = SplitFileName(exe);
544  bool isPython = (split[2] == ".py" || split[2] == ".PY");
545  bool isOctave = (split[2] == ".m" || split[2] == ".M");
546  bool isExe = (split[2] == ".exe" || split[2] == ".EXE");
547 
548  if(isPython || isOctave || isExe) {
549  if(StatFile(exe)) {
550  Msg::Error("Unable to open file '%s'", exe.c_str());
551  return 1;
552  }
553  }
554 
555  std::string command;
556  if(exe.size()) {
557  command.append("\"" + exe + "\""); // allows exe with white space
558  if(argsOrCommand.size()) command.append(" ");
559  }
560  command.append(argsOrCommand);
561 
562 #if defined(WIN32) && !defined(__CYGWIN__)
563  if(isPython || isOctave) {
564  Msg::Info("Shell opening '%s' with arguments '%s'", exe.c_str(),
565  argsOrCommand.c_str());
566  setwbuf(0, "open");
567  setwbuf(1, exe.c_str());
568  setwbuf(2, argsOrCommand.c_str());
569  ShellExecuteW(nullptr, wbuf[0], wbuf[1], wbuf[2], nullptr, 0);
570  }
571  else {
572  STARTUPINFOW suInfo;
573  PROCESS_INFORMATION prInfo;
574  memset(&suInfo, 0, sizeof(suInfo));
575  suInfo.cb = sizeof(suInfo);
576  Msg::Info("Calling '%s'", command.c_str());
577  setwbuf(0, command.c_str());
578  if(blocking) {
579  CreateProcessW(nullptr, wbuf[0], nullptr, nullptr, FALSE,
580  NORMAL_PRIORITY_CLASS, nullptr, nullptr, &suInfo, &prInfo);
581  // wait until child process exits.
582  WaitForSingleObject(prInfo.hProcess, INFINITE);
583  // close process and thread handles.
584  CloseHandle(prInfo.hProcess);
585  CloseHandle(prInfo.hThread);
586  }
587  else {
588  // DETACHED_PROCESS removes the console (useful if the program to launch
589  // is a console-mode exe)
590  DWORD dwCreationFlags;
591  if(CTX::instance()->detachedProcess)
592  dwCreationFlags = NORMAL_PRIORITY_CLASS | DETACHED_PROCESS;
593  else
594  dwCreationFlags = NORMAL_PRIORITY_CLASS;
595 
596  CreateProcessW(nullptr, wbuf[0], nullptr, nullptr, FALSE,
597  dwCreationFlags, nullptr, nullptr, &suInfo, &prInfo);
598  }
599  }
600 #elif(BUILD_IOS)
601  Msg::Warning("SystemCall is not supported on iOS");
602 #else
603  std::string cmd(command);
604  if(isPython || isOctave || isExe) {
605  if(access(exe.c_str(), X_OK)) {
606  if(isPython) {
607  Msg::Info("Script '%s' is not executable: running with `%s'",
608  exe.c_str(),
610  cmd = CTX::instance()->solver.pythonInterpreter + " " + cmd;
611  }
612  else if(isOctave) {
613  Msg::Info("Script '%s' is not executable: running with `%s'",
614  exe.c_str(),
616  cmd = CTX::instance()->solver.octaveInterpreter + " " + cmd;
617  }
618  else
619  Msg::Warning("File '%s' is not executable", exe.c_str());
620  }
621  else if(split[0].empty()) {
622  // workaround if pwd is not in PATH
623  cmd = "./" + cmd;
624  }
625  }
626  if(!system(nullptr)) {
627  Msg::Error("Could not find /bin/sh: aborting system call");
628  return 1;
629  }
630  if(!blocking) cmd += " &";
631  Msg::Info("Calling '%s'", cmd.c_str());
632  return system(cmd.c_str());
633 #endif
634  return 0;
635 }
636 
637 int SystemCall(const std::string &command, bool blocking)
638 {
639  return SystemCallExe("", command, blocking);
640 }
641 
643 {
644 #if defined(WIN32) && !defined(__CYGWIN__)
645  // Win32 GUI apps do not write to the DOS console; make it work again by
646  // attaching to parent console, which allows one to use the DOS shell to work
647  // with Gmsh on the command line (without this hack, you need to either use a
648  // better shell (e.g. bash), or compile a /subsystem:console version
649  if(!AttachConsole(ATTACH_PARENT_PROCESS)) return;
650  // redirect unbuffered stdout, stdin and stderr to the console
651  {
652  intptr_t lStdHandle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE);
653  if(lStdHandle != (intptr_t)INVALID_HANDLE_VALUE) {
654  int hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
655  if(hConHandle >= 0) {
656  FILE *fp = _fdopen(hConHandle, "w");
657  if(fp) {
658  *stdout = *fp;
659  setvbuf(stdout, nullptr, _IONBF, 0);
660  }
661  }
662  }
663  }
664  {
665  intptr_t lStdHandle = (intptr_t)GetStdHandle(STD_INPUT_HANDLE);
666  if(lStdHandle != (intptr_t)INVALID_HANDLE_VALUE) {
667  int hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
668  if(hConHandle >= 0) {
669  FILE *fp = _fdopen(hConHandle, "r");
670  if(fp) {
671  *stdin = *fp;
672  setvbuf(stdin, nullptr, _IONBF, 0);
673  }
674  }
675  }
676  }
677  {
678  intptr_t lStdHandle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE);
679  if(lStdHandle != (intptr_t)INVALID_HANDLE_VALUE) {
680  int hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
681  if(hConHandle >= 0) {
682  FILE *fp = _fdopen(hConHandle, "w");
683  if(fp) {
684  *stderr = *fp;
685  setvbuf(stderr, nullptr, _IONBF, 0);
686  }
687  }
688  }
689  }
690  // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog point to console
691  // as well
692  std::ios::sync_with_stdio();
693 #endif
694 }
695 
696 void UnzipFile(const std::string &fileName, const std::string &prependDir)
697 {
698 #if defined(HAVE_ZIPPER)
699  std::string dir = prependDir;
700  if(dir.size() && dir[dir.size() - 1] != '/' && dir[dir.size() - 1] != '\\')
701  dir.push_back('/');
702 
703  ziputils::unzipper zipFile;
704  zipFile.open(fileName.c_str());
705  std::vector<std::string> dirnames = zipFile.getFolders();
706  for(auto it = dirnames.begin(); it != dirnames.end(); it++) {
707  std::string folder = dir + *it;
708  Msg::Info("Creating folder `%s'", folder.c_str());
709  CreatePath(folder);
710  }
711  std::vector<std::string> filenames = zipFile.getFilenames();
712  for(auto it = filenames.begin(); it != filenames.end(); it++) {
713  zipFile.openEntry(it->c_str());
714  std::string name = dir + *it;
715  Msg::Info("Extracting file `%s'", name.c_str());
716  std::ofstream ofs;
717  ofs.open(name.c_str());
718  if(ofs.is_open()) {
719  zipFile >> ofs;
720  ofs.close();
721  }
722  else
723  Msg::Error("Could not create file `%s'", name.c_str());
724  }
725 #else
726  Msg::Error("Gmsh must be compiled with Zipper support to extract zip files");
727 #endif
728 }
RedirectIOToConsole
void RedirectIOToConsole()
Definition: OS.cpp:642
GetProcessId
int GetProcessId()
Definition: OS.cpp:414
TimeOfDay
double TimeOfDay()
Definition: OS.cpp:399
SystemCall
int SystemCall(const std::string &command, bool blocking)
Definition: OS.cpp:637
SplitFileName
std::vector< std::string > SplitFileName(const std::string &fileName)
Definition: StringUtils.cpp:93
CTX::pythonInterpreter
std::string pythonInterpreter
Definition: Context.h:330
Msg::Info
static void Info(const char *fmt,...)
Definition: GmshMessage.cpp:587
GetHostName
std::string GetHostName()
Definition: OS.cpp:472
c
static double c(int i, int j, fullMatrix< double > &CA, const std::vector< SPoint3 > &P, const std::vector< SPoint3 > &Q)
Definition: discreteFrechetDistance.cpp:15
Msg::Warning
static void Warning(const char *fmt,...)
Definition: GmshMessage.cpp:543
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
LegendrePolynomials::f
void f(int n, double u, double *val)
Definition: orthogonalBasis.cpp:77
CTX::solver
struct CTX::@1 solver
GmshMessage.h
UnlinkFile
int UnlinkFile(const std::string &fileName)
Definition: OS.cpp:479
Fopen
FILE * Fopen(const char *f, const char *mode)
Definition: OS.cpp:273
SystemCallExe
int SystemCallExe(const std::string &exe, const std::string &argsOrCommand, bool blocking)
Definition: OS.cpp:539
CTX::instance
static CTX * instance()
Definition: Context.cpp:122
CheckResources
void CheckResources()
Definition: OS.cpp:347
CreatePath
void CreatePath(const std::string &fullPath)
Definition: OS.cpp:513
CreateSingleDir
int CreateSingleDir(const std::string &dirName)
Definition: OS.cpp:502
KillProcess
int KillProcess(int pid)
Definition: OS.cpp:525
SetEnvironmentVar
void SetEnvironmentVar(const std::string &var, const std::string &val)
Definition: OS.cpp:305
Cpu
double Cpu()
Definition: OS.cpp:366
FALSE
#define FALSE
Definition: gl2gif.cpp:555
UnzipFile
void UnzipFile(const std::string &fileName, const std::string &prependDir)
Definition: OS.cpp:696
GetEnvironmentVar
std::string GetEnvironmentVar(const std::string &var)
Definition: OS.cpp:284
TotalRam
double TotalRam()
Definition: OS.cpp:374
CTX::octaveInterpreter
std::string octaveInterpreter
Definition: Context.h:330
StatFile
int StatFile(const std::string &fileName)
Definition: OS.cpp:489
StringUtils.h
GetAbsolutePath
std::string GetAbsolutePath(const std::string &fileName)
Definition: OS.cpp:454
Context.h
GetResources
static void GetResources(double *s, long *mem)
Definition: OS.cpp:324
GetMemoryUsage
long GetMemoryUsage()
Definition: OS.cpp:406
SleepInSeconds
void SleepInSeconds(double s)
Definition: OS.cpp:315
GetExecutableFileName
std::string GetExecutableFileName()
Definition: OS.cpp:423