/**************************************************************************
* files.c:	File open/close, input/output, etc.
*		Part of MP, the MIDI Playground.
*
* Author:	Daniel Barrett
* Version:	See the file "version.h".
* Copyright:	None!  This program is in the Public Domain.
*		Please share it with others.
***************************************************************************/

	
#include <stdio.h>
#include <exec/types.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>


/* Open a file and return a pointer to it. */
	
FILE *OpenAFile(char *filename, char *mode, char *readOrWrite)
{
	FILE *fp;

    	if ((fp = fopen(filename, mode)) == NULL)
		fprintf(stderr, "Cannot %s the file \"%s\",\n",
			readOrWrite, filename);
	return(fp);
}

	
/* Open a file for reading.  We might also have a macro with the same name,
 * so we used ifdef's. */

#ifndef OpenReadFile	
FILE *OpenReadFile(char *filename)
{
	return(OpenAFile(filename, "r", "read"));
}
#endif

	
/* Open a file for writing.  We might also have a macro with the same name,
 * so we used ifdef's. */

#ifndef OpenWriteFile
FILE *OpenWriteFile(char *filename)
{
	return(OpenAFile(filename, "w", "write"));
}
#endif

	
/* Tell me whether or not a file exists. */

BOOL FileExists(char *filename)
{
	BPTR lock;

	if ((lock = Lock(filename, ACCESS_READ)) != NULL)
	{
		UnLock(lock);
		return(TRUE);
	}
	return(FALSE);
}

	
/* See if we want to overwrite an existing file. */

/* BUFSIZ is too large, perhaps -- change later -- but don't compromise
 * on correct reading of the user's input.  Too small, and extra unread
 * chars are read on the NEXT call (yuck).  If you use gets(), too much
 * input crashes the program. */

BOOL DontOverwriteExistingFile(char *filename)
{
	char buf[BUFSIZ];
	BOOL safety = TRUE;

	if (FileExists(filename))
	{
		fprintf(stderr,
		     "File \"%s\" already exists.\nOVERWRITE it? [%s]: ",
		     filename, "Yes/No, RETURN=No");
		fflush(stderr);

		if (fgets(buf, BUFSIZ-1, stdin))
			safety = (toupper(buf[0]) != 'Y');
		else
			safety = TRUE;
	}
	else
		safety = FALSE;			/* File doesn't exist. */

	if (safety)
		fprintf(stderr, "%s not overwritten.\n\n", filename);

	return(safety);
}


/* Open the input and output files.
 * We open input AFTER output in case the user has specified CON: windows,
 * so the input window is the active one. */

BOOL SetupFiles(char *infile, char *outfile, FILE **in, FILE **out)
{
	if (!outfile || (outfile[0] == '\0'))
		*out = stdout;
	else if (DontOverwriteExistingFile(outfile))
		return(FALSE);
	else if ((*out = OpenWriteFile(outfile)) == NULL)
		return(FALSE);

	if (!infile || (infile[0] == '\0'))
		*in = stdin;
	else if ((*in = OpenReadFile(infile)) == NULL)
		return(FALSE);

	return(TRUE);
}

	
/* Close the files used by the program, and deallocate the arrays used
 * for storing the filenames. */

void CloseFiles(FILE *in, FILE *out, char *filein, char *fileout)
{
	if (in && (in != stdin))	fclose(in);
	if (out && (out != stdout))	fclose(out);
	if (filein)			free(filein);
	if (fileout)			free(fileout);
}

	
/* Allocate space to store a filename, and copy the contents of "src" into
 * it. */

BOOL MakeFilename(char **dest, char *src)
{
	if ((!src) || (*src == '\0')
	||  ((*dest = (char *)malloc(strlen(src)+1)) == NULL))
		return(FALSE);
	else
	{
		strcpy(*dest, src);
		return(TRUE);
	}
}
