Main MRPT website > C++ reference for 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
21 {
22 /** This namespace provides a OS-independent interface to many useful functions:
23  * filenames manipulation, time and date, string parsing, file I/O, threading,
24  * memory allocation, etc.
25  * \sa mrpt::system::os \ingroup mrpt_system_grp
26  */
27 namespace system
28 {
29 /** \defgroup mrpt_system_os OS and compiler abstraction (in #include
30  * <mrpt/system/os.h>)
31  * \ingroup mrpt_system_grp */
32 
33 /** This namespace provides a OS-independent interface to low-level functions.
34  * Most of these functions are converted into calls to standard functions,
35  * unless we are into Visual Studio 2005 (or newer). In that case the secure
36  * version
37  * of the standard library functions (prefix "_s") are used instead.
38  * \ingroup mrpt_system_grp mrpt_system_os
39  */
40 namespace os
41 {
42 /** \addtogroup mrpt_system_os
43  * @{ */
44 
45 /** An OS-independent version of sprintf (Notice the bufSize param, which may be
46  * ignored in some compilers)
47  * \sa mrpt::format
48  */
49 int sprintf(
50  char* buf, size_t bufSize, const char* format,
51  ...) noexcept MRPT_printf_format_check(3, 4);
52 
53 /** An OS-independent version of vsprintf (Notice the bufSize param, which may
54  * be ignored in some compilers)
55  */
56 int vsprintf(
57  char* buf, size_t bufSize, const char* format, va_list args) noexcept;
58 
59 /** An OS-independent version of vsnprintf (Notice the bufSize param, which may
60  * be ignored in some compilers)
61  */
62 int vsnprintf(
63  char* buf, size_t bufSize, const char* format, va_list args) noexcept;
64 
65 /** An OS-independent version of fopen.
66  * \return It will always return nullptr on any error.
67  */
68 FILE* fopen(const char* fileName, const char* mode) noexcept;
69 
70 /** An OS-independent version of fopen (std::string version)
71  * \return It will always return nullptr on any error.
72  */
73 FILE* fopen(const std::string& fileName, const char* mode) noexcept;
74 
75 /** An OS-independent version of fprintf
76  */
77 int fprintf(
78  FILE* fil, const char* format, ...) noexcept MRPT_printf_format_check(2, 3);
79 
80 /** An OS-independent version of fclose.
81  * \exception std::exception On trying to close a nullptr file descriptor.
82  */
83 void fclose(FILE* f);
84 
85 /** An OS-independent version of strcat.
86  * \return It will always return the "dest" pointer.
87  */
88 char* strcat(char* dest, size_t destSize, const char* source) noexcept;
89 
90 /** An OS-independent version of strcpy.
91  * \return It will always return the "dest" pointer.
92  */
93 char* strcpy(char* dest, size_t destSize, const char* source) noexcept;
94 
95 /** An OS-independent version of strcmp.
96  * \return It will return 0 when both strings are equal, casi sensitive.
97  */
98 int _strcmp(const char* str1, const char* str2) noexcept;
99 
100 /** An OS-independent version of strcmpi.
101  * \return It will return 0 when both strings are equal, casi insensitive.
102  */
103 int _strcmpi(const char* str1, const char* str2) noexcept;
104 
105 /** An OS-independent version of strncmp.
106  * \return It will return 0 when both strings are equal, casi sensitive.
107  */
108 int _strncmp(const char* str, const char* subStr, size_t count) noexcept;
109 
110 /** An OS-independent version of strnicmp.
111  * \return It will return 0 when both strings are equal, casi insensitive.
112  */
113 int _strnicmp(const char* str, const char* subStr, size_t count) noexcept;
114 
115 /** An OS-independent version of strtoll.
116  */
117 int64_t _strtoll(const char* nptr, char** endptr, int base);
118 
119 /** An OS-independent version of strtoull.
120  */
121 uint64_t _strtoull(const char* nptr, char** endptr, int base);
122 
123 /** An OS-independent version of timegm (which is not present in all compilers):
124  * converts a time structure into an UTM time_t */
125 time_t timegm(struct tm* tm);
126 
127 /** An OS and compiler independent version of "memcpy"
128  */
129 void memcpy(
130  void* dest, size_t destSize, const void* src, size_t copyCount) noexcept;
131 
132 /** An OS-independent version of getch, which waits until a key is pushed.
133  * \return The pushed key code
134  */
135 int getch() noexcept;
136 
137 /** An OS-independent version of kbhit, which returns true if a key has been
138  * pushed.
139  */
140 bool kbhit() noexcept;
141 
142 /** @} */
143 
144 } // end namespace "os"
145 
146 /** \addtogroup mrpt_system_os
147  * @{ */
148 
149 /** Shows the message "Press any key to continue" (or other custom message) to
150  * the current standard output and returns when a key is pressed */
151 void pause(
152  const std::string& msg =
153  std::string("Press any key to continue...")) noexcept;
154 
155 /** Clears the console window */
156 void clearConsole();
157 
158 /** Returns the MRPT source code timestamp, according to the Reproducible-Builds
159  * specifications: https://reproducible-builds.org/specs/source-date-epoch/ */
160 std::string MRPT_getCompilationDate();
161 
162 /** Returns a string describing the MRPT version */
163 std::string MRPT_getVersion();
164 
165 /** Returns a const ref to a text with the same text that appears at the
166  * beginning of each MRPT file (useful for displaying the License text in GUIs)
167  */
168 const std::string& getMRPTLicense();
169 
170 /** Finds the "[MRPT]/share/mrpt/" directory, if available in the system. This
171  * searches in (1) source code tree, (2) install target paths. */
172 std::string find_mrpt_shared_dir();
173 
174 /** For use in setConsoleColor */
176 {
181 };
182 
183 /** Changes the text color in the console for the text written from now on.
184  * The parameter "color" can be any value in TConsoleColor.
185  *
186  * By default the color of "cout" is changed, unless changeStdErr=true, in
187  * which case "cerr" is changed.
188  */
189 void setConsoleColor(TConsoleColor color, bool changeStdErr = false);
190 
191 /** @brief Execute Generic Shell Command
192  *
193  * @param[in] command Command to execute
194  * @param[out] output Pointer to string containing the shell output
195  * @param[in] mode read/write access
196  *
197  * @return 0 for success, -1 otherwise.
198  *
199  * \note Original code snippet found in http://stackoverflow.com/a/30357710
200  */
201 int executeCommand(
202  const std::string& command, std::string* output = NULL,
203  const std::string& mode = "r");
204 
205 /** Executes the given command (which may contain a program + arguments), and
206 waits until it finishes.
207 
208 * \return false on any error, true otherwise
209 
210 */
211 bool launchProcess(const std::string& command);
212 
213 /** @} */
214 
215 } // End of namespace
216 
217 } // End of namespace
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:602
int executeCommand(const std::string &command, std::string *output=NULL, const std::string &mode="r")
Execute Generic Shell Command.
Definition: os.cpp:651
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:175
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
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...
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:572
#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:532
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: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019