Merge branch 'master' into cairocanvas
[ardour.git] / libs / pbd / pbd / file_utils.h
1 /*
2     Copyright (C) 2007 Tim Mayberry 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef PBD_FILE_UTILS_INCLUDED
21 #define PBD_FILE_UTILS_INCLUDED
22
23 #include <string>
24 #include <vector>
25
26 #include <glibmm/pattern.h>
27
28 #include "pbd/search_path.h"
29
30 namespace PBD {
31
32 /**
33  * Get a list of files in a directory.
34  * @note You must join path with result to get the absolute path
35  * to the file.
36  *
37  * @param path An Absolute path to a directory
38  * @param result A vector of filenames.
39  */
40 void
41 get_files_in_directory (const std::string& path,
42                         std::vector<std::string>& result);
43
44 /**
45  * Takes a directory path and returns all the files in the directory
46  * matching a particular pattern.
47  *
48  * @param directory A directory path
49  * @param pattern A Glib::PatternSpec used to match the files.
50  * @param result A vector in which to place the resulting matches.
51  */
52 void
53 find_matching_files_in_directory (const std::string& directory,
54                                   const Glib::PatternSpec& pattern,
55                                   std::vector<std::string>& result);
56
57 /**
58  * Takes a number of directory paths and returns all the files matching
59  * a particular pattern.
60  *
61  * @param paths A vector containing the Absolute paths
62  * @param pattern A Glib::PatternSpec used to match the files
63  * @param result A vector in which to place the resulting matches.
64  */
65 void
66 find_matching_files_in_directories (const std::vector<std::string>& directory_paths,
67                                     const Glib::PatternSpec& pattern,
68                                     std::vector<std::string>& result);
69
70 /**
71  * Takes a SearchPath and puts a list of all the files in the search path
72  * that match pattern into the result vector.
73  *
74  * @param search_path A SearchPath
75  * @param pattern A Glib::PatternSpec used to match the files
76  * @param result A vector in which to place the resulting matches.
77  */
78 void
79 find_matching_files_in_search_path (const SearchPath& search_path,
80                                     const Glib::PatternSpec& pattern,
81                                     std::vector<std::string>& result);
82
83 /**
84  * Takes a search path and a file name and place the full path
85  * to that file in result if it is found within the search path.
86  *
87  * @return true If file is found within the search path.
88  */
89 bool
90 find_file_in_search_path (const SearchPath& search_path,
91                           const std::string& filename,
92                           std::string& result);
93
94 /**
95  * Attempt to copy the contents of the file from_path to a new file
96  * at path to_path. If to_path exists it is overwritten.
97  *
98  * @return true if file was successfully copied
99  */
100 bool copy_file(const std::string & from_path, const std::string & to_path);
101
102 /**
103  * Attempt to copy all regular files from from_path to a new directory.
104  * This method does not recurse.
105  */
106 void copy_files(const std::string & from_path, const std::string & to_dir);
107
108 /**
109  * Take a (possibly) relative path and make it absolute
110  * @return An absolute path
111  */
112 std::string get_absolute_path (const std::string &);
113
114 /**
115  * Find out if `needle' is a file or directory within the
116  * directory `haystack'.
117  * @return true if it is.
118  */
119 bool path_is_within (const std::string &, std::string);
120
121 /**
122  * @return true if p1 and p2 both resolve to the same file
123  * @param p1 a file path.
124  * @param p2 a file path.
125  *
126  * Uses g_stat to check for identical st_dev and st_ino values.
127  */
128 bool equivalent_paths (const std::string &p1, const std::string &p2);
129
130 /// @return true if path at p exists and is writable, false otherwise
131 bool exists_and_writable(const std::string & p);
132
133 } // namespace PBD
134
135 #endif