MRPT  1.9.9
os.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "system-precomp.h" // Precompiled headers
11 
12 #include <mrpt/system/os.h>
13 #include <mrpt/core/format.h>
14 #include <mrpt/core/exceptions.h>
15 #include <mrpt/system/filesystem.h>
16 
17 #ifndef HAVE_TIMEGM
18 #endif // HAVE_TIMEGM
19 
20 #include <cstring>
21 #include <float.h>
22 #include <iostream>
23 #include <algorithm>
24 #include <cctype>
25 #include <ctime>
26 #include <cstdio>
27 
28 #ifdef _WIN32
29 #include <conio.h>
30 #include <windows.h>
31 #include <tlhelp32.h>
32 #include <sys/utime.h>
33 #include <io.h>
34 #include <direct.h>
35 #else
36 #include <pthread.h>
37 #include <termios.h>
38 #include <poll.h>
39 #include <unistd.h>
40 #include <sys/select.h>
41 #include <sys/time.h>
42 #include <time.h>
43 #include <unistd.h>
44 #include <utime.h>
45 #include <errno.h>
46 // #include <signal.h>
47 #endif
48 
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 
52 #ifdef MRPT_OS_LINUX
53 #define _access access
54 #define _rmdir rmdir
55 #define _stat stat
56 #endif
57 
58 #include <sstream>
59 
60 using namespace mrpt;
61 using namespace mrpt::system;
62 using namespace std;
63 
64 #ifndef _WIN32
65 /** By ninjalj in
66  * http://stackoverflow.com/questions/3962263/checking-if-a-key-was-pressed
67  */
68 void my_aux_sighandler(int) {}
69 int myKbhit(void)
70 {
71  struct termios oldtio, curtio;
72  // struct sigaction sa;
73 
74  /* Save stdin terminal attributes */
75  tcgetattr(0, &oldtio);
76 
77  // memset(&sa, 0, sizeof(struct sigaction));
78 
79  /* Set non-canonical no-echo for stdin */
80  tcgetattr(0, &curtio);
81  curtio.c_lflag &= ~(ICANON | ECHO);
82  tcsetattr(0, TCSANOW, &curtio);
83 
84  struct pollfd pfds[1];
85 
86  /* See if there is data available */
87  pfds[0].fd = 0;
88  pfds[0].events = POLLIN;
89  const int ret = poll(pfds, 1, 0);
90 
91  /* restore terminal attributes */
92  tcsetattr(0, TCSANOW, &oldtio);
93 
94  return (ret > 0);
95 }
96 
97 #endif
98 
99 /*---------------------------------------------------------------
100  timegm
101  ---------------------------------------------------------------*/
102 #ifdef HAVE_TIMEGM
103 time_t mrpt::system::os::timegm(struct tm* tm) { return ::timegm(tm); }
104 #else
105 // Version for MSVC>=2005, which lacks "timegm"
106 #ifdef HAVE_MKGMTIME
107 time_t mrpt::system::os::timegm(struct tm* tm) { return ::_mkgmtime(tm); }
108 #else
109 // generic version, slower but probably not used in any modern compiler!
110 time_t mrpt::system::os::timegm(struct tm* tm)
111 {
112  static std::mutex cs;
113  std::lock_guard<std::mutex> lock(cs);
114 
115  time_t ret;
116  char tz[256];
117 
118  /* save current timezone and set UTC */
119  char* org_tz = getenv("TZ");
120  if (org_tz) os::strcpy(tz, sizeof(tz), org_tz);
121 
122  putenv("TZ=UTC"); /* use Coordinated Universal Time (i.e. zero offset) */
123  tzset();
124 
125  ret = mktime(tm);
126  if (org_tz)
127  {
128  char buf[256];
129  mrpt::system::os::sprintf(buf, sizeof(buf), "TZ=%s", tz);
130  putenv(buf);
131  }
132  else
133  putenv("TZ=");
134  tzset();
135 
136  return ret;
137 }
138 
139 #endif
140 #endif // HAVE_TIMEGM
141 
142 /*---------------------------------------------------------------
143  mrpt::system::MRPT_getCompilationDate
144 ---------------------------------------------------------------*/
145 #include <mrpt/version.h>
146 #include <errno.h>
147 #include <limits>
148 #include <climits>
149 #include <ctime>
150 #include <cstdlib>
151 
153 {
154  time_t now;
155  char* endptr;
156  const char* source_date_epoch = MRPT_SOURCE_DATE_EPOCH;
157 
158  errno = 0;
159  unsigned long epoch = strtoul(source_date_epoch, &endptr, 10);
160  if (epoch == 0 ||
161  ((errno == ERANGE &&
162  (epoch == std::numeric_limits<unsigned long>::max() || epoch == 0)) ||
163  (errno != 0 && epoch == 0)))
164  {
165  // Last resort:
166  now = time(nullptr);
167  }
168  else
169  {
170  now = epoch;
171  }
172  struct tm* build_time = gmtime(&now);
173  const int year = build_time->tm_year + 1900;
174  const int month = build_time->tm_mon + 1;
175  const int day = build_time->tm_mday;
176 
177  return mrpt::format(
178  "%i-%02i-%02i %02i:%02i:%02i UTC", year, month, day,
179  build_time->tm_hour, build_time->tm_min, build_time->tm_sec);
180 }
181 
182 /*---------------------------------------------------------------
183  mrpt::system::MRPT_getVersion
184 ---------------------------------------------------------------*/
185 string mrpt::system::MRPT_getVersion() { return string(::MRPT_version_str); }
186 /*---------------------------------------------------------------
187  sprintf
188 ---------------------------------------------------------------*/
189 int os::sprintf(char* buf, size_t bufSize, const char* format, ...) noexcept
190 {
192 
193  int result;
194  va_list ap;
195  va_start(ap, format);
196 
197 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
198  // Use a secure version in Visual Studio 2005:
199  result = ::vsprintf_s(buf, bufSize, format, ap);
200 #else
201  // Use standard version:
202  result = ::vsprintf(buf, format, ap);
203 #endif
204 
205  va_end(ap);
206  return result;
207 }
208 
209 /*---------------------------------------------------------------
210  vsprintf
211 ---------------------------------------------------------------*/
213  char* buf, size_t bufSize, const char* format, va_list args) noexcept
214 {
216 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
217  // Use a secure version in Visual Studio 2005:
218  return ::vsprintf_s(buf, bufSize, format, args);
219 #else
220  // Use standard version:
221  return ::vsprintf(buf, format, args);
222 #endif
223 }
224 
225 /*---------------------------------------------------------------
226  vsnprintf
227 ---------------------------------------------------------------*/
229  char* buf, size_t bufSize, const char* format, va_list args) noexcept
230 {
231 #if defined(_MSC_VER)
232 #if (_MSC_VER >= 1400)
233  // Use a secure version in Visual Studio 2005:
234  return ::vsnprintf_s(buf, bufSize, _TRUNCATE, format, args);
235 #else
236  return ::vsprintf(buf, format, args);
237 #endif
238 #else
239  // Use standard version:
240  return ::vsnprintf(buf, bufSize, format, args);
241 #endif
242 }
243 
244 /*---------------------------------------------------------------
245  fopen
246 ---------------------------------------------------------------*/
247 FILE* os::fopen(const std::string& fileName, const char* mode) noexcept
248 {
249  return fopen(fileName.c_str(), mode);
250 }
251 
252 /*---------------------------------------------------------------
253  fopen
254 ---------------------------------------------------------------*/
255 FILE* os::fopen(const char* fileName, const char* mode) noexcept
256 {
257 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
258  // Use a secure version in Visual Studio 2005:
259  FILE* f;
260  if (0 != ::fopen_s(&f, fileName, mode))
261  return NULL;
262  else
263  return f;
264 #else
265  // Use standard version:
266  return ::fopen(fileName, mode);
267 #endif
268 }
269 
270 /*---------------------------------------------------------------
271  fclose
272 ---------------------------------------------------------------*/
273 void os::fclose(FILE* f)
274 {
275  if (!f) THROW_EXCEPTION("Trying to close a nullptr 'FILE*' descriptor");
276  ::fclose(f);
277 }
278 
279 /*---------------------------------------------------------------
280  strcat
281 ---------------------------------------------------------------*/
282 char* os::strcat(char* dest, size_t destSize, const char* source) noexcept
283 {
284  MRPT_UNUSED_PARAM(destSize);
285 
286 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
287  ::strcat_s(dest, destSize, source);
288 #else
289  ::strcat(dest, source);
290 #endif
291  return dest;
292 }
293 
294 /*---------------------------------------------------------------
295  strcpy
296 ---------------------------------------------------------------*/
297 char* os::strcpy(char* dest, size_t destSize, const char* source) noexcept
298 {
299  MRPT_UNUSED_PARAM(destSize);
300 
301 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
302  ::strcpy_s(dest, destSize, source);
303 #else
304  ::strcpy(dest, source);
305 #endif
306  return dest;
307 }
308 
309 /*---------------------------------------------------------------
310  strcmp
311 ---------------------------------------------------------------*/
312 int os::_strcmp(const char* str1, const char* str2) noexcept
313 {
314  return ::strcmp(str1, str2);
315 }
316 
317 /*---------------------------------------------------------------
318  strcmpi
319 ---------------------------------------------------------------*/
320 int os::_strcmpi(const char* str1, const char* str2) noexcept
321 {
322 #ifdef _WIN32
323 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
324  return ::_strcmpi(str1, str2);
325 #else
326  return ::strcmpi(str1, str2);
327 #endif
328 #else
329  return ::strcasecmp(str1, str2);
330 #endif
331 }
332 
333 /** An OS-independent version of strncmp.
334  * \return It will return 0 when both strings are equal, casi sensitive.
335  */
336 int os::_strncmp(const char* str1, const char* str2, size_t count) noexcept
337 {
338  return ::strncmp(str1, str2, count);
339 }
340 
341 /** An OS-independent version of strnicmp.
342  * \return It will return 0 when both strings are equal, casi insensitive.
343  */
344 int os::_strnicmp(const char* str1, const char* str2, size_t count) noexcept
345 {
346 #if defined(_MSC_VER)
347  return ::_strnicmp(str1, str2, count);
348 #else
349  return ::strncasecmp(str1, str2, count);
350 #endif
351 }
352 
353 /*---------------------------------------------------------------
354  memcpy
355 ---------------------------------------------------------------*/
357  void* dest, size_t destSize, const void* src, size_t copyCount) noexcept
358 {
359 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
360  ::memcpy_s(dest, destSize, src, copyCount);
361 #else
362  MRPT_UNUSED_PARAM(destSize);
363  ::memcpy(dest, src, copyCount);
364 #endif
365 }
366 
367 /*---------------------------------------------------------------
368  getch
369 ---------------------------------------------------------------*/
370 int os::getch() noexcept
371 {
372 #ifdef _WIN32
373  return ::getch(); // cin.get();
374 #else
375  struct termios oldt, newt;
376  int ch;
377  tcgetattr(STDIN_FILENO, &oldt);
378  newt = oldt;
379  newt.c_lflag &= ~(ICANON | ECHO);
380  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
381  ch = getchar();
382  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
383  return ch;
384 #endif
385 }
386 
387 /*---------------------------------------------------------------
388  kbhit
389 ---------------------------------------------------------------*/
390 bool os::kbhit() noexcept
391 {
392 #ifdef _WIN32
393 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
394  return ::_kbhit() != 0;
395 #else
396  return ::kbhit() != 0;
397 #endif
398 #else
399  return myKbhit();
400 #endif
401 }
402 
403 /*---------------------------------------------------------------
404  os::fprintf
405 ---------------------------------------------------------------*/
406 int os::fprintf(FILE* fil, const char* frm, ...) noexcept
407 {
408  int result;
409  va_list ap;
410  va_start(ap, frm);
411 
412 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
413  // Use a secure version in Visual Studio 2005:
414  result = ::vfprintf_s(fil, frm, ap);
415 
416 #else
417  // Use standard version:
418  result = ::vfprintf(fil, frm, ap);
419 #endif
420 
421  va_end(ap);
422  return result;
423 }
424 
425 /*---------------------------------------------------------------
426  mrpt::system::pause
427 ---------------------------------------------------------------*/
428 void mrpt::system::pause(const std::string& msg) noexcept
429 {
430  std::cout << msg << std::endl;
431  os::getch();
432 }
433 
434 /*---------------------------------------------------------------
435  clearConsole
436 ---------------------------------------------------------------*/
438 {
439 #ifdef _WIN32
440  int ret = ::system("cls");
441 #else
442  int ret = ::system("clear");
443 #endif
444  if (ret)
445  cerr << "[mrpt::system::clearConsole] Error invoking 'clear screen' "
446  << endl;
447 }
448 
449 /*---------------------------------------------------------------
450  _strtoll
451  ---------------------------------------------------------------*/
452 int64_t mrpt::system::os::_strtoll(const char* nptr, char** endptr, int base)
453 {
454 #ifdef _WIN32
455  return (int64_t)::strtol(nptr, endptr, base);
456 #else
457  return (int64_t)::strtoll(nptr, endptr, base);
458 #endif
459 }
460 
461 /*---------------------------------------------------------------
462  _strtoull
463  ---------------------------------------------------------------*/
464 uint64_t mrpt::system::os::_strtoull(const char* nptr, char** endptr, int base)
465 {
466 #ifdef _WIN32
467  return (uint64_t)::strtoul(nptr, endptr, base);
468 #else
469  return (uint64_t)::strtoull(nptr, endptr, base);
470 #endif
471 }
472 
473 /** Changes the text color in the console for the text written from now on.
474  * The parameter "color" can be:
475  * - 0 : Normal text color
476  * - 1 : Blue text color
477  * - 2 : Green text color
478  * - 4 : Red text color
479  */
481 {
482  static const int TS_NORMAL = 0;
483  static const int TS_BLUE = 1;
484  static const int TS_GREEN = 2;
485  static const int TS_RED = 4;
486 #ifdef _WIN32
487  static int normal_attributes = -1;
488  HANDLE hstdout =
489  GetStdHandle(changeStdErr ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
490  fflush(changeStdErr ? stderr : stdout);
491 
492  if (normal_attributes < 0)
493  {
494  CONSOLE_SCREEN_BUFFER_INFO info;
495  GetConsoleScreenBufferInfo(hstdout, &info);
496  normal_attributes = info.wAttributes;
497  }
498 
499  SetConsoleTextAttribute(
500  hstdout,
501  (WORD)(
502  color == TS_NORMAL ? normal_attributes
503  : ((color & TS_BLUE ? FOREGROUND_BLUE : 0) |
504  (color & TS_GREEN ? FOREGROUND_GREEN : 0) |
505  (color & TS_RED ? FOREGROUND_RED : 0) |
506  FOREGROUND_INTENSITY)));
507 #else
508  // *nix:
509  static TConsoleColor last_color = mrpt::system::CONCOL_NORMAL;
510  if (color == last_color) return;
511  last_color = color;
512 
513  static const uint8_t ansi_tab[] = {30, 34, 32, 36, 31, 35, 33, 37};
514  int code = 0;
515  fflush(changeStdErr ? stdout : stderr);
516  if (color != TS_NORMAL)
517  code = ansi_tab[color & (TS_BLUE | TS_GREEN | TS_RED)];
518  fprintf(changeStdErr ? stdout : stderr, "\x1b[%dm", code);
519 #endif
520 }
521 
522 const char* sLicenseTextF =
523  " Mobile Robot Programming Toolkit (MRPT) "
524  " \n"
525  " http://www.mrpt.org/ "
526  " \n"
527  " "
528  " \n"
529  " Copyright (c) 2005-%Y, Individual contributors, see AUTHORS file "
530  "\n"
531  " See: http://www.mrpt.org/Authors - All rights reserved. "
532  " \n"
533  " Released under BSD License. See details in http://www.mrpt.org/License "
534  " \n";
535 
537 {
538  static bool sLicenseTextReady = false;
539  static std::string sLicenseText;
540 
541  if (!sLicenseTextReady)
542  {
543  // Automatically update the last year of the copyright to the
544  // compilation date:
545  time_t rawtime;
546  struct tm* timeinfo;
547  time(&rawtime);
548  timeinfo = localtime(&rawtime);
549 
550  char buf[1024];
551  ::strftime(buf, sizeof(buf), sLicenseTextF, timeinfo);
552  sLicenseText = std::string(buf);
553  sLicenseTextReady = true;
554  }
555  return sLicenseText;
556 }
557 
558 #ifdef _WIN32
559 std::string winerror2str(const char* errorPlaceName)
560 {
561  char str[700];
562  DWORD e = GetLastError();
563  FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, e, 0, str, sizeof(str), NULL);
564  std::string s;
565  s = "[";
566  s += errorPlaceName;
567  s += "] Error: ";
568  s += str;
569  return s;
570 }
571 #endif
572 
573 /*---------------------------------------------------------------
574 launchProcess
575 ---------------------------------------------------------------*/
577 {
578 #ifdef _WIN32
579  STARTUPINFOA SI;
580  PROCESS_INFORMATION PI;
581  memset(&SI, 0, sizeof(STARTUPINFOA));
582  SI.cb = sizeof(STARTUPINFOA);
583  if (CreateProcessA(
584  NULL, (LPSTR)command.c_str(), NULL, NULL, true, 0, NULL, NULL, &SI,
585  &PI))
586  {
587  // Wait:
588  WaitForSingleObject(PI.hProcess, INFINITE);
589  return true;
590  } // End of process executed OK
591  else
592  {
593  std::cerr << winerror2str("launchProcess");
594  return false;
595  }
596 
597 #else
598 
599  return 0 == ::system(command.c_str());
600 
601 #endif
602 
603 } // end launchProcess
604 
605 #include <mrpt/mrpt_paths_config.h>
607 {
608  static bool mrpt_shared_first_call = true;
609  static std::string found_mrpt_shared_dir;
610 
611  if (mrpt_shared_first_call)
612  {
613  mrpt_shared_first_call = false;
614 
615  for (int attempt = 0;; attempt++)
616  {
618  switch (attempt)
619  {
620  case 0:
621  dir = string(MRPT_SOURCE_BASE_DIRECTORY) +
622  string("/share/mrpt/");
623  break;
624  case 1:
625  dir = string(MRPT_INSTALL_PREFIX_DIRECTORY) +
626  string("/share/mrpt/");
627  break;
628 #ifdef _WIN32
629  case 2:
630  {
631  char curExe[4096];
632  GetModuleFileNameA(nullptr, curExe, sizeof(curExe));
633 
635  std::string(curExe)) +
636  "/../share/mrpt/";
637  }
638  break;
639 #endif
640 
641  default:
642  found_mrpt_shared_dir = ".";
643  break;
644  };
645  if (!dir.empty() && mrpt::system::directoryExists(dir))
646  found_mrpt_shared_dir = dir;
647 
648  if (!found_mrpt_shared_dir.empty()) break;
649  }
650  }
651 
652  return found_mrpt_shared_dir;
653 } // end of find_mrpt_shared_dir
654 
656  const std::string& command, std::string* output /*=NULL*/,
657  const std::string& mode /*="r"*/)
658 {
659  using namespace std;
660 
661  // Create the stringstream
662  stringstream sout;
663  int exit_code = -1;
664 
665 #ifdef MRPT_OS_LINUX
666  // Run Popen
667  FILE* in;
668  char buff[512];
669 
670  // Test output
671  if (!(in = popen(command.c_str(), mode.c_str())))
672  {
673  sout << "Popen Execution failed!" << endl;
674  *output = sout.str();
675 
676  return -1;
677  }
678 
679  // Parse output
680  while (fgets(buff, sizeof(buff), in) != NULL)
681  {
682  sout << buff;
683  }
684 
685  // Close
686  exit_code = pclose(in);
687 #else
688  try
689  {
690  exit_code = -1;
691 
692  HANDLE g_hChildStd_IN_Rd = NULL;
693  HANDLE g_hChildStd_IN_Wr = NULL;
694  HANDLE g_hChildStd_OUT_Rd = NULL;
695  HANDLE g_hChildStd_OUT_Wr = NULL;
696 
697  HANDLE g_hInputFile = NULL;
698  SECURITY_ATTRIBUTES saAttr;
699  // Set the bInheritHandle flag so pipe handles are inherited.
700  saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
701  saAttr.bInheritHandle = TRUE;
702  saAttr.lpSecurityDescriptor = NULL;
703  // Create a pipe for the child process's STDOUT.
704  if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0))
705  throw winerror2str("StdoutRd CreatePipe");
706 
707  // Ensure the read handle to the pipe for STDOUT is not inherited.
708 
709  if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
710  throw winerror2str("Stdout SetHandleInformation");
711 
712  // Create a pipe for the child process's STDIN.
713 
714  if (!CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0))
715  throw winerror2str("Stdin CreatePipe");
716 
717  // Ensure the write handle to the pipe for STDIN is not inherited.
718 
719  if (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0))
720  throw winerror2str("Stdin SetHandleInformation");
721 
722  // Create the child process:
723  PROCESS_INFORMATION piProcInfo;
724  STARTUPINFOA siStartInfo;
725  BOOL bSuccess = FALSE;
726 
727  // Set up members of the PROCESS_INFORMATION structure.
728 
729  ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
730 
731  // Set up members of the STARTUPINFO structure.
732  // This structure specifies the STDIN and STDOUT handles for
733  // redirection.
734 
735  ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
736  siStartInfo.cb = sizeof(STARTUPINFO);
737  siStartInfo.hStdError = g_hChildStd_OUT_Wr;
738  siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
739  siStartInfo.hStdInput = g_hChildStd_IN_Rd;
740  siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
741 
742  // Create the child process.
743  bSuccess = CreateProcessA(
744  NULL,
745  (LPSTR)command.c_str(), // command line
746  NULL, // process security attributes
747  NULL, // primary thread security attributes
748  TRUE, // handles are inherited
749  0, // creation flags
750  NULL, // use parent's environment
751  NULL, // use parent's current directory
752  &siStartInfo, // STARTUPINFO pointer
753  &piProcInfo); // receives PROCESS_INFORMATION
754 
755  // If an error occurs, exit the application.
756  if (!bSuccess) throw winerror2str("CreateProcess");
757 
758  // Read from pipe that is the standard output for child process.
759  DWORD dwRead;
760  CHAR chBuf[4096];
761  bSuccess = FALSE;
762  DWORD exitval = 0;
763  exit_code = 0;
764  for (;;)
765  {
766  DWORD dwAvailable = 0;
767  PeekNamedPipe(
768  g_hChildStd_OUT_Rd, NULL, NULL, NULL, &dwAvailable, NULL);
769  if (dwAvailable)
770  {
771  bSuccess = ReadFile(
772  g_hChildStd_OUT_Rd, chBuf, sizeof(chBuf), &dwRead, NULL);
773  if (!bSuccess || dwRead == 0) break;
774  sout.write(chBuf, dwRead);
775  }
776  else
777  {
778  // process ended?
779  if (GetExitCodeProcess(piProcInfo.hProcess, &exitval))
780  {
781  if (exitval != STILL_ACTIVE)
782  {
783  exit_code = exitval;
784  break;
785  }
786  }
787  }
788  }
789 
790  // Close handles to the child process and its primary thread.
791  CloseHandle(piProcInfo.hProcess);
792  CloseHandle(piProcInfo.hThread);
793  }
794  catch (std::string& errStr)
795  {
796  std::cerr << errStr;
797  return 1; // !=0 means error
798  }
799 #endif
800  // set output - if valid pointer given
801  if (output)
802  {
803  *output = sout.str();
804  }
805 
806  // Return exit code
807  return exit_code;
808 } // end of executeCommand
GLuint GLuint GLsizei count
Definition: glext.h:3528
const char * sLicenseTextF
Definition: os.cpp:522
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
int getch() noexcept
An OS-independent version of getch, which waits until a key is pushed.
Definition: os.cpp:370
int _strncmp(const char *str, const char *subStr, size_t count) noexcept
An OS-independent version of strncmp.
Definition: os.cpp:336
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:273
time_t timegm(struct tm *tm)
An OS-independent version of timegm (which is not present in all compilers): converts a time structur...
Definition: os.cpp:110
int _strnicmp(const char *str, const char *subStr, size_t count) noexcept
An OS-independent version of strnicmp.
Definition: os.cpp:344
void setConsoleColor(TConsoleColor color, bool changeStdErr=false)
Changes the text color in the console for the text written from now on.
Definition: os.cpp:480
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:87
STL namespace.
std::string MRPT_getCompilationDate()
Returns the MRPT source code timestamp, according to the Reproducible-Builds specifications: https://...
Definition: os.cpp:152
int BOOL
Definition: xstypedefs.h:76
GLdouble s
Definition: glext.h:3676
GLuint src
Definition: glext.h:7278
unsigned char uint8_t
Definition: rptypes.h:41
std::string find_mrpt_shared_dir()
Finds the "[MRPT]/share/mrpt/" directory, if available in the system.
Definition: os.cpp:606
int executeCommand(const std::string &command, std::string *output=NULL, const std::string &mode="r")
Execute Generic Shell Command.
Definition: os.cpp:655
GLuint color
Definition: glext.h:8300
std::string winerror2str(const char *errorPlaceName)
Definition: os.cpp:559
__int64 int64_t
Definition: rptypes.h:49
void clearConsole()
Clears the console window.
Definition: os.cpp:437
#define vsnprintf
Definition: zutil.h:203
TConsoleColor
For use in setConsoleColor.
Definition: os.h:162
GLsizei const GLchar ** string
Definition: glext.h:4101
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:406
void pause(const std::string &msg=std::string("Press any key to continue...")) noexcept
Shows the message "Press any key to continue" (or other custom message) to the current standard outpu...
Definition: os.cpp:428
GLint mode
Definition: glext.h:5669
unsigned __int64 uint64_t
Definition: rptypes.h:50
auto dir
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
int vsnprintf(char *buf, size_t bufSize, const char *format, va_list args) noexcept
An OS-independent version of vsnprintf (Notice the bufSize param, which may be ignored in some compil...
Definition: os.cpp:228
int int vsprintf(char *buf, size_t bufSize, const char *format, va_list args) noexcept
An OS-independent version of vsprintf (Notice the bufSize param, which may be ignored in some compile...
Definition: os.cpp:212
Definition: inftrees.h:28
char * strcat(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcat.
Definition: os.cpp:282
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
#define FALSE
Definition: xmlParser.h:231
GLuint in
Definition: glext.h:7274
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:390
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
uint64_t _strtoull(const char *nptr, char **endptr, int base)
An OS-independent version of strtoull.
Definition: os.cpp:464
GLbyte GLbyte tz
Definition: glext.h:6092
GLuint GLsizei bufSize
Definition: glext.h:4064
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:255
GLenum GLsizei GLenum format
Definition: glext.h:3531
bool directoryExists(const std::string &fileName)
Test if a given directory exists (it fails if the given path refers to an existing file)...
Definition: filesystem.cpp:136
std::string MRPT_getVersion()
Returns a string describing the MRPT version.
Definition: os.cpp:185
int64_t _strtoll(const char *nptr, char **endptr, int base)
An OS-independent version of strtoll.
Definition: os.cpp:452
char * strcpy(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcpy.
Definition: os.cpp:297
bool launchProcess(const std::string &command)
Executes the given command (which may contain a program + arguments), and waits until it finishes...
Definition: os.cpp:576
#define TRUE
Definition: xmlParser.h:234
const std::string & getMRPTLicense()
Returns a const ref to a text with the same text that appears at the beginning of each MRPT file (use...
Definition: os.cpp:536
std::string extractFileDirectory(const std::string &filePath)
Extract the whole path (the directory) of a filename from a complete path plus name plus extension...
Definition: filesystem.cpp:77
int _strcmp(const char *str1, const char *str2) noexcept
An OS-independent version of strcmp.
Definition: os.cpp:312
int sprintf(char *buf, size_t bufSize, const char *format,...) noexcept MRPT_printf_format_check(3
An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compiler...
Definition: os.cpp:189
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
Definition: os.cpp:356
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.
Definition: os.cpp:320



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020