Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/osdir.h @ 7425

Last change on this file since 7425 was 7425, checked in by bensch, 18 years ago

orxonox/trunk: should compile on Windows goin to check this today

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