2
Bear Engine - Editor library
4
Copyright (C) 20052011 Julien Jorge, Sebastien Angibaud
6
This program is free software; you can redistribute it and/or modify it
7
under the terms of the GNU General Public License as published by the
8
Free Software Foundation; either version 2 of the License, or (at your
9
option) any later version.
11
This program is distributed in the hope that it will be useful, but WITHOUT
12
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16
You should have received a copy of the GNU General Public License along
17
with this program; if not, write to the Free Software Foundation, Inc.,
18
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
contact: plee-the-bear@gamned.org
22
Please add the tag [Bear] in the subject of your mails.
25
* \file bf/impl/scan_dir.tpp
26
* \brief Implementation of the bf::scan_dir class.
27
* \author Julien Jorge
30
#include <boost/filesystem/path.hpp>
31
#include <boost/filesystem/convenience.hpp>
34
/*----------------------------------------------------------------------------*/
36
* \brief Read all item files from a given directory and in its subdirectories.
37
* \param dir The path where the files are searched.
38
* \param f A copyable function object called on each file found in the
40
* \param first_ext Iterator on the first valid extension.
41
* \param last_ext Iterator just after the last valid extension.
43
template<typename Func>
44
template<typename Iterator>
45
void bf::scan_dir<Func>::operator()
46
( const std::string& dir, Func& f, Iterator first_ext, Iterator last_ext )
48
std::queue<boost::filesystem::path> pending;
49
boost::filesystem::path path(dir, boost::filesystem::native);
51
if ( !boost::filesystem::exists(path) )
56
while ( !pending.empty() )
58
path = pending.front();
61
boost::filesystem::directory_iterator it(path);
62
boost::filesystem::directory_iterator eit;
64
for ( ; it!=eit; ++it)
65
if ( boost::filesystem::is_directory(*it) )
67
else if (supported_extension( it->string(), first_ext, last_ext ))
70
} // scan_dir::operator()
72
/*----------------------------------------------------------------------------*/
74
* \brief Tell if a path correspond to a supported extension.
75
* \param path The path to check.
76
* \param first_ext Iterator on the first valid extension.
77
* \param last_ext Iterator just after the last valid extension.
79
template<typename Func>
80
template<typename Iterator>
81
bool bf::scan_dir<Func>::supported_extension
82
( const std::string& path, Iterator first_ext, Iterator last_ext )
84
// return true if no extension is given
85
bool result( first_ext == last_ext );
87
for ( ; !result && (first_ext!=last_ext); ++first_ext )
88
if ( path.size() >= first_ext->size() )
89
result = path.rfind(*first_ext) == path.size() - first_ext->size();
92
} // scan_dir::supported_extension()