Main MRPT website > C++ reference
MRPT logo
filesystem.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef MRPT_FILESYSTEM_H
29 #define MRPT_FILESYSTEM_H
30 
31 #include <mrpt/utils/utils_defs.h>
32 
33 namespace mrpt
34 {
35  namespace system
36  {
37  /** @defgroup filesystem Directories, files, and file names (in #include <mrpt/system/filesystem.h>)
38  * \ingroup mrpt_base_grp
39  * @{ */
40 
41  /** Returns the name of a proposed temporary file name */
42  std::string BASE_IMPEXP getTempFileName();
43 
44  /** Returns the current working directory.
45  */
46  std::string BASE_IMPEXP getcwd();
47 
48  /** Creates a directory
49  * \return Returns false on any error, true on everything OK.
50  * \todo Seems to return false on Linux even after creating the directory OK.
51  */
52  bool BASE_IMPEXP createDirectory( const std::string &dirName );
53 
54  /** Deletes a single file. For multiple files see deleteFiles
55  * \return Returns false on any error, true on everything OK.
56  * \sa deleteFiles
57  */
58  bool BASE_IMPEXP deleteFile( const std::string &fileName );
59 
60  /** 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.
61  * \sa deleteFile
62  */
63  void BASE_IMPEXP deleteFiles(const std::string &s);
64 
65  /** Renames a file - If the target path is different and the filesystem allows 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 is passed in error_msg, a description of the error is saved there.
67  */
68  bool BASE_IMPEXP renameFile( const std::string &oldFileName, const std::string &newFileName, std::string *error_msg=NULL );
69 
70  /** Delete all the files in a given directory (nothing done if directory does not exists, or path is a file).
71  * \sa deleteFile
72  * \return true on success
73  */
74  bool BASE_IMPEXP deleteFilesInDirectory(const std::string &s, bool deleteDirectoryAsWell = false );
75 
76  /** Extract just the name (without extension) of a filename from a complete path plus name plus extension.
77  * This function works for either "/" or "\" directory separators.
78  * \sa extractFileExtension,extractFileDirectory
79  */
80  std::string BASE_IMPEXP 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 name
85  * ends in ".gz", for example, for "foo.map.gz", this will return "map".
86  * \sa extractFileName,extractFileDirectory
87  */
88  std::string BASE_IMPEXP extractFileExtension(const std::string &filePath, bool ignore_gz = false );
89 
90  /** Extract the whole path (the directory) of a filename from a complete path plus name plus extension.
91  * This function works for either "/" or "\" directory separators.
92  * \sa extractFileName,extractFileExtension
93  */
94  std::string BASE_IMPEXP extractFileDirectory(const std::string &filePath);
95 
96  /** Test if a given file (or directory) exists.
97  * \sa directoryExists
98  */
99  bool BASE_IMPEXP fileExists(const std::string& fileName);
100 
101  /** Test if a given directory exists (it fails if the given path refers to an existing file).
102  * \sa fileExists
103  */
104  bool BASE_IMPEXP directoryExists(const std::string& fileName);
105 
106  /** Replace invalid filename chars by underscores ('_').
107  * Invalid chars are identified by those not being alphanumeric or: ".-#%$&()+[]{}"
108  */
109  std::string BASE_IMPEXP fileNameStripInvalidChars( const std::string &filename);
110 
111  /** Replace the filename extension by another one.
112  * Example:
113  * \code
114  * fileNameChangeExtension("cool.txt","bar") // -> "cool.bar"
115  * \endcode
116  */
117  std::string BASE_IMPEXP fileNameChangeExtension( const std::string &filename, const std::string &newExtension );
118 
119  /** Return the size of the given file, or size_t(-1) if some error is found accessing that file.
120  */
121  uint64_t BASE_IMPEXP getFileSize(const std::string &fileName);
122 
123  /** Windows: replace all '/'->'\' , in Linux/MacOS: replace all '\'->'/' */
124  std::string BASE_IMPEXP filePathSeparatorsToNative(const std::string & filePath);
125 
126  /** Copies file \a sourceFile to \a targetFile. If the target file exists, it will be overwritten.
127  * If the target file cannot be overwritten, the function first tries to change its permissions/attributes and retries opening it for write.
128  *
129  * \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
130  * attributes can be copied: http://msdn2.microsoft.com/en-us/library/aa365535.aspx
131  *
132  * \return true on success, false on any error, whose description can be optionally get in outErrStr
133  */
134  bool BASE_IMPEXP copyFile(
135  const std::string &sourceFile,
136  const std::string &targetFile,
137  std::string *outErrStr = NULL,
138  bool copyAttribs = true );
139 
140 
141  /** @} */
142 
143 
144  } // End of namespace
145 
146 } // End of namespace
147 
148 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013