Fix comment.
[ardour.git] / libs / pbd / file_utils.cc
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 #include <algorithm>
21
22 #include <glibmm/fileutils.h>
23 #include <glibmm/pattern.h>
24
25 #include "pbd/compose.h"
26 #include "pbd/file_utils.h"
27
28 #include "pbd/error.h"
29
30 using namespace std;
31
32 namespace PBD {
33
34 void
35 get_files_in_directory (const sys::path& directory_path, vector<string>& result)
36 {
37         if (!is_directory(directory_path)) return;
38
39         try
40         {
41                 Glib::Dir dir(directory_path.to_string());
42                 std::copy(dir.begin(), dir.end(), std::back_inserter(result));
43         }
44         catch (Glib::FileError& err)
45         {
46                 warning << err.what() << endmsg;
47         }
48 }
49
50 void
51 find_matching_files_in_directory (const sys::path& directory,
52                                   const Glib::PatternSpec& pattern,
53                                   vector<sys::path>& result)
54 {
55         vector<string> tmp_files;
56
57         get_files_in_directory (directory, tmp_files);
58         result.reserve(tmp_files.size());
59
60         for (vector<string>::iterator file_iter = tmp_files.begin();
61                         file_iter != tmp_files.end();
62                         ++file_iter)
63         {
64                 if (!pattern.match(*file_iter)) continue;
65
66                 sys::path full_path(directory);
67                 full_path /= *file_iter;
68
69                 result.push_back(full_path);
70         }
71 }
72
73 void
74 find_matching_files_in_directories (const vector<sys::path>& paths,
75                                     const Glib::PatternSpec& pattern,
76                                     vector<sys::path>& result)
77 {
78         for (vector<sys::path>::const_iterator path_iter = paths.begin();
79                         path_iter != paths.end();
80                         ++path_iter)
81         {
82                 find_matching_files_in_directory (*path_iter, pattern, result);
83         }               
84 }
85
86 void
87 find_matching_files_in_search_path (const SearchPath& search_path,
88                                     const Glib::PatternSpec& pattern,
89                                     vector<sys::path>& result)
90 {
91         find_matching_files_in_directories (search_path, pattern, result);    
92 }
93
94 bool
95 find_file_in_search_path(const SearchPath& search_path,
96                          const string& filename,
97                          sys::path& result)
98 {
99         vector<sys::path> tmp;
100         Glib::PatternSpec tmp_pattern(filename);
101
102         find_matching_files_in_search_path (search_path, tmp_pattern, tmp);
103
104         if (tmp.size() == 0)
105         {
106                 return false;
107         }
108
109 #if 0
110         if (tmp.size() != 1)
111         {
112                 info << string_compose
113                         (
114                          "Found more than one file matching %1 in search path %2",
115                          filename,
116                          search_path.to_string ()
117                         )
118                         << endmsg;
119         }
120 #endif
121
122         result = tmp.front();
123
124         return true;
125 }
126
127 } // namespace PBD