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