comparison src/libsndfile-1.0.27/programs/sndfile-metadata-set.c @ 125:cd6cdf86811e

Current libsndfile source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 13:22:47 +0100
parents
children
comparison
equal deleted inserted replaced
124:e3d5853d5918 125:cd6cdf86811e
1 /*
2 ** Copyright (C) 2008-2016 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2008-2010 George Blood Audio
4 **
5 ** All rights reserved.
6 **
7 ** Redistribution and use in source and binary forms, with or without
8 ** modification, are permitted provided that the following conditions are
9 ** met:
10 **
11 ** * Redistributions of source code must retain the above copyright
12 ** notice, this list of conditions and the following disclaimer.
13 ** * Redistributions in binary form must reproduce the above copyright
14 ** notice, this list of conditions and the following disclaimer in
15 ** the documentation and/or other materials provided with the
16 ** distribution.
17 ** * Neither the author nor the names of any contributors may be used
18 ** to endorse or promote products derived from this software without
19 ** specific prior written permission.
20 **
21 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <config.h>
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <time.h>
41
42 #include <sndfile.h>
43
44 #include "common.h"
45
46 #define BUFFER_LEN (1 << 16)
47
48
49 static void usage_exit (const char *progname, int exit_code) ;
50 static void missing_param (const char * option) ;
51 static void read_localtime (struct tm * timedata) ;
52 static int has_bext_fields_set (const METADATA_INFO * info) ;
53
54 int
55 main (int argc, char *argv [])
56 { METADATA_INFO info ;
57 struct tm timedata ;
58 const char *progname ;
59 const char * filenames [2] = { NULL, NULL } ;
60 char date [128], time [128] ;
61 int k ;
62
63 /* Store the program name. */
64 progname = program_name (argv [0]) ;
65
66 /* Check if we've been asked for help. */
67 if (argc < 3 || strcmp (argv [1], "--help") == 0 || strcmp (argv [1], "-h") == 0)
68 usage_exit (progname, 0) ;
69
70 /* Set all fields of the struct to zero bytes. */
71 memset (&info, 0, sizeof (info)) ;
72
73 /* Get the time in case we need it later. */
74 read_localtime (&timedata) ;
75
76 for (k = 1 ; k < argc ; k++)
77 { if (argv [k][0] != '-')
78 { if (filenames [0] == NULL)
79 filenames [0] = argv [k] ;
80 else if (filenames [1] == NULL)
81 filenames [1] = argv [k] ;
82 else
83 { printf ("Error : Already have two file names on the command line and then found '%s'.\n\n", argv [k]) ;
84 usage_exit (progname, 1) ;
85 } ;
86 continue ;
87 } ;
88
89 #define HANDLE_BEXT_ARG(cmd, field) \
90 if (strcmp (argv [k], cmd) == 0) \
91 { k ++ ; \
92 if (k == argc) missing_param (argv [k - 1]) ; \
93 info.field = argv [k] ; \
94 continue ; \
95 } ;
96
97 HANDLE_BEXT_ARG ("--bext-description", description) ;
98 HANDLE_BEXT_ARG ("--bext-originator", originator) ;
99 HANDLE_BEXT_ARG ("--bext-orig-ref", originator_reference) ;
100 HANDLE_BEXT_ARG ("--bext-umid", umid) ;
101 HANDLE_BEXT_ARG ("--bext-orig-date", origination_date) ;
102 HANDLE_BEXT_ARG ("--bext-orig-time", origination_time) ;
103 HANDLE_BEXT_ARG ("--bext-coding-hist", coding_history) ;
104 HANDLE_BEXT_ARG ("--bext-time-ref", time_ref) ;
105
106 #define HANDLE_STR_ARG(cmd, field) \
107 if (strcmp (argv [k], cmd) == 0) \
108 { k ++ ; \
109 if (k == argc) missing_param (argv [k - 1]) ; \
110 info.field = argv [k] ; \
111 continue ; \
112 } ;
113
114 HANDLE_STR_ARG ("--str-comment", comment) ;
115 HANDLE_STR_ARG ("--str-title", title) ;
116 HANDLE_STR_ARG ("--str-copyright", copyright) ;
117 HANDLE_STR_ARG ("--str-artist", artist) ;
118 HANDLE_STR_ARG ("--str-date", date) ;
119 HANDLE_STR_ARG ("--str-album", album) ;
120 HANDLE_STR_ARG ("--str-license", license) ;
121
122 /* Following options do not take an argument. */
123 if (strcmp (argv [k], "--bext-auto-time-date") == 0)
124 { snprintf (time, sizeof (time), "%02d:%02d:%02d", timedata.tm_hour, timedata.tm_min, timedata.tm_sec) ;
125 info.origination_time = time ;
126
127 snprintf (date, sizeof (date), "%04d-%02d-%02d", timedata.tm_year + 1900, timedata.tm_mon + 1, timedata.tm_mday) ;
128 info.origination_date = date ;
129 continue ;
130 } ;
131
132 if (strcmp (argv [k], "--bext-auto-time") == 0)
133 { snprintf (time, sizeof (time), "%02d:%02d:%02d", timedata.tm_hour, timedata.tm_min, timedata.tm_sec) ;
134 info.origination_time = time ;
135 continue ;
136 } ;
137
138 if (strcmp (argv [k], "--bext-auto-date") == 0)
139 { snprintf (date, sizeof (date), "%04d-%02d-%02d", timedata.tm_year + 1900, timedata.tm_mon + 1, timedata.tm_mday) ;
140 info.origination_date = strdup (date) ;
141 continue ;
142 } ;
143
144 if (strcmp (argv [k], "--str-auto-date") == 0)
145 { snprintf (date, sizeof (date), "%04d-%02d-%02d", timedata.tm_year + 1900, timedata.tm_mon + 1, timedata.tm_mday) ;
146
147 info.date = strdup (date) ;
148 continue ;
149 } ;
150
151 printf ("Error : Don't know what to do with command line arg '%s'.\n\n", argv [k]) ;
152 usage_exit (progname, 1) ;
153 } ;
154
155 /* Find out if any of the 'bext' fields are set. */
156 info.has_bext_fields = has_bext_fields_set (&info) ;
157
158 if (filenames [0] == NULL)
159 { printf ("Error : No input file specificed.\n\n") ;
160 exit (1) ;
161 } ;
162
163 if (filenames [1] != NULL && strcmp (filenames [0], filenames [1]) == 0)
164 { printf ("Error : Input and output files are the same.\n\n") ;
165 exit (1) ;
166 } ;
167
168 if (info.coding_history != NULL && filenames [1] == NULL)
169 { printf ("\n"
170 "Error : Trying to update coding history of an existing file which unfortunately\n"
171 " is not supported. Instead, create a new file using :\n"
172 "\n"
173 " %s --bext-coding-hist \"Coding history\" old_file.wav new_file.wav\n"
174 "\n",
175 progname) ;
176 exit (1) ;
177 } ;
178
179 sfe_apply_metadata_changes (filenames, &info) ;
180
181 return 0 ;
182 } /* main */
183
184 /*==============================================================================
185 ** Print version and usage.
186 */
187
188 static void
189 usage_exit (const char *progname, int exit_code)
190 { printf ("\nUsage :\n\n"
191 " %s [options] <file>\n"
192 " %s [options] <input file> <output file>\n"
193 "\n",
194 progname, progname) ;
195
196 puts (
197 "Where an option is made up of a pair of a field to set (one of\n"
198 "the 'bext' or metadata fields below) and a string. Fields are\n"
199 "as follows :\n"
200 ) ;
201
202 puts (
203 " --bext-description Set the 'bext' description.\n"
204 " --bext-originator Set the 'bext' originator.\n"
205 " --bext-orig-ref Set the 'bext' originator reference.\n"
206 " --bext-umid Set the 'bext' UMID.\n"
207 " --bext-orig-date Set the 'bext' origination date.\n"
208 " --bext-orig-time Set the 'bext' origination time.\n"
209 " --bext-coding-hist Set the 'bext' coding history.\n"
210 " --bext-time-ref Set the 'bext' Time ref.\n"
211 "\n"
212 " --str-comment Set the metadata comment.\n"
213 " --str-title Set the metadata title.\n"
214 " --str-copyright Set the metadata copyright.\n"
215 " --str-artist Set the metadata artist.\n"
216 " --str-date Set the metadata date.\n"
217 " --str-album Set the metadata album.\n"
218 " --str-license Set the metadata license.\n"
219 ) ;
220
221 puts (
222 "There are also the following arguments which do not take a\n"
223 "parameter :\n\n"
224 " --bext-auto-time-date Set the 'bext' time and date to current time/date.\n"
225 " --bext-auto-time Set the 'bext' time to current time.\n"
226 " --bext-auto-date Set the 'bext' date to current date.\n"
227 " --str-auto-date Set the metadata date to current date.\n"
228 ) ;
229
230 puts (
231 "Most of the above operations can be done in-place on an existing\n"
232 "file. If any operation cannot be performed, the application will\n"
233 "exit with an appropriate error message.\n"
234 ) ;
235
236 printf ("Using %s.\n\n", sf_version_string ()) ;
237 exit (exit_code) ;
238 } /* usage_exit */
239
240 static void
241 missing_param (const char * option)
242 {
243 printf ("Error : Option '%s' needs a parameter but doesn't seem to have one.\n\n", option) ;
244 exit (1) ;
245 } /* missing_param */
246
247 /*==============================================================================
248 */
249
250 static int
251 has_bext_fields_set (const METADATA_INFO * info)
252 {
253 if (info->description || info->originator || info->originator_reference)
254 return 1 ;
255
256 if (info->origination_date || info->origination_time || info->umid || info->coding_history || info->time_ref)
257 return 1 ;
258
259 return 0 ;
260 } /* has_bext_fields_set */
261
262 static void
263 read_localtime (struct tm * timedata)
264 { time_t current ;
265
266 time (&current) ;
267 memset (timedata, 0, sizeof (struct tm)) ;
268
269 #if defined (HAVE_LOCALTIME_R)
270 /* If the re-entrant version is available, use it. */
271 localtime_r (&current, timedata) ;
272 #elif defined (HAVE_LOCALTIME)
273 {
274 struct tm *tmptr ;
275 /* Otherwise use the standard one and copy the data to local storage. */
276 if ((tmptr = localtime (&current)) != NULL)
277 memcpy (timedata, tmptr, sizeof (struct tm)) ;
278 }
279 #endif
280
281 return ;
282 } /* read_localtime */
283