Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

directory.cpp

Go to the documentation of this file.
00001 // directory.h
00002 // 
00003 // Copyright (C) 2003, Chris Laurel <claurel@shatters.net>
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 
00010 #include <iostream>
00011 #include "directory.h"
00012 
00013 using namespace std;
00014 
00015 
00016 bool Directory::enumFiles(EnumFilesHandler& handler, bool deep)
00017 {
00018     string filename;
00019 
00020     while (nextFile(filename))
00021     {
00022         // Skip all files beginning with a period, most importantly, . and ..
00023         if (filename[0] == '.')
00024             continue;
00025 
00026         // TODO: optimize this to avoid allocating so many strings
00027         string pathname = handler.getPath() + string("/") + filename;
00028         if (IsDirectory(pathname))
00029         {
00030             if (deep)
00031             {
00032                 Directory* dir = OpenDirectory(pathname);
00033                 bool cont = true;
00034 
00035                 if (dir != NULL)
00036                 {
00037                     handler.pushDir(filename);
00038                     cont = dir->enumFiles(handler, deep);
00039                     handler.popDir();
00040                     delete dir;
00041                 }
00042 
00043                 if (!cont)
00044                     return false;
00045             }
00046         }
00047         else
00048         {
00049             if (!handler.process(filename))
00050                 return false;
00051         }
00052     }
00053 
00054     return true;
00055 }
00056 
00057 
00058 EnumFilesHandler::EnumFilesHandler()
00059 {
00060 }
00061 
00062 
00063 void EnumFilesHandler::pushDir(const std::string& dirName)
00064 {
00065     if (dirStack.size() > 0)
00066         dirStack.push_back(dirStack.back() + string("/") + dirName);
00067     else
00068         dirStack.push_back(dirName);
00069 }
00070 
00071 
00072 void EnumFilesHandler::popDir()
00073 {
00074     dirStack.pop_back();
00075 }
00076 
00077 
00078 const string& EnumFilesHandler::getPath() const
00079 {
00080     // need this so we can return a non-temporary value:
00081     static const string emptyString("");
00082 
00083     if (dirStack.size() > 0)
00084         return dirStack.back();
00085     else
00086         return emptyString;
00087 }

Generated on Sat Jan 14 22:30:32 2006 for Celestia by  doxygen 1.4.1