2 Copyright (C) 2007 Tim Mayberry
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.
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.
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.
20 #ifndef PBD_FILE_UTILS_INCLUDED
21 #define PBD_FILE_UTILS_INCLUDED
26 #include <glibmm/pattern.h>
28 #include "pbd/libpbd_visibility.h"
29 #include "pbd/search_path.h"
34 * Get a contents of directory.
35 * @note paths in result will be absolute
37 * @param path An absolute path to a directory in the filename encoding
38 * @param result A vector of absolute paths to the directory entries in filename
40 * @param files_only Only include file entries in result
41 * @param recurse Recurse into child directories
44 get_directory_contents (const std::string& path,
45 std::vector<std::string>& result,
46 bool files_only = true,
47 bool recurse = false);
50 * Get a list of files in a directory.
51 * @note You must join path with result to get the absolute path
54 * @param path An Absolute path to a directory
55 * @param result A vector of filenames.
58 get_files_in_directory (const std::string& path,
59 std::vector<std::string>& result);
62 * Takes a directory path and returns all the files in the directory
63 * matching a particular pattern.
65 * @param directory A directory path
66 * @param pattern A Glib::PatternSpec used to match the files.
67 * @param result A vector in which to place the resulting matches.
70 find_matching_files_in_directory (const std::string& directory,
71 const Glib::PatternSpec& pattern,
72 std::vector<std::string>& result);
75 * Takes a number of directory paths and returns all the files matching
76 * a particular pattern.
78 * @param paths A vector containing the Absolute paths
79 * @param pattern A Glib::PatternSpec used to match the files
80 * @param result A vector in which to place the resulting matches.
83 find_matching_files_in_directories (const std::vector<std::string>& directory_paths,
84 const Glib::PatternSpec& pattern,
85 std::vector<std::string>& result);
88 * Takes a Searchpath and puts a list of all the files in the search path
89 * that match pattern into the result vector.
91 * @param search_path A Searchpath
92 * @param pattern A Glib::PatternSpec used to match the files
93 * @param result A vector in which to place the resulting matches.
96 find_matching_files_in_search_path (const Searchpath& search_path,
97 const Glib::PatternSpec& pattern,
98 std::vector<std::string>& result);
101 * Takes a search path and a file name and place the full path
102 * to that file in result if it is found within the search path.
104 * @return true If file is found within the search path.
107 find_file_in_search_path (const Searchpath& search_path,
108 const std::string& filename,
109 std::string& result);
113 * @return files in dirpath that match a regular expression
116 find_files_matching_regex (std::vector<std::string>& results,
117 const Searchpath& dirpath,
118 const std::string& regexp);
121 * @return files in a Searchpath that match a supplied filter(functor)
124 find_files_matching_filter (std::vector<std::string>&,
125 const Searchpath& paths,
126 bool (*filter)(const std::string &, void *),
129 bool return_fullpath,
130 bool recurse = false);
133 * Attempt to copy the contents of the file from_path to a new file
134 * at path to_path. If to_path exists it is overwritten.
136 * @return true if file was successfully copied
138 LIBPBD_API bool copy_file(const std::string & from_path, const std::string & to_path);
141 * Attempt to copy all regular files from from_path to a new directory.
142 * This method does not recurse.
144 LIBPBD_API void copy_files(const std::string & from_path, const std::string & to_dir);
147 * Take a (possibly) relative path and make it absolute
148 * @return An absolute path
150 LIBPBD_API std::string get_absolute_path (const std::string &);
153 * Find out if `needle' is a file or directory within the
154 * directory `haystack'.
155 * @return true if it is.
157 LIBPBD_API bool path_is_within (const std::string &, std::string);
160 * @return true if p1 and p2 both resolve to the same file
161 * @param p1 a file path.
162 * @param p2 a file path.
164 * Uses g_stat to check for identical st_dev and st_ino values.
166 LIBPBD_API bool equivalent_paths (const std::string &p1, const std::string &p2);
168 /// @return true if path at p exists and is writable, false otherwise
169 LIBPBD_API bool exists_and_writable(const std::string & p);