Changeset 8224 in orxonox.OLD for branches/gui/src/lib/util
- Timestamp:
- Jun 8, 2006, 10:10:51 AM (19 years ago)
- Location:
- branches/gui/src/lib/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/gui/src/lib/util/directory.cc
r7661 r8224 38 38 39 39 #if not defined (__WIN32__) 40 #include <sys/stat.h> 41 #include <sys/types.h> 40 #include <sys/types.h> 41 #include <sys/stat.h> 42 #include <dirent.h> 43 #else 44 #include <windows.h> 45 #include <winbase.h> 42 46 #endif 47 48 #include <iostream> 49 43 50 Directory::Directory(const std::string& directoryName) 44 : File(directoryName), willfail(false)51 : File(directoryName) 45 52 { 46 this->handle = 0; 47 this->willfail = true; 53 this->_opened = false; 48 54 } 49 55 50 56 Directory::~Directory() 51 57 { 58 this->close(); 52 59 } 60 53 61 54 62 bool Directory::open() 55 63 { 64 if (this->_opened) 65 this->close(); 66 67 // Openes the Directory for reading: 56 68 #if not defined(__WIN32__) 57 if (this->handle != NULL) 69 DIR* handle; 70 if (handle != NULL) 58 71 this->close(); 59 this->handle = opendir(this->name().c_str());72 handle = opendir(this->name().c_str()); 60 73 if (!handle) 61 willfail = true; 74 { 75 std::cerr << "could not open directory " << this->name() << " for reading" << std::endl; 76 return false; 77 } 78 #else 79 HANDLE handle; 80 if (handle != INVALID_HANDLE_VALUE) 81 this->close(); 82 83 // First check the attributes trying to access a non-Directory with 84 // FindFirstFile takes ages 85 DWORD attrs = GetFileAttributes(this->name().c_str()); 86 if ( (attrs == 0xFFFFFFFF) || ((attrs && FILE_ATTRIBUTE_DIRECTORY) == 0) ) 87 { 88 return false; 89 } 90 std::string Full(this->name()); 91 // Circumvent a problem in FindFirstFile with c:\\* as parameter 92 if ( (Full.length() > 0) && (Full[Full.length()-1] != '\\') ) 93 Full += "\\"; 94 WIN32_FIND_DATA entry; 95 handle = FindFirstFile( (Full+"*").c_str(), &entry); 96 if (handle == INVALID_HANDLE_VALUE) 97 { 98 std::cerr << "could not open directory " << this->name() << " for reading" << std::endl; 99 return false; 100 } 62 101 else 63 102 { 64 willfail = false; 65 dirent* entry = readdir(handle); 66 if (entry) 67 current = entry->d_name; 68 else 69 willfail = true; 103 this->_fileNames.push_back(entry.cFileName); 70 104 } 71 #else72 if (handle != INVALID_HANDLE_VALUE)73 this->close();74 75 // First check the attributes trying to access a non-Directory with76 // FindFirstFile takes ages77 DWORD attrs = GetFileAttributes(this->name().c_str());78 if ( (attrs == 0xFFFFFFFF) || ((attrs && FILE_ATTRIBUTE_DIRECTORY) == 0) )79 {80 willfail = true;81 return false;82 }83 std::string Full(this->name());84 // Circumvent a problem in FindFirstFile with c:\\* as parameter85 if ( (Full.length() > 0) && (Full[Full.length()-1] != '\\') )86 Full += "\\";87 WIN32_FIND_DATA entry;88 handle = FindFirstFile( (Full+"*").c_str(), &entry);89 if (handle == INVALID_HANDLE_VALUE)90 {91 willfail = true;92 return false;93 }94 else95 {96 willfail = false;97 current = entry.cFileName;98 return true;99 }100 105 #endif 101 106 107 // BUILDING the list of contained Files. (only the names) 108 #if not defined(__WIN32__) 109 while (dirent* entry = readdir(handle)) 110 this->_fileNames.push_back(entry->d_name); 111 closedir(handle); 112 #else 113 WIN32_FIND_DATA entry; 114 while (int ok = FindNextFile(handle, &entry)) 115 this->_fileNames.push_back(entry.cFileName); 116 FindClose(handle); 117 #endif 118 this->_opened = true; 119 return true; 102 120 } 103 121 104 122 bool Directory::close() 105 123 { 106 #if not defined(__WIN32__) 107 if (this->handle != NULL) 108 closedir(handle); 109 this->handle = NULL; 110 #else 111 if (handle != INVALID_HANDLE_VALUE) 112 FindClose(handle); 113 handle = 0; 114 #endif 115 this->willfail = true; 116 this->current = ""; 124 this->_opened = false; 125 this->_fileNames.clear(); 117 126 return true; 118 127 } 119 128 120 121 122 std::string Directory::next()123 {124 #if not defined(__WIN32__)125 std::string prev(current);126 dirent* entry = readdir(handle);127 if (entry)128 current = entry->d_name;129 else130 willfail = true;131 return prev;132 133 #else134 std::string prev = current;135 WIN32_FIND_DATA entry;136 int ok = FindNextFile(handle, &entry);137 if (!ok)138 willfail = true;139 else140 current = entry.cFileName;141 return current;142 #endif143 }144 129 145 130 -
branches/gui/src/lib/util/directory.h
r7661 r8224 27 27 28 28 #include "file.h" 29 30 #if not defined (__WIN32__) 31 #include <sys/types.h> 32 #include <dirent.h> 33 #else 34 #include <windows.h> 35 #include <winbase.h> 36 #endif 29 #include <vector> 37 30 38 31 class Directory : public File … … 44 37 virtual bool open(); 45 38 virtual bool close(); 46 operator void*() const { return willfail ? (void*)0:(void*)(-1); }47 48 std::string next();49 39 50 40 bool create(); 51 41 42 /** @returns the FileNames contained inside of the Directory */ 43 const std::vector<std::string>& fileNames() const { return this->_fileNames; }; 44 const std::string& operator[](unsigned int fileNumber) const { return this->_fileNames[fileNumber]; }; 45 unsigned int fileCount() const { return _fileNames.size(); }; 46 47 std::string fileNameInDir(unsigned int fileNumber) const { return this->name() + "/" + _fileNames[fileNumber]; }; 48 49 File getFile(unsigned int fileNumber) const { return File(fileNameInDir(fileNumber)); }; 50 52 51 private: 53 #if not defined(__WIN32__) 54 DIR* handle; 55 #else 56 HANDLE handle; 57 #endif 58 bool willfail; 59 std::string current; 52 bool _opened; 53 std::vector<std::string> _fileNames; 60 54 }; 61 55
Note: See TracChangeset
for help on using the changeset viewer.