cleanup and comments

This commit is contained in:
dockes 2006-01-17 09:31:10 +00:00
parent d9acca162b
commit 56e1daac50
3 changed files with 54 additions and 28 deletions

View File

@ -1,5 +1,5 @@
#ifndef lint #ifndef lint
static char rcsid[] = "@(#$Id: indexer.cpp,v 1.21 2006-01-09 16:53:31 dockes Exp $ (C) 2004 J.F.Dockes"; static char rcsid[] = "@(#$Id: indexer.cpp,v 1.22 2006-01-17 09:31:05 dockes Exp $ (C) 2004 J.F.Dockes";
#endif #endif
#include <stdio.h> #include <stdio.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -46,7 +46,7 @@ DbIndexer::~DbIndexer() {
db.close(); db.close();
} }
// Index each directory in the topdirs for a given db
bool DbIndexer::indexDb(bool resetbefore, list<string> *topdirs) bool DbIndexer::indexDb(bool resetbefore, list<string> *topdirs)
{ {
if (!init(resetbefore)) if (!init(resetbefore))
@ -63,17 +63,12 @@ bool DbIndexer::indexDb(bool resetbefore, list<string> *topdirs)
// Set up skipped patterns for this subtree. This probably should be // Set up skipped patterns for this subtree. This probably should be
// done in the directory change code in processone() instead. // done in the directory change code in processone() instead.
{ walker.clearSkippedNames();
walker.clearSkippedNames(); string skipped;
string skipped; if (config->getConfParam("skippedNames", skipped)) {
if (config->getConfParam("skippedNames", skipped)) { list<string> skpl;
list<string> skpl; stringToStrings(skipped, skpl);
stringToStrings(skipped, skpl); walker.setSkippedNames(skpl);
list<string>::const_iterator it;
for (it = skpl.begin(); it != skpl.end(); it++) {
walker.addSkippedName(*it);
}
}
} }
// Walk the directory tree // Walk the directory tree

View File

@ -1,5 +1,5 @@
#ifndef lint #ifndef lint
static char rcsid[] = "@(#$Id: fstreewalk.cpp,v 1.6 2005-12-13 12:42:59 dockes Exp $ (C) 2004 J.F.Dockes"; static char rcsid[] = "@(#$Id: fstreewalk.cpp,v 1.7 2006-01-17 09:31:05 dockes Exp $ (C) 2004 J.F.Dockes";
#endif #endif
#ifndef TEST_FSTREEWALK #ifndef TEST_FSTREEWALK
@ -63,7 +63,11 @@ bool FsTreeWalker::addSkippedName(const string& pattern)
data->skippedNames.push_back(pattern); data->skippedNames.push_back(pattern);
return true; return true;
} }
bool FsTreeWalker::setSkippedNames(const list<string> &patterns)
{
data->skippedNames = patterns;
return true;
}
void FsTreeWalker::clearSkippedNames() void FsTreeWalker::clearSkippedNames()
{ {
data->skippedNames.clear(); data->skippedNames.clear();
@ -195,13 +199,20 @@ class myCB : public FsTreeWalkerCB {
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
if (argc != 2) { if (argc < 2) {
cerr << "Usage: fstreewalk <topdir>" << endl; cerr << "Usage: fstreewalk <topdir> [ignpat [ignpat] ...]" << endl;
exit(1); exit(1);
} }
argv++;argc--;
string topdir = *argv++;argc--;
list<string> ignpats;
while (argc > 0) {
ignpats.push_back(*argv++);argc--;
}
FsTreeWalker walker; FsTreeWalker walker;
walker.setSkippedNames(ignpats);
myCB cb; myCB cb;
walker.walk(argv[1], cb); walker.walk(topdir, cb);
if (walker.getErrCnt() > 0) if (walker.getErrCnt() > 0)
cout << walker.getReason(); cout << walker.getReason();
} }

View File

@ -1,15 +1,25 @@
#ifndef _FSTREEWALK_H_INCLUDED_ #ifndef _FSTREEWALK_H_INCLUDED_
#define _FSTREEWALK_H_INCLUDED_ #define _FSTREEWALK_H_INCLUDED_
/* @(#$Id: fstreewalk.h,v 1.3 2005-04-04 13:18:47 dockes Exp $ (C) 2004 J.F.Dockes */ /* @(#$Id: fstreewalk.h,v 1.4 2006-01-17 09:31:10 dockes Exp $ (C) 2004 J.F.Dockes */
#include <string> #include <string>
#include <list>
#ifndef NO_NAMESPACES #ifndef NO_NAMESPACES
using std::string; using std::string;
using std::list;
#endif #endif
class FsTreeWalkerCB; class FsTreeWalkerCB;
/**
* Class implementing a unix directory recursive walk.
*
* A user-defined function object is called for every file or
* directory. Patterns to be ignored can be set before starting the
* walk. Options control whether we follow symlinks and whether we recurse
* on subdirectories.
*/
class FsTreeWalker { class FsTreeWalker {
public: public:
enum CbFlag {FtwRegular, FtwDirEnter, FtwDirReturn}; enum CbFlag {FtwRegular, FtwDirEnter, FtwDirReturn};
@ -19,16 +29,26 @@ class FsTreeWalker {
FsTreeWalker(Options opts = FtwOptNone); FsTreeWalker(Options opts = FtwOptNone);
~FsTreeWalker(); ~FsTreeWalker();
Status walk(const std::string &dir, FsTreeWalkerCB& cb); /**
std::string getReason(); * Begin file system walk.
* @param dir is not checked against the ignored patterns (this is
* a feature and must not change.
* @param cb the function object that will be called back for every
* file-system object (called both at entry and exit for directories).
*/
Status walk(const string &dir, FsTreeWalkerCB& cb);
/** Get explanation for error */
string getReason();
int getErrCnt(); int getErrCnt();
bool addSkippedName(const std::string &pattern); // Add a pattern /**
// for directory * Add a pattern to the list of things (file or dir) to be ignored
// entries (file * (ie: #* , *~)
// or dir) to be */
// ignored (ie: bool addSkippedName(const string &pattern);
// #* , *~) /** Set the ignored patterns list */
void clearSkippedNames(); // Clear all patterns bool setSkippedNames(const list<string> &patlist);
/** Clear the ignored patterns list */
void clearSkippedNames();
private: private:
class Internal; class Internal;