Main MRPT website > C++ reference for 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 const uint8_t ansi_tab[] = {30, 34, 32, 36, 31, 35, 33, 37};
510  int code = 0;
511  fflush(changeStdErr ? stdout : stderr);
512  if (color != TS_NORMAL)
513  code = ansi_tab[color & (TS_BLUE | TS_GREEN | TS_RED)];
514  fprintf(changeStdErr ? stdout : stderr, "\x1b[%dm", code);
515 #endif
516 }
517 
518 const char* sLicenseTextF =
519  " Mobile Robot Programming Toolkit (MRPT) "
520  " \n"
521  " http://www.mrpt.org/ "
522  " \n"
523  " "
524  " \n"
525  " Copyright (c) 2005-%Y, Individual contributors, see AUTHORS file "
526  "\n"
527  " See: http://www.mrpt.org/Authors - All rights reserved. "
528  " \n"
529  " Released under BSD License. See details in http://www.mrpt.org/License "
530  " \n";
531 
533 {
534  static bool sLicenseTextReady = false;
535  static std::string sLicenseText;
536 
537  if (!sLicenseTextReady)
538  {
539  // Automatically update the last year of the copyright to the
540  // compilation date:
541  time_t rawtime;
542  struct tm* timeinfo;
543  time(&rawtime);
544  timeinfo = localtime(&rawtime);
545 
546  char buf[1024];
547  ::strftime(buf, sizeof(buf), sLicenseTextF, timeinfo);
548  sLicenseText = std::string(buf);
549  sLicenseTextReady = true;
550  }
551  return sLicenseText;
552 }
553 
554 #ifdef _WIN32
555 std::string winerror2str(const char* errorPlaceName)
556 {
557  char str[700];
558  DWORD e = GetLastError();
559  FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, e, 0, str, sizeof(str), NULL);
560  std::string s;
561  s = "[";
562  s += errorPlaceName;
563  s += "] Error: ";
564  s += str;
565  return s;
566 }
567 #endif
568 
569 /*---------------------------------------------------------------
570 launchProcess
571 ---------------------------------------------------------------*/
573 {
574 #ifdef _WIN32
575  STARTUPINFOA SI;
576  PROCESS_INFORMATION PI;
577  memset(&SI, 0, sizeof(STARTUPINFOA));
578  SI.cb = sizeof(STARTUPINFOA);
579  if (CreateProcessA(
580  NULL, (LPSTR)command.c_str(), NULL, NULL, true, 0, NULL, NULL, &SI,
581  &PI))
582  {
583  // Wait:
584  WaitForSingleObject(PI.hProcess, INFINITE);
585  return true;
586  } // End of process executed OK
587  else
588  {
589  std::cerr << winerror2str("launchProcess");
590  return false;
591  }
592 
593 #else
594 
595  return 0 == ::system(command.c_str());
596 
597 #endif
598 
599 } // end launchProcess
600 
601 #include <mrpt/mrpt_paths_config.h>
603 {
604  static bool mrpt_shared_first_call = true;
605  static std::string found_mrpt_shared_dir;
606 
607  if (mrpt_shared_first_call)
608  {
609  mrpt_shared_first_call = false;
610 
611  for (int attempt = 0;; attempt++)
612  {
613  std::string dir;
614  switch (attempt)
615  {
616  case 0:
617  dir = string(MRPT_SOURCE_BASE_DIRECTORY) +
618  string("/share/mrpt/");
619  break;
620  case 1:
621  dir = string(MRPT_INSTALL_PREFIX_DIRECTORY) +
622  string("/share/mrpt/");
623  break;
624 #ifdef _WIN32
625  case 2:
626  {
627  char curExe[4096];
628  GetModuleFileNameA(nullptr, curExe, sizeof(curExe));
629 
631  std::string(curExe)) +
632  "/../share/mrpt/";
633  }
634  break;
635 #endif
636 
637  default:
638  found_mrpt_shared_dir = ".";
639  break;
640  };
641  if (!dir.empty() && mrpt::system::directoryExists(dir))
642  found_mrpt_shared_dir = dir;
643 
644  if (!found_mrpt_shared_dir.empty()) break;
645  }
646  }
647 
648  return found_mrpt_shared_dir;
649 } // end of find_mrpt_shared_dir
650 
652  const std::string& command, std::string* output /*=NULL*/,
653  const std::string& mode /*="r"*/)
654 {
655  using namespace std;
656 
657  // Create the stringstream
658  stringstream sout;
659  int exit_code = -1;
660 
661 #ifdef MRPT_OS_LINUX
662  // Run Popen
663  FILE* in;
664  char buff[512];
665 
666  // Test output
667  if (!(in = popen(command.c_str(), mode.c_str())))
668  {
669  sout << "Popen Execution failed!" << endl;
670  *output = sout.str();
671 
672  return -1;
673  }
674 
675  // Parse output
676  while (fgets(buff, sizeof(buff), in) != NULL)
677  {
678  sout << buff;
679  }
680 
681  // Close
682  exit_code = pclose(in);
683 #else
684  try
685  {
686  exit_code = -1;
687 
688  HANDLE g_hChildStd_IN_Rd = NULL;
689  HANDLE g_hChildStd_IN_Wr = NULL;
690  HANDLE g_hChildStd_OUT_Rd = NULL;
691  HANDLE g_hChildStd_OUT_Wr = NULL;
692 
693  HANDLE g_hInputFile = NULL;
694  SECURITY_ATTRIBUTES saAttr;
695  // Set the bInheritHandle flag so pipe handles are inherited.
696  saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
697  saAttr.bInheritHandle = TRUE;
698  saAttr.lpSecurityDescriptor = NULL;
699  // Create a pipe for the child process's STDOUT.
700  if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0))
701  throw winerror2str("StdoutRd CreatePipe");
702 
703  // Ensure the read handle to the pipe for STDOUT is not inherited.
704 
705  if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
706  throw winerror2str("Stdout SetHandleInformation");
707 
708  // Create a pipe for the child process's STDIN.
709 
710  if (!CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0))
711  throw winerror2str("Stdin CreatePipe");
712 
713  // Ensure the write handle to the pipe for STDIN is not inherited.
714 
715  if (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0))
716  throw winerror2str("Stdin SetHandleInformation");
717 
718  // Create the child process:
719  PROCESS_INFORMATION piProcInfo;
720  STARTUPINFOA siStartInfo;
721  BOOL bSuccess = FALSE;
722 
723  // Set up members of the PROCESS_INFORMATION structure.
724 
725  ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
726 
727  // Set up members of the STARTUPINFO structure.
728  // This structure specifies the STDIN and STDOUT handles for
729  // redirection.
730 
731  ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
732  siStartInfo.cb = sizeof(STARTUPINFO);
733  siStartInfo.hStdError = g_hChildStd_OUT_Wr;
734  siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
735  siStartInfo.hStdInput = g_hChildStd_IN_Rd;
736  siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
737 
738  // Create the child process.
739  bSuccess = CreateProcessA(
740  NULL,
741  (LPSTR)command.c_str(), // command line
742  NULL, // process security attributes
743  NULL, // primary thread security attributes
744  TRUE, // handles are inherited
745  0, // creation flags
746  NULL, // use parent's environment
747  NULL, // use parent's current directory
748  &siStartInfo, // STARTUPINFO pointer
749  &piProcInfo); // receives PROCESS_INFORMATION
750 
751  // If an error occurs, exit the application.
752  if (!bSuccess) throw winerror2str("CreateProcess");
753 
754  // Read from pipe that is the standard output for child process.
755  DWORD dwRead;
756  CHAR chBuf[4096];
757  bSuccess = FALSE;
758  DWORD exitval = 0;
759  exit_code = 0;
760  for (;;)
761  {
762  DWORD dwAvailable = 0;
763  PeekNamedPipe(
764  g_hChildStd_OUT_Rd, NULL, NULL, NULL, &dwAvailable, NULL);
765  if (dwAvailable)
766  {
767  bSuccess = ReadFile(
768  g_hChildStd_OUT_Rd, chBuf, sizeof(chBuf), &dwRead, NULL);
769  if (!bSuccess || dwRead == 0) break;
770  sout.write(chBuf, dwRead);
771  }
772  else
773  {
774  // process ended?
775  if (GetExitCodeProcess(piProcInfo.hProcess, &exitval))
776  {
777  if (exitval != STILL_ACTIVE)
778  {
779  exit_code = exitval;
780  break;
781  }
782  }
783  }
784  }
785 
786  // Close handles to the child process and its primary thread.
787  CloseHandle(piProcInfo.hProcess);
788  CloseHandle(piProcInfo.hThread);
789  }
790  catch (std::string& errStr)
791  {
792  std::cerr << errStr;
793  return 1; // !=0 means error
794  }
795 #endif
796  // set output - if valid pointer given
797  if (output)
798  {
799  *output = sout.str();
800  }
801 
802  // Return exit code
803  return exit_code;
804 } // end of executeCommand
mrpt::system::os::kbhit
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:390
os.h
filesystem.h
mrpt::system::os::_strtoll
int64_t _strtoll(const char *nptr, char **endptr, int base)
An OS-independent version of strtoll.
Definition: os.cpp:452
format
GLenum GLsizei GLenum format
Definition: glext.h:3531
exceptions.h
mrpt::system::os::fclose
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:273
mrpt::system::extractFileDirectory
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
s
GLdouble s
Definition: glext.h:3676
src
GLuint src
Definition: glext.h:7278
mrpt::system::os::vsprintf
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
mrpt::system::directoryExists
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
mrpt::system::os::timegm
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
format.h
system-precomp.h
mrpt::system::now
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:75
MRPT_UNUSED_PARAM
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
int64_t
__int64 int64_t
Definition: rptypes.h:49
TRUE
#define TRUE
Definition: xmlParser.h:234
mrpt::system::os::_strtoull
uint64_t _strtoull(const char *nptr, char **endptr, int base)
An OS-independent version of strtoull.
Definition: os.cpp:464
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
uint8_t
unsigned char uint8_t
Definition: rptypes.h:41
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
tz
GLbyte GLbyte tz
Definition: glext.h:6092
source
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
mrpt::system::os::sprintf
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
count
GLuint GLuint GLsizei count
Definition: glext.h:3528
mrpt::system::os::vsnprintf
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
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
uint64_t
unsigned __int64 uint64_t
Definition: rptypes.h:50
color
GLuint color
Definition: glext.h:8300
mrpt::system::clearConsole
void clearConsole()
Clears the console window.
Definition: os.cpp:437
mrpt::system::os::strcpy
char * strcpy(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcpy.
Definition: os.cpp:297
mrpt::system::MRPT_getCompilationDate
std::string MRPT_getCompilationDate()
Returns the MRPT source code timestamp, according to the Reproducible-Builds specifications: https://...
Definition: os.cpp:152
mrpt::system::os::_strcmp
int _strcmp(const char *str1, const char *str2) noexcept
An OS-independent version of strcmp.
Definition: os.cpp:312
mrpt::system::executeCommand
int executeCommand(const std::string &command, std::string *output=NULL, const std::string &mode="r")
Execute Generic Shell Command.
Definition: os.cpp:651
FALSE
#define FALSE
Definition: xmlParser.h:231
mrpt::system::launchProcess
bool launchProcess(const std::string &command)
Executes the given command (which may contain a program + arguments), and waits until it finishes.
Definition: os.cpp:572
winerror2str
std::string winerror2str(const char *errorPlaceName)
Definition: os.cpp:555
mrpt::system::os::fprintf
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:406
BOOL
int BOOL
Definition: xstypedefs.h:76
bufSize
GLuint GLsizei bufSize
Definition: glext.h:4064
vsnprintf
#define vsnprintf
Definition: zutil.h:203
mrpt::system::TConsoleColor
TConsoleColor
For use in setConsoleColor.
Definition: os.h:175
mode
GLint mode
Definition: glext.h:5669
mrpt::system::os::getch
int getch() noexcept
An OS-independent version of getch, which waits until a key is pushed.
Definition: os.cpp:370
mrpt::system::os::_strnicmp
int _strnicmp(const char *str, const char *subStr, size_t count) noexcept
An OS-independent version of strnicmp.
Definition: os.cpp:344
in
GLuint in
Definition: glext.h:7274
mrpt::system::os::fopen
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:255
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::system::find_mrpt_shared_dir
std::string find_mrpt_shared_dir()
Finds the "[MRPT]/share/mrpt/" directory, if available in the system.
Definition: os.cpp:602
code
Definition: inftrees.h:28
sLicenseTextF
const char * sLicenseTextF
Definition: os.cpp:518
mrpt::system::os::_strcmpi
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.
Definition: os.cpp:320
mrpt::system::os::strcat
char * strcat(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcat.
Definition: os.cpp:282
mrpt::system::setConsoleColor
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::MRPT_getVersion
std::string MRPT_getVersion()
Returns a string describing the MRPT version.
Definition: os.cpp:185
mrpt::system::os::_strncmp
int _strncmp(const char *str, const char *subStr, size_t count) noexcept
An OS-independent version of strncmp.
Definition: os.cpp:336
mrpt::system::pause
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
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
mrpt::system::getMRPTLicense
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:532
mrpt::system::os::memcpy
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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST