| 1 | /* | 
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 | Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 | This program is free software; you can redistribute it and/or modify | 
|---|
| 7 | it under the terms of the GNU General Public License as published by | 
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 | any later version. | 
|---|
| 10 |  | 
|---|
| 11 | ### File Specific: | 
|---|
| 12 | main-programmer: Benjamin Grauer | 
|---|
| 13 | co-programmer: ... | 
|---|
| 14 |  | 
|---|
| 15 | Borrowed the Main code from 'Bart Vanhauwaert' (license below) | 
|---|
| 16 | and adopted it to my likings. | 
|---|
| 17 | */ | 
|---|
| 18 |  | 
|---|
| 19 | /** | 
|---|
| 20 | * Copyright (C) 2002 Bart Vanhauwaert | 
|---|
| 21 | * | 
|---|
| 22 | * Permission to use, copy, modify, distribute and sell this software | 
|---|
| 23 | * for any purpose is hereby granted without fee. This license | 
|---|
| 24 | * includes (but is not limited to) standalone compilation or as part | 
|---|
| 25 | * of a larger project. | 
|---|
| 26 | * | 
|---|
| 27 | * This software is provided "as is" without express or implied warranty. | 
|---|
| 28 | * | 
|---|
| 29 | * For a full statement on warranty and terms and conditions for | 
|---|
| 30 | * copying, distribution and modification, please see the comment block | 
|---|
| 31 | * at the end of this file. | 
|---|
| 32 | * | 
|---|
| 33 | * Version 1 | 
|---|
| 34 | * | 
|---|
| 35 | */ | 
|---|
| 36 |  | 
|---|
| 37 | #include "directory.h" | 
|---|
| 38 |  | 
|---|
| 39 | #if not defined (__WIN32__) | 
|---|
| 40 | #include <sys/stat.h> | 
|---|
| 41 | #include <sys/types.h> | 
|---|
| 42 | #endif | 
|---|
| 43 | Directory::Directory(const std::string& directoryName) | 
|---|
| 44 | : File(directoryName), willfail(false) | 
|---|
| 45 | { | 
|---|
| 46 | this->handle = 0; | 
|---|
| 47 | this->willfail = true; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | Directory::~Directory() | 
|---|
| 51 | { | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | bool Directory::open() | 
|---|
| 55 | { | 
|---|
| 56 | #if not defined(__WIN32__) | 
|---|
| 57 | if (this->handle != NULL) | 
|---|
| 58 | this->close(); | 
|---|
| 59 | this->handle = opendir(this->name().c_str()); | 
|---|
| 60 | if (!handle) | 
|---|
| 61 | willfail = true; | 
|---|
| 62 | else | 
|---|
| 63 | { | 
|---|
| 64 | willfail = false; | 
|---|
| 65 | dirent* entry = readdir(handle); | 
|---|
| 66 | if (entry) | 
|---|
| 67 | current = entry->d_name; | 
|---|
| 68 | else | 
|---|
| 69 | willfail = true; | 
|---|
| 70 | } | 
|---|
| 71 | #else | 
|---|
| 72 | if (handle != INVALID_HANDLE_VALUE) | 
|---|
| 73 | this->close(); | 
|---|
| 74 |  | 
|---|
| 75 | // First check the attributes trying to access a non-Directory with | 
|---|
| 76 | // FindFirstFile takes ages | 
|---|
| 77 | 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 parameter | 
|---|
| 85 | 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 | else | 
|---|
| 95 | { | 
|---|
| 96 | willfail = false; | 
|---|
| 97 | current = entry.cFileName; | 
|---|
| 98 | return true; | 
|---|
| 99 | } | 
|---|
| 100 | #endif | 
|---|
| 101 |  | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | bool Directory::close() | 
|---|
| 105 | { | 
|---|
| 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 = ""; | 
|---|
| 117 | return true; | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 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 | 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 | bool Directory::create() | 
|---|
| 147 | { | 
|---|
| 148 | #if not defined (__WIN32__) | 
|---|
| 149 | return (!mkdir(this->name().c_str(), 0777)); | 
|---|
| 150 | #else | 
|---|
| 151 | return (!CreateDirectory(this->name().c_str(), NULL)); | 
|---|
| 152 | #endif | 
|---|
| 153 | } | 
|---|