third_party_libsnd/examples/sndfile-metadata-set.c

240 lines
7.3 KiB
C

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