header list functionality
[platform/upstream/flac.git] / src / metaflac / main.c
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 /* 
20  * WATCHOUT - this is meant to be very lightweight an not even dependent
21  * on libFLAC, so there are a couple places where FLAC__* variables are
22  * duplicated here.  Look for 'DUPLICATE:' in comments.
23  */
24
25 #include <assert.h>
26 #include <ctype.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "FLAC/all.h"
32
33 static const char *sync_string_ = "fLaC"; /* DUPLICATE:FLAC__STREAM_SYNC_STRING */
34
35 static int usage(const char *message, ...);
36 static bool list(FILE *f, bool verbose);
37
38 int main(int argc, char *argv[])
39 {
40         int i;
41         bool verbose = false, list_mode = true;
42
43         if(argc <= 1)
44                 return usage(0);
45
46         /* get the options */
47         for(i = 1; i < argc; i++) {
48                 if(argv[i][0] != '-' || argv[i][1] == 0)
49                         break;
50                 if(0 == strcmp(argv[i], "-l"))
51                         list_mode = true;
52                 else if(0 == strcmp(argv[i], "-v"))
53                         verbose = true;
54                 else if(0 == strcmp(argv[i], "-v-"))
55                         verbose = false;
56                 else {
57                         return usage("ERROR: invalid option '%s'\n", argv[i]);
58                 }
59         }
60         if(i + (list_mode? 1:2) != argc)
61                 return usage("ERROR: invalid arguments (more/less than %d filename%s?)\n", (list_mode? 1:2), (list_mode? "":"s"));
62
63         if(list_mode) {
64                 FILE *f = fopen(argv[i], "r");
65
66                 if(0 == f) {
67                         fprintf(stderr, "ERROR opening %s\n", argv[i]);
68                         return 1;
69                 }
70
71                 if(!list(f, verbose))
72                         return 1;
73
74                 fclose(f);
75         }
76
77         return 0;
78 }
79
80 int usage(const char *message, ...)
81 {
82         va_list args;
83
84         if(message) {
85                 va_start(args, message);
86
87                 (void) vfprintf(stderr, message, args);
88
89                 va_end(args);
90
91         }
92         printf("==============================================================================\n");
93         printf("metaflac - Command-line FLAC metadata editor version %s\n", FLAC__VERSION_STRING);
94         printf("Copyright (C) 2001  Josh Coalson\n");
95         printf("\n");
96         printf("This program is free software; you can redistribute it and/or\n");
97         printf("modify it under the terms of the GNU General Public License\n");
98         printf("as published by the Free Software Foundation; either version 2\n");
99         printf("of the License, or (at your option) any later version.\n");
100         printf("\n");
101         printf("This program is distributed in the hope that it will be useful,\n");
102         printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
103         printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
104         printf("GNU General Public License for more details.\n");
105         printf("\n");
106         printf("You should have received a copy of the GNU General Public License\n");
107         printf("along with this program; if not, write to the Free Software\n");
108         printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\n");
109         printf("==============================================================================\n");
110         printf("Usage:\n");
111         printf("  metaflac [options] infile [outfile]\n");
112         printf("\n");
113         printf("options:\n");
114         printf("  -l : list metadata blocks\n");
115         printf("  -v : verbose\n");
116         printf("  -v- can all be used to turn off a particular option\n");
117
118         return 1;
119 }
120
121 bool list(FILE *f, bool verbose)
122 {
123         static byte buf[65536];
124         FLAC__StreamMetaData metadata;
125
126         /* read the stream sync code */
127         if(fread(buf, 1, 4, f) < 4 || memcmp(buf, sync_string_, 4)) {
128                 fprintf(stderr, "ERROR: not a FLAC file (no '%s' header)\n", sync_string_);
129                 return false;
130         }
131
132         /* read the metadata blocks */
133         do {
134                 /* read the metadata block header */
135                 if(fread(buf, 1, 4, f) < 4) {
136                         fprintf(stderr, "ERROR: short count reading metadata block header\n");
137                         return false;
138                 }
139                 metadata.is_last = (buf[0] & 0x80)? true:false;
140                 metadata.type = (FLAC__MetaDataType)(buf[0] & 0x7f);
141                 metadata.length = (((unsigned)buf[1]) << 16) | (((unsigned)buf[2]) << 8) | ((unsigned)buf[3]);
142
143                 /* read the metadata block data */
144
145                 /* print it */
146                 printf("METADATA block:\n");    
147                 printf("\ttype: %u\n", (unsigned)metadata.type);
148                 printf("\tis last: %s\n", metadata.is_last? "true":"false");
149                 printf("\tlength: %u\n", metadata.length);
150         } while (!metadata.is_last);
151
152         return true;
153 }