-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptHandler.cpp
707 lines (628 loc) · 19.1 KB
/
ScriptHandler.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// -----------------------------------------------------------------------------
// ScriptHandler.cpp ScriptHandler.cpp
// -----------------------------------------------------------------------------
/**
* @file
* @brief This file holds the implementation of the @ref ScriptHandler class.
* @author Col. Walter E. Kurtz
* @version 2016-11-25
* @copyright GNU General Public License - Version 3.0
*/
// -----------------------------------------------------------------------------
// Includes Includes
// -----------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include "keyinfo.h"
#include "ScriptHandler.h"
// -----------------------------------------------------------------------------
// Used namespaces Used namespaces
// -----------------------------------------------------------------------------
using namespace std;
// -----------------------------------------------------------------------------
// Construction Construction
// -----------------------------------------------------------------------------
// -------------
// ScriptHandler
// -------------
/*
*
*/
ScriptHandler::ScriptHandler()
{
// nothing
}
// -----------------------------------------------------------------------------
// Callback handler Callback handler
// -----------------------------------------------------------------------------
// --------------
// OnBeginParsing
// --------------
/*
*
*/
void ScriptHandler::OnBeginParsing(const string& filename)
{
// update healthy flag
setHealthy();
// empty buffers
m_keys.clear();
m_values.clear();
m_ichecks.clear();
m_dchecks.clear();
m_fchecks.clear();
// reset stringstream
m_fcbuffer.str("");
m_fcbuffer.clear();
}
// ------------
// OnEndParsing
// ------------
/*
*
*/
void ScriptHandler::OnEndParsing(bool healthy)
{
if (healthy && this->healthy())
{
// print initial bash code
beginScript();
// check directories, files and images
printCheckCommands();
// print buffered file commands
cout << m_fcbuffer.str();
// print final bash code
endScript();
}
// empty buffers
m_keys.clear();
m_values.clear();
m_ichecks.clear();
m_dchecks.clear();
m_fchecks.clear();
// reset stringstream
m_fcbuffer.str("");
m_fcbuffer.clear();
}
// ------
// OnData
// ------
/*
*
*/
void ScriptHandler::OnData(const string& key, const string& value)
{
// don't run in bad state
if ( !healthy() ) return;
// buffer tag
m_keys.push_back(key);
m_values.push_back(value);
// trigger found
if (key == "COMPILATIONINDEX")
{
// buffer bash code
bufferFileCommands();
// empty buffers
m_keys.clear();
m_values.clear();
}
}
// -----------------------------------------------------------------------------
// Internal methods Internal methods
// -----------------------------------------------------------------------------
// -------
// dirname
// -------
/*
*
*/
string ScriptHandler::dirname(const string& path) const
{
// start with empty directory
string dir("");
// this buffer holds one directory name
string buffer;
// extract directory part
for(string::size_type i = 0; i < path.size(); i++)
{
// get current character
unsigned char uc = static_cast<unsigned char>(path[i]);
// append character
buffer += uc;
// separator found
if (uc == '/')
{
// append directory
dir.append(buffer);
// reset buffer
buffer = "";
}
}
// return directory part
return dir;
}
// -----
// quote
// -----
/*
*
*/
string ScriptHandler::quote(const string& line, bool outer) const
{
// start with empty string
string quoted("");
// add outer quotation mark
if (outer) quoted.append("\"");
// copy characters
for(string::size_type i = 0; i < line.size(); i++)
{
// get current character
const char& c = line[i];
// these characters have to be escaped to get them literally
if ( (c == '"')
|| (c == '$')
|| (c == '`')
|| (c == '\\') )
{
// insert escape character
quoted += '\\';
quoted += c;
}
// history expansion
else if (c == '!')
{
// switch to strong quoting
quoted += "\"'!'\"";
}
// characters without special meaning
else
{
// append character
quoted += c;
}
}
// add outer quotation mark
if (outer) quoted.append("\"");
// return quoted version
return quoted;
}
// --------
// getValue
// --------
/*
*
*/
string ScriptHandler::getValue(const string& key) const
{
// find related value
for(unsigned i = 0; i < m_keys.size(); i++)
{
if (m_keys[i] == key)
{
return m_values[i];
}
}
// key not found
return "";
}
// ------------------
// bufferFileCommands
// ------------------
/*
*
*/
void ScriptHandler::bufferFileCommands()
{
// set comments in this order
vector<string> order;
order.push_back( keyinfo::name(keyinfo::COMPILATIONID) );
order.push_back( keyinfo::name(keyinfo::COMPILATIONINDEX) );
order.push_back( keyinfo::name(keyinfo::AUTHOR) );
order.push_back( keyinfo::name(keyinfo::COMPOSER) );
order.push_back( keyinfo::name(keyinfo::LYRICIST) );
order.push_back( keyinfo::name(keyinfo::OPUS) );
order.push_back( keyinfo::name(keyinfo::VERSION) );
order.push_back( keyinfo::name(keyinfo::ARRANGER) );
order.push_back( keyinfo::name(keyinfo::PERFORMER) );
order.push_back( keyinfo::name(keyinfo::CONDUCTOR) );
order.push_back( keyinfo::name(keyinfo::ENSEMBLE) );
order.push_back( keyinfo::name(keyinfo::ALBUMARTIST) );
order.push_back( keyinfo::name(keyinfo::ALBUM) );
order.push_back( keyinfo::name(keyinfo::GENRE) );
order.push_back( keyinfo::name(keyinfo::DATE) );
order.push_back( keyinfo::name(keyinfo::TRACKTOTAL) );
order.push_back( keyinfo::name(keyinfo::TRACKNUMBER) );
order.push_back( keyinfo::name(keyinfo::ARTIST) );
order.push_back( keyinfo::name(keyinfo::TITLE) );
order.push_back( keyinfo::name(keyinfo::COMMENT) );
// get special values
string cmpindex = getValue( keyinfo::name(keyinfo::COMPILATIONINDEX) );
string filename = getValue( keyinfo::name(keyinfo::FILENAME) );
string image = getValue( keyinfo::name(keyinfo::IMAGE) );
// filenames (wav and flac)
string infile;
string outfile;
// compile filenames
stringstream strstr;
strstr << "track";
strstr << setw(3) << setfill('0') << cmpindex;
strstr << ".cdda.wav";
strstr << " ";
strstr << "track";
strstr << setw(3) << setfill('0') << cmpindex;
strstr << ".flac";
// get filenames
strstr >> infile >> outfile;
// add empty line
m_fcbuffer << "# add empty line" << endl;
m_fcbuffer << "echo" << endl;
m_fcbuffer << endl;
// start if block
m_fcbuffer << "# create flac file" << endl;
m_fcbuffer << "if [ -s " << quote(infile) << " ] ; then" << endl;
m_fcbuffer << endl;
m_fcbuffer << " # show progress" << endl;
m_fcbuffer << " infomsg \"converting " << quote(infile, false) << "\"" << endl;
m_fcbuffer << endl;
// create flac command
m_fcbuffer << " # convert wav to flac" << endl;
m_fcbuffer << " flac --force \\" << endl;
m_fcbuffer << " --verify \\" << endl;
m_fcbuffer << " --compression-level-8 \\" << endl;
if ( !image.empty() )
{
// add check
m_ichecks.insert(image);
m_fcbuffer << " --picture=\"3||||" << quote(image, false) << "\" \\" << endl;
}
m_fcbuffer << " --output-name=" << quote(outfile) << " \\" << endl;
m_fcbuffer << " " << quote(infile) << endl;
m_fcbuffer << endl;
// call metaflac once
bool first = true;
// create metaflac command
for(unsigned n = 0; n < order.size(); n++)
{
// get related value
string val = getValue( order[n] );
// don't set empty comments
if ( !val.empty() )
{
if (first)
{
// print comment
m_fcbuffer << " # set comments" << endl;
// call metaflac
m_fcbuffer << " metaflac ";
// update flac
first = false;
}
else
{
// print indent
m_fcbuffer << " ";
}
// print next option
m_fcbuffer << "--set-tag=\"" << order[n] << "=" << quote(val, false) << "\" \\" << endl;
}
}
// add filename
if (!first)
{
m_fcbuffer << " " << quote(outfile) << endl;
}
m_fcbuffer << endl;
// create mv command
if ( !filename.empty() )
{
// add checks
m_dchecks.insert( dirname(filename) );
m_fchecks.insert( filename );
m_fcbuffer << " # rename file" << endl;
m_fcbuffer << " mv -f " << quote(outfile) << " " << quote(filename) << endl;
m_fcbuffer << endl;
}
// start else block
m_fcbuffer << "# missing or empty wav file" << endl;
m_fcbuffer << "else" << endl;
m_fcbuffer << endl;
m_fcbuffer << " # notify user" << endl;
m_fcbuffer << " warnmsg \"skipping missing (or empty) wav file: " << quote(infile, false) << "\"" << endl;
m_fcbuffer << endl;
if ( !filename.empty() )
{
m_fcbuffer << " # remove (touched resp. truncated) flac file" << endl;
m_fcbuffer << " rm -f " << quote(filename) << endl;
m_fcbuffer << endl;
}
m_fcbuffer << "fi" << endl;
m_fcbuffer << endl;
}
// -----------
// beginScript
// -----------
/*
*
*/
void ScriptHandler::beginScript() const
{
cout << "#!/bin/bash" << endl;
cout << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << "# settings settings" << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << endl;
cout << "# terminal colors" << endl;
cout << " NONE=$(tput sgr0)" << endl;
cout << " RED=$(tput setaf 1)" << endl;
cout << " GREEN=$(tput setaf 2)" << endl;
cout << " YELLOW=$(tput setaf 3)" << endl;
cout << " BLUE=$(tput setaf 4)" << endl;
cout << "MAGENTA=$(tput setaf 5)" << endl;
cout << " CYAN=$(tput setaf 6)" << endl;
cout << " WHITE=$(tput setaf 7)" << endl;
cout << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << "# functions functions" << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << endl;
cout << "# -------" << endl;
cout << "# failmsg" << endl;
cout << "# -------" << endl;
cout << "#" << endl;
cout << "# This function prints a fail message via stderr." << endl;
cout << "#" << endl;
cout << "function failmsg()" << endl;
cout << "{" << endl;
cout << " # push to stderr" << endl;
cout << " echo -e \"${RED}[FAIL]${NONE} $1\" 1>&2" << endl;
cout << "}" << endl;
cout << endl;
cout << "# -------" << endl;
cout << "# warnmsg" << endl;
cout << "# -------" << endl;
cout << "#" << endl;
cout << "# This function prints a warn message via stderr." << endl;
cout << "#" << endl;
cout << "function warnmsg()" << endl;
cout << "{" << endl;
cout << " # push to stderr" << endl;
cout << " echo -e \"${YELLOW}[WARN]${NONE} $1\" 1>&2" << endl;
cout << "}" << endl;
cout << endl;
cout << "# -------" << endl;
cout << "# infomsg" << endl;
cout << "# -------" << endl;
cout << "#" << endl;
cout << "# This function prints an info message via stderr." << endl;
cout << "#" << endl;
cout << "function infomsg()" << endl;
cout << "{" << endl;
cout << " # push to stderr" << endl;
cout << " echo -e \"${BLUE}[INFO]${NONE} $1\" 1>&2" << endl;
cout << "}" << endl;
cout << endl;
cout << "# -------" << endl;
cout << "# chktool" << endl;
cout << "# -------" << endl;
cout << "#" << endl;
cout << "# This function checks if an external tool is installed." << endl;
cout << "#" << endl;
cout << "# $1 name of the tool to check" << endl;
cout << "#" << endl;
cout << "function chktool()" << endl;
cout << "{" << endl;
cout << " # query type" << endl;
cout << " TOOLTYPE=$(type -t \"$1\")" << endl;
cout << endl;
cout << " # check type" << endl;
cout << " if [ \"$TOOLTYPE\" != 'file' ] ; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg \"external tool is missing: \\\"$1\\\"\"" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " exit 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << "}" << endl;
cout << endl;
cout << "# --------" << endl;
cout << "# chkimage" << endl;
cout << "# --------" << endl;
cout << "#" << endl;
cout << "# This function checks type, size and dimension of the cover image." << endl;
cout << "#" << endl;
cout << "# $1 filename" << endl;
cout << "#" << endl;
cout << "function chkimage()" << endl;
cout << "{" << endl;
cout << " # check if the image exists" << endl;
cout << " if [ ! -f \"$1\" ] ; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg \"unable to locate file: \\\"$1\\\"\"" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " exit 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " # get file size in byte" << endl;
cout << " FILESIZE=$(stat --printf '%s' \"$1\")" << endl;
cout << endl;
cout << " # get image format" << endl;
cout << " FORMAT=$(identify -format '%m' \"$1\")" << endl;
cout << endl;
cout << " # jpeg found" << endl;
cout << " if [ \"$FORMAT\" == 'JPEG' ] ; then" << endl;
cout << endl;
cout << " # check file size" << endl;
cout << " if (( FILESIZE > 85000 )) ; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg \"the image's file size should not exeed 80K (found: $FILESIZE)\"" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " exit 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " # png found" << endl;
cout << " elif [ \"$FORMAT\" == 'PNG' ] ; then" << endl;
cout << endl;
cout << " # check file size" << endl;
cout << " if (( FILESIZE > 505000 )) ; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg \"the image's file size should not exeed 500K (found: $FILESIZE)\"" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " exit 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " # other image types" << endl;
cout << " else" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg \"the image must have either JPEG or PNG format (found: $FORMAT)\"" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " exit 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " # get image dimensions" << endl;
cout << " DIMENSIONS=$(identify -format '%wx%h' \"$1\")" << endl;
cout << endl;
cout << " # check dimensions" << endl;
cout << " if [ \"$DIMENSIONS\" != '300x300' ] ; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg \"the image must must be 300px wide and 300px high (found: $DIMENSIONS)\"" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " exit 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << "}" << endl;
cout << endl;
cout << "# -------" << endl;
cout << "# chktdir" << endl;
cout << "# -------" << endl;
cout << "#" << endl;
cout << "# This function checks if the target directory can be created." << endl;
cout << "#" << endl;
cout << "# $1 name of the directory" << endl;
cout << "#" << endl;
cout << "function chktdir()" << endl;
cout << "{" << endl;
cout << " # try to create directory" << endl;
cout << " mkdir --parents \"$1\" &>'/dev/null'" << endl;
cout << endl;
cout << " # check if directory exists" << endl;
cout << " if [ ! -d \"$1\" ] ; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg \"unable to create target directory: \\\"$1\\\"\"" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " exit 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << "}" << endl;
cout << endl;
cout << "# --------" << endl;
cout << "# chktfile" << endl;
cout << "# --------" << endl;
cout << "#" << endl;
cout << "# This function checks if the target file can be created." << endl;
cout << "#" << endl;
cout << "# $1 filename" << endl;
cout << "#" << endl;
cout << "function chktfile()" << endl;
cout << "{" << endl;
cout << " # try to truncate (resp. create) file" << endl;
cout << " if ! truncate --size='0' \"$1\" &>'/dev/null' ; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg \"unable to create target file: \\\"$1\\\"\"" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " exit 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << "}" << endl;
cout << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << "# commands commands" << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << endl;
cout << "# check required tools" << endl;
cout << "chktool 'flac'" << endl;
cout << "chktool 'metaflac'" << endl;
cout << "chktool 'identify'" << endl;
cout << endl;
}
// ------------------
// printCheckCommands
// ------------------
/*
*
*/
void ScriptHandler::printCheckCommands() const
{
// abbreviation
typedef set<string>::const_iterator itt;
// directory images
if ( !m_ichecks.empty() )
{
// print comment
cout << "# check images" << endl;
// print all images to check
for(itt it = m_ichecks.begin(); it != m_ichecks.end(); ++it)
{
cout << "chkimage " << quote(*it) << endl;
}
// print empty line
cout << endl;
}
// directory checks
if ( !m_dchecks.empty() )
{
// print comment
cout << "# check directories" << endl;
// print all directories to check
for(itt it = m_dchecks.begin(); it != m_dchecks.end(); ++it)
{
cout << "chktdir " << quote(*it) << endl;
}
// print empty line
cout << endl;
}
// file checks
if ( !m_fchecks.empty() )
{
// print comment
cout << "# check files" << endl;
// print all files to check
for(itt it = m_fchecks.begin(); it != m_fchecks.end(); ++it)
{
cout << "chktfile " << quote(*it) << endl;
}
// print empty line
cout << endl;
}
}
// ---------
// endScript
// ---------
/*
*
*/
void ScriptHandler::endScript() const
{
cout << "# signalize success" << endl;
cout << "exit 0" << endl;
}