home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Audio 4.94 - Over 11,000 Files
/
audio-11000.iso
/
amiga
/
midi
/
obrst103.lha
/
OberSuite-1.03
/
SourceCode
/
tofile.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-01-23
|
4KB
|
168 lines
/**************************************************************************
* tofile.c: Functions for storing patch data in a disk file.
* A part of OberSuite for the Commodore Amiga.
*
* Author: Daniel Barrett, barrett@cs.umass.edu.
* Version: 1.0.
* Copyright: None! This program is in the Public Domain.
* Please share it with others.
***************************************************************************/
#include "decl.h"
/*
* Write patch data from the PATCHINFO structure into the named file.
* Return TRUE if successful, or FALSE if not.
* Possible things that can go wrong:
*
* OpenWriteFile() might be unable to open the file.
* The user could press ^C.
* write() might not write the correct number of bytes to the file.
* (Correct number == (number of patches) * (size of 1 patch))
*/
BOOL PutPatchToFile(PATCHINFO *pi, char *filename)
{
int fd = 0;
long written, shouldWrite;
if (fd = OpenWriteFile(filename))
{
if (CtrlcCheck())
{
close(fd);
ErrorMsg(ERROR_CTRLC);
return(FALSE);
}
shouldWrite = pi->numPatches * pi->rightSize;
written = write(fd, pi->data, shouldWrite);
if (written != shouldWrite)
ErrorMsg(ERROR_WRITEFILE);
close(fd);
return(written == shouldWrite);
}
else
return(FALSE);
}
/*
* Open a file for writing.
*/
int OpenWriteFile(char *filename)
{
int fd=NULL;
if ((fd = open(filename, O_WRONLY+O_CREAT+O_TRUNC)) == -1)
{
if (ERR_OUTPUT_ALLOWED)
fprintf(stderr, "%s: ", filename);
ErrorMsg(ERROR_CREATEFILE);
return(NULL);
}
else
return(fd);
}
/*
* If the named file already exists, ask the user whether or not it should
* be overwritten. The first character of the user's response is converted
* to upper case. User responses have the following effects:
*
* RESPONSE MEANING EFFECT
* -------------------------------------------------------------------
* Y Yes It is OK to overwrite the file.
* Return FALSE.
*
* A All It is OK to overwrite the file.
* Return FALSE, and cause
* all subsequent calls of this
* function automatically to return
* FALSE, as if the user typed "Y" at
* every prompt.
*
* <else> No Anything else the user types is
* considered a negative response.
* Return TRUE.
*/
BOOL DontOverwriteExistingFile(char *filename)
{
char buf[3]; /* Contains the user's response. */
static BOOL all = FALSE; /* User chose "A". */
BOOL safety = TRUE; /* Preserve the original file? */
if (all) /* Overwrite all files. */
return(FALSE);
if (EXISTS(filename))
{
fprintf(stderr,
"File \"%s\" already exists.\nOVERWRITE it? [%s]: ",
filename, "Yes/No/All, RETURN=No");
fflush(stderr);
if (fgets_and_flush(buf, 2, stdin))
{
buf[0] = toupper(buf[0]);
if (CtrlcCheck())
safety = TRUE; /* Don't overwrite. */
else if (buf[0] == 'A')
{
all = TRUE;
safety = FALSE; /* Overwrite ALL. */
}
else if (buf[0] == 'Y')
safety = FALSE; /* Overwrite one. */
else
safety = TRUE; /* Don't overwrite. */
}
else
safety = TRUE; /* Could not read answer. */
}
else
safety = FALSE; /* File doesn't exist. */
if (safety)
fprintf(stderr, "%s not overwritten.\n\n", filename);
return(safety);
}
/*
* This function solves a problem with reading input.
* When we read a line of input with fgets(), and the input line is too long,
* this function flushes all the unread input characters.
*
* Algorithm: If fgets() read a newline, then there is no unread input.
* If fgets() did NOT read a newline, grab characters until
* a new line is found.
*
* "alreadyRead" is the buffer that the previous fgets() used.
*/
char *fgets_and_flush(char *buf, int length, FILE *fp)
{
if (!fgets(buf, length, fp))
return(NULL);
else
{
if (*(buf + strlen(buf) - 1) != '\n')
{
int c;
while ((c = getc(fp)) != EOF && (c != '\n'))
;
}
return(buf);
}
}