Main MRPT website > C++ reference
MRPT logo
os.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef FILE_MRPT_OS_H
29 #define FILE_MRPT_OS_H
30 
31 #include <mrpt/config.h>
32 
33 #include <cstdarg>
34 #include <cstdlib>
35 #include <cstring>
36 #include <deque>
37 #include <vector>
38 
39 // Duplicated here since <mrpt/system/os.h> is the only header that cannot include "utils_defs.h"
40 #include <mrpt/base/link_pragmas.h> // DLL import/export definitions
41 
42 #include <mrpt/utils/types.h> // This must be AFTER <utils_impexp.h>
43 #include <mrpt/utils/mrpt_macros.h> // This must be AFTER <utils_impexp.h>
44 
45 /** Represents an invalid timestamp, where applicable.
46  */
47 #define INVALID_TIMESTAMP (0)
48 
49 namespace mrpt
50 {
51  /** This namespace provides a OS-independent interface to many useful functions: filenames manipulation, time and date, string parsing, file I/O, threading, memory allocation, etc.
52  * \sa mrpt::system::os \ingroup mrpt_base_grp
53  */
54  namespace system
55  {
56  /** \defgroup mrpt_system_os OS and compiler abstraction
57  * \ingroup mrpt_base_grp */
58 
59  /** This namespace provides a OS-independent interface to low-level functions.
60  * Most of these functions are converted into calls to standard functions, unless we are into Visual Studio 2005 (or newer). In that case the secure version
61  * of the standard library functions (prefix "_s") are used instead. \ingroup mrpt_base_grp mrpt_system_os
62  */
63  namespace os
64  {
65  /** \addtogroup mrpt_system_os
66  * @{ */
67 
68  /** An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compilers)
69  * \sa utils::format
70  */
71  int BASE_IMPEXP sprintf(char *buf, size_t bufSize, const char *format, ...) MRPT_NO_THROWS MRPT_printf_format_check(3,4);
72 
73  /** An OS-independent version of vsprintf (Notice the bufSize param, which may be ignored in some compilers)
74  */
75  int BASE_IMPEXP vsprintf(char *buf, size_t bufSize, const char *format, va_list args) MRPT_NO_THROWS;
76 
77  /** An OS-independent version of vsnprintf (Notice the bufSize param, which may be ignored in some compilers)
78  */
79  int BASE_IMPEXP vsnprintf(char *buf, size_t bufSize, const char *format, va_list args) MRPT_NO_THROWS;
80 
81  /** An OS-independent version of fopen.
82  * \return It will always return NULL on any error.
83  */
84  FILE BASE_IMPEXP *fopen(const char *fileName,const char *mode) MRPT_NO_THROWS;
85 
86  /** An OS-independent version of fopen (std::string version)
87  * \return It will always return NULL on any error.
88  */
89  FILE BASE_IMPEXP *fopen(const std::string &fileName,const char *mode) MRPT_NO_THROWS;
90 
91  /** An OS-independent version of fprintf
92  */
93  int BASE_IMPEXP fprintf(FILE *fil, const char *format, ...) MRPT_NO_THROWS MRPT_printf_format_check(2,3);
94 
95  /** An OS-independent version of fscanf
96  * \return The number of fields correctly assigned
97  */
98  //int BASE_IMPEXP fscanf(FILE *fil, const char *format, ...) MRPT_NO_THROWS MRPT_scanf_format_check(2,3);
99 
100  /** An OS-independent version of fclose.
101  * \exception std::exception On trying to close a NULL file descriptor.
102  */
103  void BASE_IMPEXP fclose(FILE *f);
104 
105  /** An OS-independent version of strcat.
106  * \return It will always return the "dest" pointer.
107  */
108  char BASE_IMPEXP * strcat(char *dest, size_t destSize, const char *source) MRPT_NO_THROWS;
109 
110  /** An OS-independent version of strcpy.
111  * \return It will always return the "dest" pointer.
112  */
113  char BASE_IMPEXP *strcpy(char *dest, size_t destSize, const char *source) MRPT_NO_THROWS;
114 
115  /** An OS-independent version of strcmp.
116  * \return It will return 0 when both strings are equal, casi sensitive.
117  */
118  int BASE_IMPEXP _strcmp(const char*str1,const char*str2) MRPT_NO_THROWS;
119 
120  /** An OS-independent version of strcmpi.
121  * \return It will return 0 when both strings are equal, casi insensitive.
122  */
123  int BASE_IMPEXP _strcmpi(const char*str1,const char*str2) MRPT_NO_THROWS;
124 
125  /** An OS-independent version of strncmp.
126  * \return It will return 0 when both strings are equal, casi sensitive.
127  */
128  int BASE_IMPEXP _strncmp(const char*str,const char*subStr,size_t count) MRPT_NO_THROWS;
129 
130  /** An OS-independent version of strnicmp.
131  * \return It will return 0 when both strings are equal, casi insensitive.
132  */
133  int BASE_IMPEXP _strnicmp(const char*str,const char*subStr,size_t count) MRPT_NO_THROWS;
134 
135  /** An OS-independent version of strtoll.
136  */
137  int64_t BASE_IMPEXP _strtoll(const char *nptr, char **endptr, int base);
138 
139  /** An OS-independent version of strtoull.
140  */
141  uint64_t BASE_IMPEXP _strtoull(const char *nptr, char **endptr, int base);
142 
143  /** An OS-independent version of timegm (which is not present in all compilers): converts a time structure into an UTM time_t */
144  time_t BASE_IMPEXP timegm(struct tm *tm);
145 
146  /** An OS and compiler independent version of "memcpy"
147  */
148  void BASE_IMPEXP memcpy(
149  void *dest,
150  size_t destSize,
151  const void *src,
152  size_t copyCount ) MRPT_NO_THROWS;
153 
154  /** An OS-independent version of getch, which waits until a key is pushed.
155  * \return The pushed key code
156  */
157  int BASE_IMPEXP getch() MRPT_NO_THROWS;
158 
159  /** An OS-independent version of kbhit, which returns true if a key has been pushed.
160  */
161  bool BASE_IMPEXP kbhit() MRPT_NO_THROWS;
162 
163  /** @} */
164 
165  } // end namespace "os"
166 
167  /** \addtogroup mrpt_system_os
168  * @{ */
169 
170  /** Shows the message "Press any key to continue" (or other custom message) to the current standard output and returns when a key is pressed.
171  */
172  void BASE_IMPEXP pause(const std::string &msg = std::string("Press any key to continue...") ) MRPT_NO_THROWS;
173 
174  /** Clears the console window */
175  void BASE_IMPEXP clearConsole();
176 
177  /** A useful function for debuging, which saves a std::vector into a text file (compat. with MATLAB)
178  * \return Returns false on any error, true on everything OK.
179  */
180  bool BASE_IMPEXP vectorToTextFile( const std::vector<float> &vec, const std::string &fileName, bool append = false, bool byRows=false );
181  //! \overload
182  bool BASE_IMPEXP vectorToTextFile( const std::vector<double> &vec, const std::string &fileName, bool append = false, bool byRows=false );
183  //! \overload
184  bool BASE_IMPEXP vectorToTextFile( const std::vector<int> &vec, const std::string &fileName, bool append = false, bool byRows=false );
185  //! \overload
186  bool BASE_IMPEXP vectorToTextFile( const std::vector<size_t> &vec, const std::string &fileName, bool append = false, bool byRows=false );
187  //! \overload
188  template <class Derived>
189  bool vectorToTextFile( const Eigen::MatrixBase<Derived> &vec, const std::string &fileName ) {
190  try {
191  vec.saveToTextFile(fileName);
192  return true;
193  } catch(...) {return false;}
194  }
195 
196  /** Load a std::vector from a text file (compat. with MATLAB)
197  * \return Returns false on any error, true on everything OK.
198  * \sa loadBinaryFile
199  */
200  bool BASE_IMPEXP vectorFromTextFile( std::vector<double> &vec, const std::string &fileName, const bool byRows=false );
201 
202  /** Saves a vector directly as a binary dump to a file:
203  * \return Returns false on any error, true on everything OK.
204  * \sa loadBinaryFile
205  */
206  bool BASE_IMPEXP vectorToBinaryFile( const vector_byte &vec, const std::string &fileName );
207 
208  /** Loads a entire file as a vector of bytes.
209  * \return Returns false on any error, true on everything OK.
210  * \sa vectorToBinaryFile
211  */
212  bool BASE_IMPEXP loadBinaryFile( vector_byte &out_data, const std::string &fileName );
213 
214  /** Returns the MRPT compilation date
215  */
216  std::string BASE_IMPEXP MRPT_getCompilationDate();
217 
218  /** Returns a string describing the MRPT version including the SVN number.
219  */
220  std::string BASE_IMPEXP MRPT_getVersion();
221 
222  /** Call this to register handlers for fatal erros (memory access,etc) that show useful debug information (It is called automatically normally, no need for the user to explicitly call this method.).
223  */
225 
226  /** Dumps the current program stack with detailed information of source files and lines.
227  * This function requires MRPT linked against wxWidgets. Otherwise, an empty string is returned.
228  * File names and lines won't be available in release builds.
229  */
230  std::string BASE_IMPEXP stack_trace(bool calling_from_exception = false);
231 
232  /** Only when built in debug (with _DEBUG), this function will be called just before raising any MRPT exception,
233  * so the user can conveniently put a breakpoint here to explore the call stack, etc.
234  */
235  void BASE_IMPEXP breakpoint(const std::string &exception_msg);
236 
237  /** For use in setConsoleColor */
239  {
244  };
245 
246  /** Changes the text color in the console for the text written from now on.
247  * The parameter "color" can be any value in TConsoleColor.
248  *
249  * By default the color of "cout" is changed, unless changeStdErr=true, in which case "cerr" is changed.
250  */
251  void BASE_IMPEXP setConsoleColor( TConsoleColor color, bool changeStdErr=false );
252 
253  /** @} */
254 
255  } // End of namespace
256 
257 } // End of namespace
258 
259 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013