-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDirEntries.h
144 lines (112 loc) · 2.29 KB
/
DirEntries.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
//############################################################
//
// DirEntries.h
//
// Kari Pulli
// Fri Apr 9 18:33:31 CDT 1999
//
// Read in directory entries.
// Can filter entries by the ending.
//
//############################################################
#ifndef _DIRENTRIES_H_
#define _DIRENTRIES_H_
#ifdef WIN32
# include <io.h>
# include "defines.h"
#else
# include <dirent.h>
#endif
#include <string.h>
#include <vector>
//#include <ext/rope>
#include <string>
using namespace std;
class DirEntries {
int count;
std::string m_path;
vector<std::string> m_entry;
public:
bool isdir;
DirEntries(std::string dirname,
std::string end_filter = "")
: count(0)
{
#ifdef WIN32
// Windows loop prologue
struct _finddata_t fd;
char sdfn[PATH_MAX];
sprintf (sdfn, "%s/*", dirname.c_str());
long fh = _findfirst (sdfn, &fd);
isdir = (fh > 0);
if (!isdir) return;
while (1) {
char *name = fd.name;
#else
// Unix loop prologue
DIR *dirp = opendir(dirname.c_str());
isdir = (dirp != NULL);
if (!isdir) return;
struct dirent *direntp;
while ((direntp = readdir(dirp)) != NULL) {
char *name = direntp->d_name;
#endif
// loop body, shared
if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0) {
if (end_filter.size()) {
if (strcmp(&name[strlen(name)-end_filter.size()],
end_filter.c_str()) == 0)
{
m_entry.push_back(std::string(name));
}
}
}
#ifndef WIN32
// Windows loop epilogue
}
closedir(dirp);
m_path.assign(dirname);
m_path += "/";
#else
// Unix loop epilogue
if (_findnext (fh, &fd))
break;
}
_findclose (fh);
m_path.assign(dirname);
m_path += "\\";
#endif
}
void next(void)
{
count++;
}
bool done(void)
{
return (count >= m_entry.size());
}
std::string &entry(void)
{
return m_entry[count];
}
std::string path(void)
{
return m_path + m_entry[count];
}
int size(void)
{
return m_entry.size();
}
};
#if 0
#include <iostream>
void
main(int argc, char **argv)
{
for (DirEntries de(argv[1], argc>2 ? argv[2] : NULL);
!de.done(); de.next()) {
cout << de.entry() << endl;
}
}
#endif
#endif