2008-06-02 17:41:35 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2007 Tim Mayberry
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <glibmm/fileutils.h>
|
2012-06-23 01:06:54 -04:00
|
|
|
#include <glibmm/miscutils.h>
|
2008-06-02 17:41:35 -04:00
|
|
|
#include <glibmm/pattern.h>
|
|
|
|
|
2009-02-25 13:26:51 -05:00
|
|
|
#include "pbd/compose.h"
|
|
|
|
#include "pbd/file_utils.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2009-02-25 13:26:51 -05:00
|
|
|
#include "pbd/error.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2009-05-12 13:03:42 -04:00
|
|
|
using namespace std;
|
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
namespace PBD {
|
|
|
|
|
|
|
|
void
|
2012-06-23 01:06:54 -04:00
|
|
|
get_files_in_directory (const std::string& directory_path, vector<string>& result)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2012-06-23 01:06:54 -04:00
|
|
|
if (!Glib::file_test (directory_path, Glib::FILE_TEST_IS_DIR)) return;
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2012-06-23 01:06:54 -04:00
|
|
|
Glib::Dir dir(directory_path);
|
2008-06-02 17:41:35 -04:00
|
|
|
std::copy(dir.begin(), dir.end(), std::back_inserter(result));
|
|
|
|
}
|
|
|
|
catch (Glib::FileError& err)
|
|
|
|
{
|
|
|
|
warning << err.what() << endmsg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-23 01:06:54 -04:00
|
|
|
find_matching_files_in_directory (const std::string& directory,
|
2008-06-02 17:41:35 -04:00
|
|
|
const Glib::PatternSpec& pattern,
|
2012-06-23 01:06:54 -04:00
|
|
|
vector<std::string>& result)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
vector<string> tmp_files;
|
|
|
|
|
|
|
|
get_files_in_directory (directory, tmp_files);
|
2008-09-23 11:55:34 -04:00
|
|
|
result.reserve(tmp_files.size());
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
for (vector<string>::iterator file_iter = tmp_files.begin();
|
|
|
|
file_iter != tmp_files.end();
|
|
|
|
++file_iter)
|
|
|
|
{
|
|
|
|
if (!pattern.match(*file_iter)) continue;
|
|
|
|
|
2012-06-23 01:06:54 -04:00
|
|
|
std::string full_path(directory);
|
|
|
|
full_path = Glib::build_filename (full_path, *file_iter);
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
result.push_back(full_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-23 01:06:54 -04:00
|
|
|
find_matching_files_in_directories (const vector<std::string>& paths,
|
2008-06-02 17:41:35 -04:00
|
|
|
const Glib::PatternSpec& pattern,
|
2012-06-23 01:06:54 -04:00
|
|
|
vector<std::string>& result)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2012-06-23 01:06:54 -04:00
|
|
|
for (vector<std::string>::const_iterator path_iter = paths.begin();
|
2008-06-02 17:41:35 -04:00
|
|
|
path_iter != paths.end();
|
|
|
|
++path_iter)
|
|
|
|
{
|
|
|
|
find_matching_files_in_directory (*path_iter, pattern, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
find_matching_files_in_search_path (const SearchPath& search_path,
|
|
|
|
const Glib::PatternSpec& pattern,
|
2012-06-23 01:06:54 -04:00
|
|
|
vector<std::string>& result)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2008-09-23 11:55:34 -04:00
|
|
|
find_matching_files_in_directories (search_path, pattern, result);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
find_file_in_search_path(const SearchPath& search_path,
|
|
|
|
const string& filename,
|
2012-06-23 01:06:54 -04:00
|
|
|
std::string& result)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2012-06-23 01:06:54 -04:00
|
|
|
vector<std::string> tmp;
|
2008-06-02 17:41:35 -04:00
|
|
|
Glib::PatternSpec tmp_pattern(filename);
|
|
|
|
|
|
|
|
find_matching_files_in_search_path (search_path, tmp_pattern, tmp);
|
|
|
|
|
|
|
|
if (tmp.size() == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (tmp.size() != 1)
|
|
|
|
{
|
|
|
|
info << string_compose
|
|
|
|
(
|
|
|
|
"Found more than one file matching %1 in search path %2",
|
|
|
|
filename,
|
2012-06-23 01:06:54 -04:00
|
|
|
search_path ()
|
2008-06-02 17:41:35 -04:00
|
|
|
)
|
|
|
|
<< endmsg;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
result = tmp.front();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace PBD
|