Main MRPT website > C++ reference for MRPT 1.4.0
filesystem.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-2016, 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 #ifndef MRPT_FILESYSTEM_H
10 #define MRPT_FILESYSTEM_H
11 
12 #include <mrpt/config.h>
14 #include <mrpt/base/link_pragmas.h>
16 #include <string>
17 
18 namespace mrpt
19 {
20  namespace system
21  {
22  /** @defgroup filesystem Directories, files, and file names (in #include <mrpt/system/filesystem.h>)
23  * \ingroup mrpt_base_grp
24  * @{ */
25 
26  /** Returns the name of a proposed temporary file name */
27  std::string BASE_IMPEXP getTempFileName();
28 
29  /** Returns the current working directory.
30  */
31  std::string BASE_IMPEXP getcwd();
32 
33  /** Creates a directory
34  * \return Returns false on any error, true on directory created or already existed.
35  */
36  bool BASE_IMPEXP createDirectory( const std::string &dirName );
37 
38  /** Deletes a single file. For multiple files see deleteFiles
39  * \return Returns false on any error, true on everything OK.
40  * \sa deleteFiles
41  */
42  bool BASE_IMPEXP deleteFile( const std::string &fileName );
43 
44  /** 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.
45  * \sa deleteFile
46  */
47  void BASE_IMPEXP deleteFiles(const std::string &s);
48 
49  /** Renames a file - If the target path is different and the filesystem allows it, it will be moved to the new location.
50  * \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.
51  */
52  bool BASE_IMPEXP renameFile( const std::string &oldFileName, const std::string &newFileName, std::string *error_msg=NULL );
53 
54  /** Delete all the files in a given directory (nothing done if directory does not exists, or path is a file).
55  * \sa deleteFile
56  * \return true on success
57  */
58  bool BASE_IMPEXP deleteFilesInDirectory(const std::string &s, bool deleteDirectoryAsWell = false );
59 
60  /** Extract just the name (without extension) of a filename from a complete path plus name plus extension.
61  * This function works for either "/" or "\" directory separators.
62  * \sa extractFileExtension,extractFileDirectory
63  */
64  std::string BASE_IMPEXP extractFileName(const std::string& filePath);
65 
66  /** Extract the extension of a filename.
67  * For example, for "dummy.cpp", it will return "cpp".
68  * If "ignore_gz" is true, the second extension will be returned if the file name
69  * ends in ".gz", for example, for "foo.map.gz", this will return "map".
70  * \sa extractFileName,extractFileDirectory
71  */
72  std::string BASE_IMPEXP extractFileExtension(const std::string &filePath, bool ignore_gz = false );
73 
74  /** Extract the whole path (the directory) of a filename from a complete path plus name plus extension.
75  * This function works for either "/" or "\" directory separators.
76  * \sa extractFileName,extractFileExtension
77  */
78  std::string BASE_IMPEXP extractFileDirectory(const std::string &filePath);
79 
80  /** Test if a given file (or directory) exists.
81  * \sa directoryExists
82  */
83  bool BASE_IMPEXP fileExists(const std::string& fileName);
84 
85  /** Test if a given directory exists (it fails if the given path refers to an existing file).
86  * \sa fileExists
87  */
88  bool BASE_IMPEXP directoryExists(const std::string& fileName);
89 
90  /** Replace invalid filename chars by underscores ('_').
91  * Invalid chars are identified by those not being alphanumeric or: ".-#%$&()+[]{}"
92  */
93  std::string BASE_IMPEXP fileNameStripInvalidChars( const std::string &filename);
94 
95  /** Replace the filename extension by another one.
96  * Example:
97  * \code
98  * fileNameChangeExtension("cool.txt","bar") // -> "cool.bar"
99  * \endcode
100  */
101  std::string BASE_IMPEXP fileNameChangeExtension( const std::string &filename, const std::string &newExtension );
102 
103  /** Return the size of the given file, or size_t(-1) if some error is found accessing that file. */
104  uint64_t BASE_IMPEXP getFileSize(const std::string &fileName);
105 
106  /** Return the time of the file last modification, or "0" if the file doesn't exist. */
107  time_t BASE_IMPEXP getFileModificationTime(const std::string &filename);
108 
109  /** Windows: replace all '/'->'\' , in Linux/MacOS: replace all '\'->'/' */
110  std::string BASE_IMPEXP filePathSeparatorsToNative(const std::string & filePath);
111 
112  /** Copies file \a sourceFile to \a targetFile. If the target file exists, it will be overwritten.
113  * If the target file cannot be overwritten, the function first tries to change its permissions/attributes and retries opening it for write.
114  *
115  * \note Only for Windows: After a successful copy, if \a copyAttribs is true, the attributes of the source file are also copied. Note that not all
116  * attributes can be copied: http://msdn2.microsoft.com/en-us/library/aa365535.aspx
117  *
118  * \return true on success, false on any error, whose description can be optionally get in outErrStr
119  */
120  bool BASE_IMPEXP copyFile(
121  const std::string &sourceFile,
122  const std::string &targetFile,
123  std::string *outErrStr = NULL,
124  bool copyAttribs = true );
125 
126  /** @} */
127  } // End of namespace
128 
129 } // End of namespace
130 
131 #endif
mrpt::system::copyFile
bool BASE_IMPEXP copyFile(const std::string &sourceFile, const std::string &targetFile, std::string *outErrStr=NULL, bool copyAttribs=true)
Copies file sourceFile to targetFile.
mrpt::system::extractFileDirectory
std::string BASE_IMPEXP extractFileDirectory(const std::string &filePath)
Extract the whole path (the directory) of a filename from a complete path plus name plus extension.
mrpt::system::fileNameChangeExtension
std::string BASE_IMPEXP fileNameChangeExtension(const std::string &filename, const std::string &newExtension)
Replace the filename extension by another one.
mrpt::system::renameFile
bool BASE_IMPEXP renameFile(const std::string &oldFileName, const std::string &newFileName, std::string *error_msg=NULL)
Renames a file - If the target path is different and the filesystem allows it, it will be moved to th...
mrpt::system::deleteFile
bool BASE_IMPEXP deleteFile(const std::string &fileName)
Deletes a single file.
mrpt::system::deleteFiles
void BASE_IMPEXP deleteFiles(const std::string &s)
Delete one or more files, especified by the (optional) path and the file name (including '?...
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CParticleFilter.h:16
mrpt::system::fileNameStripInvalidChars
std::string BASE_IMPEXP fileNameStripInvalidChars(const std::string &filename)
Replace invalid filename chars by underscores ('_').
mrpt::system::extractFileExtension
std::string BASE_IMPEXP extractFileExtension(const std::string &filePath, bool ignore_gz=false)
Extract the extension of a filename.
mrpt::system::getTempFileName
std::string BASE_IMPEXP getTempFileName()
Returns the name of a proposed temporary file name.
mrpt::system::fileExists
bool BASE_IMPEXP fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
types_simple.h
mrpt::system::extractFileName
std::string BASE_IMPEXP extractFileName(const std::string &filePath)
Extract just the name (without extension) of a filename from a complete path plus name plus extension...
mrpt::system::createDirectory
bool BASE_IMPEXP createDirectory(const std::string &dirName)
Creates a directory.
mrpt::system::deleteFilesInDirectory
bool BASE_IMPEXP deleteFilesInDirectory(const std::string &s, bool deleteDirectoryAsWell=false)
Delete all the files in a given directory (nothing done if directory does not exists,...
compiler_fixes.h
mrpt::system::filePathSeparatorsToNative
std::string BASE_IMPEXP filePathSeparatorsToNative(const std::string &filePath)
Windows: replace all '/'->'\' , in Linux/MacOS: replace all '\'->'/'.
mrpt::system::getFileModificationTime
time_t BASE_IMPEXP getFileModificationTime(const std::string &filename)
Return the time of the file last modification, or "0" if the file doesn't exist.
mrpt::system::directoryExists
bool BASE_IMPEXP directoryExists(const std::string &fileName)
Test if a given directory exists (it fails if the given path refers to an existing file).
mrpt::system::getcwd
std::string BASE_IMPEXP getcwd()
Returns the current working directory.
mrpt::system::getFileSize
uint64_t BASE_IMPEXP getFileSize(const std::string &fileName)
Return the size of the given file, or size_t(-1) if some error is found accessing that file.



Page generated by Doxygen 1.8.17 for MRPT 1.4.0 SVN: at Tue Mar 3 09:15:16 UTC 2020