1 mike 1.1 //BEGIN_LICENSE
2 //
3 // Copyright (c) 2000 The Open Group, BMC Software, Tivoli Systems, IBM
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18 // DEALINGS IN THE SOFTWARE.
19 //
20 //END_LICENSE
21 //BEGIN_HISTORY
22 mike 1.1 //
23 // Author:
24 //
25 // $Log: template_cpp,v $
26 // Revision 1.1.1.1 2001/01/14 19:50:23 mike
27 // Pegasus import
28 //
29 //
30 //END_HISTORY
31
32 #include "Destroyer.h"
33 #include "Dir.h"
34 #include "Exception.h"
35
36 PEGASUS_NAMESPACE_BEGIN
37
38 #ifdef PEGASUS_OS_TYPE_WINDOWS
39 # include <io.h>
40 # include <direct.h>
41
42 struct DirRep
43 mike 1.1 {
44 long file;
45 struct _finddata_t findData;
46 };
47
48 Dir::Dir(const String& path)
49 {
50 Destroyer<char> p(strcat(path.allocateCString(2), "/*"));
51 _rep = new DirRep;
52 _rep->file = _findfirst(p.getPointer(), &_rep->findData);
53
54 if (_rep->file == -1)
55 {
56 _more = false;
57 throw CannotOpenDirectory(path);
58 }
59 else
60 _more = true;
61 }
62
63 Dir::~Dir()
64 mike 1.1 {
65 if (_rep->file != -1)
66 _findclose(_rep->file);
67
68 delete _rep;
69 }
70
71 const char* Dir::getName() const
72 {
73 return _rep->findData.name;
74 }
75
76 void Dir::next()
77 {
78 if (!_more)
79 return;
80
81 _more = _findnext(_rep->file, &_rep->findData) == 0;
82 }
83
84 #else /* PEGASUS_OS_TYPE_WINDOWS */
85 mike 1.1 # include <dirent.h>
86
87 // Clone the string to a plain old C-String and null out
88 // trailing slash (if any).
89
90 static char* _clonePath(const String& path)
91 {
92 char* p = path.allocateCString();
93
94 if (!*p)
95 return p;
96
97 char* last = p + path.getLength() - 1;
98
99 if (*last == '/')
100 *last = '\0';
101
102 return p;
103 }
104
105 struct DirRep
106 mike 1.1 {
107 DIR* dir;
108 struct dirent* entry;
109 };
110
111 Dir::Dir(const String& path)
112 {
113 Destroyer<char> p(_clonePath(path));
114 _rep = new DirRep;
115 _rep->dir = opendir(p.getPointer());
116
117 if (_rep->dir)
118 {
119 _rep->entry = readdir(_rep->dir);
120 _more = _rep->entry != NULL;
121 }
122 else
123 {
124 _more = false;
125 throw CannotOpenDirectory(path);
126 }
127 mike 1.1 }
128
129 Dir::~Dir()
130 {
131 if (_rep->dir)
132 closedir(_rep->dir);
133
134 delete _rep;
135 }
136
137 const char* Dir::getName() const
138 {
139 return _more ? _rep->entry->d_name : "";
140 }
141
142 void Dir::next()
143 {
144 if (_more)
145 {
146 _rep->entry = readdir(_rep->dir);
147 _more = _rep->entry != NULL;
148 mike 1.1 }
149 }
150
151 #endif /* !PEGASUS_OS_TYPE_WINDOWS */
152
153 PEGASUS_NAMESPACE_END
|