MRPT  1.9.9
filesystem.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/core/exceptions.h> //ASSERTMSG_
12 #include <string>
13 
14 namespace mrpt::system
15 {
16 /** @defgroup filesystem Directories, files, and file names
17  * Header: `#include <mrpt/system/filesystem.h>`.
18  * Library: \ref mrpt_system_grp
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 */
34 std::string getTempFileName();
35 
36 /** Returns the current working directory */
37 std::string getcwd();
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. */
43 std::string getShareMRPTDir();
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  */
96 std::string extractFileExtension(
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  */
104 std::string extractFileDirectory(const std::string& filePath);
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  */
121 std::string fileNameStripInvalidChars(
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  */
130 std::string fileNameChangeExtension(
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 '\'->'/' */
142 std::string filePathSeparatorsToNative(const std::string& filePath);
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 } // namespace mrpt::system
bool createDirectory(const std::string &dirName)
Creates a directory.
Definition: filesystem.cpp:161
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:641
bool fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
Definition: filesystem.cpp:128
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:309
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:329
std::string getTempFileName()
Returns the name of a proposed temporary file name.
Definition: filesystem.cpp:283
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:612
std::string fileNameChangeExtension(const std::string &filename, const std::string &newExtension)
Replace the filename extension by another one.
Definition: filesystem.cpp:373
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:190
std::string extractFileExtension(const std::string &filePath, bool ignore_gz=false)
Extract the extension of a filename.
Definition: filesystem.cpp:98
std::string getcwd()
Returns the current working directory.
Definition: filesystem.cpp:251
bool deleteFile(const std::string &fileName)
Deletes a single file.
Definition: filesystem.cpp:182
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:390
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:218
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:137
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:351
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:62
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:78
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:628



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020