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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST