annotate src/libsndfile-1.0.25/programs/sndfile-metadata-set.c @ 85:545efbb81310

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