32 #include <sys/utime.h> 40 #include <sys/select.h> 49 #include <sys/types.h> 53 #define _access access 68 void my_aux_sighandler(
int) {}
71 struct termios oldtio, curtio;
75 tcgetattr(0, &oldtio);
80 tcgetattr(0, &curtio);
81 curtio.c_lflag &= ~(ICANON | ECHO);
82 tcsetattr(0, TCSANOW, &curtio);
84 struct pollfd pfds[1];
88 pfds[0].events = POLLIN;
89 const int ret = poll(pfds, 1, 0);
92 tcsetattr(0, TCSANOW, &oldtio);
112 static std::mutex cs;
113 std::lock_guard<std::mutex> lock(cs);
119 char* org_tz = getenv(
"TZ");
140 #endif // HAVE_TIMEGM 145 #include <mrpt/version.h> 156 const char* source_date_epoch = MRPT_SOURCE_DATE_EPOCH;
159 unsigned long epoch = strtoul(source_date_epoch, &endptr, 10);
162 (epoch == std::numeric_limits<unsigned long>::max() || epoch == 0)) ||
163 (errno != 0 && epoch == 0)))
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;
178 "%i-%02i-%02i %02i:%02i:%02i UTC", year, month, day,
179 build_time->tm_hour, build_time->tm_min, build_time->tm_sec);
197 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 213 char* buf,
size_t bufSize,
const char*
format, va_list args) noexcept
216 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 229 char* buf,
size_t bufSize,
const char*
format, va_list args) noexcept
231 #if defined(_MSC_VER) 232 #if (_MSC_VER >= 1400) 257 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 260 if (0 != ::fopen_s(&f, fileName,
mode))
275 if (!f)
THROW_EXCEPTION(
"Trying to close a nullptr 'FILE*' descriptor");
286 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 287 ::strcat_s(dest, destSize,
source);
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);
338 return ::strncmp(str1, str2,
count);
346 #if defined(_MSC_VER) 349 return ::strncasecmp(str1, str2,
count);
357 void* dest,
size_t destSize,
const void*
src,
size_t copyCount) noexcept
359 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 360 ::memcpy_s(dest, destSize,
src, copyCount);
375 struct termios oldt, newt;
377 tcgetattr(STDIN_FILENO, &oldt);
379 newt.c_lflag &= ~(ICANON | ECHO);
380 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
382 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
393 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 394 return ::_kbhit() != 0;
412 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 414 result = ::vfprintf_s(fil, frm, ap);
418 result = ::vfprintf(fil, frm, ap);
430 std::cout << msg << std::endl;
440 int ret = ::system(
"cls");
442 int ret = ::system(
"clear");
445 cerr <<
"[mrpt::system::clearConsole] Error invoking 'clear screen' " 455 return (
int64_t)::strtol(nptr, endptr, base);
457 return (
int64_t)::strtoll(nptr, endptr, base);
467 return (
uint64_t)::strtoul(nptr, endptr, base);
469 return (
uint64_t)::strtoull(nptr, endptr, base);
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;
487 static int normal_attributes = -1;
489 GetStdHandle(changeStdErr ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
490 fflush(changeStdErr ? stderr : stdout);
492 if (normal_attributes < 0)
494 CONSOLE_SCREEN_BUFFER_INFO info;
495 GetConsoleScreenBufferInfo(hstdout, &info);
496 normal_attributes = info.wAttributes;
499 SetConsoleTextAttribute(
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)));
510 if (
color == last_color)
return;
513 static const uint8_t ansi_tab[] = {30, 34, 32, 36, 31, 35, 33, 37};
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);
523 " Mobile Robot Programming Toolkit (MRPT) " 525 " http://www.mrpt.org/ " 529 " Copyright (c) 2005-%Y, Individual contributors, see AUTHORS file " 531 " See: http://www.mrpt.org/Authors - All rights reserved. " 533 " Released under BSD License. See details in http://www.mrpt.org/License " 538 static bool sLicenseTextReady =
false;
541 if (!sLicenseTextReady)
548 timeinfo = localtime(&rawtime);
553 sLicenseTextReady =
true;
562 DWORD e = GetLastError();
563 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, e, 0, str,
sizeof(str), NULL);
580 PROCESS_INFORMATION PI;
581 memset(&SI, 0,
sizeof(STARTUPINFOA));
582 SI.cb =
sizeof(STARTUPINFOA);
584 NULL, (LPSTR)command.c_str(), NULL, NULL,
true, 0, NULL, NULL, &SI,
588 WaitForSingleObject(PI.hProcess, INFINITE);
599 return 0 == ::system(command.c_str());
605 #include <mrpt/mrpt_paths_config.h> 608 static bool mrpt_shared_first_call =
true;
611 if (mrpt_shared_first_call)
613 mrpt_shared_first_call =
false;
615 for (
int attempt = 0;; attempt++)
621 dir =
string(MRPT_SOURCE_BASE_DIRECTORY) +
625 dir =
string(MRPT_INSTALL_PREFIX_DIRECTORY) +
632 GetModuleFileNameA(
nullptr, curExe,
sizeof(curExe));
642 found_mrpt_shared_dir =
".";
646 found_mrpt_shared_dir =
dir;
648 if (!found_mrpt_shared_dir.empty())
break;
652 return found_mrpt_shared_dir;
671 if (!(
in = popen(command.c_str(),
mode.c_str())))
673 sout <<
"Popen Execution failed!" << endl;
674 *output = sout.str();
680 while (fgets(buff,
sizeof(buff),
in) != NULL)
686 exit_code = pclose(
in);
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;
697 HANDLE g_hInputFile = NULL;
698 SECURITY_ATTRIBUTES saAttr;
700 saAttr.nLength =
sizeof(SECURITY_ATTRIBUTES);
701 saAttr.bInheritHandle =
TRUE;
702 saAttr.lpSecurityDescriptor = NULL;
704 if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0))
709 if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
714 if (!CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0))
719 if (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0))
723 PROCESS_INFORMATION piProcInfo;
724 STARTUPINFOA siStartInfo;
729 ZeroMemory(&piProcInfo,
sizeof(PROCESS_INFORMATION));
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;
743 bSuccess = CreateProcessA(
745 (LPSTR)command.c_str(),
766 DWORD dwAvailable = 0;
768 g_hChildStd_OUT_Rd, NULL, NULL, NULL, &dwAvailable, NULL);
772 g_hChildStd_OUT_Rd, chBuf,
sizeof(chBuf), &dwRead, NULL);
773 if (!bSuccess || dwRead == 0)
break;
774 sout.write(chBuf, dwRead);
779 if (GetExitCodeProcess(piProcInfo.hProcess, &exitval))
781 if (exitval != STILL_ACTIVE)
791 CloseHandle(piProcInfo.hProcess);
792 CloseHandle(piProcInfo.hThread);
803 *output = sout.str();
GLuint GLuint GLsizei count
const char * sLicenseTextF
#define THROW_EXCEPTION(msg)
int getch() noexcept
An OS-independent version of getch, which waits until a key is pushed.
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.
int executeCommand(const std::string &command, std::string *output=NULL, const std::string &mode="r")
Execute Generic Shell Command.
std::string winerror2str(const char *errorPlaceName)
void clearConsole()
Clears the console window.
TConsoleColor
For use in setConsoleColor.
GLsizei const GLchar ** string
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...
unsigned __int64 uint64_t
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.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
GLsizei GLsizei GLchar * source
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.
GLenum GLsizei GLenum format
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.