-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagreport.cc
293 lines (239 loc) · 6.69 KB
/
tagreport.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* TagReport: a C++ program that outputs HTML formatted song lists from a
* specified directory.
*
* (C) 2003 Joshua Kwan <[email protected]>
*
* 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.
*/
#include "tagreport.h"
#include "html.h"
#include "templates.h"
#include "util.h"
#include "ftfuncs.h"
/* STL includes */
#include <vector>
#include <iostream>
#include <sstream>
#include <string>
/* C++ forwarding headers */
#include <cassert>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
/* C system headers */
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
using namespace std;
static vector<struct Song*>* traverse_dir (char* begin);
#ifdef NDEBUG
bool verbose = false;
#else
bool verbose = true;
#endif
bool force = false;
char* template_fn = NULL;
int main (int argc, char* argv [])
{
bool already_template = false;
vector <struct Song*>* all_songs = NULL;
vector <char*> targets;
vector <char*>::iterator t;
ostringstream tmpout;
char *target = NULL, *outfile = NULL;
int opt;
#ifdef HAVE_GETOPT_LONG
struct option longopts [] = {
{ "help" , 0, 0, 'h' },
{ "template", 1, 0, 't' },
{ "output" , 1, 0, 'o' },
{ "verbose" , 0, 0, 'v' },
{ "force" , 0, 0, 'f' },
{ 0, 0, 0, 0 }
};
#endif
/* read all options */
#ifdef HAVE_GETOPT_LONG
while ((opt = getopt_long(argc, argv, "ht:o:vf", longopts, NULL)) != -1)
#else
while ((opt = getopt(argc, argv, "ht:o:vf")) != -1)
#endif
{
switch(opt)
{
case 'h':
puts(usage);
return 0;
case 'o':
if (outfile)
{
cerr << "Warning: " << outfile << " will no longer be written to in favor of " << optarg << "." << endl;
free(outfile);
}
outfile = strdup(optarg);
break;
case 'v':
verbose = true;
break;
case 'f':
force = true;
break;
case 't':
if (already_template)
{
assert(template_fn);
cout << "Warning: " << template_fn << " will no longer be loaded in favor of " << optarg << "." << endl;
free(template_fn);
}
if ((template_fn = guess_fn (optarg)) != NULL)
already_template = true;
break;
case '?': /* Erroneous option was passed! */
#ifdef HAVE_GETOPT_LONG
cout << "Try `tagreport --help' for more information." << endl;
#else
cout << "Try `tagreport -h' for more information." << endl;
#endif
return 1;
}
}
/* Load the template into memory. */
if (template_fn)
read_template_file(template_fn);
/* Add all targets on the command line. */
if (optind < argc)
{
while (optind < argc)
targets.push_back(strdup(argv[optind++]));
/* Weed out the paths that do not exist. */
verify (targets);
}
/* Scan the current directory tree by default. */
else
{
target = (char*)malloc(PATH_MAX + 1);
getcwd(target, PATH_MAX);
targets.push_back(target);
}
/* No valid paths? */
if (targets.size() == 0)
{
cout << "All targets invalidated! :( Exiting." << endl;
return 1;
}
target = comma_delineate(targets);
/* Default output location - $PWD/playlist.htm */
if (!outfile)
outfile = strdup("playlist.htm");
tmpout << outfile << '.' << getpid();
/* Actually scan the hierarchy. */
if (verbose)
cout << "Scanning ";
for (t = targets.begin(); t != targets.end(); t++)
{
vector<struct Song*>* this_dir = traverse_dir(*t);
/* Append subsequent traversals to all_songs */
if (all_songs != NULL)
{
all_songs->insert (all_songs->end(), this_dir->begin(), this_dir->end());
delete this_dir;
}
else
all_songs = this_dir;
}
if (verbose)
cout << endl;
write_html (tmpout.str().c_str(), all_songs, target);
/* Move temporary file to the real thing */
if (rename (tmpout.str().c_str(), outfile) == -1)
{
cout << "error moving " << tmpout.str() << " to " << outfile << ": " << strerror(errno) << endl;
cout << "Note: " << tmpout.str() << " was written to and still exists." << endl;
/* We can't create outfile. */
outfile = (char*)tmpout.str().c_str();
}
cout << "Wrote " << outfile;
if (all_songs->size() != 0)
cout << " (" << all_songs->size() << " songs) successfully." << endl;
else
cout << ", but found no valid songs!" << endl;
/* Miscellaneous memory management */
if (all_songs)
clean(all_songs);
clean(targets);
free(target);
free(outfile);
return 0;
}
/* traverse_dir()
* input: path to directory to traverse
* Returns a vector of struct Song*s that will later be parsed to
* output the HTML
*/
vector<struct Song*>* traverse_dir (char* begin)
{
DIR* root;
struct dirent * contents;
struct stat dino;
vector <struct Song *> *all_songs = new vector<struct Song*>;
if ((root = opendir(begin)) != NULL)
{
if (verbose)
{
cout << "..";
fflush(stdout);
}
while ((contents = readdir(root)) != NULL)
{
string fn = contents->d_name;
ostringstream fp;
/* Skip .. and . */
if (fn == "." || fn == "..")
continue;
fp << begin << "/" << fn;
/* Recurse if this entry is actually a directory */
stat (fp.str().c_str(), &dino);
if (S_ISDIR(dino.st_mode))
{
vector<struct Song*>* recursion;
DEBUG("Recursing into", fp.str());
recursion = traverse_dir((char*)fp.str().c_str());
all_songs->insert (all_songs->end(), recursion->begin(), recursion->end());
delete recursion; /* Already copied into all_songs */
}
/* Not a directory, assumed to be a normal file */
else
{
struct Song * this_song = new struct Song;
if (get_artist_title (this_song, fp.str(), begin))
all_songs->push_back(this_song);
else
delete this_song;
}
}
}
/* If we get here there was SOME sort of error with opendir() */
else
{
if (verbose)
cout << endl;
cerr << "Error reading directory " << begin << ": " << strerror(errno) << endl;
exit(1);
}
closedir(root);
return all_songs;
}