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