File: qmp3report.cc

package info (click to toggle)
quelcom 0.4.0-16
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 1,544 kB
  • sloc: cpp: 4,148; makefile: 158; sh: 15
file content (384 lines) | stat: -rw-r--r-- 9,970 bytes parent folder | download | duplicates (3)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# include <iostream>
# include <string>
# include <sys/stat.h>	// stat
# include <getopt.h>	// getopt
# include <dirent.h>	// scandir
# include <unistd.h>	// getcwd
# include <errno.h>	// errno
# include <string.h>    // strerror, strrchr, strlen
# include <strings.h>   // strncasecmp
# include "qmp3.hh"
# include "qfile.hh"
# include "qexception.hh"
# include "qreport.hh"
# include "qmisc.hh"

using namespace std;

#ifdef NLS
  # include <locale.h>
  # include <libintl.h>
  # define _(s) gettext (s)
#else
  # define _(s) (s)
#endif

struct options {
  bool all, html, recursive, 
       showall, showdirs, showfiles, showsummary, 
       split, verbose;
  string outfilename;
  ofstream *out;
};


void usage () {

  cerr << ' ' << APPNAME
       << ": generate reports from mp3 files and directories\n";
  cerr << " syntax: " << APPNAME << " [option]... file...\n";
  cerr << _(" options:\n");
  cerr << _("   -a, --all-files: report all files, not just *.mp3\n");
  cerr << _("   -A, --show-all: implies -d, -f, -s\n");
//  cerr << _("   --all-mp3: consider files as mp3 streams"\n");
  cerr << _("   -d, --dirs: show a report for every directory\n");
  cerr << _("   -f, --files: show a report for every file\n");
  cerr << _("   -h, --help: show this help and exit\n");
  cerr << _("   -H, --html: output in html format (default is text)\n");
//  cerr << _("   -o, --outfilename <file>: output file (by default stdout)\n");
  cerr << _("   -r, --recursive: scan directories\n");
//  cerr << _("   -s: scan streams for validity (slower but more accurate)\n");
  cerr << _("   -s, --summary: show a summary report\n");
  cerr << _("   -S, --split: split report across visited directories\n");
  cerr << _("   -v, --verbose: show more detailed info\n");
  cerr << _("   -V, --version: show version and exit\n");
// allow user defined format for showing
// treure segons com i quan el temps que triga
}
     

qreport report_file (string filename, struct options &opts) {

  try {
    if (opts.all) {
      qfile f(filename);
      if (opts.verbose)
      	cerr << _("reporting file") << " '" << filename << "'\n";
      qreport r(f);
      if (opts.showfiles) {
        if (opts.html) {
          r.html(opts.out);
          *opts.out << "<br>" << endl;
        }
        else
          *opts.out << r << endl;
      }
      return r;
    }
  }
  catch (qexception e) {
    cerr << filename << ": " << e << endl;
  }

  return qreport();
}


qreport report_mp3 (string mp3name, struct options &opts) {

  try {
    qmp3 mp3(mp3name);
    if (mp3.isVbr()) {
      if (opts.verbose)
        cerr << mp3.getName() << _(": vbr detected => automatic check") << endl;
      mp3.scan();
    }
    if (opts.verbose)
      cerr << _("reporting mp3 file") << " '" << mp3name << "'\n";
    qreport r(mp3);
    if (opts.showfiles) {
      if (opts.html) {
        r.html(opts.out);
        *opts.out << "<br>" << endl;
      }
      else
        *opts.out << r << endl;
    }
    return r;
  }
  catch (qexception e) {
    cerr << mp3name << ": " << e << endl;
    return qreport();
  }
}

qreport report_directory (string dirname, struct options &opts) {

  if (opts.verbose)
    cerr << _("reporting directory") << " '" << dirname << "'\n";

  qreport dirreport(dirname);

  if (!opts.recursive) {
    if (opts.verbose)
      cerr << APPNAME << ": '" << dirname << ' ' << _("is a directory") << '\n';
    return dirreport;
  }

  char previous_wd[1025],wd[1025];

  ofstream *out;
  if (opts.split) {
    if (getcwd(previous_wd,sizeof(previous_wd))==NULL) {
      cerr << _("cannot get current directory") << " ("
           << dirname << "): " << strerror(errno) << '\n';
      return dirreport;
    }
    if (chdir(dirname.c_str())) {
      cerr << _("cannot move to dir")
           << dirname << ": " << strerror(errno) << '\n';
      return dirreport;
    }
    if (getcwd(wd,sizeof(wd))==NULL) {
      cerr << _("cannot get current directory")
           << " (" << dirname << "): " << strerror(errno) << '\n';
      return dirreport;
    }
    char *index = strrchr(wd,'/');
    string outfilename;
    if (*index && index!=wd)
      outfilename = string((index+1));
    else
      outfilename = string ("qmp3report");
    if (opts.html)
      outfilename += string(".html");
    else
      outfilename += string(".txt");
    
    if (opts.verbose)
      cerr << _("opening output file") << " '" << outfilename << "'\n";

    out = opts.out;
    opts.out = new ofstream(outfilename.c_str());

    dirname = string(".");
  } 

  int n;
  struct dirent **namelist;

  if ((n = scandir(dirname.c_str(),&namelist,0,alphasort))<0) {
    cerr << APPNAME << ':' << _("cannot scan") << " '" << dirname << "'\n";
    return dirreport;
  }

  for (int i=0; i<n; i++) {

   // skip infinite recursion on the same directory or on the parent directory
    if (!strcmp(namelist[i]->d_name,".") || 
        !strcmp(namelist[i]->d_name,"..")) 
      continue;

    struct stat filestat;
    string filepathname;
    if (dirname==".")
      filepathname = string(namelist[i]->d_name);
    else
      filepathname = dirname + string("/") + string(namelist[i]->d_name);

    if (stat(filepathname.c_str(),&filestat)) {
      cerr << APPNAME << ": " << _("error reading")
           << " '" << dirname << "' status: "
      	   << strerror(errno) << '\n';
      continue;
    }

    if (S_ISDIR(filestat.st_mode)) {
      dirreport.add(report_directory(filepathname,opts));
      continue;
    }

    if (S_ISREG(filestat.st_mode)) {
      int length = filepathname.length();
      if (length>4 && !strncasecmp((char*)(&(filepathname.c_str()[length-4])),".mp3",4))
        dirreport.add(report_mp3(filepathname,opts));
      else {
        if (opts.all)
          dirreport.add(report_file(filepathname,opts));
      }
      continue;
    }

    if (opts.verbose)
      cerr << filepathname << _(" is a special file. skipping.") << endl;
  }

  if (opts.split) {

    if (opts.html)
      *opts.out << "&nbsp<br>" << endl;
    else
      *opts.out << endl;

    *opts.out << dirreport << endl;

    opts.out->close();
    opts.out = out;
    chdir (previous_wd);
  }

  if (opts.showdirs && dirreport.getFiles()>0) {
    if (opts.html) {
     // if split fer que en lloc d'assenyalar al directori apunti al report
     //  que cont.
      dirreport.html(opts.out,dirreport.getName()+string("/")+dirreport.getName()+string(".html"));
      *opts.out << "<br>" << endl;
    }
    else
      *opts.out << dirreport << endl;
  }

  return dirreport;
}


