-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindJpeg.cc
105 lines (88 loc) · 1.85 KB
/
FindJpeg.cc
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
#include "FindJpeg.hh"
#include <cstdio>
#include <cstring>
#include <sys/stat.h>
static const bool debug=0;
bool FindJpeg::scan ()
{
struct dirent entry;
struct dirent *de;
while (readdir_r(dh_,&entry,&de) == 0)
{
if (de == 0)
break;
if (de->d_name[0] == '.')
continue;
if (debug) fprintf(stderr, "scanning: %s\n", de->d_name);
#ifdef _DIRENT_HAVE_D_TYPE
switch (de->d_type)
{
case DT_DIR:
dirs_.push_back( dir_ + "/" + de->d_name );
break;
case DT_REG:
{
if (debug) fprintf(stderr, "reg file\n");
int len = strlen(de->d_name);
if (len < 5) break;
const char * ext = de->d_name + len - 4;
if (strncasecmp(ext,".jpg",4)) break;
file_ = dir_ + "/" + de->d_name;
if (debug) fprintf(stderr, "dir=%s, name=%s, file='%s'\n", dir_.c_str(), de->d_name, file_.c_str());
return true;
}
case DT_UNKNOWN:
#endif
{
//TODO - check for file type & repeat above logic...
std::string path = dir_ + "/" + de->d_name;
struct stat buf;
stat(path.c_str(), &buf);
if (S_ISDIR(buf.st_mode))
{
dirs_.push_back( dir_ + "/" + de->d_name );
}
else if (S_ISREG(buf.st_mode))
{
int len = strlen(de->d_name);
if (len < 5) break;
const char * ext = de->d_name + len - 4;
if (strncasecmp(ext,".jpg",4)) break;
file_ = path;
return true;
}
}
#ifdef _DIRENT_HAVE_D_TYPE
}
#endif
}
return false;
}
bool FindJpeg::next ()
{
if (dh_)
{
if ( scan() )
return true;
else
{
closedir( dh_ );
dh_ = 0;
}
}
while( dirs_.size() )
{
dir_ = dirs_.back();
dirs_.pop_back();
dh_ = opendir(dir_.c_str());
if (dh_ == NULL)
{
perror(dir_.c_str());
}
else
{
if ( scan() ) return true;
}
}
return false;
}