Added vorbis-tools.
[vorbis-lancer-gcc.git] / vorbis-tools-1.2.0 / ogg123 / vorbis_comments.c
blob589f4b795de2ee144d788901ee8d74d7f55fec4c
1 /********************************************************************
2 * *
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
6 * PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
7 * *
8 * THE Ogg123 SOURCE CODE IS (C) COPYRIGHT 2000-2001 *
9 * by Stan Seibert <volsung@xiph.org> AND OTHER CONTRIBUTORS *
10 * http://www.xiph.org/ *
11 * *
12 ********************************************************************
14 last mod: $Id: vorbis_comments.c,v 1.3 2003/06/24 12:37:52 giles Exp $
16 ********************************************************************/
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <ogg/ogg.h>
27 #include <vorbis/codec.h>
28 #include <vorbis/vorbisfile.h>
29 #include "format.h"
30 #include "utf8.h"
31 #include "i18n.h"
34 /* Vorbis comment keys that need special formatting. */
35 struct {
36 char *key; /* includes the '=' for programming convenience */
37 char *formatstr; /* formatted output */
38 } vorbis_comment_keys[] = {
39 {"TRACKNUMBER=", N_("Track number:")},
40 {"REPLAYGAIN_TRACK_GAIN=", N_("ReplayGain (Track):")},
41 {"REPLAYGAIN_ALBUM_GAIN=", N_("ReplayGain (Album):")},
42 {"REPLAYGAIN_TRACK_PEAK=", N_("ReplayGain Peak (Track):")},
43 {"REPLAYGAIN_ALBUM_PEAK=", N_("ReplayGain Peak (Album):")},
44 {"COPYRIGHT=", N_("Copyright")},
45 {"=", N_("Comment:")},
46 {NULL, N_("Comment:")}
50 char *lookup_comment_prettyprint (char *comment, int *offset)
52 int i, j;
53 char *s;
55 /* Search for special-case formatting */
56 for (i = 0; vorbis_comment_keys[i].key != NULL; i++) {
58 if ( !strncasecmp (vorbis_comment_keys[i].key, comment,
59 strlen(vorbis_comment_keys[i].key)) ) {
61 *offset = strlen(vorbis_comment_keys[i].key);
62 s = malloc(strlen(vorbis_comment_keys[i].formatstr) + 1);
63 if (s == NULL) {
64 fprintf(stderr, _("Error: Out of memory.\n"));
65 exit(1);
67 strcpy(s, vorbis_comment_keys[i].formatstr);
68 return s;
73 /* Use default formatting */
74 j = strcspn(comment, "=");
75 if (j) {
76 *offset = j + 1;
77 s = malloc(j + 2);
78 if (s == NULL) {
79 fprintf(stderr, _("Error: Out of memory.\n"));
80 exit(1);
82 strncpy(s, comment, j);
83 strcpy(s + j, ":");
85 /* Capitalize */
86 s[0] = toupper(s[0]);
87 for (i = 1; i < j; i++) {
88 s[i] = tolower(s[i]);
90 return s;
93 /* Unrecognized comment, use last format string */
94 *offset = 0;
95 s = malloc(strlen(vorbis_comment_keys[i].formatstr) + 1);
96 if (s == NULL) {
97 fprintf(stderr, _("Error: Out of memory.\n"));
98 exit(1);
100 strcpy(s, vorbis_comment_keys[i].formatstr);
101 return s;
104 void print_vorbis_comment (char *comment, decoder_callbacks_t *cb,
105 void *callback_arg)
107 char *comment_prettyprint;
108 char *decoded_value;
109 int offset;
111 if (cb == NULL || cb->printf_metadata == NULL)
112 return;
114 comment_prettyprint = lookup_comment_prettyprint(comment, &offset);
116 if (utf8_decode(comment + offset, &decoded_value) >= 0) {
117 cb->printf_metadata(callback_arg, 1, "%s %s", comment_prettyprint,
118 decoded_value);
119 free(decoded_value);
120 } else
121 cb->printf_metadata(callback_arg, 1, "%s %s", comment_prettyprint,
122 comment + offset);
123 free(comment_prettyprint);