summaryrefslogtreecommitdiff
path: root/programs/sndfile-metadata-get.c
blob: 1d370b38e5f91b2af2efd8b020a4e642edd04c4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
** Copyright (C) 2008 George Blood Audio
** Written by Erik de Castro Lopo <erikd@mega-nerd.com>
**
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**
**     * Redistributions of source code must retain the above copyright
**       notice, this list of conditions and the following disclaimer.
**     * Redistributions in binary form must reproduce the above copyright
**       notice, this list of conditions and the following disclaimer in
**       the documentation and/or other materials provided with the
**       distribution.
**     * Neither the author nor the names of any contributors may be used
**       to endorse or promote products derived from this software without
**       specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#include <sndfile.h>

#include "common.h"

#define	BUFFER_LEN		(1 << 16)

static void usage_exit (const char *progname, int exit_code) ;
static void process_args (SNDFILE * file, const SF_BROADCAST_INFO_2K * binfo, int argc, char * argv []) ;

int
main (int argc, char *argv [])
{	SNDFILE *file ;
	SF_INFO sfinfo ;
	SF_BROADCAST_INFO_2K binfo ;
	const char *progname ;
	const char * filename = NULL ;
	int	start ;

	/* Store the program name. */
	progname = strrchr (argv [0], '/') ;
	progname = progname ? progname + 1 : argv [0] ;

	/* Check if we've been asked for help. */
	if (argc <= 2 || strcmp (argv [1], "--help") == 0 || strcmp (argv [1], "-h") == 0)
		usage_exit (progname, 0) ;

	if (argv [argc - 1][0] != '-')
	{	filename = argv [argc - 1] ;
		start = 1 ;
		}
	else if (argv [1][0] != '-')
	{	filename = argv [1] ;
		start = 2 ;
		}
	else
	{	printf ("Error : Either the first or the last command line parameter should be a filename.\n\n") ;
		exit (1) ;
		} ;

	/* Get the time in case we need it later. */
	memset (&sfinfo, 0, sizeof (sfinfo)) ;
	if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
	{	printf ("Error : Open of file '%s' failed : %s\n\n", filename, sf_strerror (file)) ;
		exit (1) ;
		} ;

	memset (&binfo, 0, sizeof (binfo)) ;
	if (sf_command (file, SFC_GET_BROADCAST_INFO, &binfo, sizeof (binfo)) == 0)
		memset (&binfo, 0, sizeof (binfo)) ;

	process_args (file, &binfo, argc - 2, argv + start) ;

	sf_close (file) ;
	return 0 ;
} /* main */

/*==============================================================================
**	Print version and usage.
*/

static void
usage_exit (const char *progname, int exit_code)
{	printf ("\nUsage :\n  %s [options] <file>\n\nOptions:\n", progname) ;

	puts (
		"    --bext-description    Print the 'bext' description.\n"
		"    --bext-originator     Print the 'bext; originator info.\n"
		"    --bext-orig-ref       Print the 'bext' origination reference.\n"
		"    --bext-umid           Print the 'bext' UMID.\n"
		"    --bext-orig-date      Print the 'bext' origination date.\n"
		"    --bext-orig-time      Print the 'bext' origination time.\n"
		"    --bext-coding-hist    Print the 'bext' coding history.\n"
		) ;

	puts (
		"    --str-title           Print the title metadata.\n"
		"    --str-copyright       Print the copyright metadata.\n"
		"    --str-artist          Print the artist metadata.\n"
		"    --str-comment         Print the comment metadata.\n"
		"    --str-date            Print the creation date metadata.\n"
		"    --str-album           Print the album metadata.\n"
		"    --str-license         Print the license metadata.\n"
		) ;

	exit (exit_code) ;
} /* usage_exit */

static void
process_args (SNDFILE * file, const SF_BROADCAST_INFO_2K * binfo, int argc, char * argv [])
{	const char * str ;
	int k, do_all = 0 ;

#define HANDLE_BEXT_ARG(cmd,name,field) \
		if (do_all || strcmp (argv [k], cmd) == 0) \
		{	printf ("%-20s : %.*s\n", name, (int) sizeof (binfo->field), binfo->field) ; \
			if (! do_all) \
				continue ; \
			} ;

#define HANDLE_STR_ARG(cmd,name,id) \
		if (do_all || strcmp (argv [k], cmd) == 0) \
		{	str = sf_get_string (file, id) ; \
			printf ("%-20s : %s\n", name, str ? str : "") ; \
			if (! do_all) continue ; \
			} ;

	for (k = 0 ; k < argc ; k++)
	{	if (do_all || strcmp (argv [k], "--all") == 0)
			do_all = 1 ;

		HANDLE_BEXT_ARG ("--bext-description", "Description", description) ;
		HANDLE_BEXT_ARG ("--bext-originator", "Originator", originator) ;
		HANDLE_BEXT_ARG ("--bext-orig-ref", "Origination ref", originator_reference) ;
		HANDLE_BEXT_ARG ("--bext-umid", "UMID", umid) ;
		HANDLE_BEXT_ARG ("--bext-orig-date", "Origination date", origination_date) ;
		HANDLE_BEXT_ARG ("--bext-orig-time", "Origination time", origination_time) ;
		HANDLE_BEXT_ARG ("--bext-coding-hist", "Coding history", coding_history) ;

		HANDLE_STR_ARG ("--str-title", "Name", SF_STR_TITLE) ;
		HANDLE_STR_ARG ("--str-copyright", "Copyright", SF_STR_COPYRIGHT) ;
		HANDLE_STR_ARG ("--str-artist", "Artist", SF_STR_ARTIST) ;
		HANDLE_STR_ARG ("--str-comment", "Comment", SF_STR_COMMENT) ;
		HANDLE_STR_ARG ("--str-date", "Create date", SF_STR_DATE) ;
		HANDLE_STR_ARG ("--str-album", "Album", SF_STR_ALBUM) ;
		HANDLE_STR_ARG ("--str-license", "License", SF_STR_LICENSE) ;

		if (! do_all)
		{	printf ("Error : Don't know what to do with command line arg '%s'.\n\n", argv [k]) ;
			exit (1) ;
			} ;
		break ;
		} ;

	return ;
} /* process_args */