00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2009 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 #ifndef MRPT_OS_H 00029 #define MRPT_OS_H 00030 00031 #include <mrpt/config.h> 00032 00033 #include <cstdarg> 00034 #include <cstdlib> 00035 #include <cstring> 00036 #include <deque> 00037 #include <vector> 00038 00039 // Duplicated here since MRPT_OS.h is the only header that cannot include "utils_defs.h" 00040 #define _IAMINUTILSDEFS_H 00041 #include <mrpt/utils/utils_impexp.h> // DLL import/export definitions 00042 #undef _IAMINUTILSDEFS_H 00043 00044 #include <mrpt/utils/types.h> // This must be AFTER <utils_impexp.h> 00045 00046 // Define a decl. modifier for printf-like format checks at compile time: 00047 #ifdef __GNUC__ 00048 # define MRPT_printf_format_check(_FMT_,_VARARGS_) __attribute__ ((__format__ (__printf__, _FMT_,_VARARGS_))) 00049 #else 00050 # define MRPT_printf_format_check(_FMT_,_VARARGS_) 00051 #endif 00052 00053 // Define a decl. modifier for scanf-like format checks at compile time: 00054 #ifdef __GNUC__ 00055 # define MRPT_scanf_format_check(_FMT_,_VARARGS_) __attribute__ ((__format__ (__scanf__, _FMT_,_VARARGS_))) 00056 #else 00057 # define MRPT_scanf_format_check(_FMT_,_VARARGS_) 00058 #endif 00059 00060 00061 /** Used after member declarations */ 00062 #define MRPT_NO_THROWS throw() 00063 00064 /** Represents an invalid timestamp, where applicable. 00065 */ 00066 #define INVALID_TIMESTAMP (0) 00067 00068 00069 namespace mrpt 00070 { 00071 namespace utils { class CFileOutputStream; } 00072 00073 /** 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. 00074 * \sa mrpt::system::os 00075 */ 00076 namespace system 00077 { 00078 /** @name Threads 00079 @{*/ 00080 00081 /** This structure contains the information needed to interface the threads API on each platform: 00082 * \sa createThread 00083 */ 00084 struct TThreadHandle 00085 { 00086 #ifdef MRPT_OS_WINDOWS 00087 TThreadHandle() : //!< Sets the handle to a predefined value meaning it is uninitialized. 00088 hThread(NULL), 00089 idThread(0) 00090 { 00091 } 00092 00093 /** Mark the handle as invalid. 00094 * \sa isClear 00095 */ 00096 void clear() 00097 { 00098 idThread = 0; 00099 hThread = NULL; 00100 } 00101 void *hThread; //!< The thread "HANDLE" 00102 # if defined(HAVE_OPENTHREAD) // defined(_MSC_VER) && (_MSC_VER>=1400) 00103 uintptr_t idThread; //!< The thread ID. 00104 # else 00105 unsigned long idThread; //!< The thread ID. 00106 # endif 00107 #endif 00108 #ifdef MRPT_OS_LINUX 00109 TThreadHandle() : idThread(0) //!< Sets the handle to a predefined value meaning it is uninitialized. 00110 { 00111 } 00112 unsigned long idThread; //!< The thread ID. 00113 00114 /** Mark the handle as invalid. 00115 * \sa isClear 00116 */ 00117 void clear() 00118 { 00119 idThread = 0; 00120 } 00121 #endif 00122 /** Returns true if the handle is uninitialized */ 00123 bool isClear() { return idThread==0; } 00124 }; 00125 00126 /** The type for cross-platform process (application) priorities. 00127 * \sa changeCurrentProcessPriority 00128 */ 00129 enum TProcessPriority { 00130 ppIdle = 0, 00131 ppNormal, 00132 ppHigh, 00133 ppVeryHigh 00134 }; 00135 00136 /** The type for cross-platform thread priorities. 00137 * \sa changeThreadPriority 00138 */ 00139 enum TThreadPriority { 00140 tpLow = 0, 00141 tpNormal, 00142 tpHigh 00143 }; 00144 00145 /** Creates a new thread. 00146 * This function creates, and start, a new thread running some code given by a function. 00147 * The thread function should end by returning as normal. 00148 * \param func The function with the code to run in the thread. 00149 * \param param The parameter to be passed to the new thread function. 00150 * \return A structure that represents the thread (it contains its ID and, in Windows, its HANDLE). 00151 * \exception std::exception If the operation fails 00152 * \sa endThread, joinThread, changeThreadPriority 00153 */ 00154 TThreadHandle MRPTDLLIMPEXP createThread( 00155 void ( *func )( void * ), 00156 void *param = NULL 00157 ); 00158 00159 /** Waits until the given thread ends. 00160 * \sa createThread, endThread 00161 */ 00162 void MRPTDLLIMPEXP joinThread( const TThreadHandle &threadHandle ); 00163 00164 /** Returns the ID of the current thread. 00165 */ 00166 unsigned long MRPTDLLIMPEXP getCurrentThreadId() MRPT_NO_THROWS; 00167 00168 /** Returns the creation and exit times of the current thread and its CPU time consumed. 00169 * \param creationTime The creation time of the thread. 00170 * \param exitTime The exit time of the thread, or undefined if it is still running. 00171 * \param cpuTime The CPU time consumed by the thread, in seconds. 00172 * \exception std::exception If the operation fails 00173 * \sa getCurrentThreadId, createThread 00174 */ 00175 void MRPTDLLIMPEXP getCurrentThreadTimes( 00176 time_t &creationTime, 00177 time_t &exitTime, 00178 double &cpuTime ); 00179 00180 /** Change the priority of the given thread. 00181 * \sa createThread, changeCurrentProcessPriority 00182 */ 00183 void MRPTDLLIMPEXP changeThreadPriority( const TThreadHandle &threadHandle, TThreadPriority priority ); 00184 00185 /** Change the priority of the given process (it applies to all the threads, plus independent modifiers for each thread). 00186 * \sa createThread, changeThreadPriority 00187 */ 00188 void MRPTDLLIMPEXP changeCurrentProcessPriority( TProcessPriority priority ); 00189 00190 /** @} */ 00191 00192 00193 /** This namespace provides a OS-independent interface to low-level functions. 00194 * 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 00195 * of the standard library functions (prefix "_s") are used instead. 00196 */ 00197 namespace os 00198 { 00199 /** An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compilers) 00200 * \sa utils::format 00201 */ 00202 int MRPTDLLIMPEXP sprintf(char *buf, size_t bufSize, const char *format, ...) MRPT_NO_THROWS MRPT_printf_format_check(3,4); 00203 00204 /** An OS-independent version of vsprintf (Notice the bufSize param, which may be ignored in some compilers) 00205 */ 00206 int MRPTDLLIMPEXP vsprintf(char *buf, size_t bufSize, const char *format, va_list args) MRPT_NO_THROWS; 00207 00208 /** An OS-independent version of vsnprintf (Notice the bufSize param, which may be ignored in some compilers) 00209 */ 00210 int MRPTDLLIMPEXP vsnprintf(char *buf, size_t bufSize, const char *format, va_list args) MRPT_NO_THROWS; 00211 00212 /** An OS-independent version of fopen. 00213 * \return It will always return NULL on any error. 00214 */ 00215 FILE MRPTDLLIMPEXP *fopen(const char *fileName,const char *mode) MRPT_NO_THROWS; 00216 00217 /** An OS-independent version of fopen (std::string version) 00218 * \return It will always return NULL on any error. 00219 */ 00220 FILE MRPTDLLIMPEXP *fopen(const std::string &fileName,const char *mode) MRPT_NO_THROWS; 00221 00222 /** An OS-independent version of fprintf 00223 */ 00224 int MRPTDLLIMPEXP fprintf(FILE *fil, const char *format, ...) MRPT_NO_THROWS MRPT_printf_format_check(2,3); 00225 00226 /** An OS-independent version of fscanf 00227 * \return The number of fields correctly assigned 00228 */ 00229 int MRPTDLLIMPEXP fscanf(FILE *fil, const char *format, ...) MRPT_NO_THROWS MRPT_scanf_format_check(2,3); 00230 00231 /** An OS-independent version of fclose. 00232 * \exception std::exception On trying to close a NULL file descriptor. 00233 */ 00234 void MRPTDLLIMPEXP fclose(FILE *f); 00235 00236 /** An OS-independent version of strcat. 00237 * \return It will always return the "dest" pointer. 00238 */ 00239 char MRPTDLLIMPEXP * strcat(char *dest, size_t destSize, const char *source) MRPT_NO_THROWS; 00240 00241 /** An OS-independent version of strcpy. 00242 * \return It will always return the "dest" pointer. 00243 */ 00244 char MRPTDLLIMPEXP *strcpy(char *dest, size_t destSize, const char *source) MRPT_NO_THROWS; 00245 00246 /** An OS-independent version of strcmp. 00247 * \return It will return 0 when both strings are equal, casi sensitive. 00248 */ 00249 int MRPTDLLIMPEXP _strcmp(const char*str1,const char*str2) MRPT_NO_THROWS; 00250 00251 /** An OS-independent version of strcmpi. 00252 * \return It will return 0 when both strings are equal, casi insensitive. 00253 */ 00254 int MRPTDLLIMPEXP _strcmpi(const char*str1,const char*str2) MRPT_NO_THROWS; 00255 00256 /** An OS-independent version of strtoll. 00257 */ 00258 int64_t MRPTDLLIMPEXP _strtoll(const char *nptr, char **endptr, int base); 00259 00260 /** An OS-independent version of strtoull. 00261 */ 00262 uint64_t MRPTDLLIMPEXP _strtoull(const char *nptr, char **endptr, int base); 00263 00264 00265 /** An OS and compiler independent version of "memcpy" 00266 */ 00267 void MRPTDLLIMPEXP memcpy( 00268 void *dest, 00269 size_t destSize, 00270 const void *src, 00271 size_t copyCount ) MRPT_NO_THROWS; 00272 00273 /** An OS-independent version of getch, which waits until a key is pushed. 00274 * \return The pushed key code 00275 */ 00276 int MRPTDLLIMPEXP getch() MRPT_NO_THROWS; 00277 00278 /** An OS-independent version of kbhit, which returns true if a key has been pushed. 00279 */ 00280 bool MRPTDLLIMPEXP kbhit() MRPT_NO_THROWS; 00281 00282 /** @name Aligned memory management 00283 @{ */ 00284 00285 /** Returns an aligned memory block. 00286 * \param alignment The desired alignment, typ. 8 or 16 bytes. 1 means no alignment required. It must be a power of two. 00287 * \sa aligned_free, aligned_realloc, aligned_calloc 00288 * \note Based on code by William Chan 00289 */ 00290 void MRPTDLLIMPEXP *aligned_malloc(size_t bytes, size_t alignment); 00291 00292 /** Identical to aligned_malloc, but it zeroes the reserved memory block. 00293 */ 00294 void MRPTDLLIMPEXP *aligned_calloc(size_t bytes, size_t alignment); 00295 00296 /** Frees a memory block reserved by aligned_malloc. 00297 * \param alignment The desired alignment, typ. 8 or 16 bytes. 1 means no alignment required. 00298 * If old_ptr is NULL, a new block will be reserved from scratch. 00299 * \sa aligned_malloc, aligned_free 00300 */ 00301 void MRPTDLLIMPEXP *aligned_realloc(void* old_ptr, size_t bytes, size_t alignment); 00302 00303 /** Frees a memory block reserved by aligned_malloc 00304 * \sa aligned_malloc 00305 */ 00306 void MRPTDLLIMPEXP aligned_free(void* p); 00307 00308 /** @} */ 00309 00310 } // end namespace "os" 00311 00312 /** An OS-independent method for sending the current thread to "sleep" for a given period of time. 00313 * \param time_ms The sleep period, in miliseconds. 00314 */ 00315 void MRPTDLLIMPEXP sleep( int time_ms ) MRPT_NO_THROWS; 00316 00317 /** An OS-independent method for tokenizing a string. 00318 * The extra parameter "context" must be a pointer to a "char*" variable, which needs no initialization and is used to save information between calls to strtok. 00319 * \sa system::tokenize 00320 */ 00321 char MRPTDLLIMPEXP *strtok( char *str, const char *strDelimit, char **context ) MRPT_NO_THROWS; 00322 00323 /** Tokenizes a string according to a set of delimiting characters. 00324 * Example: 00325 * \code 00326 std::vector<std::string> tokens; 00327 tokenize( " - Pepe-Er Muo"," -",tokens); 00328 * \endcode 00329 * 00330 * Will generate 3 tokens: 00331 * - "Pepe" 00332 * - "Er" 00333 * - "Muo" 00334 */ 00335 void MRPTDLLIMPEXP tokenize( 00336 const std::string &inString, 00337 const std::string &inDelimiters, 00338 std::deque<std::string> &outTokens ) MRPT_NO_THROWS; 00339 00340 /** Tokenizes a string according to a set of delimiting characters. 00341 * Example: 00342 * \code 00343 std::vector<std::string> tokens; 00344 tokenize( " - Pepe-Er Muo"," -",tokens); 00345 * \endcode 00346 * 00347 * Will generate 3 tokens: 00348 * - "Pepe" 00349 * - "Er" 00350 * - "Muo" 00351 */ 00352 void MRPTDLLIMPEXP tokenize( 00353 const std::string &inString, 00354 const std::string &inDelimiters, 00355 std::vector<std::string> &outTokens ) MRPT_NO_THROWS; 00356 00357 /** Removes leading and trailing spaces: 00358 */ 00359 std::string MRPTDLLIMPEXP trim(const std::string &str); 00360 00361 /** Returns true if the number is NaN. */ 00362 bool MRPTDLLIMPEXP isNaN(float f) MRPT_NO_THROWS; 00363 00364 /** Returns true if the number is NaN. */ 00365 bool MRPTDLLIMPEXP isNaN(double f) MRPT_NO_THROWS; 00366 00367 /** Returns true if the number is non infinity. */ 00368 bool MRPTDLLIMPEXP isFinite(float f) MRPT_NO_THROWS; 00369 00370 /** Returns true if the number is non infinity. */ 00371 bool MRPTDLLIMPEXP isFinite(double f) MRPT_NO_THROWS; 00372 00373 #ifdef HAVE_LONG_DOUBLE 00374 /** Returns true if the number is NaN. */ 00375 bool MRPTDLLIMPEXP isNaN(long double f) MRPT_NO_THROWS; 00376 00377 /** Returns true if the number is non infinity. */ 00378 bool MRPTDLLIMPEXP isFinite(long double f) MRPT_NO_THROWS; 00379 #endif 00380 00381 /** Shows the message "Press any key to continue" (or other custom message) to the current standard output and returns when a key is pressed. 00382 */ 00383 void MRPTDLLIMPEXP pause(const std::string &msg = std::string("Press any key to continue...") ) MRPT_NO_THROWS; 00384 00385 /** Clears the console window */ 00386 void MRPTDLLIMPEXP clearConsole(); 00387 00388 /** In platforms and compilers with support to "alloca", allocate a memory block on the stack; if alloca is not supported, it is emulated as a normal "malloc" - NOTICE: Since in some platforms alloca will be emulated with malloc, alloca_free MUST BE ALWAYS CALLED to avoid memory leaks. 00389 * This method MUST BE a macro rather than a function in order to operate on the caller's stack. 00390 * \sa mrpt_alloca_free 00391 */ 00392 #if defined(_MSC_VER) && (_MSC_VER>=1400) 00393 // Visual Studio 2005, 2008 00394 # define mrpt_alloca( nBytes ) _malloca(nBytes) 00395 #elif defined(HAVE_ALLOCA) 00396 // GCC 00397 # define mrpt_alloca( nBytes ) ::alloca(nBytes) 00398 #else 00399 // Default: Emulate with memory in the heap: 00400 # define mrpt_alloca( nBytes ) ::malloc( nBytes ); 00401 #endif 00402 00403 /** This method must be called to "free" each memory block allocated with "system::alloca": If the block was really allocated in the stack, no operation is actually performed, otherwise it will be freed from the heap. 00404 * This method MUST BE a macro rather than a function in order to operate on the caller's stack. 00405 * \sa mrpt_alloca 00406 */ 00407 #if defined(_MSC_VER) && (_MSC_VER>=1400) 00408 // Visual Studio 2005, 2008 00409 # define mrpt_alloca_free( mem_block ) _freea(mem_block) 00410 #elif defined(HAVE_ALLOCA) 00411 // GCC 00412 # define mrpt_alloca_free( mem_block ) 00413 #else 00414 // Default: Emulate with memory in the heap: 00415 # define mrpt_alloca_free( mem_block ) free(mem_block) 00416 #endif 00417 00418 00419 /** @name Time and date functions 00420 @{*/ 00421 00422 /** A system independent time type, it holds the the number of 100-nanosecond intervals since January 1, 1601 (UTC). 00423 * \sa system::getCurrentTime, system::timeDifference, INVALID_TIMESTAMP, TTimeParts 00424 */ 00425 typedef uint64_t TTimeStamp; 00426 00427 /** The parts of a date/time (it's like the standard 'tm' but with fractions of seconds). 00428 * \sa TTimeStamp, timestampToParts, buildTimestampFromParts 00429 */ 00430 struct TTimeParts 00431 { 00432 uint16_t year; /** The year */ 00433 uint8_t month; /** Month (1-12) */ 00434 uint8_t day; /** Day (1-31) */ 00435 uint8_t hour; /** Hour (0-23) */ 00436 uint8_t minute; /** Minute (0-59) */ 00437 double second; /** Seconds (0.0000-59.9999) */ 00438 uint8_t day_of_week; /** Day of week (1:Sunday, 7:Saturday) */ 00439 int daylight_saving; 00440 }; 00441 00442 /** Builds a timestamp from the parts (Parts are in UTC) 00443 * \sa timestampToParts 00444 */ 00445 mrpt::system::TTimeStamp MRPTDLLIMPEXP buildTimestampFromParts( const mrpt::system::TTimeParts &p ); 00446 00447 /** Builds a timestamp from the parts (Parts are in local time) 00448 * \sa timestampToParts, buildTimestampFromParts 00449 */ 00450 mrpt::system::TTimeStamp MRPTDLLIMPEXP buildTimestampFromPartsLocalTime( const mrpt::system::TTimeParts &p ); 00451 00452 /** Gets the individual parts of a date/time (days, hours, minutes, seconds) 00453 * \sa buildTimestampFromParts 00454 */ 00455 void MRPTDLLIMPEXP timestampToParts( TTimeStamp t, TTimeParts &p ); 00456 00457 /** Returns the current (UTC) system time. 00458 * \sa now,getCurrentLocalTime 00459 */ 00460 mrpt::system::TTimeStamp MRPTDLLIMPEXP getCurrentTime( ); 00461 00462 /** A shortcut for system::getCurrentTime 00463 * \sa getCurrentTime, getCurrentLocalTime 00464 */ 00465 inline mrpt::system::TTimeStamp MRPTDLLIMPEXP now() 00466 { 00467 return getCurrentTime(); 00468 } 00469 00470 /** Returns the current (local) time. 00471 * \sa now,getCurrentTime 00472 */ 00473 mrpt::system::TTimeStamp MRPTDLLIMPEXP getCurrentLocalTime( ); 00474 00475 /** Transform from standard "time_t" (actually a double number, it can contain fractions of seconds) to TTimeStamp. 00476 * \sa timestampTotime_t 00477 */ 00478 mrpt::system::TTimeStamp MRPTDLLIMPEXP time_tToTimestamp( const double &t ); 00479 00480 /** Transform from standard "time_t" to TTimeStamp. 00481 * \sa timestampTotime_t 00482 */ 00483 mrpt::system::TTimeStamp MRPTDLLIMPEXP time_tToTimestamp( const time_t &t ); 00484 00485 /** Transform from TTimeStamp to standard "time_t" (actually a double number, it can contain fractions of seconds). 00486 * \sa time_tToTimestamp, secondsToTimestamp 00487 */ 00488 double MRPTDLLIMPEXP timestampTotime_t( const mrpt::system::TTimeStamp &t ); 00489 00490 /** Retuns the time difference from t1 to t2 (positive if t2 is posterior to t1), in seconds. 00491 * \sa secondsToTimestamp 00492 */ 00493 double MRPTDLLIMPEXP timeDifference( const mrpt::system::TTimeStamp &t_first, const mrpt::system::TTimeStamp &t_later ); 00494 00495 /** Transform a time interval (in seconds) into TTimeStamp (e.g. which can be added to an existing valid timestamp) 00496 * \sa timeDifference 00497 */ 00498 mrpt::system::TTimeStamp MRPTDLLIMPEXP secondsToTimestamp( const double &nSeconds ); 00499 00500 /** Returns a formated string with the given time difference (passed as the number of seconds), as a string [H]H:MM:SS.MILISECS 00501 */ 00502 std::string MRPTDLLIMPEXP formatTimeInterval( const double &timeSeconds ); 00503 00504 /** Convert a timestamp into this textual form (UTC time): YEAR/MONTH/DAY,HH:MM:SS.MMM 00505 * \sa dateTimeLocalToString 00506 */ 00507 std::string MRPTDLLIMPEXP dateTimeToString(const mrpt::system::TTimeStamp &t); 00508 00509 /** Convert a timestamp into this textual form (in local time): YEAR/MONTH/DAY,HH:MM:SS.MMM 00510 * \sa dateTimeToString 00511 */ 00512 std::string MRPTDLLIMPEXP dateTimeLocalToString(const mrpt::system::TTimeStamp &t); 00513 00514 /** Convert a timestamp into this textual form: YEAR/MONTH/DAY 00515 */ 00516 std::string MRPTDLLIMPEXP dateToString(const mrpt::system::TTimeStamp &t); 00517 00518 /** Returns the number of seconds ellapsed from midnight in the given timestamp 00519 */ 00520 double MRPTDLLIMPEXP extractDayTimeFromTimestamp(const mrpt::system::TTimeStamp &t); 00521 00522 /** Convert a timestamp into this textual form (UTC): HH:MM:SS.MMMMMM 00523 */ 00524 std::string MRPTDLLIMPEXP timeToString(const mrpt::system::TTimeStamp &t); 00525 00526 /** Convert a timestamp into this textual form (in local time): HH:MM:SS.MMMMMM 00527 */ 00528 std::string MRPTDLLIMPEXP timeLocalToString(const mrpt::system::TTimeStamp &t); 00529 00530 /** This function implements time interval formatting: Given a time in seconds, it will return a string describing the interval with the most appropriate unit. 00531 * E.g.: 1.23 year, 3.50 days, 9.3 hours, 5.3 minutes, 3.34 sec, 178.1 ms, 87.1 us. 00532 * \sa unitsFormat 00533 */ 00534 std::string MRPTDLLIMPEXP intervalFormat(const double seconds); 00535 00536 /** @} */ 00537 00538 /** A useful function for debuging, which saves a std::vector into a text file (compat. with MATLAB) 00539 * \return Returns false on any error, true on everything OK. 00540 */ 00541 bool MRPTDLLIMPEXP vectorToTextFile( const std::vector<float> &vec, const std::string &fileName, bool append = false, bool byRows=false ); 00542 00543 /** A useful function for debuging, which saves a std::vector into a text file (compat. with MATLAB) 00544 * \return Returns false on any error, true on everything OK. 00545 */ 00546 bool MRPTDLLIMPEXP vectorToTextFile( const std::vector<double> &vec, const std::string &fileName, bool append = false, bool byRows=false ); 00547 00548 /** A useful function for debuging, which saves a std::vector into a text file (compat. with MATLAB) 00549 * \return Returns false on any error, true on everything OK. 00550 */ 00551 bool MRPTDLLIMPEXP vectorToTextFile( const std::vector<int> &vec, const std::string &fileName, bool append = false, bool byRows=false ); 00552 00553 /** A useful function for debuging, which saves a std::vector into a text file (compat. with MATLAB) 00554 * \return Returns false on any error, true on everything OK. 00555 * \sa vectorToBinaryFile 00556 */ 00557 bool MRPTDLLIMPEXP vectorToTextFile( const std::vector<size_t> &vec, const std::string &fileName, bool append = false, bool byRows=false ); 00558 00559 /** Saves a vector directly as a binary dump to a file: 00560 * \return Returns false on any error, true on everything OK. 00561 * \sa loadBinaryFile 00562 */ 00563 bool MRPTDLLIMPEXP vectorToBinaryFile( const vector_byte &vec, const std::string &fileName ); 00564 00565 /** Loads a entire file as a vector of bytes. 00566 * \return Returns false on any error, true on everything OK. 00567 * \sa vectorToBinaryFile 00568 */ 00569 bool MRPTDLLIMPEXP loadBinaryFile( vector_byte &out_data, const std::string &fileName ); 00570 00571 /** Returns the memory occupied by this process, in bytes 00572 */ 00573 unsigned long MRPTDLLIMPEXP getMemoryUsage(); 00574 00575 00576 00577 /** @name Directories, files, and file names 00578 @{*/ 00579 00580 /** Returns the name of a proposed temporary file name */ 00581 std::string MRPTDLLIMPEXP getTempFileName(); 00582 00583 /** Returns the current working directory. 00584 */ 00585 std::string MRPTDLLIMPEXP getcwd(); 00586 00587 /** Creates a directory 00588 * \return Returns false on any error, true on everything OK. 00589 * \todo Seems to return false on Linux even after creating the directory OK. 00590 */ 00591 bool MRPTDLLIMPEXP createDirectory( const std::string &dirName ); 00592 00593 /** Deletes a single file. For multiple files see deleteFiles 00594 * \return Returns false on any error, true on everything OK. 00595 * \sa deleteFiles 00596 */ 00597 bool MRPTDLLIMPEXP deleteFile( const std::string &fileName ); 00598 00599 /** Delete one or more files, especified by the (optional) path and the file name (including '?' or '*') - Use forward slash ('/') for directories for compatibility between Windows and Linux, since they will be internally traslated into backward slashes ('\') if MRPT is compiled under Windows. 00600 * \sa deleteFile 00601 */ 00602 void MRPTDLLIMPEXP deleteFiles(const std::string &s); 00603 00604 /** Renames a file - If the target path is different and the filesystem allows it, it will be moved to the new location. 00605 * \return false on any error. In that case, if a pointer to a receiver string is passed in error_msg, a description of the error is saved there. 00606 */ 00607 bool MRPTDLLIMPEXP renameFile( const std::string &oldFileName, const std::string &newFileName, std::string *error_msg=NULL ); 00608 00609 /** Delete all the files in a given directory (nothing done if directory does not exists, or path is a file). 00610 * \sa deleteFile 00611 * \return true on success 00612 */ 00613 bool MRPTDLLIMPEXP deleteFilesInDirectory(const std::string &s, bool deleteDirectoryAsWell = false ); 00614 00615 /** Extract just the name (without extension) of a filename from a complete path plus name plus extension. 00616 * This function works for either "/" or "\" directory separators. 00617 * \sa extractFileExtension,extractFileDirectory 00618 */ 00619 std::string MRPTDLLIMPEXP extractFileName(const std::string& filePath); 00620 00621 /** Extract the extension of a filename. 00622 * For example, for "dummy.cpp", it will return "cpp". 00623 * If "ignore_gz" is true, the second extension will be returned if the file name 00624 * ends in ".gz", for example, for "foo.map.gz", this will return "map". 00625 * \sa extractFileName,extractFileDirectory 00626 */ 00627 std::string MRPTDLLIMPEXP extractFileExtension(const std::string &filePath, bool ignore_gz = false ); 00628 00629 /** Extract the whole path (the directory) of a filename from a complete path plus name plus extension. 00630 * This function works for either "/" or "\" directory separators. 00631 * \sa extractFileName,extractFileExtension 00632 */ 00633 std::string MRPTDLLIMPEXP extractFileDirectory(const std::string &filePath); 00634 00635 /** Test if a given file (or directory) exists. 00636 * \sa directoryExists 00637 */ 00638 bool MRPTDLLIMPEXP fileExists(const std::string& fileName); 00639 00640 /** Test if a given directory exists (it fails if the given path refers to an existing file). 00641 * \sa fileExists 00642 */ 00643 bool MRPTDLLIMPEXP directoryExists(const std::string& fileName); 00644 00645 /** Replace invalid filename chars by underscores ('_'). 00646 * Invalid chars are identified by those not being alphanumeric or: ".-#%$&()+[]{}" 00647 */ 00648 std::string MRPTDLLIMPEXP fileNameStripInvalidChars( const std::string &filename); 00649 00650 /** @} */ 00651 00652 /** Returns a lower-case version of a string. 00653 * \sa toLowerCase 00654 */ 00655 std::string MRPTDLLIMPEXP toUpperCase(const std::string& str); 00656 00657 /** Returns an upper-case version of a string. 00658 * \sa toUpperCase 00659 */ 00660 std::string MRPTDLLIMPEXP toLowerCase(const std::string& str); 00661 00662 /** Returns a lower-case version of a string. 00663 * \sa lowerCase 00664 */ 00665 std::string MRPTDLLIMPEXP upperCase(const std::string& str); 00666 00667 /** Returns an upper-case version of a string. 00668 * \sa upperCase 00669 */ 00670 std::string MRPTDLLIMPEXP lowerCase(const std::string& str); 00671 00672 00673 /** Returns the MRPT compilation date 00674 */ 00675 std::string MRPTDLLIMPEXP MRPT_getCompilationDate(); 00676 00677 /** Returns a string describing the MRPT version including the SVN number. 00678 */ 00679 std::string MRPTDLLIMPEXP MRPT_getVersion(); 00680 00681 /** 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.). 00682 */ 00683 void MRPTDLLIMPEXP registerFatalExceptionHandlers(); 00684 00685 /** Decodes a UTF-8 string into an UNICODE string. 00686 * See http://en.wikipedia.org/wiki/UTF-8 and http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/. 00687 */ 00688 void MRPTDLLIMPEXP decodeUTF8( const std::string &strUTF8, vector_word &out_uniStr ); 00689 00690 /** Encodes a 2-bytes UNICODE string into a UTF-8 string. 00691 * See http://en.wikipedia.org/wiki/UTF-8 and http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/. 00692 */ 00693 void MRPTDLLIMPEXP encodeUTF8( const vector_word &input, std::string &output ); 00694 00695 00696 /** Encode a sequence of bytes as a string in base-64. 00697 * \sa decodeBase64 00698 */ 00699 void MRPTDLLIMPEXP encodeBase64( const vector_byte &inputData, std::string &outString ); 00700 00701 /** Decode a base-64 string into the original sequence of bytes. 00702 * \sa encodeBase64 00703 * \return false on invalid base-64 string passed as input, true on success. 00704 */ 00705 bool MRPTDLLIMPEXP decodeBase64( const std::string &inString, vector_byte &outData ); 00706 00707 00708 /** Dumps the current program stack with detailed information of source files and lines. 00709 * This function requires MRPT linked against wxWidgets. Otherwise, an empty string is returned. 00710 * File names and lines won't be available in release builds. 00711 */ 00712 std::string MRPTDLLIMPEXP stack_trace(bool calling_from_exception = false); 00713 00714 /** Executes the given command (which may contain a program + arguments), and waits until it finishes. 00715 * \return false on any error, true otherwise 00716 */ 00717 bool MRPTDLLIMPEXP launchProcess( const std::string & command ); 00718 00719 /** For use in setConsoleColor */ 00720 enum TConsoleColor 00721 { 00722 CONCOL_NORMAL = 0, 00723 CONCOL_BLUE = 1, 00724 CONCOL_GREEN = 2, 00725 CONCOL_RED = 4 00726 }; 00727 00728 /** Changes the text color in the console for the text written from now on. 00729 * The parameter "color" can be any value in TConsoleColor. 00730 * 00731 * By default the color of "cout" is changed, unless changeStdErr=true, in which case "cerr" is changed. 00732 */ 00733 void MRPTDLLIMPEXP setConsoleColor( TConsoleColor color, bool changeStdErr=false ); 00734 00735 00736 /** This function implements formatting with the appropriate SI metric unit prefix: 1e-12->'p', 1e-9->'n', 1e-6->'u', 1e-3->'m', 1->'', 1e3->'K', 1e6->'M', 1e9->'G', 1e12->'T' 00737 * \sa intervalFormat 00738 */ 00739 std::string MRPTDLLIMPEXP unitsFormat(const double val,int nDecimalDigits=2); 00740 00741 } // End of namespace 00742 00743 } // End of namespace 00744 00745 #endif
Page generated by Doxygen 1.5.9 for MRPT 0.7.1 SVN: at Mon Aug 17 22:21:34 EDT 2009 |