Chris@0: /* Chris@0: ** Copyright (C) 2008-2011 Erik de Castro Lopo Chris@0: ** Copyright (C) 2008-2010 George Blood Audio Chris@0: ** Chris@0: ** All rights reserved. Chris@0: ** Chris@0: ** Redistribution and use in source and binary forms, with or without Chris@0: ** modification, are permitted provided that the following conditions are Chris@0: ** met: Chris@0: ** Chris@0: ** * Redistributions of source code must retain the above copyright Chris@0: ** notice, this list of conditions and the following disclaimer. Chris@0: ** * Redistributions in binary form must reproduce the above copyright Chris@0: ** notice, this list of conditions and the following disclaimer in Chris@0: ** the documentation and/or other materials provided with the Chris@0: ** distribution. Chris@0: ** * Neither the author nor the names of any contributors may be used Chris@0: ** to endorse or promote products derived from this software without Chris@0: ** specific prior written permission. Chris@0: ** Chris@0: ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@0: ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED Chris@0: ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR Chris@0: ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR Chris@0: ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, Chris@0: ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, Chris@0: ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; Chris@0: ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, Chris@0: ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR Chris@0: ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF Chris@0: ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@0: */ Chris@0: Chris@0: #include Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #include "common.h" Chris@0: Chris@0: #define BUFFER_LEN (1 << 16) Chris@0: Chris@0: Chris@0: static void usage_exit (const char *progname, int exit_code) ; Chris@0: static void missing_param (const char * option) ; Chris@0: static void read_localtime (struct tm * timedata) ; Chris@0: static int has_bext_fields_set (const METADATA_INFO * info) ; Chris@0: Chris@0: int Chris@0: main (int argc, char *argv []) Chris@0: { METADATA_INFO info ; Chris@0: struct tm timedata ; Chris@0: const char *progname ; Chris@0: const char * filenames [2] = { NULL, NULL } ; Chris@0: int k ; Chris@0: Chris@0: /* Store the program name. */ Chris@0: progname = program_name (argv [0]) ; Chris@0: Chris@0: /* Check if we've been asked for help. */ Chris@0: if (argc < 3 || strcmp (argv [1], "--help") == 0 || strcmp (argv [1], "-h") == 0) Chris@0: usage_exit (progname, 0) ; Chris@0: Chris@0: /* Clear set all fields of the struct to zero bytes. */ Chris@0: memset (&info, 0, sizeof (info)) ; Chris@0: Chris@0: /* Get the time in case we need it later. */ Chris@0: read_localtime (&timedata) ; Chris@0: Chris@0: for (k = 1 ; k < argc ; k++) Chris@0: { char tmp [20] ; Chris@0: Chris@0: if (argv [k][0] != '-') Chris@0: { if (filenames [0] == NULL) Chris@0: filenames [0] = argv [k] ; Chris@0: else if (filenames [1] == NULL) Chris@0: filenames [1] = argv [k] ; Chris@0: else Chris@0: { printf ("Error : Already have two file names on the command line and then found '%s'.\n\n", argv [k]) ; Chris@0: usage_exit (progname, 1) ; Chris@0: } ; Chris@0: continue ; Chris@0: } ; Chris@0: Chris@0: #define HANDLE_BEXT_ARG(cmd,field) \ Chris@0: if (strcmp (argv [k], cmd) == 0) \ Chris@0: { k ++ ; \ Chris@0: if (k == argc) missing_param (argv [k - 1]) ; \ Chris@0: info.field = argv [k] ; \ Chris@0: continue ; \ Chris@0: } ; Chris@0: Chris@0: HANDLE_BEXT_ARG ("--bext-description", description) ; Chris@0: HANDLE_BEXT_ARG ("--bext-originator", originator) ; Chris@0: HANDLE_BEXT_ARG ("--bext-orig-ref", originator_reference) ; Chris@0: HANDLE_BEXT_ARG ("--bext-umid", umid) ; Chris@0: HANDLE_BEXT_ARG ("--bext-orig-date", origination_date) ; Chris@0: HANDLE_BEXT_ARG ("--bext-orig-time", origination_time) ; Chris@0: HANDLE_BEXT_ARG ("--bext-coding-hist", coding_history) ; Chris@0: HANDLE_BEXT_ARG ("--bext-time-ref", time_ref) ; Chris@0: Chris@0: #define HANDLE_STR_ARG(cmd,field) \ Chris@0: if (strcmp (argv [k], cmd) == 0) \ Chris@0: { k ++ ; \ Chris@0: if (k == argc) missing_param (argv [k - 1]) ; \ Chris@0: info.field = argv [k] ; \ Chris@0: continue ; \ Chris@0: } ; Chris@0: Chris@0: HANDLE_STR_ARG ("--str-comment", comment) ; Chris@0: HANDLE_STR_ARG ("--str-title", title) ; Chris@0: HANDLE_STR_ARG ("--str-copyright", copyright) ; Chris@0: HANDLE_STR_ARG ("--str-artist", artist) ; Chris@0: HANDLE_STR_ARG ("--str-date", date) ; Chris@0: HANDLE_STR_ARG ("--str-album", album) ; Chris@0: HANDLE_STR_ARG ("--str-license", license) ; Chris@0: Chris@0: /* Following options do not take an argument. */ Chris@0: if (strcmp (argv [k], "--bext-auto-time-date") == 0) Chris@0: { snprintf (tmp, sizeof (tmp), "%02d:%02d:%02d", timedata.tm_hour, timedata.tm_min, timedata.tm_sec) ; Chris@0: info.origination_time = strdup (tmp) ; Chris@0: Chris@0: snprintf (tmp, sizeof (tmp), "%04d-%02d-%02d", timedata.tm_year + 1900, timedata.tm_mon + 1, timedata.tm_mday) ; Chris@0: info.origination_date = strdup (tmp) ; Chris@0: continue ; Chris@0: } ; Chris@0: Chris@0: if (strcmp (argv [k], "--bext-auto-time") == 0) Chris@0: { snprintf (tmp, sizeof (tmp), "%02d:%02d:%02d", timedata.tm_hour, timedata.tm_min, timedata.tm_sec) ; Chris@0: info.origination_time = strdup (tmp) ; Chris@0: continue ; Chris@0: } ; Chris@0: Chris@0: if (strcmp (argv [k], "--bext-auto-date") == 0) Chris@0: { snprintf (tmp, sizeof (tmp), "%04d-%02d-%02d", timedata.tm_year + 1900, timedata.tm_mon + 1, timedata.tm_mday) ; Chris@0: info.origination_date = strdup (tmp) ; Chris@0: continue ; Chris@0: } ; Chris@0: Chris@0: if (strcmp (argv [k], "--str-auto-date") == 0) Chris@0: { snprintf (tmp, sizeof (tmp), "%04d-%02d-%02d", timedata.tm_year + 1900, timedata.tm_mon + 1, timedata.tm_mday) ; Chris@0: Chris@0: info.date = strdup (tmp) ; Chris@0: continue ; Chris@0: } ; Chris@0: Chris@0: printf ("Error : Don't know what to do with command line arg '%s'.\n\n", argv [k]) ; Chris@0: usage_exit (progname, 1) ; Chris@0: } ; Chris@0: Chris@0: /* Find out if any of the 'bext' fields are set. */ Chris@0: info.has_bext_fields = has_bext_fields_set (&info) ; Chris@0: Chris@0: if (filenames [0] == NULL) Chris@0: { printf ("Error : No input file specificed.\n\n") ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (filenames [1] != NULL && strcmp (filenames [0], filenames [1]) == 0) Chris@0: { printf ("Error : Input and output files are the same.\n\n") ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (info.coding_history != NULL && filenames [1] == NULL) Chris@0: { printf ("\n" Chris@0: "Error : Trying to update coding history of an existing file which unfortunately\n" Chris@0: " is not supported. Instead, create a new file using :\n" Chris@0: "\n" Chris@0: " %s --bext-coding-hist \"Coding history\" old_file.wav new_file.wav\n" Chris@0: "\n", Chris@0: progname) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: sfe_apply_metadata_changes (filenames, &info) ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: /*============================================================================== Chris@0: ** Print version and usage. Chris@0: */ Chris@0: Chris@0: static void Chris@0: usage_exit (const char *progname, int exit_code) Chris@0: { printf ("\nUsage :\n\n" Chris@0: " %s [options] \n" Chris@0: " %s [options] \n" Chris@0: "\n", Chris@0: progname, progname) ; Chris@0: Chris@0: puts ( Chris@0: "Where an option is made up of a pair of a field to set (one of\n" Chris@0: "the 'bext' or metadata fields below) and a string. Fields are\n" Chris@0: "as follows :\n" Chris@0: ) ; Chris@0: Chris@0: puts ( Chris@0: " --bext-description Set the 'bext' description.\n" Chris@0: " --bext-originator Set the 'bext' originator.\n" Chris@0: " --bext-orig-ref Set the 'bext' originator reference.\n" Chris@0: " --bext-umid Set the 'bext' UMID.\n" Chris@0: " --bext-orig-date Set the 'bext' origination date.\n" Chris@0: " --bext-orig-time Set the 'bext' origination time.\n" Chris@0: " --bext-coding-hist Set the 'bext' coding history.\n" Chris@0: " --bext-time-raf Set the 'bext' Time ref.\n" Chris@0: "\n" Chris@0: " --str-comment Set the metadata comment.\n" Chris@0: " --str-title Set the metadata title.\n" Chris@0: " --str-copyright Set the metadata copyright.\n" Chris@0: " --str-artist Set the metadata artist.\n" Chris@0: " --str-date Set the metadata date.\n" Chris@0: " --str-album Set the metadata album.\n" Chris@0: " --str-license Set the metadata license.\n" Chris@0: ) ; Chris@0: Chris@0: puts ( Chris@0: "There are also the following arguments which do not take a\n" Chris@0: "parameter :\n\n" Chris@0: " --bext-auto-time-date Set the 'bext' time and date to current time/date.\n" Chris@0: " --bext-auto-time Set the 'bext' time to current time.\n" Chris@0: " --bext-auto-date Set the 'bext' date to current date.\n" Chris@0: " --str-auto-date Set the metadata date to current date.\n" Chris@0: ) ; Chris@0: Chris@0: puts ( Chris@0: "Most of the above operations can be done in-place on an existing\n" Chris@0: "file. If any operation cannot be performed, the application will\n" Chris@0: "exit with an appropriate error message.\n" Chris@0: ) ; Chris@0: Chris@0: printf ("Using %s.\n\n", sf_version_string ()) ; Chris@0: exit (exit_code) ; Chris@0: } /* usage_exit */ Chris@0: Chris@0: static void Chris@0: missing_param (const char * option) Chris@0: { Chris@0: printf ("Error : Option '%s' needs a parameter but doesn't seem to have one.\n\n", option) ; Chris@0: exit (1) ; Chris@0: } /* missing_param */ Chris@0: Chris@0: /*============================================================================== Chris@0: */ Chris@0: Chris@0: static int Chris@0: has_bext_fields_set (const METADATA_INFO * info) Chris@0: { Chris@0: if (info->description || info->originator || info->originator_reference) Chris@0: return 1 ; Chris@0: Chris@0: if (info->origination_date || info->origination_time || info->umid || info->coding_history || info->time_ref) Chris@0: return 1 ; Chris@0: Chris@0: return 0 ; Chris@0: } /* has_bext_fields_set */ Chris@0: Chris@0: static void Chris@0: read_localtime (struct tm * timedata) Chris@0: { time_t current ; Chris@0: Chris@0: time (¤t) ; Chris@0: memset (timedata, 0, sizeof (struct tm)) ; Chris@0: Chris@0: #if defined (HAVE_LOCALTIME_R) Chris@0: /* If the re-entrant version is available, use it. */ Chris@0: localtime_r (¤t, timedata) ; Chris@0: #elif defined (HAVE_LOCALTIME) Chris@0: { Chris@0: struct tm *tmptr ; Chris@0: /* Otherwise use the standard one and copy the data to local storage. */ Chris@0: if ((tmptr = localtime (¤t)) != NULL) Chris@0: memcpy (timedata, tmptr, sizeof (struct tm)) ; Chris@0: } Chris@0: #endif Chris@0: Chris@0: return ; Chris@0: } /* read_localtime */ Chris@0: