-
Notifications
You must be signed in to change notification settings - Fork 2
/
dirscanner.cpp
150 lines (138 loc) · 3.79 KB
/
dirscanner.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
/****
BpmDj v4.2-pl2: Free Dj Tools
Copyright (C) 2001-2011 Werner Van Belle
http://bpmdj.yellowcouch.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose. See the
GNU General Public License for more details.
See the authors.txt for a full list of people involved.
****/
#ifndef __loaded__dirscanner_cpp__
#define __loaded__dirscanner_cpp__
using namespace std;
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include "dirscanner.h"
#include "common.h"
#include "capacity.h"
#include "constants.h"
#include "info.h"
bool DirectoryScanner::goodName(QString name)
{
// must end on .idx or a song extension
if (!goodExtension(name) && !name.endsWith(IdxExt))
return false;
// no spaces or any special characters
// should occur anywhere in the filename
if (name.contains(OneSpace)) return false;
if (name.count("_")>1) return false;
if (name.contains("_"))
{
int pos = name.lastIndexOf("_");
int pos2 = name.indexOf("[");
if ((pos2-pos>3) || (pos<2))
return false;
}
if (name.contains("'")) return false;
if (name.contains("-")) return false;
if (name.contains("[")!=1) return false;
if (name.contains("]")!=1) return false;
int leftbrace=name.indexOf("[");
if (leftbrace>name.indexOf("]")) return false;
if (name.at(leftbrace+1).isLetter())
{
QChar c = name.at(leftbrace+1);
if (c<'A' || c>'Z')
return false;
}
return true;
}
DirectoryScanner::DirectoryScanner(QString extension):
recurse(true),
required_extension(extension)
{
};
DirectoryScanner::DirectoryScanner(QString dir, QString extension, bool rec):
recurse(rec),
required_extension(extension)
{
reset(dir);
}
void DirectoryScanner::recursing(const QString dirname)
{
Info("Recursing into %s",dirname.toAscii().data());
}
void DirectoryScanner::reset(const QString dirname, unsigned4 fpt)
{
DirPos pos;
pos.dir = opendir(dirname.toAscii().data());
pos.path = dirname;
prefix_length=dirname.length();
dir_stack.push(pos);
files_per_turn=fpt;
}
unsigned4 DirectoryScanner::scan()
{
unsigned4 real_files=0;
while(real_files < files_per_turn)
{
if (dir_stack.size()==0)
return real_files;
DirPos dirpos = dir_stack.top();
if (!dirpos.dir) dir_stack.pop();
else
{
struct dirent * entry=NULL;
while ( real_files<files_per_turn && (entry=readdir(dirpos.dir)))
{
if (strcmp(entry->d_name,".")==0 ||
strcmp(entry->d_name,"..")==0) continue;
QString d_name(entry->d_name);
QString newdir = dirpos.path+slash+d_name;
// is it a directory
DIR * nd = opendir(newdir.toAscii().data());
if (nd)
{
if (!recurse) continue;
DirPos pos;
pos.dir = nd;
pos.path = newdir;
dir_stack.push(pos);
recursing(newdir);
break;
}
else
{
real_files++;
scanfile(dirpos.path,d_name);
}
}
if (!entry)
{
closedir(dirpos.dir);
dir_stack.pop();
}
}
}
return real_files;
}
bool DirectoryScanner::matchextension(const QString filename)
{
if (required_extension.isEmpty()) return true;
if (filename.length()<required_extension.length()) return false;
return filename.right(required_extension.length()).
contains(required_extension,Qt::CaseInsensitive);
}
void DirectoryScanner::scanfile(const QString dir, const QString filename)
{
if (matchextension(filename))
checkfile(dir,filename);
}
#endif // __loaded__dirscanner_cpp__