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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020