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