Changeset 8276 in orxonox.OLD for trunk/src/lib/util/directory.cc
- Timestamp:
- Jun 8, 2006, 5:21:29 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/util/directory.cc
r7661 r8276 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 50 /** 51 * @brief Constructs a Directory handler. 52 * @param directoryName the name of the Directory to access. 53 */ 43 54 Directory::Directory(const std::string& directoryName) 44 : File(directoryName), willfail(false)55 : File(directoryName) 45 56 { 46 this->handle = 0; 47 this->willfail = true; 57 this->_opened = false; 48 58 } 49 59 60 /** 61 * @brief destructs the Directory. 62 */ 50 63 Directory::~Directory() 51 64 { 65 this->close(); 52 66 } 53 67 68 69 /** 70 * @brief openes the Directory 71 * @returns true on success, false on error. (does not exist, no rights -> test with functions of File) 72 * 73 * Fills the List of Files, and sets the Directory to open state 74 */ 54 75 bool Directory::open() 55 76 { 77 if (this->_opened) 78 this->close(); 79 80 // Openes the Directory for reading: 56 81 #if not defined(__WIN32__) 57 if (this->handle != NULL) 58 this->close(); 59 this->handle = opendir(this->name().c_str()); 82 DIR* handle; 83 handle = opendir(this->name().c_str()); 60 84 if (!handle) 61 willfail = true; 85 { 86 std::cerr << "could not open directory " << this->name() << " for reading" << std::endl; 87 return false; 88 } 89 #else 90 HANDLE handle; 91 92 // First check the attributes trying to access a non-Directory with 93 // FindFirstFile takes ages 94 DWORD attrs = GetFileAttributes(this->name().c_str()); 95 if ( (attrs == 0xFFFFFFFF) || ((attrs && FILE_ATTRIBUTE_DIRECTORY) == 0) ) 96 { 97 return false; 98 } 99 std::string Full(this->name()); 100 // Circumvent a problem in FindFirstFile with c:\\* as parameter 101 if ( (Full.length() > 0) && (Full[Full.length()-1] != '\\') ) 102 Full += "\\"; 103 WIN32_FIND_DATA entry; 104 handle = FindFirstFile( (Full+"*").c_str(), &entry); 105 if (handle == INVALID_HANDLE_VALUE) 106 { 107 std::cerr << "could not open directory " << this->name() << " for reading" << std::endl; 108 return false; 109 } 62 110 else 63 111 { 64 willfail = false; 65 dirent* entry = readdir(handle); 66 if (entry) 67 current = entry->d_name; 68 else 69 willfail = true; 112 this->_fileNames.push_back(entry.cFileName); 70 113 } 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 114 #endif 101 115 116 // BUILDING the list of contained Files. (only the names) 117 #if not defined(__WIN32__) 118 dirent* entry; 119 while ((entry = readdir(handle)) != NULL) 120 this->_fileNames.push_back(entry->d_name); 121 closedir(handle); 122 #else 123 WIN32_FIND_DATA entry; 124 while ((int ok = FindNextFile(handle, &entry)) != 0) 125 this->_fileNames.push_back(entry.cFileName); 126 FindClose(handle); 127 #endif 128 this->_opened = true; 129 return true; 102 130 } 103 131 132 /** 133 * @brief closes the directory 134 * @returns true. 135 * 136 * Clears the List of Files in the Directory. 137 */ 104 138 bool Directory::close() 105 139 { 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 = ""; 140 this->_opened = false; 141 this->_fileNames.clear(); 117 142 return true; 118 143 } 119 144 120 145 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 else 130 willfail = true; 131 return prev; 132 133 #else 134 std::string prev = current; 135 WIN32_FIND_DATA entry; 136 int ok = FindNextFile(handle, &entry); 137 if (!ok) 138 willfail = true; 139 else 140 current = entry.cFileName; 141 return current; 142 #endif 143 } 144 145 146 /** 147 * @brief creates the directory 148 * @returns true on success, false on error 149 */ 146 150 bool Directory::create() 147 151 {
Note: See TracChangeset
for help on using the changeset viewer.