-
Notifications
You must be signed in to change notification settings - Fork 9
/
dir_enum.cpp
171 lines (130 loc) · 3.27 KB
/
dir_enum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// AYA version 5
//
// ƒfƒBƒŒƒNƒgƒŠ“à—ñ‹“@CDirEnum
//
#if defined(WIN32) || defined(_WIN32_WCE)
# include "stdafx.h"
#elif defined(POSIX)
# include "posix_utils.h"
#endif
#include <list>
#include <algorithm>
#include "misc.h"
#include "globaldef.h"
#include "dir_enum.h"
#include "ccct.h"
#include "manifest.h"
//////////DEBUG/////////////////////////
#ifdef _WINDOWS
#ifdef _DEBUG
#include <crtdbg.h>
#define new new( _NORMAL_BLOCK, __FILE__, __LINE__)
#endif
#endif
////////////////////////////////////////
CDirEnum::CDirEnum(const yaya::string_t &ep)
{
enumpath = ep;
is_init = false;
}
CDirEnum::~CDirEnum()
{
if ( is_init ) {
#if defined(WIN32)
::FindClose(dh);
#elif defined(POSIX)
closedir(dh);
#endif
}
}
bool CDirEnum::next(CDirEnumEntry &entry)
{
std::string name_a;
yaya::string_t name_w;
bool isdir = false;
bool isUnicode = false;
#if defined(WIN32)
isUnicode = IsUnicodeAware();
#endif
while ( true ) {
if ( ! is_init ) {
#if defined(WIN32)
yaya::string_t tmp_str = enumpath + L"\\*.*";
if ( isUnicode ) {
WIN32_FIND_DATAW w32FindData;
dh = ::FindFirstFileW(tmp_str.c_str(),&w32FindData);
if ( dh == INVALID_HANDLE_VALUE ) { return false; }
is_init = true;
name_w = w32FindData.cFileName;
isdir = (w32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
else {
WIN32_FIND_DATAA w32FindData;
char *s_filestr = Ccct::Ucs2ToMbcs(tmp_str, CHARSET_DEFAULT);
if ( ! s_filestr ) { return false; }
dh = ::FindFirstFileA(s_filestr,&w32FindData);
free(s_filestr);
s_filestr = NULL;
if ( dh == INVALID_HANDLE_VALUE ) { return false; }
is_init = true;
name_a = w32FindData.cFileName;
isdir = (w32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
#elif defined(POSIX)
std::string path = narrow(enumpath);
fix_filepath(path);
dh = opendir(path.c_str());
if ( ! dh ) { return false; }
struct dirent* ent = readdir(dh);
if ( ! ent ) { closedir(dh); return false; }
is_init = true;
name_a = ent->d_name;
isdir = ent->d_type == DT_DIR;
#endif
}
else {
#if defined(WIN32)
if ( isUnicode ) {
WIN32_FIND_DATAW w32FindData;
if ( ::FindNextFileW(dh,&w32FindData) == 0 ) { return false; }
name_w = w32FindData.cFileName;
isdir = (w32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
else {
WIN32_FIND_DATAA w32FindData;
if ( ::FindNextFileA(dh,&w32FindData) == 0 ) { return false; }
name_a = w32FindData.cFileName;
isdir = (w32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
#elif defined(POSIX)
struct dirent* ent = readdir(dh);
if ( ! ent ) { return false; }
name_a = ent->d_name;
isdir = ent->d_type == DT_DIR;
#endif
}
if ( isUnicode ) {
if (name_w != L"." && name_w != L"..") {
break;
}
}
else {
if (name_a != "." && name_a != "..") {
break;
}
}
}
#if defined(WIN32)
if ( isUnicode ) {
entry.name = name_w;
}
else {
Ccct::MbcsToUcs2Buf(entry.name, name_a, CHARSET_DEFAULT);
}
#elif defined(POSIX)
entry.name = widen(name_a);
#endif
entry.isdir = isdir;
return true;
}