2012-05-02 16:46:10 +04:00
|
|
|
/*============================================================================
|
|
|
|
KWSys - Kitware System Library
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
|
|
|
|
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
|
|
|
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
|
|
|
#ifndef @KWSYS_NAMESPACE@_Glob_hxx
|
|
|
|
#define @KWSYS_NAMESPACE@_Glob_hxx
|
|
|
|
|
|
|
|
#include <@KWSYS_NAMESPACE@/Configure.h>
|
|
|
|
#include <@KWSYS_NAMESPACE@/Configure.hxx>
|
|
|
|
|
2015-08-28 21:50:49 +03:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2012-05-02 16:46:10 +04:00
|
|
|
|
|
|
|
namespace @KWSYS_NAMESPACE@
|
|
|
|
{
|
|
|
|
|
|
|
|
class GlobInternals;
|
|
|
|
|
|
|
|
/** \class Glob
|
|
|
|
* \brief Portable globbing searches.
|
|
|
|
*
|
|
|
|
* Globbing expressions are much simpler than regular
|
|
|
|
* expressions. This class will search for files using
|
|
|
|
* globbing expressions.
|
|
|
|
*
|
|
|
|
* Finds all files that match a given globbing expression.
|
|
|
|
*/
|
|
|
|
class @KWSYS_NAMESPACE@_EXPORT Glob
|
|
|
|
{
|
2015-03-03 16:50:09 +03:00
|
|
|
public:
|
|
|
|
enum MessageType
|
|
|
|
{
|
|
|
|
error,
|
|
|
|
cyclicRecursion
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Message
|
|
|
|
{
|
|
|
|
MessageType type;
|
2015-08-28 21:50:49 +03:00
|
|
|
std::string content;
|
2015-03-03 16:50:09 +03:00
|
|
|
|
2015-08-28 21:50:49 +03:00
|
|
|
Message(MessageType t, const std::string& c) :
|
2015-03-03 16:50:09 +03:00
|
|
|
type(t),
|
|
|
|
content(c)
|
|
|
|
{}
|
|
|
|
Message(const Message& msg) :
|
|
|
|
type(msg.type),
|
|
|
|
content(msg.content)
|
|
|
|
{}
|
2015-05-04 17:27:45 +03:00
|
|
|
Message& operator=(Message const& msg)
|
|
|
|
{
|
|
|
|
this->type = msg.type;
|
|
|
|
this->content = msg.content;
|
|
|
|
return *this;
|
|
|
|
}
|
2015-03-03 16:50:09 +03:00
|
|
|
};
|
|
|
|
|
2015-08-28 21:50:49 +03:00
|
|
|
typedef std::vector<Message> GlobMessages;
|
|
|
|
typedef std::vector<Message>::iterator GlobMessagesIterator;
|
2012-05-02 16:46:10 +04:00
|
|
|
public:
|
|
|
|
Glob();
|
|
|
|
~Glob();
|
|
|
|
|
|
|
|
//! Find all files that match the pattern.
|
2015-08-28 21:50:49 +03:00
|
|
|
bool FindFiles(const std::string& inexpr,
|
2015-03-03 16:50:09 +03:00
|
|
|
GlobMessages* messages = 0);
|
2012-05-02 16:46:10 +04:00
|
|
|
|
|
|
|
//! Return the list of files that matched.
|
2015-08-28 21:50:49 +03:00
|
|
|
std::vector<std::string>& GetFiles();
|
2012-05-02 16:46:10 +04:00
|
|
|
|
|
|
|
//! Set recurse to true to match subdirectories.
|
|
|
|
void RecurseOn() { this->SetRecurse(true); }
|
|
|
|
void RecurseOff() { this->SetRecurse(false); }
|
|
|
|
void SetRecurse(bool i) { this->Recurse = i; }
|
|
|
|
bool GetRecurse() { return this->Recurse; }
|
|
|
|
|
|
|
|
//! Set recurse through symlinks to true if recursion should traverse the
|
|
|
|
// linked-to directories
|
|
|
|
void RecurseThroughSymlinksOn() { this->SetRecurseThroughSymlinks(true); }
|
|
|
|
void RecurseThroughSymlinksOff() { this->SetRecurseThroughSymlinks(false); }
|
|
|
|
void SetRecurseThroughSymlinks(bool i) { this->RecurseThroughSymlinks = i; }
|
|
|
|
bool GetRecurseThroughSymlinks() { return this->RecurseThroughSymlinks; }
|
|
|
|
|
|
|
|
//! Get the number of symlinks followed through recursion
|
|
|
|
unsigned int GetFollowedSymlinkCount() { return this->FollowedSymlinkCount; }
|
|
|
|
|
|
|
|
//! Set relative to true to only show relative path to files.
|
|
|
|
void SetRelative(const char* dir);
|
|
|
|
const char* GetRelative();
|
|
|
|
|
|
|
|
/** Convert the given globbing pattern to a regular expression.
|
|
|
|
There is no way to quote meta-characters. The
|
|
|
|
require_whole_string argument specifies whether the regex is
|
|
|
|
automatically surrounded by "^" and "$" to match the whole
|
|
|
|
string. This is on by default because patterns always match
|
|
|
|
whole strings, but may be disabled to support concatenating
|
|
|
|
expressions more easily (regex1|regex2|etc). */
|
2015-08-28 21:50:49 +03:00
|
|
|
static std::string PatternToRegex(const std::string& pattern,
|
2012-05-02 16:46:10 +04:00
|
|
|
bool require_whole_string = true,
|
|
|
|
bool preserve_case = false);
|
|
|
|
|
2015-03-03 16:50:09 +03:00
|
|
|
/** Getters and setters for enabling and disabling directory
|
|
|
|
listing in recursive and non recursive globbing mode.
|
|
|
|
If listing is enabled in recursive mode it also lists
|
|
|
|
directory symbolic links even if follow symlinks is enabled. */
|
|
|
|
void SetListDirs(bool list) { this->ListDirs=list; }
|
|
|
|
bool GetListDirs() const { return this->ListDirs; }
|
|
|
|
void SetRecurseListDirs(bool list) { this->RecurseListDirs=list; }
|
|
|
|
bool GetRecurseListDirs() const { return this->RecurseListDirs; }
|
|
|
|
|
2012-05-02 16:46:10 +04:00
|
|
|
protected:
|
|
|
|
//! Process directory
|
2015-08-28 21:50:49 +03:00
|
|
|
void ProcessDirectory(std::string::size_type start,
|
|
|
|
const std::string& dir,
|
2015-03-03 16:50:09 +03:00
|
|
|
GlobMessages* messages);
|
2012-05-02 16:46:10 +04:00
|
|
|
|
|
|
|
//! Process last directory, but only when recurse flags is on. That is
|
|
|
|
// effectively like saying: /path/to/file/**/file
|
2015-08-28 21:50:49 +03:00
|
|
|
bool RecurseDirectory(std::string::size_type start,
|
|
|
|
const std::string& dir,
|
2015-03-03 16:50:09 +03:00
|
|
|
GlobMessages* messages);
|
2012-05-02 16:46:10 +04:00
|
|
|
|
|
|
|
//! Add regular expression
|
2015-08-28 21:50:49 +03:00
|
|
|
void AddExpression(const std::string& expr);
|
2012-05-02 16:46:10 +04:00
|
|
|
|
|
|
|
//! Add a file to the list
|
2015-08-28 21:50:49 +03:00
|
|
|
void AddFile(std::vector<std::string>& files, const std::string& file);
|
2012-05-02 16:46:10 +04:00
|
|
|
|
|
|
|
GlobInternals* Internals;
|
|
|
|
bool Recurse;
|
2015-08-28 21:50:49 +03:00
|
|
|
std::string Relative;
|
2012-05-02 16:46:10 +04:00
|
|
|
bool RecurseThroughSymlinks;
|
|
|
|
unsigned int FollowedSymlinkCount;
|
2015-08-28 21:50:49 +03:00
|
|
|
std::vector<std::string> VisitedSymlinks;
|
2015-03-03 16:50:09 +03:00
|
|
|
bool ListDirs;
|
|
|
|
bool RecurseListDirs;
|
2012-05-02 16:46:10 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
Glob(const Glob&); // Not implemented.
|
|
|
|
void operator=(const Glob&); // Not implemented.
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace @KWSYS_NAMESPACE@
|
|
|
|
|
|
|
|
#endif
|