osdir.hh

Go to the documentation of this file.
00001 /**
00002  * Copyright (C) 2002 Bart Vanhauwaert
00003  *
00004  * Permission to use, copy, modify, distribute and sell this software
00005  * for any purpose is hereby granted without fee. This license
00006  * includes (but is not limited to) standalone compilation or as part
00007  * of a larger project.
00008  *
00009  * This software is provided "as is" without express or implied warranty.
00010  *
00011  * For a full statement on warranty and terms and conditions for
00012  * copying, distribution and modification, please see the comment block
00013  * at the end of this file.
00014  *
00015  * Version 1
00016  *
00017  */
00018 
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 
00160 /**
00161  *
00162  * The "library", below, refers to the collection of software functions
00163  * and/or data contained in this file, prepared so as to be conveniently
00164  * compiled and linked with application programs (which use some of those
00165  * functions and data) to form executables.
00166  *
00167  *                             NO WARRANTY
00168  *
00169  * 1. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
00170  * WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
00171  * EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
00172  * OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
00173  * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
00174  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00175  * PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
00176  * LIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
00177  * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
00178  *
00179  * 2. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
00180  * WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
00181  * AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
00182  * FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
00183  * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
00184  * LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
00185  * RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
00186  * FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
00187  * SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
00188  * DAMAGES.
00189  *
00190  * END OF TERMS AND CONDITIONS
00191  *
00192  */
00193