Commit 149edaec authored by Davis King's avatar Davis King

Added the get_files_in_directory_tree() function.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403031
parent a8ce79c9
......@@ -25,6 +25,7 @@
#include "../dir_nav/dir_nav_kernel_1.cpp"
#include "../dir_nav/dir_nav_kernel_2.cpp"
#include "../dir_nav/dir_nav_extensions.cpp"
#include "../linker/linker_kernel_1.cpp"
#include "../logger/extra_logger_headers.cpp"
#include "../logger/logger_kernel_1.cpp"
......
......@@ -15,6 +15,7 @@
#include "dir_nav/posix.h"
#endif
#include "dir_nav/dir_nav_extensions.h"
#endif // DLIB_DIR_NAv_
// Copyright (C) 2009 Davis E. King (davisking@users.sourceforge.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_DIR_NAV_EXTENSIONs_CPP_
#define DLIB_DIR_NAV_EXTENSIONs_CPP_
#include "dir_nav_extensions.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
namespace implementation_details
{
void get_all_sub_dirs (
const directory& top_of_tree,
unsigned long max_depth,
std::vector<directory>& result,
std::vector<directory>& temp
)
{
if (max_depth > 0)
{
top_of_tree.get_dirs(temp);
const unsigned long start = result.size();
result.insert(result.end(), temp.begin(), temp.end());
const unsigned long end = start + temp.size();
for (unsigned long i = start; i < end; ++i)
{
get_all_sub_dirs(result[i], max_depth-1, result, temp);
}
}
}
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_DIR_NAV_EXTENSIONs_CPP_
// Copyright (C) 2009 Davis E. King (davisking@users.sourceforge.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_DIR_NAV_EXTENSIONs_H_
#define DLIB_DIR_NAV_EXTENSIONs_H_
#include <string>
#include <vector>
#include <algorithm>
#include "dir_nav_extensions_abstract.h"
#include "../dir_nav.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
namespace implementation_details
{
void get_all_sub_dirs (
const directory& top_of_tree,
unsigned long max_depth,
std::vector<directory>& result,
std::vector<directory>& temp
);
}
// ----------------------------------------------------------------------------------------
template <typename T>
const std::vector<file> get_files_in_directory_tree (
const directory& top_of_tree,
const T& add_file,
unsigned long max_depth = 30
)
{
std::vector<file> result, temp;
std::vector<directory> dirs, dirs_temp;
dirs.push_back(top_of_tree);
// get all the directories in the tree first
implementation_details::get_all_sub_dirs(top_of_tree, max_depth, dirs, dirs_temp);
// now just loop over all the directories and pick out the files we want to keep
for (unsigned long d = 0; d < dirs.size(); ++d)
{
dirs[d].get_files(temp);
// pick out the members of temp that we should keep
for (unsigned long i = 0; i < temp.size(); ++i)
{
if (add_file(temp[i]))
result.push_back(temp[i]);
}
}
return result;
}
// ----------------------------------------------------------------------------------------
class match_ending
{
public:
match_ending (
const std::string& ending_
) : ending(ending_) {}
bool operator() (
const file& f
) const
{
// if the ending is bigger than f's name then it obviously doesn't match
if (ending.size() > f.name().size())
return false;
// now check if the actual characters that make up the end of the file name
// matches what is in ending.
return std::equal(ending.begin(), ending.end(), f.name().end()-ending.size());
}
private:
std::string ending;
};
// ----------------------------------------------------------------------------------------
class match_all
{
public:
bool operator() (
const file&
) const { return true; }
};
// ----------------------------------------------------------------------------------------
}
#ifdef NO_MAKEFILE
#include "dir_nav_extensions.cpp"
#endif
#endif // DLIB_DIR_NAV_EXTENSIONs_H_
// Copyright (C) 2009 Davis E. King (davisking@users.sourceforge.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_DIR_NAV_EXTENSIONs_ABSTRACT_
#ifdef DLIB_DIR_NAV_EXTENSIONs_ABSTRACT_
#include <string>
#include <vector>
#include "dir_nav_kernel_abstract.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <typename T>
const std::vector<file> get_files_in_directory_tree (
const directory& top_of_tree,
const T& add_file,
unsigned long max_depth = 30
);
/*!
requires
- add_file must be a function object with the following prototype:
bool add_file (file f);
ensures
- performs a recursive search through the directory top_of_tree and all
its sub-directories (up do the given max depth). All files in these
directories are examined by passing them to add_file() and if it
returns true then they will be included in the returned std::vector<file>
object.
- Note that a max_depth of 0 means that only the files in the directory
top_of_tree will be considered. A depth of 1 means that only files in
top_of_tree and its immediate sub-directories will be considered. And
so on...
!*/
// ----------------------------------------------------------------------------------------
class match_ending
{
/*!
WHAT THIS OBJECT REPRESENTS
This is a simple function object that can be used with the
above get_files_in_directory_tree() function. This object
just looks for files with a certain ending.
!*/
public:
match_ending (
const std::string& ending
);
/*!
ensures
- this object will be a function that checks if a file has a
name that ends with the given ending string.
!*/
bool operator() (
const file& f
) const;
/*!
ensures
- if (the file f has a name that ends with the ending string given
to this object's constructor) then
- returns true
- else
- returns false
!*/
};
// ----------------------------------------------------------------------------------------
class match_all
{
/*!
WHAT THIS OBJECT REPRESENTS
This is a simple function object that can be used with the
above get_files_in_directory_tree() function. This object
matches all files.
!*/
public:
bool operator() (
const file& f
) const;
/*!
ensures
- returns true
(i.e. this function doesn't do anything. It just says it
matches all files no matter what)
!*/
};
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_DIR_NAV_EXTENSIONs_ABSTRACT_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment