cgma
CubitDirIterator.cpp
Go to the documentation of this file.
00001 //- Class:          CubitDirIterator
00002 //- Description:    Class with functions to get files from a directory, etc.
00003 
00004 #include "CubitDirIterator.hpp"
00005 #include "CubitString.hpp"
00006 
00007 #ifdef _WIN32
00008   #include <sys/types.h>
00009   #include <sys/stat.h>
00010   #include <windows.h>
00011 #else
00012   #include <sys/types.h>
00013   #include <sys/stat.h>
00014   #include <dirent.h>
00015   #include <cstdlib>
00016   #include <sys/param.h>
00017   #include <unistd.h>
00018   #include <pwd.h>
00019   #include <fnmatch.h>
00020 #endif
00021 #include <errno.h>
00022 #include <string.h>
00023 
00024 struct CubitDirIterator::Helper
00025 {
00026   Helper()
00027   {
00028 #ifdef _WIN32
00029     mFileHandle = INVALID_HANDLE_VALUE;
00030 #else
00031     mDirHandle = NULL;
00032     mFileHandle = NULL;
00033 #endif
00034   }
00035 
00036 #ifdef _WIN32
00037   WIN32_FIND_DATAW mDirHandle;
00038   HANDLE mFileHandle;
00039 #else
00040   DIR* mDirHandle;
00041   dirent* mFileHandle;
00042 #endif
00043 };
00044   
00045 
00046 CubitDirIterator::CubitDirIterator(const CubitString& path, const CubitString& pattern)
00047 {
00048   this->mHelper = new Helper;
00049   open(path, pattern);
00050 }
00051 
00052 CubitDirIterator::~CubitDirIterator()
00053 {
00054   cleanup();
00055   delete this->mHelper;
00056 }
00057 
00058 void CubitDirIterator::open(const CubitString& path, const CubitString& pattern)
00059 {
00060   cleanup();
00061 
00062   if(pattern.length())
00063     mPattern = pattern;
00064   else
00065     mPattern = "";
00066 
00067   this->atEnd = false;
00068 #ifdef _WIN32
00069   CubitString p = path;
00070   if (p.get_at(p.length()-1) != '\\')
00071     p += "\\";
00072   p += mPattern.length() == 0 ? "*" : mPattern;
00073   this->mHelper->mFileHandle = FindFirstFileW(CubitString::toUtf16(p).c_str(), &this->mHelper->mDirHandle);
00074   if(this->mHelper->mFileHandle == INVALID_HANDLE_VALUE)
00075     this->atEnd = true;
00076 #else
00077   this->mHelper->mDirHandle = opendir(path.c_str());
00078   if(this->mHelper->mDirHandle)
00079   {
00080     do
00081     {
00082       this->mHelper->mFileHandle = readdir(this->mHelper->mDirHandle);
00083     } while (this->mHelper->mFileHandle && 
00084              (mPattern.length() != 0 && 
00085              fnmatch(mPattern.c_str(), this->mHelper->mFileHandle->d_name, 0))
00086              );
00087   }
00088   if(!this->mHelper->mDirHandle || !this->mHelper->mFileHandle)
00089     this->atEnd = true;
00090 #endif
00091 }
00092 
00093 void CubitDirIterator::cleanup()
00094 {
00095 #ifdef _WIN32
00096   if(this->mHelper->mFileHandle != 0)
00097     FindClose(this->mHelper->mFileHandle);
00098   this->mHelper->mFileHandle = 0;
00099 #else
00100   if(this->mHelper->mDirHandle)
00101     closedir(this->mHelper->mDirHandle);
00102   this->mHelper->mFileHandle = 0;
00103   this->mHelper->mDirHandle = 0;
00104 #endif
00105 }
00106 
00107 bool CubitDirIterator::has_next()
00108 {
00109   return !this->atEnd;
00110 }
00111 
00112 CubitString CubitDirIterator::next()
00113 {
00114   CubitString file;
00115   if(this->atEnd)
00116     return file;
00117 
00118 #ifdef _WIN32
00119   file = CubitString::toUtf8(this->mHelper->mDirHandle.cFileName);
00120   BOOL result = FindNextFileW(this->mHelper->mFileHandle, &this->mHelper->mDirHandle);
00121   if(result == 0)
00122     this->atEnd = true;
00123 #else
00124   file = this->mHelper->mFileHandle->d_name;
00125   do
00126   {
00127     this->mHelper->mFileHandle = readdir(this->mHelper->mDirHandle);
00128   } while (this->mHelper->mFileHandle && 
00129            (mPattern.length() != 0 && 
00130            fnmatch(mPattern.c_str(), this->mHelper->mFileHandle->d_name, 0))
00131            );
00132   if(this->mHelper->mFileHandle == 0)
00133     this->atEnd = true;
00134 #endif
00135   return file;
00136 }
00137 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines