Update copyright notices.
[mpd-mk.git] / test / read_tags.c
bloba16b703f0b3e034837a6f864fa883e2de65fca5a
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "decoder_list.h"
22 #include "decoder_api.h"
23 #include "input_init.h"
24 #include "input_stream.h"
25 #include "audio_format.h"
26 #include "pcm_volume.h"
27 #include "tag_ape.h"
28 #include "tag_id3.h"
29 #include "idle.h"
31 #include <glib.h>
33 #include <assert.h>
34 #include <unistd.h>
36 #ifdef HAVE_LOCALE_H
37 #include <locale.h>
38 #endif
40 /**
41 * No-op dummy.
43 void
44 idle_add(G_GNUC_UNUSED unsigned flags)
48 /**
49 * No-op dummy.
51 bool
52 pcm_volume(G_GNUC_UNUSED void *buffer, G_GNUC_UNUSED int length,
53 G_GNUC_UNUSED const struct audio_format *format,
54 G_GNUC_UNUSED int volume)
56 return true;
59 void
60 decoder_initialized(G_GNUC_UNUSED struct decoder *decoder,
61 G_GNUC_UNUSED const struct audio_format *audio_format,
62 G_GNUC_UNUSED bool seekable,
63 G_GNUC_UNUSED float total_time)
67 char *decoder_get_uri(G_GNUC_UNUSED struct decoder *decoder)
69 return NULL;
72 enum decoder_command
73 decoder_get_command(G_GNUC_UNUSED struct decoder *decoder)
75 return DECODE_COMMAND_NONE;
78 void decoder_command_finished(G_GNUC_UNUSED struct decoder *decoder)
82 double decoder_seek_where(G_GNUC_UNUSED struct decoder *decoder)
84 return 1.0;
87 void decoder_seek_error(G_GNUC_UNUSED struct decoder *decoder)
91 size_t
92 decoder_read(G_GNUC_UNUSED struct decoder *decoder,
93 struct input_stream *is,
94 void *buffer, size_t length)
96 return input_stream_read(is, buffer, length, NULL);
99 void
100 decoder_timestamp(G_GNUC_UNUSED struct decoder *decoder,
101 G_GNUC_UNUSED double t)
105 enum decoder_command
106 decoder_data(G_GNUC_UNUSED struct decoder *decoder,
107 G_GNUC_UNUSED struct input_stream *is,
108 const void *data, size_t datalen,
109 G_GNUC_UNUSED uint16_t bit_rate,
110 G_GNUC_UNUSED struct replay_gain_info *replay_gain_info)
112 write(1, data, datalen);
113 return DECODE_COMMAND_NONE;
116 enum decoder_command
117 decoder_tag(G_GNUC_UNUSED struct decoder *decoder,
118 G_GNUC_UNUSED struct input_stream *is,
119 G_GNUC_UNUSED const struct tag *tag)
121 return DECODE_COMMAND_NONE;
124 static void
125 print_tag(const struct tag *tag)
127 if (tag->time >= 0)
128 g_print("time=%d\n", tag->time);
130 for (unsigned i = 0; i < tag->num_items; ++i)
131 g_print("%s=%s\n",
132 tag_item_names[tag->items[i]->type],
133 tag->items[i]->value);
136 int main(int argc, char **argv)
138 GError *error = NULL;
139 const char *decoder_name, *path;
140 const struct decoder_plugin *plugin;
141 struct tag *tag;
142 bool empty;
144 #ifdef HAVE_LOCALE_H
145 /* initialize locale */
146 setlocale(LC_CTYPE,"");
147 #endif
149 if (argc != 3) {
150 g_printerr("Usage: read_tags DECODER FILE\n");
151 return 1;
154 decoder_name = argv[1];
155 path = argv[2];
157 if (!input_stream_global_init(&error)) {
158 g_warning("%s", error->message);
159 g_error_free(error);
160 return 2;
163 decoder_plugin_init_all();
165 plugin = decoder_plugin_from_name(decoder_name);
166 if (plugin == NULL) {
167 g_printerr("No such decoder: %s\n", decoder_name);
168 return 1;
171 tag = decoder_plugin_tag_dup(plugin, path);
172 if (tag == NULL && plugin->stream_tag != NULL) {
173 struct input_stream is;
175 if (!input_stream_open(&is, path, &error)) {
176 g_printerr("Failed to open %s: %s\n",
177 path, error->message);
178 g_error_free(error);
179 return 1;
182 tag = decoder_plugin_stream_tag(plugin, &is);
183 input_stream_close(&is);
186 decoder_plugin_deinit_all();
187 input_stream_global_finish();
188 if (tag == NULL) {
189 g_printerr("Failed to read tags\n");
190 return 1;
193 print_tag(tag);
195 empty = tag_is_empty(tag);
196 tag_free(tag);
198 if (empty) {
199 tag = tag_ape_load(path);
200 if (tag == NULL)
201 tag = tag_id3_load(path);
202 if (tag != NULL) {
203 print_tag(tag);
204 tag_free(tag);
208 return 0;