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.
22 #include <glibmm/fileutils.h>
23 #include <glibmm/miscutils.h>
24 #include <glibmm/pattern.h>
26 #include <giomm/file.h>
28 #include "pbd/compose.h"
29 #include "pbd/file_utils.h"
30 #include "pbd/error.h"
31 #include "pbd/pathscanner.h"
40 get_files_in_directory (const std::string& directory_path, vector<string>& result)
42 if (!Glib::file_test (directory_path, Glib::FILE_TEST_IS_DIR)) return;
46 Glib::Dir dir(directory_path);
47 std::copy(dir.begin(), dir.end(), std::back_inserter(result));
49 catch (Glib::FileError& err)
51 warning << err.what() << endmsg;
56 find_matching_files_in_directory (const std::string& directory,
57 const Glib::PatternSpec& pattern,
58 vector<std::string>& result)
60 vector<string> tmp_files;
62 get_files_in_directory (directory, tmp_files);
63 result.reserve(tmp_files.size());
65 for (vector<string>::iterator file_iter = tmp_files.begin();
66 file_iter != tmp_files.end();
69 if (!pattern.match(*file_iter)) continue;
71 std::string full_path(directory);
72 full_path = Glib::build_filename (full_path, *file_iter);
74 result.push_back(full_path);
79 find_matching_files_in_directories (const vector<std::string>& paths,
80 const Glib::PatternSpec& pattern,
81 vector<std::string>& result)
83 for (vector<std::string>::const_iterator path_iter = paths.begin();
84 path_iter != paths.end();
87 find_matching_files_in_directory (*path_iter, pattern, result);
92 find_matching_files_in_search_path (const SearchPath& search_path,
93 const Glib::PatternSpec& pattern,
94 vector<std::string>& result)
96 find_matching_files_in_directories (search_path, pattern, result);
100 find_file_in_search_path(const SearchPath& search_path,
101 const string& filename,
104 vector<std::string> tmp;
105 Glib::PatternSpec tmp_pattern(filename);
107 find_matching_files_in_search_path (search_path, tmp_pattern, tmp);
117 info << string_compose
119 "Found more than one file matching %1 in search path %2",
127 result = tmp.front();
133 copy_file(const std::string & from_path, const std::string & to_path)
135 if (!Glib::file_test (from_path, Glib::FILE_TEST_EXISTS)) return false;
137 Glib::RefPtr<Gio::File> from_file = Gio::File::create_for_path(from_path);
138 Glib::RefPtr<Gio::File> to_file = Gio::File::create_for_path(to_path);
142 from_file->copy (to_file, Gio::FILE_COPY_OVERWRITE);
144 catch(const Glib::Exception& ex)
146 error << string_compose (_("Unable to Copy file %1 to %2 (%3)"),
147 from_path, to_path, ex.what())
155 bool accept_all_files (string const &, void *)
161 copy_files(const std::string & from_path, const std::string & to_dir)
164 vector<string*>* files = scanner (from_path, accept_all_files, 0, true, false);
165 for (vector<string*>::iterator i = files->begin(); i != files->end(); ++i) {
166 std::string from = Glib::build_filename (from_path, **i);
167 std::string to = Glib::build_filename (to_dir, **i);
168 copy_file (from, to);