MRPT  1.9.9
os.h
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 #pragma once
10 
11 #include <mrpt/config.h>
12 
13 #include <cstdlib>
14 #include <cstdarg>
15 #include <cstdint>
16 #include <cstdio> // FILE
17 #include <string>
18 #include <mrpt/core/common.h>
19 
20 namespace mrpt::system
21 {
22 /** \defgroup mrpt_system_os OS and compiler abstraction
23  * Header: `#include <mrpt/system/os.h>`.
24  * Library: \ref mrpt_system_grp
25  * \ingroup mrpt_system_grp */
26 
27 namespace os
28 {
29 /** \addtogroup mrpt_system_os
30  * @{ */
31 
32 /** An OS-independent version of sprintf (Notice the bufSize param, which may be
33  * ignored in some compilers)
34  * \sa mrpt::format
35  */
36 int sprintf(
37  char* buf, size_t bufSize, const char* format,
38  ...) noexcept MRPT_printf_format_check(3, 4);
39 
40 /** An OS-independent version of vsprintf (Notice the bufSize param, which may
41  * be ignored in some compilers)
42  */
43 int vsprintf(
44  char* buf, size_t bufSize, const char* format, va_list args) noexcept;
45 
46 /** An OS-independent version of vsnprintf (Notice the bufSize param, which may
47  * be ignored in some compilers)
48  */
49 int vsnprintf(
50  char* buf, size_t bufSize, const char* format, va_list args) noexcept;
51 
52 /** An OS-independent version of fopen.
53  * \return It will always return nullptr on any error.
54  */
55 FILE* fopen(const char* fileName, const char* mode) noexcept;
56 
57 /** An OS-independent version of fopen (std::string version)
58  * \return It will always return nullptr on any error.
59  */
60 FILE* fopen(const std::string& fileName, const char* mode) noexcept;
61 
62 /** An OS-independent version of fprintf
63  */
64 int fprintf(
65  FILE* fil, const char* format, ...) noexcept MRPT_printf_format_check(2, 3);
66 
67 /** An OS-independent version of fclose.
68  * \exception std::exception On trying to close a nullptr file descriptor.
69  */
70 void fclose(FILE* f);
71 
72 /** An OS-independent version of strcat.
73  * \return It will always return the "dest" pointer.
74  */
75 char* strcat(char* dest, size_t destSize, const char* source) noexcept;
76 
77 /** An OS-independent version of strcpy.
78  * \return It will always return the "dest" pointer.
79  */
80 char* strcpy(char* dest, size_t destSize, const char* source) noexcept;
81 
82 /** An OS-independent version of strcmp.
83  * \return It will return 0 when both strings are equal, casi sensitive.
84  */
85 int _strcmp(const char* str1, const char* str2) noexcept;
86 
87 /** An OS-independent version of strcmpi.
88  * \return It will return 0 when both strings are equal, casi insensitive.
89  */
90 int _strcmpi(const char* str1, const char* str2) noexcept;
91 
92 /** An OS-independent version of strncmp.
93  * \return It will return 0 when both strings are equal, casi sensitive.
94  */
95 int _strncmp(const char* str, const char* subStr, size_t count) noexcept;
96 
97 /** An OS-independent version of strnicmp.
98  * \return It will return 0 when both strings are equal, casi insensitive.
99  */
100 int _strnicmp(const char* str, const char* subStr, size_t count) noexcept;
101 
102 /** An OS-independent version of strtoll.
103  */
104 int64_t _strtoll(const char* nptr, char** endptr, int base);
105 
106 /** An OS-independent version of strtoull.
107  */
108 uint64_t _strtoull(const char* nptr, char** endptr, int base);
109 
110 /** An OS-independent version of timegm (which is not present in all compilers):
111  * converts a time structure into an UTM time_t */
112 time_t timegm(struct tm* tm);
113 
114 /** An OS and compiler independent version of "memcpy"
115  */
116 void memcpy(
117  void* dest, size_t destSize, const void* src, size_t copyCount) noexcept;
118 
119 /** An OS-independent version of getch, which waits until a key is pushed.
120  * \return The pushed key code
121  */
122 int getch() noexcept;
123 
124 /** An OS-independent version of kbhit, which returns true if a key has been
125  * pushed.
126  */
127 bool kbhit() noexcept;
128 
129 /** @} */
130 
131 } // namespace os
132 
133 /** \addtogroup mrpt_system_os
134  * @{ */
135 
136 /** Shows the message "Press any key to continue" (or other custom message) to
137  * the current standard output and returns when a key is pressed */
138 void pause(
139  const std::string& msg =
140  std::string("Press any key to continue...")) noexcept;
141 
142 /** Clears the console window */
143 void clearConsole();
144 
145 /** Returns the MRPT source code timestamp, according to the Reproducible-Builds
146  * specifications: https://reproducible-builds.org/specs/source-date-epoch/ */
147 std::string MRPT_getCompilationDate();
148 
149 /** Returns a string describing the MRPT version */
150 std::string MRPT_getVersion();
151 
152 /** Returns a const ref to a text with the same text that appears at the
153  * beginning of each MRPT file (useful for displaying the License text in GUIs)
154  */
155 const std::string& getMRPTLicense();
156 
157 /** Finds the "[MRPT]/share/mrpt/" directory, if available in the system. This
158  * searches in (1) source code tree, (2) install target paths. */
159 std::string find_mrpt_shared_dir();
160 
161 /** For use in setConsoleColor */
163 {
168 };
169 
170 /** Changes the text color in the console for the text written from now on.
171  * The parameter "color" can be any value in TConsoleColor.
172  *
173  * By default the color of "cout" is changed, unless changeStdErr=true, in
174  * which case "cerr" is changed.
175  */
176 void setConsoleColor(TConsoleColor color, bool changeStdErr = false);
177 
178 /** @brief Execute Generic Shell Command
179  *
180  * @param[in] command Command to execute
181  * @param[out] output Pointer to string containing the shell output
182  * @param[in] mode read/write access
183  *
184  * @return 0 for success, -1 otherwise.
185  *
186  * \note Original code snippet found in http://stackoverflow.com/a/30357710
187  */
188 int executeCommand(
189  const std::string& command, std::string* output = NULL,
190  const std::string& mode = "r");
191 
192 /** Executes the given command (which may contain a program + arguments), and
193 waits until it finishes.
194 
195 * \return false on any error, true otherwise
196 
197 */
198 bool launchProcess(const std::string& command);
199 
200 /** @} */
201 
202 } // namespace mrpt::system
GLuint GLuint GLsizei count
Definition: glext.h:3528
int getch() noexcept
An OS-independent version of getch, which waits until a key is pushed.
Definition: os.cpp:370
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:273
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
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:480
STL namespace.
std::string MRPT_getCompilationDate()
Returns the MRPT source code timestamp, according to the Reproducible-Builds specifications: https://...
Definition: os.cpp:152
GLuint src
Definition: glext.h:7278
std::string find_mrpt_shared_dir()
Finds the "[MRPT]/share/mrpt/" directory, if available in the system.
Definition: os.cpp:606
int executeCommand(const std::string &command, std::string *output=NULL, const std::string &mode="r")
Execute Generic Shell Command.
Definition: os.cpp:655
GLuint color
Definition: glext.h:8300
__int64 int64_t
Definition: rptypes.h:49
void clearConsole()
Clears the console window.
Definition: os.cpp:437
TConsoleColor
For use in setConsoleColor.
Definition: os.h:162
GLsizei const GLchar ** string
Definition: glext.h:4101
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:406
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
GLint mode
Definition: glext.h:5669
unsigned __int64 uint64_t
Definition: rptypes.h:50
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
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
char * strcat(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcat.
Definition: os.cpp:282
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:390
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
uint64_t _strtoull(const char *nptr, char **endptr, int base)
An OS-independent version of strtoull.
Definition: os.cpp:464
GLuint GLsizei bufSize
Definition: glext.h:4064
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:255
GLenum GLsizei GLenum format
Definition: glext.h:3531
std::string MRPT_getVersion()
Returns a string describing the MRPT version.
Definition: os.cpp:185
int64_t _strtoll(const char *nptr, char **endptr, int base)
An OS-independent version of strtoll.
Definition: os.cpp:452
char * strcpy(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcpy.
Definition: os.cpp:297
bool launchProcess(const std::string &command)
Executes the given command (which may contain a program + arguments), and waits until it finishes...
Definition: os.cpp:576
#define MRPT_printf_format_check(_FMT_, _VARARGS_)
Definition: common.h:158
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:536
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...
Definition: os.cpp:189
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
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 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020