Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/lib/util/osdir.h @ 7914

Last change on this file since 7914 was 7433, checked in by bensch, 19 years ago

orxonox/trunk: Working on Windows too :)

File size: 6.0 KB
Line 
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
34namespace 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
53namespace 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#ifdef DATADIR
124#undef DATADIR
125#endif
126
127#include <windows.h>
128#include <winbase.h>
129
130namespace OS
131{
132  class Directory
133  {
134    public:
135      Directory(const std::string& directoryName = "")
136          : handle(INVALID_HANDLE_VALUE), willfail(true)
137      {
138        this->open(directoryName);
139      }
140
141      ~Directory()
142      {
143        this->close();
144      }
145
146      bool open(const std::string& directoryName)
147      {
148        if (handle != INVALID_HANDLE_VALUE)
149          this->close();
150
151        // First check the attributes trying to access a non-Directory with
152        // FindFirstFile takes ages
153        DWORD attrs = GetFileAttributes(directoryName.c_str());
154        if ( (attrs == 0xFFFFFFFF) || ((attrs && FILE_ATTRIBUTE_DIRECTORY) == 0) )
155        {
156          willfail = true;
157          return false;
158        }
159        std::string Full(directoryName);
160        // Circumvent a problem in FindFirstFile with c:\\* as parameter
161        if ( (Full.length() > 0) && (Full[Full.length()-1] != '\\') )
162          Full += "\\";
163        WIN32_FIND_DATA entry;
164        handle = FindFirstFile( (Full+"*").c_str(), &entry);
165        if (handle == INVALID_HANDLE_VALUE)
166          {
167            willfail = true;
168            return false;
169          }
170        else
171          {
172            willfail = false;
173            current = entry.cFileName;
174            return true;
175          }
176      }
177
178      void close()
179      {
180        if (handle != INVALID_HANDLE_VALUE)
181          FindClose(handle);
182        this->willfail = true;
183      }
184
185      operator void*() const
186      {
187        return willfail ? (void*)0:(void*)(-1);
188      }
189
190      std::string next()
191      {
192        std::string prev = current;
193        WIN32_FIND_DATA entry;
194        int ok = FindNextFile(handle, &entry);
195        if (!ok)
196          willfail = true;
197        else
198          current = entry.cFileName;
199        return current;
200      }
201
202
203    private:
204      HANDLE    handle;
205      bool      willfail;
206      std::string current;
207  };
208}
209
210
211#endif
212
213#endif
214
215/**
216 *
217 * The "library", above, refers to the collection of software functions
218 * and/or data contained in this file, prepared so as to be conveniently
219 * compiled and linked with application programs (which use some of those
220 * functions and data) to form executables.
221 *
222 *                             NO WARRANTY
223 *
224 * 1. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
225 * WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
226 * EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
227 * OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
228 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
229 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
230 * PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
231 * LIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
232 * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
233 *
234 * 2. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
235 * WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
236 * AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
237 * FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
238 * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
239 * LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
240 * RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
241 * FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
242 * SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
243 * DAMAGES.
244 *
245 * END OF TERMS AND CONDITIONS
246 *
247 */
248
Note: See TracBrowser for help on using the repository browser.