DCL 4.0
Loading...
Searching...
No Matches
Dir.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2
3#if __DCL_WINDOWS
4 #include <windows.h>
5#else
6 #include <errno.h>
7 #include <stdlib.h> // alloca
8 #include <string.h> // memset
9 #include <dcl/_string.h>
10 #include <dcl/_stat.h>
11#endif
12
13#include <dcl/Charset.h>
14#include <dcl/Dir.h>
15
16#include "__strumbs.h"
17
18#if __DCL_HAVE_ALLOC_DEBUG
19#undef __DCL_ALLOC_LEVEL
20#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
21#endif
22
23#if __DCL_DEBUG
24#undef __THIS_FILE__
25static const char_t __THIS_FILE__[] = __T("dcl/Dir.cpp");
26#endif
27
28__DCL_BEGIN_NAMESPACE
29
30#ifndef INVALID_HANDLE_VALUE
31#define INVALID_HANDLE_VALUE ((DIR*)-1)
32#endif
33
35
36Dir::Entry::Entry()
37{
38 memset(this, 0,
39#if __DCL_WINDOWS
40 sizeof(WIN32_FIND_DATAW)
41#else
42 sizeof(struct dirent)
43#endif
44 );
45}
46
47String Dir::Entry::name() const
48 __DCL_THROWS1(CharsetConvertException*)
49{
50#if __DCL_WINDOWS
51 return String(this->cFileName);
52#else
53 return __mbstostr(this->d_name);
54#endif
55}
56
57String Dir::Entry::toString() const
58 __DCL_THROWS1(CharsetConvertException*)
59{
60 StringBuilder r = name();
61
62 if (isDir() || isLink()) {
63 r +=__T(" [");
64 if (isDir())
65 r += __T("directory");
66 if (isLink()) {
67 if (isDir())
68 r += __T(", ");
69 r += __T("symbolic link");
70 }
71 r += __T(']');
72 }
73 return r;
74}
75
76Dir::~Dir()
77{
79 try {
80 close();
81 }
82 catch (Exception* e) {
83 __DCL_TRACE1(__T("Dir::close Error! %ls\n"), e->toString().data());
84 e->destroy();
85 }
86 }
87}
88
89Dir::Dir(const String& _path) __DCL_THROWS1(IOException*)
90{
91 __DCL_ASSERT(!_path.isEmpty());
92
93 // 모든 OS에서 경로 구분자는 '/'를 사용한다.
94 String path = _path.replace(__T('\\'), __T('/'));
95 if (!path.endsWith(__T('/')))
96 path = path + __T("/");
97
98#if __DCL_WINDOWS
99 WIN32_FIND_DATAW findData;
100 HANDLE h = FindFirstFileW(path + __T('*'), &findData);
101 if (h == INVALID_HANDLE_VALUE)
102 throw new IOException(_path, GetLastError());
103 FindClose(h);
104 if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
105 throw new IOException(_path, ERROR_DIRECTORY);
106#else
107 struct stat st;
108 if (__stat(path, &st) == -1)
109 throw new IOException(path, errno);
110
111 if (!S_ISDIR(st.st_mode))
112 throw new IOException(path, ENOTDIR);
113#endif
114
116 __path = path;
117 rewind();
118}
119
120void Dir::close() __DCL_THROWS1(IOException*)
121{
123 HandleType handle = __handle;
125
126#if __DCL_WINDOWS
127 if (!FindClose(handle))
128 throw new IOException(__path, GetLastError());
129#else
130 if (closedir(handle))
131 throw new IOException(__path, errno);
132#endif
133}
134
135void Dir::rewind() __DCL_THROWS1(IOException*)
136{
137#if __DCL_WINDOWS
139 close();
140#else
142 rewinddir(__handle);
143 return;
144 }
145
146 STRTOMBS(__path, path)
147 else {
148 throw new IOException(__path, EILSEQ);
149 }
150
151 DIR* handle = opendir(path);
152 if (handle == NULL) {
153 throw new IOException(__path, errno);
154 }
155 __handle = handle;
156#endif
157}
158
159bool Dir::read(Dir::Entry& _buf) __DCL_THROWS1(IOException*)
160{
161#if __DCL_WINDOWS
163 HANDLE h = FindFirstFileW(__path + __T('*'), &_buf);
164 if (h == INVALID_HANDLE_VALUE)
165 throw new IOException(__path, GetLastError());
166 __handle = h;
167 }
168 else {
169 if (!FindNextFileW(__handle, &_buf)) {
170 if (GetLastError() == ERROR_NO_MORE_FILES)
171 return false;
172 else
173 throw new IOException(__path, GetLastError());
174 }
175 }
176 return true;
177#else
179 errno = 0;
180 struct dirent* result = readdir(__handle);
181 if (result != NULL) {
182 memcpy((void*) &_buf, result, sizeof(struct dirent));
183 }
184 else if (errno != 0 ) {
185 throw new IOException(__path, errno);
186 }
187 return result != NULL; // EOF
188#endif
189}
190
191__DCL_END_NAMESPACE
__DCL_BEGIN_NAMESPACE String __mbstostr(const char *_mbs, size_t _nmbs) __DCL_THROWS1(CharsetConvertException *)
Definition __strumbs.cpp:8
#define STRTOMBS(str, mbs)
Definition __strumbs.h:25
DCLCAPI int __stat(const String &_path, struct stat *_buf)
Definition _stat.cpp:21
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:340
wchar_t char_t
Definition Config.h:275
#define __DCL_THROWS1(e)
Definition Config.h:167
#define INVALID_HANDLE_VALUE
Definition Dir.cpp:31
#define __DCL_TRACE1(fmt, arg1)
Definition Object.h:376
#define __DCL_ASSERT(expr)
Definition Object.h:371
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
Definition Object.h:228
#define __T(str)
Definition Object.h:44
ByteString r
return result
Definition Dir.h:38
String __path
Definition Dir.h:141
HandleType __handle
Definition Dir.h:140
virtual String toString() const
Definition Exception.cpp:40
virtual void destroy()
Definition Exception.cpp:74