wsdlpull svntrunk
Loading...
Searching...
No Matches
osdir.h
Go to the documentation of this file.
1#ifndef OSLINK_OSDIR_HEADER_H_
2#define OSLINK_OSDIR_HEADER_H_
3
4#if defined(unix) || defined(__unix) || defined(__unix__)
5#define OSLINK_OSDIR_POSIX
6#elif defined(_WIN32)
7#define OSLINK_OSDIR_WINDOWS
8#else
9#define OSLINK_OSDIR_NOTSUPPORTED
10#endif
11
12#include <string>
13
14#if defined(OSLINK_OSDIR_NOTSUPPORTED)
15
16namespace oslink
17{
19 {
20 public:
21 directory(const std::string&) { }
22 operator void*() const { return (void*)0; }
23 std::string next() { return ""; }
24 };
25}
26
27#elif defined(OSLINK_OSDIR_POSIX)
28
29#include <sys/types.h>
30#include <dirent.h>
31
32namespace oslink
33{
34 class directory
35 {
36 public:
37 directory(const std::string& aName)
38 : handle(opendir(aName.c_str())), willfail(false)
39 {
40 if (!handle)
41 willfail = true;
42 else
43 {
44 dirent* entry = readdir(handle);
45 if (entry)
46 current = entry->d_name;
47 else
48 willfail = true;
49 }
50 }
51 ~directory()
52 {
53 if (handle)
54 closedir(handle);
55 }
56 operator void*() const
57 {
58 return willfail ? (void*)0:(void*)(-1);
59 }
60 std::string next()
61 {
62 std::string prev(current);
63 dirent* entry = readdir(handle);
64 if (entry)
65 current = entry->d_name;
66 else
67 willfail = true;
68 return prev;
69 }
70 private:
71 DIR* handle;
72 bool willfail;
73 std::string current;
74 };
75}
76
77#elif defined(OSLINK_OSDIR_WINDOWS)
78
79#include <windows.h>
80#include <winbase.h>
81
82namespace oslink
83{
84 class directory
85 {
86 public:
87 directory(const std::string& aName)
88 : handle(INVALID_HANDLE_VALUE), willfail(false)
89 {
90 // First check the attributes trying to access a non-directory with
91 // FindFirstFile takes ages
92 DWORD attrs = GetFileAttributes(aName.c_str());
93 if ( (attrs == 0xFFFFFFFF) || ((attrs && FILE_ATTRIBUTE_DIRECTORY) == 0) )
94 {
95 willfail = true;
96 return;
97 }
98 std::string Full(aName);
99 // Circumvent a problem in FindFirstFile with c:\\* as parameter
100 if ( (Full.length() > 0) && (Full[Full.length()-1] != '\\') )
101 Full += "\\";
102 WIN32_FIND_DATA entry;
103 handle = FindFirstFile( (Full+"*").c_str(), &entry);
104 if (handle == INVALID_HANDLE_VALUE)
105 willfail = true;
106 else
107 current = entry.cFileName;
108 }
109 ~directory()
110 {
111 if (handle != INVALID_HANDLE_VALUE)
112 FindClose(handle);
113 }
114
115 operator void*() const
116 {
117 return willfail ? (void*)0:(void*)(-1);
118 }
119 std::string next()
120 {
121 std::string prev = current;
122 WIN32_FIND_DATA entry;
123 int ok = FindNextFile(handle, &entry);
124 if (!ok)
125 willfail = true;
126 else
127 current = entry.cFileName;
128 return current;
129 }
130 private:
131 HANDLE handle;
132 bool willfail;
133 std::string current;
134 };
135}
136
137
138#endif
139
140#endif