int main (int argc, char **argv) {

  int option;
  struct options opts={false,false,false,false,false,false,false,false,false,""};

  static struct option long_options[] = {
    {"all-files",no_argument,0,'a'},
    {"show-all",no_argument,0,'A'},
//    {"all-mp3",no_argument,0,'Q'},	??????
    {"dirs",no_argument,0,'d'},
    {"files",no_argument,0,'f'},
    {"help",no_argument,0,'h'},
    {"html",no_argument,0,'H'},
    {"recursive",no_argument,0,'r'},
    {"summary",no_argument,0,'s'},
    {"split",no_argument,0,'S'},
    {"verbose",no_argument,0,'v'},
    {"version",no_argument,0,'V'},
    {0,0,0,0}
  };

#ifdef NLS
  setlocale(LC_ALL, "");
  bindtextdomain(PACKAGE, LOCALEDIR);
  textdomain(PACKAGE);
#endif

 // un altre dia, per defecte llegir *.mp3 ...
  if (argc==1) {
    usage();
    return 1;
  }

 // supress getopt error message
  opterr = 0;	

  while ((option = getopt_long(argc, argv, "aAdfhHo:rsSvV",long_options,0)) != EOF) 
    switch (option) {
      case 'a':	opts.all = true;	break;
      case 'A': opts.showdirs=opts.showfiles=opts.showsummary = true;	break;
      case 'd': opts.showdirs = true;	break;
      case 'f': opts.showfiles = true;	break;
      case 'h': usage(); return 0;	break;
      case 'H': opts.html = true;	break;
      case 'r': opts.recursive = true;	break;
      case 's': opts.showsummary = true;break;
      case 'S': opts.split = true;	break;
      case 'v': opts.verbose = true;	break;
      case 'V': 
        cerr << APPNAME << " - " << _("version") << ' ' << VERSION
	     << _("build") << ' ' << __DATE__ << '\n';
        return 0;
      break;
      case '?':
      default: 
        cerr << APPNAME << ": " << _("option") << " '" << argv[optind-1]
	     << "' " << _("is not recognized or bad used") << '\n';
        usage();
        return 1;
    } 

  if (argc<=optind) {
   // haurem de fer *.mp3 ...
    cerr << APPNAME << _(": no input file(s)") << endl;
    usage();
    return 1;
  }

  qreport base;
  
  opts.out = new ofstream;

  if (opts.split)
    opts.out = new ofstream("/dev/null");
  else {
    opts.out = new ofstream;
    if (opts.html)
      *opts.out << "<html>\n<head><title>qmp3report</title>\n</head>\n\n<body>\n";
  }

  for (; optind<argc; optind++) {

    struct stat filestat;

    if (stat(argv[optind],&filestat)) {
      cerr << APPNAME << ": " << _("error reading") << " '"
           << argv[optind] << "' " << _("status") << ": "
	   << strerror(errno) << '\n';
      continue;
    }

    try { 

      if (S_ISDIR(filestat.st_mode)) {
        base.add(report_directory(argv[optind],opts));
        continue;
      }

      if (S_ISREG(filestat.st_mode)) {

        int length = strlen(argv[optind]);

        if (length>4 && !strncasecmp((char*)(&(argv[optind][length-4])),".mp3",4))
          base.add(report_mp3(argv[optind],opts));
        else {
          if (opts.all)
            base.add(report_file(argv[optind],opts));
        }

        continue;
      }

      if (opts.verbose)
        cerr << argv[optind] << _(" is a special file. skipping.") << endl;
    }
    catch (qexception e) {
      cerr << e << endl;
    }
  }

  if (opts.showsummary) {
    if (!opts.html || opts.split) {
      cout << base << endl;
    }
    else {
      base.html(new ofstream);
      cout << "<br>" << endl;
    }
  }

  if (!opts.split) {

    if (opts.html) {
      *opts.out << "\n</body></html>\n";
    }
  }

  opts.out->close();
}