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);
192 char* buf, [[maybe_unused]]
size_t bufSize,
const char*
format,
199 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 201 result = ::vsprintf_s(buf, bufSize,
format, ap);
215 char* buf, [[maybe_unused]]
size_t bufSize,
const char*
format,
216 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");
285 char* dest, [[maybe_unused]]
size_t destSize,
const char* source) noexcept
287 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 288 ::strcat_s(dest, destSize, source);
299 char* dest, [[maybe_unused]]
size_t destSize,
const char* source) noexcept
301 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 302 ::strcpy_s(dest, destSize, source);
314 return ::strcmp(str1, str2);
323 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 326 return ::strcmpi(str1, str2);
329 return ::strcasecmp(str1, str2);
336 int os::_strncmp(
const char* str1,
const char* str2,
size_t count) noexcept
338 return ::strncmp(str1, str2, count);
344 int os::_strnicmp(
const char* str1,
const char* str2,
size_t count) noexcept
346 #if defined(_MSC_VER) 349 return ::strncasecmp(str1, str2, count);
357 void* dest, [[maybe_unused]]
size_t destSize,
const void* src,
358 size_t copyCount) noexcept
360 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 361 ::memcpy_s(dest, destSize, src, copyCount);
379 tcgetattr(STDIN_FILENO, &oldt);
381 newt.c_lflag &= ~(ICANON | ECHO);
382 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
384 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
395 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 396 return ::_kbhit() != 0;
414 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 416 result = ::vfprintf_s(fil, frm, ap);
420 result = ::vfprintf(fil, frm, ap);
432 std::cout << msg << std::endl;
442 int ret = ::system(
"cls");
444 int ret = ::system(
"clear");
447 cerr <<
"[mrpt::system::clearConsole] Error invoking 'clear screen' " 457 return (int64_t)::strtol(nptr, endptr, base);
459 return (int64_t)::strtoll(nptr, endptr, base);
469 return (uint64_t)::strtoul(nptr, endptr, base);
471 return (uint64_t)::strtoull(nptr, endptr, base);
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;
489 static int normal_attributes = -1;
491 GetStdHandle(changeStdErr ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
492 fflush(changeStdErr ? stderr : stdout);
494 if (normal_attributes < 0)
496 CONSOLE_SCREEN_BUFFER_INFO info;
497 GetConsoleScreenBufferInfo(hstdout, &info);
498 normal_attributes = info.wAttributes;
501 SetConsoleTextAttribute(
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)));
512 if (color == last_color)
return;
515 static const uint8_t ansi_tab[] = {30, 34, 32, 36, 31, 35, 33, 37};
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);
525 " Mobile Robot Programming Toolkit (MRPT) " 527 " https://www.mrpt.org/ " 532 " Copyright (c) 2005-%Y, Individual contributors, see AUTHORS file " 534 " See: https://www.mrpt.org/Authors - All rights reserved. " 537 " Released under BSD License. See details in https://www.mrpt.org/License " 543 static bool sLicenseTextReady =
false;
544 static std::string sLicenseText;
546 if (!sLicenseTextReady)
553 timeinfo = localtime(&rawtime);
557 sLicenseText = std::string(buf);
558 sLicenseTextReady =
true;
567 DWORD e = GetLastError();
568 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, e, 0, str,
sizeof(str), NULL);
585 PROCESS_INFORMATION PI;
586 memset(&SI, 0,
sizeof(STARTUPINFOA));
587 SI.cb =
sizeof(STARTUPINFOA);
589 NULL, (LPSTR)command.c_str(), NULL, NULL,
true, 0, NULL, NULL, &SI,
593 WaitForSingleObject(PI.hProcess, INFINITE);
604 return 0 == ::system(command.c_str());
610 #include <mrpt/mrpt_paths_config.h> 613 static bool mrpt_shared_first_call =
true;
614 static std::string found_mrpt_shared_dir;
616 if (mrpt_shared_first_call)
618 mrpt_shared_first_call =
false;
620 for (
int attempt = 0;; attempt++)
626 dir = string(MRPT_SOURCE_BASE_DIRECTORY) +
627 string(
"/share/mrpt/");
630 dir = string(MRPT_INSTALL_PREFIX_DIRECTORY) +
631 string(
"/share/mrpt/");
637 GetModuleFileNameA(
nullptr, curExe,
sizeof(curExe));
640 std::string(curExe)) +
647 found_mrpt_shared_dir =
".";
651 found_mrpt_shared_dir =
dir;
653 if (!found_mrpt_shared_dir.empty())
break;
657 return found_mrpt_shared_dir;
661 const std::string& command, std::string* output ,
662 const std::string& mode )
676 if (!(in = popen(command.c_str(), mode.c_str())))
678 sout <<
"Popen Execution failed!" << endl;
679 *output = sout.str();
685 while (fgets(buff,
sizeof(buff), in) !=
nullptr)
691 exit_code = pclose(in);
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;
702 HANDLE g_hInputFile = NULL;
703 SECURITY_ATTRIBUTES saAttr;
705 saAttr.nLength =
sizeof(SECURITY_ATTRIBUTES);
706 saAttr.bInheritHandle = TRUE;
707 saAttr.lpSecurityDescriptor = NULL;
709 if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0))
714 if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
719 if (!CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0))
724 if (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0))
728 PROCESS_INFORMATION piProcInfo;
729 STARTUPINFOA siStartInfo;
730 BOOL bSuccess = FALSE;
734 ZeroMemory(&piProcInfo,
sizeof(PROCESS_INFORMATION));
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;
748 bSuccess = CreateProcessA(
750 (LPSTR)command.c_str(),
771 DWORD dwAvailable = 0;
773 g_hChildStd_OUT_Rd, NULL, NULL, NULL, &dwAvailable, NULL);
777 g_hChildStd_OUT_Rd, chBuf,
sizeof(chBuf), &dwRead, NULL);
778 if (!bSuccess || dwRead == 0)
break;
779 sout.write(chBuf, dwRead);
784 if (GetExitCodeProcess(piProcInfo.hProcess, &exitval))
786 if (exitval != STILL_ACTIVE)
796 CloseHandle(piProcInfo.hProcess);
797 CloseHandle(piProcInfo.hThread);
799 catch (std::string& errStr)
808 *output = sout.str();
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
#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.
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...
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://...
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.
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...
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.
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".
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.