Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mix-to-mono: '-' is not a clash of names #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/mix-to-mono.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,45 @@ static void mix_to_mono (SNDFILE * infile, SNDFILE * outfile) ;
int
main (int argc, char ** argv)
{
char *iname, *oname;
SNDFILE *infile, *outfile ;
SF_INFO sfinfo = { } ;

if (argc != 3)
if (argc != 3) {
usage_exit () ;

if (strcmp (argv [argc - 2], argv [argc - 1]) == 0)
{ printf ("Error : input and output file names are the same.\n") ;
} else {
iname = argv[1];
oname = argv[2];
}

if (strcmp(iname, "-") == 0 || strcmp(oname, "-") == 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second strcmp() is not required, because the error is only raised if the names are the same. So if the input isn't a dash and the names are the same, then we know the output also isn't a dash. Or to put it another way, we only need to check for name collisions when the input isn't a dash, and if that is the case, an output dash won't collide with it.

/* a name of "-" is never a clash */
} else if (strcmp (iname, oname) == 0) {
printf ("Error : input and output file names are the same.\n") ;
exit (1) ;
} ;
}

if ((infile = sf_open (argv [argc - 2], SFM_READ, &sfinfo)) == NULL)
{ printf ("Error : Not able to open input file '%s'\n", argv [argc - 2]) ;
if ((infile = sf_open (iname, SFM_READ, &sfinfo)) == NULL)
{ printf ("Error : Not able to open input file '%s'\n", iname) ;
sf_close (infile) ;
exit (1) ;
} ;

if (sfinfo.channels == 1)
{ printf ("Input file '%s' already mono. Exiting.\n", argv [argc - 2]) ;
{ printf ("Input file '%s' already mono. Exiting.\n", iname) ;
sf_close (infile) ;
exit (0) ;
} ;

/* Force output channels to mono. */
sfinfo.channels = 1 ;

if ((outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL)
{ printf ("Error : Not able to open output file '%s'\n", argv [argc - 1]) ;
if ((outfile = sf_open (oname, SFM_WRITE, &sfinfo)) == NULL)
{ printf ("Error : Not able to open output file '%s'\n", oname) ;
sf_close (infile) ;
exit (1) ;
} ;

/* Delete the output file length to zero if already exists. */
remove (argv [argc - 1]) ;

mix_to_mono (infile, outfile) ;

sf_close (infile) ;
Expand Down