Hydra 0.20
hydra.src/hydra/FileIterator.h
00001 
00002 /*
00003  * Copyright (c) 2009 Aleksander B. Demko
00004  * This source code is distributed under the MIT license.
00005  * See the accompanying file LICENSE.MIT.txt for details.
00006  */
00007 
00008 #ifndef __INCLUDED_HYDRA_FILEITERATOR_H__
00009 #define __INCLUDED_HYDRA_FILEITERATOR_H__
00010 
00011 #include <list>
00012 
00013 #include <QString>
00014 #include <QDirIterator>
00015 
00016 #include <hydra/TR1.h>
00017 
00018 namespace hydra
00019 {
00020   /**
00021    * Return the given file as a unique, absolute filename.
00022    *
00023    * @author Aleksander Demko
00024    */ 
00025   QString makeAbsolute(const QString &s);
00026 
00027   /**
00028    * Returns just the filename and extension.
00029    *
00030    * @author Aleksander Demko
00031    */
00032   QString justName(const QString &s);
00033 
00034   /**
00035    * Is this a normal file.
00036    * A normal file doesnt begin with _ . or ,
00037    *
00038    * @author Aleksander Demko
00039    */ 
00040   bool isNormalFile(const QString &justname);
00041 
00042   /**
00043    * Returns true if the given directory is "normal".
00044    * A normal file is one that isn't hidden.
00045    * A hidden file starts with a .
00046    *
00047    * @author Aleksander Demko
00048    */ 
00049   bool isNormalDirectory(const QString &justdirname);
00050 
00051   /**
00052    * Is this an image file.
00053    *
00054    * @author Aleksander Demko
00055    */
00056   bool isImageFile(const QString &justname);
00057 
00058   /**
00059    * Creates a directory.
00060    *
00061    * @author Aleksander Demko
00062    */
00063   bool mkDir(const QString &name);
00064 
00065   class FileIterator;
00066 }
00067 
00068 /**
00069  * This iterateos over a directory and all its subdirectories
00070  * enumeratoring all the files.
00071  *
00072  * @author Aleksander Demko
00073  */
00074 class hydra::FileIterator
00075 {
00076   public:
00077     /**
00078      * Construtor. The initial dir (which can also just be a file)
00079      * is supplied.
00080      *
00081      * Unless allfiles is true, then hidden directories (.*) will
00082      * be skippeded.
00083      *
00084      * If recurse is true, then all subdirectories will be examined too.
00085      *
00086      * @author Aleksander Demko
00087      */
00088     FileIterator(const QString &fileOrDir, bool allfiles = false, bool recurse = true);
00089 
00090     /**
00091      * Is this iterator traversing a directory set? If false, then the
00092      * iterator was only fed one file.
00093      *
00094      * @author Aleksander Demko
00095      */ 
00096     bool isTraversing(void) const { return dm_istraversing; }
00097 
00098     /**
00099      * Is there another file in the iteration?
00100      *
00101      * @author Aleksander Demko
00102      */
00103     bool hasNext(void) const { return !dm_current.isEmpty(); }
00104 
00105     /**
00106      * Returns the next file in the iteration.
00107      * Note that all the files will be returned, so you will
00108      * need to filter further if you only want files of a certain
00109      * type, etc.
00110      *
00111      * @return the next filename, which will always be a full, absolute filename
00112      *
00113      * @author Aleksander Demko
00114      */
00115     QString next(void);
00116 
00117   private:
00118     void loadNextFile(void);
00119 
00120   private:
00121     QString dm_current;
00122     bool dm_allfiles;
00123     bool dm_istraversing;
00124 
00125     std::auto_ptr<QDirIterator> dm_iterator;
00126 };
00127 
00128 #endif
00129 
 All Classes Namespaces Functions Variables