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

osdir.h

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

Generated on Mon Dec 12 15:20:26 2005 for CCube by  doxygen 1.4.1