-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdirectorylisting.h
executable file
·145 lines (114 loc) · 2.89 KB
/
directorylisting.h
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
/*
* Tamgu (탐구)
*
* Copyright 2019-present NAVER Corp.
* under BSD 3-clause
*/
/* --- CONTENTS ---
Project : Tamgu (탐구)
Version : See tamgu.cxx for the version number
filename : directorylisting.h
Date : 2017/09/01
Purpose : listing the content of directory
Programmer : Claude ROUX (claude.roux@naverlabs.com)
Reviewer :
*/
#ifndef directorylisting_h
#define directorylisting_h
#ifdef WIN32
#include <windows.h>
#include <winbase.h>
#else
#include <sys/types.h>
#include <dirent.h>
#endif
class directorylisting {
public:
string current;
#ifdef WIN32
HANDLE thedirectory;
WIN32_FIND_DATAA dir;
#else
DIR* thedirectory;
dirent* dir;
#endif
directorylisting(string n) : current(n) {}
#ifdef WIN32
bool initial() {
thedirectory = INVALID_HANDLE_VALUE;
DWORD attribs = GetFileAttributesA(STR(current));
if (attribs == 0xFFFFFFFF || !(attribs && FILE_ATTRIBUTE_DIRECTORY))
return false;
string fullname(current);
if (fullname.size() > 0 && fullname.back() != '\\')
fullname += "\\";
fullname += "*";
thedirectory = FindFirstFileA(STR(fullname), &dir);
if (thedirectory == INVALID_HANDLE_VALUE)
return false;
current = dir.cFileName;
return true;
}
~directorylisting() {
if (thedirectory != INVALID_HANDLE_VALUE)
FindClose(thedirectory);
}
bool getnext(string& name) {
if (current == "*")
return false;
name = current;
if (FindNextFileA(thedirectory, &dir))
current = dir.cFileName;
else
current = "*";
return true;
}
bool getnext() {
if (current == "*")
return false;
if (FindNextFileA(thedirectory, &dir))
current = dir.cFileName;
else
current = "*";
return true;
}
#else
bool initial() {
thedirectory = opendir(STR(current));
if (!thedirectory)
return false;
dir = readdir(thedirectory);
if (dir) {
current = dir->d_name;
return true;
}
return false;
}
~directorylisting() {
if (thedirectory)
closedir(thedirectory);
}
bool getnext(string& name) {
if (current == "*")
return false;
name = current;
dir = readdir(thedirectory);
if (dir)
current = dir->d_name;
else
current="*";
return true;
}
bool getnext() {
if (current == "*")
return false;
dir = readdir(thedirectory);
if (dir)
current = dir->d_name;
else
current="*";
return true;
}
#endif
};
#endif