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
/
fromfile.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-01-23
|
4KB
|
194 lines
/**************************************************************************
* fromfile.c: Functions for reading patch data from 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"
/*
* Read patch data from the named file into the PATCHINFO structure.
* Return TRUE if successful, or FALSE if not.
* Possible things that can go wrong:
*
* OpenReadFile() might be unable to open the file.
* The user could press ^C.
* read() might not read the correct number of bytes from the file.
* (Correct number == (number of patches) * (size of 1 patch))
*/
BOOL GetPatchFromFile(PATCHINFO *pi, char *filename)
{
int fd = 0;
long bytesRead, shouldRead;
if (fd = OpenReadFile(filename))
{
if (CtrlcCheck())
{
close(fd);
ErrorMsg(ERROR_CTRLC);
return(FALSE);
}
shouldRead = pi->numPatches * pi->rightSize;
bytesRead = read(fd, pi->data, shouldRead);
if (shouldRead != bytesRead)
ErrorMsg(ERROR_READFILE);
close(fd);
return(bytesRead == shouldRead);
}
else
return(FALSE);
}
/*
* Return TRUE if the size of the given file is a valid size for a patch
* file. From the actual size, determine the "rightSize" and "numPatches"
* values for the PATCHINFO structure.
*/
BOOL LookAtFileSize(PATCHINFO *pi, char *filename)
{
long size = 0L;
BOOL success = TRUE;
if ((size = FileSize(filename)) < 0L)
{
success = FALSE;
ErrorMsg(ERROR_FINDFILE);
}
else if ((size == SINGLE_SIZE)
|| (size == XPANDER_MULTI_SIZE)
|| (size == MATRIX12_MULTI_SIZE))
{
pi->rightSize = size;
pi->numPatches = 1;
}
else if ((size == ALL_SINGLE_SIZE)
|| (size == ALL_XPANDER_MULTI_SIZE)
|| (size == ALL_MATRIX12_MULTI_SIZE))
{
pi->rightSize = size / NUM_PATCHES;
pi->numPatches = NUM_PATCHES;
}
else
{
success = FALSE;
ErrorMsg(ERROR_FILESIZE);
}
if (!success && ERR_OUTPUT_ALLOWED)
{
fprintf(stderr, "(File \"%s\"", filename);
if (size > 0)
fprintf(stderr, ", size=%ld", size);
fprintf(stderr, ")\n");
}
return(success);
}
/*
* Given a PATCHINFO structure filled with patch data, verify that the
* data is consistent.
*/
BOOL VerifyAllPatches(PATCHINFO *pi)
{
int i;
long offset = 0L;
for (i=0; (pi) && (i < pi->numPatches); i++)
{
if (!VerifyPatch(pi, offset))
{
if (ERR_OUTPUT_ALLOWED)
{
fprintf(stderr,
"Verify failed after checking %ld patch(es).\n",
i);
}
return(FALSE);
}
offset += pi->rightSize;
}
return(TRUE);
}
/*
* Look at the patch data in a PATCHINFO structure, and determine:
*
* The patch mode.
* The (string) name of the patch mode.
*
* Fill in these fields in the PATCHINFO struct.
*/
BOOL FigureThingsOutFromHeader(PATCHINFO *pi)
{
/* We do this to get through Verify(). actualSize is meaningless
* until we actually see the results of DoTheIO(). We calculated
* rightSize when we stat()-ed the file. */
pi->actualSize = pi->rightSize;
pi->mode = pi->data[BYTE_PATCHTYPE];
switch (pi->mode)
{
case SINGLE_DATA:
strcpy(pi->modeName, NAME_SINGLE);
return(TRUE);
case MULTI_DATA:
strcpy(pi->modeName, NAME_MULTI);
return(TRUE);
default:
ErrorMsg(ERROR_UNKNOWNVOICEDATA);
return(FALSE);
}
}
/*
* Given a filename, open that file for reading. Return the file descriptor.
*/
int OpenReadFile(char *filename)
{
int fd=NULL;
if ((fd = open(filename, O_RDONLY)) == -1)
{
if (ERR_OUTPUT_ALLOWED)
fprintf(stderr, "%s: ", filename);
ErrorMsg(ERROR_FINDFILE);
return(NULL);
}
else
return(fd);
}
/*
* Given a filename, return the size (in bytes) of that file.
* If the file cannot be found, return the error value returned by stat().
* which is -1.
*/
long FileSize(char *filename)
{
struct stat statbuf;
int result;
if ((result = stat(filename, &statbuf)) < 0)
return((long)result);
else
return(statbuf.st_size);
}