]>
diplodocus.org Git - flac-archive/blob - id3v2-grep.cc
90bbeeabcfc315bc845f5df5674e8106ea623d6d
9 #include <id3v2frame.h>
17 using std::stringstream
;
19 const string
FRAME_ID_OPT("id");
20 const string
FRAME_TEXT_OPT("text");
22 static bool diag
= true;
23 static enum { PRINT_BOTH
, PRINT_FILE
, PRINT_MATCH
, PRINT_NONE
,
24 PRINT_DEFAULT
} print
= PRINT_DEFAULT
;
25 static enum { GREP_BOTH
, GREP_ID
, GREP_TEXT
} grep_frame
= GREP_BOTH
;
27 // TODO: use regexec(3)
29 grep(const string
&pattern
, TagLib::MPEG::File
&file
, const char *fn
)
31 if (!file
.hasID3v2Tag()) {
35 for (const auto *frame
: file
.ID3v2Tag()->frameList()) {
39 sstream
<< frame
->frameID() << "=" << frame
->toString();
42 sstream
<< frame
->frameID();
45 sstream
<< frame
->toString();
48 const string
&s
= sstream
.str();
49 if (s
.find(pattern
, 0) == string::npos
) {
53 if (print
== PRINT_BOTH
) {
54 cout
<< fn
<< ":" << s
<< endl
;
55 } else if (print
== PRINT_MATCH
) {
59 if (found
&& print
== PRINT_FILE
) {
66 main(int argc
, char **argv
)
68 char *pattern_c
= nullptr;
70 while ((c
= getopt(argc
, argv
, "e:HhlqsQ:")) != -1) {
91 if (optarg
== FRAME_ID_OPT
) {
93 } else if (optarg
== FRAME_TEXT_OPT
) {
94 grep_frame
= GREP_TEXT
;
96 cerr
<< "invalid option -- '-Q" << optarg
<< endl
;
101 // getopt printed an error.
105 argc
-= optind
; argv
+= optind
;
107 // TODO: multiple patterns
108 if (pattern_c
== nullptr) {
110 cerr
<< "no pattern" << endl
;
116 string
pattern(pattern_c
);
118 if (print
== PRINT_DEFAULT
) {
119 print
= argc
> 1 ? PRINT_BOTH
: PRINT_MATCH
;
124 for (; *argv
!= nullptr; argv
++) {
126 TagLib::MPEG::File
f(fn
);
129 cerr
<< "failed to open " << fn
<< endl
;
134 found
|= grep(pattern
, f
, fn
);