osdir.h

Go to the documentation of this file.
00001 #ifndef OSLINK_OSDIR_HEADER_H_
00002 #define OSLINK_OSDIR_HEADER_H_
00003 
00004 #if defined(unix) || defined(__unix) || defined(__unix__)
00005 #define OSLINK_OSDIR_POSIX
00006 #elif defined(_WIN32)
00007 #define OSLINK_OSDIR_WINDOWS
00008 #else
00009 #define OSLINK_OSDIR_NOTSUPPORTED
00010 #endif
00011 
00012 #include <string>
00013 
00014 #if defined(OSLINK_OSDIR_NOTSUPPORTED)
00015 
00016 namespace oslink
00017 {
00018   class directory
00019   {
00020   public:
00021     directory(const std::string&)               { }
00022     operator void*() const                              { return (void*)0; }
00023     std::string next()                                  { return ""; }
00024   };
00025 }
00026 
00027 #elif defined(OSLINK_OSDIR_POSIX)
00028 
00029 #include <sys/types.h>
00030 #include <dirent.h>
00031 
00032 namespace oslink
00033 {
00034   class directory
00035   {
00036   public:
00037     directory(const std::string& aName)
00038       : handle(opendir(aName.c_str())), willfail(false)
00039     {
00040       if (!handle)
00041         willfail = true;
00042       else
00043       {
00044         dirent* entry = readdir(handle);
00045         if (entry)
00046           current = entry->d_name;
00047         else
00048           willfail = true;
00049       }
00050     }
00051     ~directory()
00052     {
00053       if (handle)
00054         closedir(handle);
00055     }
00056     operator void*() const
00057     {
00058       return willfail ? (void*)0:(void*)(-1);
00059     }
00060     std::string next()
00061     {
00062       std::string prev(current);
00063       dirent* entry = readdir(handle);
00064       if (entry)
00065         current = entry->d_name;
00066       else
00067         willfail = true;
00068       return prev;
00069     }
00070   private:
00071     DIR* handle;
00072     bool willfail;
00073     std::string current;
00074   };
00075 }
00076 
00077 #elif defined(OSLINK_OSDIR_WINDOWS)
00078 
00079 #include <windows.h>
00080 #include <winbase.h>
00081 
00082 namespace oslink
00083 {
00084   class directory
00085   {
00086   public:
00087     directory(const std::string& aName)
00088       : handle(INVALID_HANDLE_VALUE), willfail(false)
00089     {
00090       // First check the attributes trying to access a non-directory with 
00091       // FindFirstFile takes ages
00092       DWORD attrs = GetFileAttributes(aName.c_str());
00093       if ( (attrs == 0xFFFFFFFF) || ((attrs && FILE_ATTRIBUTE_DIRECTORY) == 0) )
00094       {
00095         willfail = true;
00096         return;
00097       }
00098       std::string Full(aName);
00099       // Circumvent a problem in FindFirstFile with c:\\* as parameter 
00100       if ( (Full.length() > 0) && (Full[Full.length()-1] != '\\') )
00101         Full += "\\";
00102       WIN32_FIND_DATA entry;
00103       handle = FindFirstFile( (Full+"*").c_str(), &entry);
00104       if (handle == INVALID_HANDLE_VALUE)
00105         willfail = true;
00106       else
00107         current = entry.cFileName;
00108     }
00109     ~directory()
00110     {
00111       if (handle != INVALID_HANDLE_VALUE)
00112         FindClose(handle);
00113     }
00114 
00115     operator void*() const                              
00116     {
00117       return willfail ? (void*)0:(void*)(-1);
00118     }
00119     std::string next()
00120     {
00121       std::string prev = current;
00122       WIN32_FIND_DATA entry;
00123       int ok = FindNextFile(handle, &entry);
00124       if (!ok)
00125         willfail = true;
00126       else
00127         current = entry.cFileName;
00128       return current; 
00129     }
00130   private:
00131     HANDLE      handle;
00132     bool        willfail;
00133     std::string current;
00134   };
00135 }
00136 
00137 
00138 #endif
00139 
00140 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by  doxygen 1.6.2