annotate src/libsndfile-1.0.27/programs/sndfile-metadata-set.c @ 127:7867fa7e1b6b

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