Main MRPT website > C++ reference for MRPT 1.5.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>
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 */
28 
29  /** Returns the current working directory */
31 
32  /** Attempts to find the directory `[PREFIX/]share/mrpt/` and returns its absolute path, or empty string if not found.
33  * Example return paths: Linux after installing = `/usr/share/mrpt/`; manually-built system = `[CMAKE_SOURCE_DIR]/share/mrpt/`, etc. */
35 
36  /** Creates a directory
37  * \return Returns false on any error, true on directory created or already existed.
38  */
39  bool BASE_IMPEXP createDirectory( const std::string &dirName );
40 
41  /** Deletes a single file. For multiple files see deleteFiles
42  * \return Returns false on any error, true on everything OK.
43  * \sa deleteFiles
44  */
45  bool BASE_IMPEXP deleteFile( const std::string &fileName );
46 
47  /** 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.
48  * \sa deleteFile
49  */
50  void BASE_IMPEXP deleteFiles(const std::string &s);
51 
52  /** Renames a file - If the target path is different and the filesystem allows it, it will be moved to the new location.
53  * \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.
54  */
55  bool BASE_IMPEXP renameFile( const std::string &oldFileName, const std::string &newFileName, std::string *error_msg=NULL );
56 
57  /** Delete all the files in a given directory (nothing done if directory does not exists, or path is a file).
58  * \sa deleteFile
59  * \return true on success
60  */
61  bool BASE_IMPEXP deleteFilesInDirectory(const std::string &s, bool deleteDirectoryAsWell = false );
62 
63  /** Extract just the name (without extension) of a filename from a complete path plus name plus extension.
64  * This function works for either "/" or "\" directory separators.
65  * \sa extractFileExtension,extractFileDirectory
66  */
68 
69  /** Extract the extension of a filename.
70  * For example, for "dummy.cpp", it will return "cpp".
71  * If "ignore_gz" is true, the second extension will be returned if the file name
72  * ends in ".gz", for example, for "foo.map.gz", this will return "map".
73  * \sa extractFileName,extractFileDirectory
74  */
75  std::string BASE_IMPEXP extractFileExtension(const std::string &filePath, bool ignore_gz = false );
76 
77  /** Extract the whole path (the directory) of a filename from a complete path plus name plus extension.
78  * This function works for either "/" or "\" directory separators.
79  * \sa extractFileName,extractFileExtension
80  */
82 
83  /** Test if a given file (or directory) exists.
84  * \sa directoryExists
85  */
86  bool BASE_IMPEXP fileExists(const std::string& fileName);
87 
88  /** Test if a given directory exists (it fails if the given path refers to an existing file).
89  * \sa fileExists
90  */
91  bool BASE_IMPEXP directoryExists(const std::string& fileName);
92 
93  /** Replace invalid filename chars by underscores ('_') or any other user-given char.
94  * Invalid chars are: '<','>',':','"','/','\\','|','?','*'
95  */
96  std::string BASE_IMPEXP fileNameStripInvalidChars( const std::string &filename, const char replacement_to_invalid_chars = '_');
97 
98  /** Replace the filename extension by another one.
99  * Example:
100  * \code
101  * fileNameChangeExtension("cool.txt","bar") // -> "cool.bar"
102  * \endcode
103  */
104  std::string BASE_IMPEXP fileNameChangeExtension( const std::string &filename, const std::string &newExtension );
105 
106  /** Return the size of the given file, or size_t(-1) if some error is found accessing that file. */
107  uint64_t BASE_IMPEXP getFileSize(const std::string &fileName);
108 
109  /** Return the time of the file last modification, or "0" if the file doesn't exist. */
110  time_t BASE_IMPEXP getFileModificationTime(const std::string &filename);
111 
112  /** Windows: replace all '/'->'\' , in Linux/MacOS: replace all '\'->'/' */
114 
115  /** Copies file \a sourceFile to \a targetFile. If the target file exists, it will be overwritten.
116  * If the target file cannot be overwritten, the function first tries to change its permissions/attributes and retries opening it for write.
117  *
118  * \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
119  * attributes can be copied: http://msdn2.microsoft.com/en-us/library/aa365535.aspx
120  *
121  * \return true on success, false on any error, whose description can be optionally get in outErrStr
122  */
123  bool BASE_IMPEXP copyFile(
124  const std::string &sourceFile,
125  const std::string &targetFile,
126  std::string *outErrStr = NULL,
127  bool copyAttribs = true );
128 
129  /** @} */
130  } // End of namespace
131 
132 } // End of namespace
133 
134 #endif
bool BASE_IMPEXP createDirectory(const std::string &dirName)
Creates a directory.
Definition: filesystem.cpp:154
std::string BASE_IMPEXP getShareMRPTDir()
Attempts to find the directory [PREFIX/]share/mrpt/ and returns its absolute path, or empty string if not found.
Definition: filesystem.cpp:567
bool BASE_IMPEXP fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
Definition: filesystem.cpp:124
std::string BASE_IMPEXP 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:315
GLdouble s
Definition: glext.h:3602
std::string BASE_IMPEXP getTempFileName()
Returns the name of a proposed temporary file name.
Definition: filesystem.cpp:273
std::string BASE_IMPEXP filePathSeparatorsToNative(const std::string &filePath)
Windows: replace all &#39;/&#39;->&#39;\&#39; , in Linux/MacOS: replace all &#39;\&#39;->&#39;/&#39;.
Definition: filesystem.cpp:543
std::string BASE_IMPEXP fileNameChangeExtension(const std::string &filename, const std::string &newExtension)
Replace the filename extension by another one.
Definition: filesystem.cpp:356
void BASE_IMPEXP deleteFiles(const std::string &s)
Delete one or more files, especified by the (optional) path and the file name (including &#39;...
Definition: filesystem.cpp:183
std::string BASE_IMPEXP extractFileExtension(const std::string &filePath, bool ignore_gz=false)
Extract the extension of a filename.
Definition: filesystem.cpp:96
GLsizei const GLchar ** string
Definition: glext.h:3919
unsigned __int64 uint64_t
Definition: rptypes.h:52
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string BASE_IMPEXP getcwd()
Returns the current working directory.
Definition: filesystem.cpp:239
bool BASE_IMPEXP deleteFile(const std::string &fileName)
Deletes a single file.
Definition: filesystem.cpp:175
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, or path is a file).
Definition: filesystem.cpp:209
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)...
Definition: filesystem.cpp:132
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...
Definition: filesystem.cpp:338
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...
Definition: filesystem.cpp:61
bool BASE_IMPEXP copyFile(const std::string &sourceFile, const std::string &targetFile, std::string *outErrStr=NULL, bool copyAttribs=true)
Copies file sourceFile to targetFile.
Definition: filesystem.cpp:374
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...
Definition: filesystem.cpp:297
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...
Definition: filesystem.cpp:77
time_t BASE_IMPEXP 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:558



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020