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