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 #include <mrpt/core/common.h>
13 #include <mrpt/core/optional_ref.h>
14 
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(char* buf, size_t bufSize, const char* format, ...) noexcept
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(FILE* fil, const char* format, ...) noexcept
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  * \note GNU/Linux: If stdout/stderr is not a real terminal with color support,
177  * calling this function will have no effect (i.e. no escape characters will be
178  * emitted).
179  */
180 void setConsoleColor(TConsoleColor color, bool changeStdErr = false);
181 
182 /** @brief Execute Generic Shell Command
183  *
184  * @param[in] command Command to execute
185  * @param[out] output Pointer to string containing the shell output
186  * @param[in] mode read/write access
187  *
188  * @return 0 for success, -1 otherwise.
189  *
190  * \note Original code snippet found in http://stackoverflow.com/a/30357710
191  */
192 int executeCommand(
193  const std::string& command, std::string* output = nullptr,
194  const std::string& mode = "r");
195 
196 /** Executes the given command (which may contain a program + arguments), and
197 waits until it finishes.
198 
199 * \return false on any error, true otherwise
200 
201 */
202 bool launchProcess(const std::string& command);
203 
204 /** Loads a dynamically-linked "plug-in" module (Windows: .dll, GNU/Linux: .so).
205  * Useful to register `mrpt-rtti`-based classes defined in external user code.
206  *
207  * \param[in] moduleFileName Absolute or relative path to the module file.
208  * \param[in,out] outErrorMsgs If provided, error messages will be saved here.
209  * If not, errors will be dumped to std::cerr. \return true If modules could be
210  * loaded without errors.
211  * Upon mrpt-system library unloading, all loaded modules will be automatically
212  * unloaded too. Manual unload is possible with \a unloadPluginModule().
213  */
214 bool loadPluginModule(
215  const std::string& moduleFileName,
216  mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt);
217 
218 /** Unloads "plug-in" modules loaded with loadPluginModule().
219  * \return true if module could be unloaded without errors.
220  * \note Unloaded is done automatically before program exit even if this is not
221  * explicitly called.
222  */
223 bool unloadPluginModule(
224  const std::string& moduleFileName,
225  mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt);
226 
227 /** Like loadPluginModule(), but loads a comma (`,`) separated list of "plug-in"
228  * modules.
229  * \return true if all modules could be loaded without errors.
230  * Upon mrpt-system library unloading, all loaded modules will be automatically
231  * unloaded too. Manual unload is possible with \a unloadPluginModules().
232  */
233 bool loadPluginModules(
234  const std::string& moduleFileNames,
235  mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt);
236 
237 /** Unloads "plug-in" modules loaded with loadPluginModules().
238  * \return true if all modules could be unloaded without errors.
239  * \note Unloaded is done automatically before program exit even if this is not
240  * explicitly called.
241  */
243  const std::string& moduleFileNames,
244  mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt);
245 
246 /** @} */
247 
248 } // 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:381
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:347
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:286
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:122
int _strnicmp(const char *str, const char *subStr, size_t count) noexcept
An OS-independent version of strnicmp.
Definition: os.cpp:355
void setConsoleColor(TConsoleColor color, bool changeStdErr=false)
Changes the text color in the console for the text written from now on.
Definition: os.cpp:486
STL namespace.
std::string MRPT_getCompilationDate()
Returns the MRPT source code timestamp, according to the Reproducible-Builds specifications: https://...
Definition: os.cpp:165
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:621
void clearConsole()
Clears the console window.
Definition: os.cpp:450
TConsoleColor
For use in setConsoleColor.
Definition: os.h:162
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:419
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:441
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:670
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:241
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:403
uint64_t _strtoull(const char *nptr, char **endptr, int base)
An OS-independent version of strtoull.
Definition: os.cpp:477
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:268
std::string MRPT_getVersion()
Returns a string describing the MRPT version.
Definition: os.cpp:198
int64_t _strtoll(const char *nptr, char **endptr, int base)
An OS-independent version of strtoll.
Definition: os.cpp:465
bool launchProcess(const std::string &command)
Executes the given command (which may contain a program + arguments), and waits until it finishes...
Definition: os.cpp:591
#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:551
int _strcmp(const char *str1, const char *str2) noexcept
An OS-independent version of strcmp.
Definition: os.cpp:323
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:331



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020