| 1 | /* |
| 2 | * Copyright (C) 2001,2006 Dizzy |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | #ifndef INCLUDED_PVPGN_PDIR |
| 19 | #define INCLUDED_PVPGN_PDIR |
| 20 | |
| 21 | #include <string> |
| 22 | #include <stdexcept> |
| 23 | |
| 24 | #ifdef HAVE_DIRENT_H |
| 25 | # include <dirent.h> |
| 26 | #else |
| 27 | # ifdef HAVE_SYS_NDIR_H |
| 28 | # include <sys/ndir.h> |
| 29 | # endif |
| 30 | # if HAVE_SYS_DIR_H |
| 31 | # include <sys/dir.h> |
| 32 | # endif |
| 33 | # if HAVE_NDIR_H |
| 34 | # include <ndir.h> |
| 35 | # endif |
| 36 | # define dirent direct |
| 37 | #endif |
| 38 | #ifdef WIN32 |
| 39 | # include <io.h> /* for _findfirst(), _findnext(), etc */ |
| 40 | #endif |
| 41 | |
| 42 | #include <vector> |
| 43 | |
| 44 | namespace pvpgn |
| 45 | { |
| 46 | |
| 47 | class Directory |
| 48 | { |
| 49 | public: |
| 50 | class OpenError : public std::runtime_error |
| 51 | { |
| 52 | public: |
| 53 | OpenError(const std::string& path) |
| 54 | :std::runtime_error("Error opening directory: " + path) {} |
| 55 | ~OpenError() throw() {} |
| 56 | }; |
| 57 | |
| 58 | explicit Directory(const std::string& fname, bool lazyread = false); |
| 59 | ~Directory() throw(); |
| 60 | void rewind(); |
| 61 | char const * read() const; |
| 62 | void open(const std::string& fname, bool lazyread = false); |
| 63 | operator bool() const; |
| 64 | |
| 65 | private: |
| 66 | std::string path; |
| 67 | bool lazyread; |
| 68 | #ifdef WIN32 |
| 69 | long lFindHandle; |
| 70 | mutable struct _finddata_t fileinfo; |
| 71 | mutable int status; /* -1 == failure, 0 == freshly opened, 1 == opened and read, 2 == eof */ |
| 72 | #else /* POSIX */ |
| 73 | DIR * dir; |
| 74 | #endif |
| 75 | |
| 76 | /* do not allow copying for the time being */ |
| 77 | Directory(const Directory&); |
| 78 | Directory& operator=(const Directory&); |
| 79 | void close(); |
| 80 | |
| 81 | }; |
| 82 | |
| 83 | extern std::vector<std::string> dir_getfiles(const char * directory, const char* ext, bool recursive); |
| 84 | |
| 85 | } |
| 86 | |
| 87 | #endif |
| 88 |