MRPT  2.0.4
os.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/config.h>
12 
13 #include <mrpt/core/common.h>
14 #include <mrpt/core/optional_ref.h>
15 #include <cstdarg>
16 #include <cstdint>
17 #include <cstdio> // FILE
18 #include <cstdlib>
19 #include <string>
20 
21 namespace mrpt::system
22 {
23 /** \defgroup mrpt_system_os OS and compiler abstraction
24  * Header: `#include <mrpt/system/os.h>`.
25  * Library: \ref mrpt_system_grp
26  * \ingroup mrpt_system_grp */
27 
28 namespace os
29 {
30 /** \addtogroup mrpt_system_os
31  * @{ */
32 
33 /** An OS-independent version of sprintf (Notice the bufSize param, which may be
34  * ignored in some compilers)
35  * \sa mrpt::format
36  */
37 int sprintf(
38  char* buf, size_t bufSize, const char* format,
39  ...) noexcept MRPT_printf_format_check(3, 4);
40 
41 /** An OS-independent version of vsprintf (Notice the bufSize param, which may
42  * be ignored in some compilers)
43  */
44 int vsprintf(
45  char* buf, size_t bufSize, const char* format, va_list args) noexcept;
46 
47 /** An OS-independent version of vsnprintf (Notice the bufSize param, which may
48  * be ignored in some compilers)
49  */
50 int vsnprintf(
51  char* buf, size_t bufSize, const char* format, va_list args) noexcept;
52 
53 /** An OS-independent version of fopen.
54  * \return It will always return nullptr on any error.
55  */
56 FILE* fopen(const char* fileName, const char* mode) noexcept;
57 
58 /** An OS-independent version of fopen (std::string version)
59  * \return It will always return nullptr on any error.
60  */
61 FILE* fopen(const std::string& fileName, const char* mode) noexcept;
62 
63 /** An OS-independent version of fprintf
64  */
65 int fprintf(
66  FILE* fil, const char* format, ...) noexcept MRPT_printf_format_check(2, 3);
67 
68 /** An OS-independent version of fclose.
69  * \exception std::exception On trying to close a nullptr file descriptor.
70  */
71 void fclose(FILE* f);
72 
73 /** An OS-independent version of strcat.
74  * \return It will always return the "dest" pointer.
75  */
76 char* strcat(char* dest, size_t destSize, const char* source) noexcept;
77 
78 /** An OS-independent version of strcpy.
79  * \return It will always return the "dest" pointer.
80  */
81 char* strcpy(char* dest, size_t destSize, const char* source) noexcept;
82 
83 /** An OS-independent version of strcmp.
84  * \return It will return 0 when both strings are equal, casi sensitive.
85  */
86 int _strcmp(const char* str1, const char* str2) noexcept;
87 
88 /** An OS-independent version of strcmpi.
89  * \return It will return 0 when both strings are equal, casi insensitive.
90  */
91 int _strcmpi(const char* str1, const char* str2) noexcept;
92 
93 /** An OS-independent version of strncmp.
94  * \return It will return 0 when both strings are equal, casi sensitive.
95  */
96 int _strncmp(const char* str, const char* subStr, size_t count) noexcept;
97 
98 /** An OS-independent version of strnicmp.
99  * \return It will return 0 when both strings are equal, casi insensitive.
100  */
101 int _strnicmp(const char* str, const char* subStr, size_t count) noexcept;
102 
103 /** An OS-independent version of strtoll.
104  */
105 int64_t _strtoll(const char* nptr, char** endptr, int base);
106 
107 /** An OS-independent version of strtoull.
108  */
109 uint64_t _strtoull(const char* nptr, char** endptr, int base);
110 
111 /** An OS-independent version of timegm (which is not present in all compilers):
112  * converts a time structure into an UTM time_t */
113 time_t timegm(struct tm* tm);
114 
115 /** An OS and compiler independent version of "memcpy"
116  */
117 void memcpy(
118  void* dest, size_t destSize, const void* src, size_t copyCount) noexcept;
119 
120 /** An OS-independent version of getch, which waits until a key is pushed.
121  * \return The pushed key code
122  */
123 int getch() noexcept;
124 
125 /** An OS-independent version of kbhit, which returns true if a key has been
126  * pushed.
127  */
128 bool kbhit() noexcept;
129 
130 /** @} */
131 
132 } // namespace os
133 
134 /** \addtogroup mrpt_system_os
135  * @{ */
136 
137 /** Shows the message "Press any key to continue" (or other custom message) to
138  * the current standard output and returns when a key is pressed */
139 void pause(
140  const std::string& msg =
141  std::string("Press any key to continue...")) noexcept;
142 
143 /** Clears the console window */
144 void clearConsole();
145 
146 /** Returns the MRPT source code timestamp, according to the Reproducible-Builds
147  * specifications: https://reproducible-builds.org/specs/source-date-epoch/ */
148 std::string MRPT_getCompilationDate();
149 
150 /** Returns a string describing the MRPT version */
151 std::string MRPT_getVersion();
152 
153 /** Returns a const ref to a text with the same text that appears at the
154  * beginning of each MRPT file (useful for displaying the License text in GUIs)
155  */
156 const std::string& getMRPTLicense();
157 
158 /** Finds the "[MRPT]/share/mrpt/" directory, if available in the system. This
159  * searches in (1) source code tree, (2) install target paths. */
160 std::string find_mrpt_shared_dir();
161 
162 /** For use in setConsoleColor */
164 {
169 };
170 
171 /** Changes the text color in the console for the text written from now on.
172  * The parameter "color" can be any value in TConsoleColor.
173  *
174  * By default the color of "cout" is changed, unless changeStdErr=true, in
175  * which case "cerr" is changed.
176  */
177 void setConsoleColor(TConsoleColor color, bool changeStdErr = false);
178 
179 /** @brief Execute Generic Shell Command
180  *
181  * @param[in] command Command to execute
182  * @param[out] output Pointer to string containing the shell output
183  * @param[in] mode read/write access
184  *
185  * @return 0 for success, -1 otherwise.
186  *
187  * \note Original code snippet found in http://stackoverflow.com/a/30357710
188  */
189 int executeCommand(
190  const std::string& command, std::string* output = nullptr,
191  const std::string& mode = "r");
192 
193 /** Executes the given command (which may contain a program + arguments), and
194 waits until it finishes.
195 
196 * \return false on any error, true otherwise
197 
198 */
199 bool launchProcess(const std::string& command);
200 
201 /** Loads a dynamically-linked "plug-in" module (Windows: .dll, GNU/Linux: .so).
202  * Useful to register `mrpt-rtti`-based classes defined in external user code.
203  *
204  * \param[in] moduleFileName Absolute or relative path to the module file.
205  * \param[in,out] outErrorMsgs If provided, error messages will be saved here.
206  * If not, errors will be dumped to std::cerr. \return true If modules could be
207  * loaded without errors.
208  * Upon mrpt-system library unloading, all loaded modules will be automatically
209  * unloaded too. Manual unload is possible with \a unloadPluginModule().
210  */
211 bool loadPluginModule(
212  const std::string& moduleFileName,
213  mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt);
214 
215 /** Unloads "plug-in" modules loaded with loadPluginModule().
216  * \return true if module could be unloaded without errors.
217  * \note Unloaded is done automatically before program exit even if this is not
218  * explicitly called.
219  */
220 bool unloadPluginModule(
221  const std::string& moduleFileName,
222  mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt);
223 
224 /** Like loadPluginModule(), but loads a comma (`,`) separated list of "plug-in"
225  * modules.
226  * \return true if all modules could be loaded without errors.
227  * Upon mrpt-system library unloading, all loaded modules will be automatically
228  * unloaded too. Manual unload is possible with \a unloadPluginModules().
229  */
230 bool loadPluginModules(
231  const std::string& moduleFileNames,
232  mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt);
233 
234 /** Unloads "plug-in" modules loaded with loadPluginModules().
235  * \return true if all modules could be unloaded without errors.
236  * \note Unloaded is done automatically before program exit even if this is not
237  * explicitly called.
238  */
240  const std::string& moduleFileNames,
241  mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt);
242 
243 /** @} */
244 
245 } // namespace mrpt::system
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...
int getch() noexcept
An OS-independent version of getch, which waits until a key is pushed.
Definition: os.cpp:377
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
int _strncmp(const char *str, const char *subStr, size_t count) noexcept
An OS-independent version of strncmp.
Definition: os.cpp:343
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:282
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...
Definition: os.cpp:119
int _strnicmp(const char *str, const char *subStr, size_t count) noexcept
An OS-independent version of strnicmp.
Definition: os.cpp:351
void setConsoleColor(TConsoleColor color, bool changeStdErr=false)
Changes the text color in the console for the text written from now on.
Definition: os.cpp:489
STL namespace.
std::string MRPT_getCompilationDate()
Returns the MRPT source code timestamp, according to the Reproducible-Builds specifications: https://...
Definition: os.cpp:161
std::optional< std::reference_wrapper< T > > optional_ref
Shorter name for std::optional<std::reference_wrapper<T>>
Definition: optional_ref.h:20
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.
Definition: os.cpp:618
void clearConsole()
Clears the console window.
Definition: os.cpp:446
TConsoleColor
For use in setConsoleColor.
Definition: os.h:163
bool unloadPluginModules(const std::string &moduleFileNames, mrpt::optional_ref< std::string > outErrorMsgs=std::nullopt)
Unloads "plug-in" modules loaded with loadPluginModules().
bool loadPluginModules(const std::string &moduleFileNames, mrpt::optional_ref< std::string > outErrorMsgs=std::nullopt)
Like loadPluginModule(), but loads a comma (,) separated list of "plug-in" modules.
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:415
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:437
bool unloadPluginModule(const std::string &moduleFileName, mrpt::optional_ref< std::string > outErrorMsgs=std::nullopt)
Unloads "plug-in" modules loaded with loadPluginModule().
int executeCommand(const std::string &command, std::string *output=nullptr, const std::string &mode="r")
Execute Generic Shell Command.
Definition: os.cpp:667
bool loadPluginModule(const std::string &moduleFileName, mrpt::optional_ref< std::string > outErrorMsgs=std::nullopt)
Loads a dynamically-linked "plug-in" module (Windows: .dll, GNU/Linux: .so).
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:237
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:399
uint64_t _strtoull(const char *nptr, char **endptr, int base)
An OS-independent version of strtoull.
Definition: os.cpp:473
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:264
std::string MRPT_getVersion()
Returns a string describing the MRPT version.
Definition: os.cpp:194
int64_t _strtoll(const char *nptr, char **endptr, int base)
An OS-independent version of strtoll.
Definition: os.cpp:461
bool launchProcess(const std::string &command)
Executes the given command (which may contain a program + arguments), and waits until it finishes...
Definition: os.cpp:588
#define MRPT_printf_format_check(_FMT_, _VARARGS_)
Definition: common.h:142
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:548
int _strcmp(const char *str1, const char *str2) noexcept
An OS-independent version of strcmp.
Definition: os.cpp:319
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.
Definition: os.cpp:327



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 1e710274b Thu May 28 15:30:14 2020 +0200 at jue may 28 15:45:10 CEST 2020