Drizzled Public API Documentation

cached_directory.h

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00028 #pragma once
00029 
00030 #include <dirent.h>
00031 
00032 #include <iostream>
00033 #include <vector>
00034 #include <set>
00035 #include <string>
00036 #include <cstdlib>
00037 #include <cerrno>
00038 
00039 namespace drizzled
00040 {
00041 
00048 class CachedDirectory
00049 {
00050 public:
00051   enum FILTER {
00052     NONE,
00053     DIRECTORY,
00054     FILE,
00055     MAX
00056   };
00057 
00058   class Entry
00059   {
00060     Entry();
00061   public:
00062     std::string filename;
00063     explicit Entry(std::string in_name)
00064       : filename(in_name)
00065     {}
00066   };
00067   typedef std::vector<Entry *> Entries;
00071   CachedDirectory();
00072       
00079   CachedDirectory(const std::string& in_path); 
00080 
00087   CachedDirectory(const std::string& in_path, std::set<std::string>& allowed_exts);
00088   CachedDirectory(const std::string& in_path, enum CachedDirectory::FILTER filter, bool use_full_path= false);
00089 
00093   ~CachedDirectory();
00094 
00098   inline bool fail() const 
00099   {
00100     return error != 0;
00101   }
00102 
00107   inline int getError() const
00108   {
00109     return error;
00110   }
00111 
00115   inline const char *getPath() const
00116   {
00117     return path.c_str();
00118   }
00119 
00126   inline const Entries &getEntries()
00127   {
00128     return entries;
00129   }
00130 private:
00131   std::string path; 
00132   int error; 
00133   bool use_full_path;
00134   Entries entries; 
00135 
00143   bool open(const std::string &in_path);
00144 
00155   bool open(const std::string &in_path, std::set<std::string> &allowable_exts);
00156   bool open(const std::string &in_path, std::set<std::string> &allowed_exts, enum CachedDirectory::FILTER filter);
00157 
00158   friend std::ostream& operator<<(std::ostream& output, CachedDirectory &directory)
00159   {
00160     output << "CachedDirectory:(Path: " << directory.getPath() << ")\n";
00161 
00162     CachedDirectory::Entries files= directory.getEntries();
00163 
00164     for (CachedDirectory::Entries::iterator fileIter= files.begin();
00165          fileIter != files.end(); fileIter++)
00166     {
00167       CachedDirectory::Entry *entry= *fileIter;
00168       output << "\t(" << entry->filename << ")\n";
00169     }
00170 
00171     return output;  // for multiple << operators.
00172   }
00173 
00174 };
00175 
00176 } /* namespace drizzled */
00177