[7422] | 1 | /** |
---|
| 2 | * Copyright (C) 2002 Bart Vanhauwaert |
---|
| 3 | * |
---|
| 4 | * Permission to use, copy, modify, distribute and sell this software |
---|
| 5 | * for any purpose is hereby granted without fee. This license |
---|
| 6 | * includes (but is not limited to) standalone compilation or as part |
---|
| 7 | * of a larger project. |
---|
| 8 | * |
---|
| 9 | * This software is provided "as is" without express or implied warranty. |
---|
| 10 | * |
---|
| 11 | * For a full statement on warranty and terms and conditions for |
---|
| 12 | * copying, distribution and modification, please see the comment block |
---|
| 13 | * at the end of this file. |
---|
| 14 | * |
---|
| 15 | * Version 1 |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #ifndef OSLINK_OSDIR_HEADER_H_ |
---|
| 20 | #define OSLINK_OSDIR_HEADER_H_ |
---|
| 21 | |
---|
| 22 | #if defined(unix) || defined(__unix) || defined(__unix__) |
---|
| 23 | #define OSLINK_OSDIR_POSIX |
---|
| 24 | #elif defined(_WIN32) |
---|
| 25 | #define OSLINK_OSDIR_WINDOWS |
---|
| 26 | #else |
---|
| 27 | #define OSLINK_OSDIR_NOTSUPPORTED |
---|
| 28 | #endif |
---|
| 29 | |
---|
| 30 | #include <string> |
---|
| 31 | |
---|
| 32 | #if defined(OSLINK_OSDIR_NOTSUPPORTED) |
---|
| 33 | |
---|
| 34 | namespace OS |
---|
| 35 | { |
---|
| 36 | class Directory |
---|
| 37 | { |
---|
| 38 | public: |
---|
| 39 | Directory(const std::string&) { } |
---|
| 40 | bool open(const std::string&) {}; |
---|
| 41 | void close() {}; |
---|
| 42 | |
---|
| 43 | operator void*() const { return (void*)0; } |
---|
| 44 | std::string next() { return ""; } |
---|
| 45 | }; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | #elif defined(OSLINK_OSDIR_POSIX) |
---|
| 49 | |
---|
| 50 | #include <sys/types.h> |
---|
| 51 | #include <dirent.h> |
---|
| 52 | |
---|
| 53 | namespace OS |
---|
| 54 | { |
---|
| 55 | class Directory |
---|
| 56 | { |
---|
| 57 | public: |
---|
| 58 | Directory(const std::string& directoryName = "") |
---|
| 59 | : willfail(false) |
---|
| 60 | { |
---|
| 61 | this->handle = NULL; |
---|
| 62 | this->willfail = true; |
---|
| 63 | this->open(directoryName); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | ~Directory() |
---|
| 67 | { |
---|
| 68 | this->close(); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | bool open(const std::string& directoryName) |
---|
| 72 | { |
---|
| 73 | if (this->handle != NULL) |
---|
| 74 | this->close(); |
---|
| 75 | this->handle = opendir(directoryName.c_str()); |
---|
| 76 | if (!handle) |
---|
| 77 | willfail = true; |
---|
| 78 | else |
---|
| 79 | { |
---|
| 80 | willfail = false; |
---|
| 81 | dirent* entry = readdir(handle); |
---|
| 82 | if (entry) |
---|
| 83 | current = entry->d_name; |
---|
| 84 | else |
---|
| 85 | willfail = true; |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | void close() |
---|
| 90 | { |
---|
| 91 | if (this->handle != NULL) |
---|
| 92 | closedir(handle); |
---|
| 93 | this->handle = NULL; |
---|
| 94 | this->willfail = true; |
---|
| 95 | this->current = ""; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | operator void*() const |
---|
| 99 | { |
---|
| 100 | return willfail ? (void*)0:(void*)(-1); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | std::string next() |
---|
| 104 | { |
---|
| 105 | std::string prev(current); |
---|
| 106 | dirent* entry = readdir(handle); |
---|
| 107 | if (entry) |
---|
| 108 | current = entry->d_name; |
---|
| 109 | else |
---|
| 110 | willfail = true; |
---|
| 111 | return prev; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | private: |
---|
| 115 | DIR* handle; |
---|
| 116 | bool willfail; |
---|
| 117 | std::string current; |
---|
| 118 | }; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | #elif defined(OSLINK_OSDIR_WINDOWS) |
---|
| 122 | |
---|
| 123 | #include <windows.h> |
---|
| 124 | #include <winbase.h> |
---|
| 125 | |
---|
| 126 | namespace OS |
---|
| 127 | { |
---|
| 128 | class Directory |
---|
| 129 | { |
---|
| 130 | public: |
---|
| 131 | Directory(const std::string& aName) |
---|
| 132 | : handle(INVALID_HANDLE_VALUE), willfail(false) |
---|
| 133 | { |
---|
| 134 | // First check the attributes trying to access a non-Directory with |
---|
| 135 | // FindFirstFile takes ages |
---|
| 136 | DWORD attrs = GetFileAttributes(aName.c_str()); |
---|
| 137 | if ( (attrs == 0xFFFFFFFF) || ((attrs && FILE_ATTRIBUTE_DIRECTORY) == 0) ) |
---|
| 138 | { |
---|
| 139 | willfail = true; |
---|
| 140 | return; |
---|
| 141 | } |
---|
| 142 | std::string Full(aName); |
---|
| 143 | // Circumvent a problem in FindFirstFile with c:\\* as parameter |
---|
| 144 | if ( (Full.length() > 0) && (Full[Full.length()-1] != '\\') ) |
---|
| 145 | Full += "\\"; |
---|
| 146 | WIN32_FIND_DATA entry; |
---|
| 147 | handle = FindFirstFile( (Full+"*").c_str(), &entry); |
---|
| 148 | if (handle == INVALID_HANDLE_VALUE) |
---|
| 149 | willfail = true; |
---|
| 150 | else |
---|
| 151 | current = entry.cFileName; |
---|
| 152 | } |
---|
| 153 | ~Directory() |
---|
| 154 | { |
---|
| 155 | if (handle != INVALID_HANDLE_VALUE) |
---|
| 156 | FindClose(handle); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | operator void*() const |
---|
| 160 | { |
---|
| 161 | return willfail ? (void*)0:(void*)(-1); |
---|
| 162 | } |
---|
| 163 | std::string next() |
---|
| 164 | { |
---|
| 165 | std::string prev = current; |
---|
| 166 | WIN32_FIND_DATA entry; |
---|
| 167 | int ok = FindNextFile(handle, &entry); |
---|
| 168 | if (!ok) |
---|
| 169 | willfail = true; |
---|
| 170 | else |
---|
| 171 | current = entry.cFileName; |
---|
| 172 | return current; |
---|
| 173 | } |
---|
| 174 | private: |
---|
| 175 | HANDLE handle; |
---|
| 176 | bool willfail; |
---|
| 177 | std::string current; |
---|
| 178 | }; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | #endif |
---|
| 183 | |
---|
| 184 | #endif |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | * |
---|
| 188 | * The "library", above, refers to the collection of software functions |
---|
| 189 | * and/or data contained in this file, prepared so as to be conveniently |
---|
| 190 | * compiled and linked with application programs (which use some of those |
---|
| 191 | * functions and data) to form executables. |
---|
| 192 | * |
---|
| 193 | * NO WARRANTY |
---|
| 194 | * |
---|
| 195 | * 1. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO |
---|
| 196 | * WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. |
---|
| 197 | * EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR |
---|
| 198 | * OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY |
---|
| 199 | * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 200 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
---|
| 201 | * PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE |
---|
| 202 | * LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME |
---|
| 203 | * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. |
---|
| 204 | * |
---|
| 205 | * 2. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN |
---|
| 206 | * WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY |
---|
| 207 | * AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU |
---|
| 208 | * FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR |
---|
| 209 | * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE |
---|
| 210 | * LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING |
---|
| 211 | * RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A |
---|
| 212 | * FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF |
---|
| 213 | * SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
---|
| 214 | * DAMAGES. |
---|
| 215 | * |
---|
| 216 | * END OF TERMS AND CONDITIONS |
---|
| 217 | * |
---|
| 218 | */ |
---|
| 219 | |
---|