34 #include <sys/utime.h> 39 #include <sys/select.h> 50 #include <sys/types.h> 53 #define _access access 68 void my_aux_sighandler(
int) {}
77 tcgetattr(0, &oldtio);
82 tcgetattr(0, &curtio);
83 curtio.c_lflag &= ~(ICANON | ECHO);
84 tcsetattr(0, TCSANOW, &curtio);
86 struct pollfd pfds[1];
90 pfds[0].events = POLLIN;
91 const int ret = poll(pfds, 1, 0);
94 tcsetattr(0, TCSANOW, &oldtio);
114 static std::mutex cs;
115 std::lock_guard<std::mutex> lock(cs);
121 char* org_tz = getenv(
"TZ");
122 if (org_tz)
os::strcpy(tz,
sizeof(tz), org_tz);
142 #endif // HAVE_TIMEGM 147 #include <mrpt/version.h> 158 const char* source_date_epoch = MRPT_SOURCE_DATE_EPOCH;
161 unsigned long epoch = strtoul(source_date_epoch, &endptr, 10);
164 (epoch == std::numeric_limits<unsigned long>::max() || epoch == 0)) ||
165 (errno != 0 && epoch == 0)))
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;
180 "%i-%02i-%02i %02i:%02i:%02i UTC", year, month, day,
181 build_time->tm_hour, build_time->tm_min, build_time->tm_sec);
199 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 201 result = ::vsprintf_s(buf, bufSize,
format, ap);
215 char* buf,
size_t bufSize,
const char*
format, va_list args) noexcept
218 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 220 return ::vsprintf_s(buf, bufSize,
format, args);
231 char* buf,
size_t bufSize,
const char*
format, va_list args) noexcept
233 #if defined(_MSC_VER) 234 #if (_MSC_VER >= 1400) 236 return ::vsnprintf_s(buf, bufSize, _TRUNCATE,
format, args);
249 FILE*
os::fopen(
const std::string& fileName,
const char* mode) noexcept
251 return fopen(fileName.c_str(), mode);
257 FILE*
os::fopen(
const char* fileName,
const char* mode) noexcept
259 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 262 if (0 != ::fopen_s(&f, fileName, mode))
277 if (!f)
THROW_EXCEPTION(
"Trying to close a nullptr 'FILE*' descriptor");
284 char*
os::strcat(
char* dest,
size_t destSize,
const char* source) noexcept
288 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 289 ::strcat_s(dest, destSize, source);
299 char*
os::strcpy(
char* dest,
size_t destSize,
const char* source) noexcept
303 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 304 ::strcpy_s(dest, destSize, source);
316 return ::strcmp(str1, str2);
325 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 328 return ::strcmpi(str1, str2);
331 return ::strcasecmp(str1, str2);
338 int os::_strncmp(
const char* str1,
const char* str2,
size_t count) noexcept
340 return ::strncmp(str1, str2, count);
346 int os::_strnicmp(
const char* str1,
const char* str2,
size_t count) noexcept
348 #if defined(_MSC_VER) 351 return ::strncasecmp(str1, str2, count);
359 void* dest,
size_t destSize,
const void* src,
size_t copyCount) noexcept
361 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 362 ::memcpy_s(dest, destSize, src, copyCount);
381 tcgetattr(STDIN_FILENO, &oldt);
383 newt.c_lflag &= ~(ICANON | ECHO);
384 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
386 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
397 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 398 return ::_kbhit() != 0;
416 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 418 result = ::vfprintf_s(fil, frm, ap);
422 result = ::vfprintf(fil, frm, ap);
434 std::cout << msg << std::endl;
444 int ret = ::system(
"cls");
446 int ret = ::system(
"clear");
449 cerr <<
"[mrpt::system::clearConsole] Error invoking 'clear screen' " 459 return (int64_t)::strtol(nptr, endptr, base);
461 return (int64_t)::strtoll(nptr, endptr, base);
471 return (uint64_t)::strtoul(nptr, endptr, base);
473 return (uint64_t)::strtoull(nptr, endptr, base);
486 static const int TS_NORMAL = 0;
487 static const int TS_BLUE = 1;
488 static const int TS_GREEN = 2;
489 static const int TS_RED = 4;
491 static int normal_attributes = -1;
493 GetStdHandle(changeStdErr ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
494 fflush(changeStdErr ? stderr : stdout);
496 if (normal_attributes < 0)
498 CONSOLE_SCREEN_BUFFER_INFO info;
499 GetConsoleScreenBufferInfo(hstdout, &info);
500 normal_attributes = info.wAttributes;
503 SetConsoleTextAttribute(
506 color == TS_NORMAL ? normal_attributes
507 : ((color & TS_BLUE ? FOREGROUND_BLUE : 0) |
508 (color & TS_GREEN ? FOREGROUND_GREEN : 0) |
509 (color & TS_RED ? FOREGROUND_RED : 0) |
510 FOREGROUND_INTENSITY)));
514 if (color == last_color)
return;
517 static const uint8_t ansi_tab[] = {30, 34, 32, 36, 31, 35, 33, 37};
519 fflush(changeStdErr ? stdout : stderr);
520 if (color != TS_NORMAL)
521 code = ansi_tab[color & (TS_BLUE | TS_GREEN | TS_RED)];
522 fprintf(changeStdErr ? stdout : stderr,
"\x1b[%dm", code);
527 " Mobile Robot Programming Toolkit (MRPT) " 529 " https://www.mrpt.org/ " 534 " Copyright (c) 2005-%Y, Individual contributors, see AUTHORS file " 536 " See: https://www.mrpt.org/Authors - All rights reserved. " 539 " Released under BSD License. See details in https://www.mrpt.org/License " 545 static bool sLicenseTextReady =
false;
546 static std::string sLicenseText;
548 if (!sLicenseTextReady)
555 timeinfo = localtime(&rawtime);
559 sLicenseText = std::string(buf);
560 sLicenseTextReady =
true;
569 DWORD e = GetLastError();
570 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, e, 0, str,
sizeof(str), NULL);
587 PROCESS_INFORMATION PI;
588 memset(&SI, 0,
sizeof(STARTUPINFOA));
589 SI.cb =
sizeof(STARTUPINFOA);
591 NULL, (LPSTR)command.c_str(), NULL, NULL,
true, 0, NULL, NULL, &SI,
595 WaitForSingleObject(PI.hProcess, INFINITE);
606 return 0 == ::system(command.c_str());
612 #include <mrpt/mrpt_paths_config.h> 615 static bool mrpt_shared_first_call =
true;
616 static std::string found_mrpt_shared_dir;
618 if (mrpt_shared_first_call)
620 mrpt_shared_first_call =
false;
622 for (
int attempt = 0;; attempt++)
628 dir = string(MRPT_SOURCE_BASE_DIRECTORY) +
629 string(
"/share/mrpt/");
632 dir = string(MRPT_INSTALL_PREFIX_DIRECTORY) +
633 string(
"/share/mrpt/");
639 GetModuleFileNameA(
nullptr, curExe,
sizeof(curExe));
642 std::string(curExe)) +
649 found_mrpt_shared_dir =
".";
653 found_mrpt_shared_dir =
dir;
655 if (!found_mrpt_shared_dir.empty())
break;
659 return found_mrpt_shared_dir;
663 const std::string& command, std::string* output ,
664 const std::string& mode )
678 if (!(in = popen(command.c_str(), mode.c_str())))
680 sout <<
"Popen Execution failed!" << endl;
681 *output = sout.str();
687 while (fgets(buff,
sizeof(buff), in) !=
nullptr)
693 exit_code = pclose(in);
699 HANDLE g_hChildStd_IN_Rd = NULL;
700 HANDLE g_hChildStd_IN_Wr = NULL;
701 HANDLE g_hChildStd_OUT_Rd = NULL;
702 HANDLE g_hChildStd_OUT_Wr = NULL;
704 HANDLE g_hInputFile = NULL;
705 SECURITY_ATTRIBUTES saAttr;
707 saAttr.nLength =
sizeof(SECURITY_ATTRIBUTES);
708 saAttr.bInheritHandle = TRUE;
709 saAttr.lpSecurityDescriptor = NULL;
711 if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0))
716 if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
721 if (!CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0))
726 if (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0))
730 PROCESS_INFORMATION piProcInfo;
731 STARTUPINFOA siStartInfo;
732 BOOL bSuccess = FALSE;
736 ZeroMemory(&piProcInfo,
sizeof(PROCESS_INFORMATION));
742 ZeroMemory(&siStartInfo,
sizeof(STARTUPINFO));
743 siStartInfo.cb =
sizeof(STARTUPINFO);
744 siStartInfo.hStdError = g_hChildStd_OUT_Wr;
745 siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
746 siStartInfo.hStdInput = g_hChildStd_IN_Rd;
747 siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
750 bSuccess = CreateProcessA(
752 (LPSTR)command.c_str(),
773 DWORD dwAvailable = 0;
775 g_hChildStd_OUT_Rd, NULL, NULL, NULL, &dwAvailable, NULL);
779 g_hChildStd_OUT_Rd, chBuf,
sizeof(chBuf), &dwRead, NULL);
780 if (!bSuccess || dwRead == 0)
break;
781 sout.write(chBuf, dwRead);
786 if (GetExitCodeProcess(piProcInfo.hProcess, &exitval))
788 if (exitval != STILL_ACTIVE)
798 CloseHandle(piProcInfo.hProcess);
799 CloseHandle(piProcInfo.hThread);
801 catch (std::string& errStr)
810 *output = sout.str();
const char * sLicenseTextF
#define THROW_EXCEPTION(msg)
int getch() noexcept
An OS-independent version of getch, which waits until a key is pushed.
std::string std::string format(std::string_view fmt, ARGS &&... args)
int _strncmp(const char *str, const char *subStr, size_t count) noexcept
An OS-independent version of strncmp.
int void fclose(FILE *f)
An OS-independent version of fclose.
time_t timegm(struct tm *tm)
An OS-independent version of timegm (which is not present in all compilers): converts a time structur...
int _strnicmp(const char *str, const char *subStr, size_t count) noexcept
An OS-independent version of strnicmp.
void setConsoleColor(TConsoleColor color, bool changeStdErr=false)
Changes the text color in the console for the text written from now on.
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
std::string MRPT_getCompilationDate()
Returns the MRPT source code timestamp, according to the Reproducible-Builds specifications: https://...
std::string find_mrpt_shared_dir()
Finds the "[MRPT]/share/mrpt/" directory, if available in the system.
std::string winerror2str(const char *errorPlaceName)
void clearConsole()
Clears the console window.
TConsoleColor
For use in setConsoleColor.
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
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...
int executeCommand(const std::string &command, std::string *output=nullptr, const std::string &mode="r")
Execute Generic Shell Command.
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...
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...
char * strcat(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcat.
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
uint64_t _strtoull(const char *nptr, char **endptr, int base)
An OS-independent version of strtoull.
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
bool directoryExists(const std::string &fileName)
Test if a given directory exists (it fails if the given path refers to an existing file)...
std::string MRPT_getVersion()
Returns a string describing the MRPT version.
int64_t _strtoll(const char *nptr, char **endptr, int base)
An OS-independent version of strtoll.
char * strcpy(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcpy.
bool launchProcess(const std::string &command)
Executes the given command (which may contain a program + arguments), and waits until it finishes...
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...
std::string extractFileDirectory(const std::string &filePath)
Extract the whole path (the directory) of a filename from a complete path plus name plus extension...
int _strcmp(const char *str1, const char *str2) noexcept
An OS-independent version of strcmp.
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".
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.