Main MRPT website > C++ reference for MRPT 1.9.9
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-2017, 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>
15 #include <string>
16 
17 namespace mrpt
18 {
19 namespace system
20 {
21 /** @defgroup filesystem Directories, files, and file names (in #include
22  * <mrpt/system/filesystem.h>)
23  * \ingroup mrpt_base_grp
24  * @{ */
25 
26 /** Returns the name of a proposed temporary file name */
28 
29 /** Returns the current working directory */
31 
32 /** Attempts to find the directory `[PREFIX/]share/mrpt/` and returns its
33  * absolute path, or empty string if not found.
34  * Example return paths: Linux after installing = `/usr/share/mrpt/`;
35  * manually-built system = `[CMAKE_SOURCE_DIR]/share/mrpt/`, etc. */
37 
38 /** Creates a directory
39  * \return Returns false on any error, true on directory created or already
40  * existed.
41  */
42 bool createDirectory(const std::string& dirName);
43 
44 /** Deletes a single file. For multiple files see deleteFiles
45  * \return Returns false on any error, true on everything OK.
46  * \sa deleteFiles
47  */
48 bool deleteFile(const std::string& fileName);
49 
50 /** Delete one or more files, especified by the (optional) path and the file
51  * name (including '?' or '*') - Use forward slash ('/') for directories for
52  * compatibility between Windows and Linux, since they will be internally
53  * traslated into backward slashes ('\') if MRPT is compiled under Windows.
54  * \sa deleteFile
55  */
56 void deleteFiles(const std::string& s);
57 
58 /** Renames a file - If the target path is different and the filesystem allows
59  * it, it will be moved to the new location.
60  * \return false on any error. In that case, if a pointer to a receiver string
61  * is passed in error_msg, a description of the error is saved there.
62  */
63 bool renameFile(
64  const std::string& oldFileName, const std::string& newFileName,
65  std::string* error_msg = nullptr);
66 
67 /** Delete all the files in a given directory (nothing done if directory does
68  * not exists, or path is a file).
69  * \sa deleteFile
70  * \return true on success
71  */
73  const std::string& s, bool deleteDirectoryAsWell = false);
74 
75 /** Extract just the name (without extension) of a filename from a complete path
76  * plus name plus extension.
77  * This function works for either "/" or "\" directory separators.
78  * \sa extractFileExtension,extractFileDirectory
79  */
80 std::string extractFileName(const std::string& filePath);
81 
82 /** Extract the extension of a filename.
83  * For example, for "dummy.cpp", it will return "cpp".
84  * If "ignore_gz" is true, the second extension will be returned if the file
85  * name
86  * ends in ".gz", for example, for "foo.map.gz", this will return "map".
87  * \sa extractFileName,extractFileDirectory
88  */
90  const std::string& filePath, bool ignore_gz = false);
91 
92 /** Extract the whole path (the directory) of a filename from a complete path
93  * plus name plus extension.
94  * This function works for either "/" or "\" directory separators.
95  * \sa extractFileName,extractFileExtension
96  */
98 
99 /** Test if a given file (or directory) exists.
100  * \sa directoryExists
101  */
102 bool fileExists(const std::string& fileName);
103 
104 /** Test if a given directory exists (it fails if the given path refers to an
105  * existing file).
106  * \sa fileExists
107  */
108 bool directoryExists(const std::string& fileName);
109 
110 /** Replace invalid filename chars by underscores ('_') or any other user-given
111  * char.
112  * Invalid chars are: '<','>',':','"','/','\\','|','?','*'
113  */
115  const std::string& filename, const char replacement_to_invalid_chars = '_');
116 
117 /** Replace the filename extension by another one.
118  * Example:
119  * \code
120  * fileNameChangeExtension("cool.txt","bar") // -> "cool.bar"
121  * \endcode
122  */
124  const std::string& filename, const std::string& newExtension);
125 
126 /** Return the size of the given file, or size_t(-1) if some error is found
127  * accessing that file. */
128 uint64_t getFileSize(const std::string& fileName);
129 
130 /** Return the time of the file last modification, or "0" if the file doesn't
131  * exist. */
132 time_t getFileModificationTime(const std::string& filename);
133 
134 /** Windows: replace all '/'->'\' , in Linux/MacOS: replace all '\'->'/' */
136 
137 /** Copies file \a sourceFile to \a targetFile. If the target file exists, it
138  * will be overwritten.
139  * If the target file cannot be overwritten, the function first tries to
140  * change its permissions/attributes and retries opening it for write.
141  *
142  * \note Only for Windows: After a successful copy, if \a copyAttribs is true,
143  * the attributes of the source file are also copied. Note that not all
144  * attributes can be copied:
145  * http://msdn2.microsoft.com/en-us/library/aa365535.aspx
146  *
147  * \return true on success, false on any error, whose description can be
148  * optionally get in outErrStr
149  */
150 bool copyFile(
151  const std::string& sourceFile, const std::string& targetFile,
152  std::string* outErrStr = nullptr, bool copyAttribs = true);
153 
154 /** @} */
155 } // End of namespace
156 
157 } // End of namespace
158 
159 #endif
bool createDirectory(const std::string &dirName)
Creates a directory.
Definition: filesystem.cpp:158
std::string getShareMRPTDir()
Attempts to find the directory [PREFIX/]share/mrpt/ and returns its absolute path, or empty string if not found.
Definition: filesystem.cpp:635
bool fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
Definition: filesystem.cpp:127
bool renameFile(const std::string &oldFileName, const std::string &newFileName, std::string *error_msg=nullptr)
Renames a file - If the target path is different and the filesystem allows it, it will be moved to th...
Definition: filesystem.cpp:307
std::string fileNameStripInvalidChars(const std::string &filename, const char replacement_to_invalid_chars='_')
Replace invalid filename chars by underscores (&#39;_&#39;) or any other user-given char. ...
Definition: filesystem.cpp:327
GLdouble s
Definition: glext.h:3676
std::string getTempFileName()
Returns the name of a proposed temporary file name.
Definition: filesystem.cpp:281
std::string filePathSeparatorsToNative(const std::string &filePath)
Windows: replace all &#39;/&#39;->&#39;\&#39; , in Linux/MacOS: replace all &#39;\&#39;->&#39;/&#39;.
Definition: filesystem.cpp:608
std::string fileNameChangeExtension(const std::string &filename, const std::string &newExtension)
Replace the filename extension by another one.
Definition: filesystem.cpp:369
void deleteFiles(const std::string &s)
Delete one or more files, especified by the (optional) path and the file name (including &#39;...
Definition: filesystem.cpp:187
std::string extractFileExtension(const std::string &filePath, bool ignore_gz=false)
Extract the extension of a filename.
Definition: filesystem.cpp:97
GLsizei const GLchar ** string
Definition: glext.h:4101
unsigned __int64 uint64_t
Definition: rptypes.h:50
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string getcwd()
Returns the current working directory.
Definition: filesystem.cpp:247
bool deleteFile(const std::string &fileName)
Deletes a single file.
Definition: filesystem.cpp:179
bool copyFile(const std::string &sourceFile, const std::string &targetFile, std::string *outErrStr=nullptr, bool copyAttribs=true)
Copies file sourceFile to targetFile.
Definition: filesystem.cpp:386
bool deleteFilesInDirectory(const std::string &s, bool deleteDirectoryAsWell=false)
Delete all the files in a given directory (nothing done if directory does not exists, or path is a file).
Definition: filesystem.cpp:214
bool directoryExists(const std::string &fileName)
Test if a given directory exists (it fails if the given path refers to an existing file)...
Definition: filesystem.cpp:136
uint64_t getFileSize(const std::string &fileName)
Return the size of the given file, or size_t(-1) if some error is found accessing that file...
Definition: filesystem.cpp:349
std::string extractFileName(const std::string &filePath)
Extract just the name (without extension) of a filename from a complete path plus name plus extension...
Definition: filesystem.cpp:61
std::string extractFileDirectory(const std::string &filePath)
Extract the whole path (the directory) of a filename from a complete path plus name plus extension...
Definition: filesystem.cpp:77
time_t getFileModificationTime(const std::string &filename)
Return the time of the file last modification, or "0" if the file doesn&#39;t exist.
Definition: filesystem.cpp:624



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019