]>
diplodocus.org Git - flac-archive/blob - id3v2-grep.cc
549644ee2d9681c69b4a1c9ab537452731cf2806
10 #include <id3v2frame.h>
18 using std::stringstream
;
20 const string
FRAME_ID_OPT("id");
21 const string
FRAME_TEXT_OPT("text");
23 static bool file_diag
= true;
24 static enum { PRINT_BOTH
, PRINT_FILE
, PRINT_MATCH
, PRINT_NONE
,
25 PRINT_DEFAULT
} print
= PRINT_DEFAULT
;
26 static enum { GREP_BOTH
, GREP_ID
, GREP_TEXT
} grep_frame
= GREP_BOTH
;
29 grep(const regex_t
®ex
, 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 int status
= regexec(®ex
, s
.c_str(), 0, nullptr, 0);
50 if (status
== REG_NOMATCH
) {
57 if (print
== PRINT_BOTH
) {
58 cout
<< fn
<< ":" << s
<< endl
;
59 } else if (print
== PRINT_MATCH
) {
63 if (found
&& print
== PRINT_FILE
) {
66 return found
? 0 : REG_NOMATCH
;
70 regex_error(int status
, const regex_t
®ex
)
73 regerror(status
, ®ex
, message
, sizeof(message
));
74 cerr
<< message
<< endl
;
78 main(int argc
, char **argv
)
80 bool ignore_case
= false, use_ere
= false;
81 char *pattern
= nullptr;
83 while ((c
= getopt(argc
, argv
, "Eie:HhlqsQ:")) != -1) {
110 if (optarg
== FRAME_ID_OPT
) {
111 grep_frame
= GREP_ID
;
112 } else if (optarg
== FRAME_TEXT_OPT
) {
113 grep_frame
= GREP_TEXT
;
115 cerr
<< "invalid option -- '-Q" << optarg
<< endl
;
120 // getopt printed an error.
124 argc
-= optind
; argv
+= optind
;
126 // TODO: multiple patterns
127 if (pattern
== nullptr) {
129 cerr
<< "no pattern" << endl
;
135 int cflags
= REG_NOSUB
;
136 if (ignore_case
) cflags
|= REG_ICASE
;
137 if (use_ere
) cflags
|= REG_EXTENDED
;
139 int status
= regcomp(®ex
, pattern
, cflags
);
141 regex_error(status
, regex
);
145 if (print
== PRINT_DEFAULT
) {
146 print
= argc
> 1 ? PRINT_BOTH
: PRINT_MATCH
;
151 for (; *argv
!= nullptr; argv
++) {
153 TagLib::MPEG::File
f(fn
);
156 cerr
<< "failed to open " << fn
<< endl
;
161 int status
= grep(regex
, f
, fn
);
164 } else if (status
!= REG_NOMATCH
) {
165 regex_error(status
, regex
);