rename from .c to .cpp, and updated 23 year old IFF source code (1985)

This commit is contained in:
erwin.coumans
2008-10-26 23:47:35 +00:00
parent 2a721c7489
commit 14ba3eaff7
3 changed files with 128 additions and 126 deletions

View File

@@ -55,7 +55,8 @@ extern IFFP GetForm(GroupContext*);
extern IFFP GetProp(GroupContext*);
extern IFFP GetCat (GroupContext*);
void IFFCheck(name) char *name; {
void IFFCheck(char *name)
{
IFFP iffp;
BPTR file = fopen(name,"rb");//Open(name, MODE_OLDFILE);
Frame frame;
@@ -87,16 +88,14 @@ void main(int argc, char **argv)
/* ---------- Put... ---------------------------------------------------*/
PutLevels(count)
int count;
void PutLevels(int count)
{
for ( ; count > 0; --count) {
printf(".");
}
}
PutID(id)
ID id;
void PutID(int id)
{
long int i = 1;
const char *p = (const char *) &i;
@@ -114,14 +113,14 @@ ID id;
/* printf("id = %lx", id); */
}
PutN(n)
long n;
void PutN(long n)
{
printf(" %ld ", n);
}
/* Put something like "...BMHD 14" or "...LIST 14 PLBM". */
PutHdr(context) GroupContext *context; {
void PutHdr(GroupContext *context)
{
PutLevels( ((Frame *)context->clientFrame)->levels );
PutID(context->ckHdr.ckID);
PutN(context->ckHdr.ckSize);
@@ -135,8 +134,9 @@ PutHdr(context) GroupContext *context; {
/* ---------- AtLeaf ---------------------------------------------------*/
/* At Leaf chunk. That is, a chunk which does NOT contain other chunks.
* Print "ID size".*/
IFFP AtLeaf(context) GroupContext *context; {
* Print "int size".*/
IFFP AtLeaf(GroupContext *context)
{
PutHdr(context);
/* A typical reader would read the chunk's contents, using the "Frame"
@@ -148,7 +148,8 @@ IFFP AtLeaf(context) GroupContext *context; {
/* ---------- GetList --------------------------------------------------*/
/* Handle a LIST chunk. Print "LIST size subTypeID".
* Then dive into it.*/
IFFP GetList(parent) GroupContext *parent; {
IFFP GetList(GroupContext *parent)
{
Frame newFrame;
newFrame = *(Frame *)parent->clientFrame; /* copy parent's frame*/
@@ -162,9 +163,10 @@ IFFP GetList(parent) GroupContext *parent; {
/* ---------- GetForm --------------------------------------------------*/
/* Handle a FORM chunk. Print "FORM size subTypeID".
* Then dive into it.*/
IFFP GetForm(parent) GroupContext *parent; {
IFFP GetForm(GroupContext *parent)
{
/*CompilerBug register*/ IFFP iffp;
GroupContext new;
GroupContext newptr;
Frame newFrame;
newFrame = *(Frame *)parent->clientFrame; /* copy parent's frame*/
@@ -172,49 +174,51 @@ IFFP GetForm(parent) GroupContext *parent; {
PutHdr(parent);
iffp = OpenRGroup(parent, &new);
iffp = OpenRGroup(parent, &newptr);
CheckIFFP();
new.clientFrame = (ClientFrame *)&newFrame;
newptr.clientFrame = (ClientFrame *)&newFrame;
/* FORM reader for Checker. */
/* LIST, FORM, PROP, CAT already handled by GetF1ChunkHdr. */
do {if ( (iffp = GetF1ChunkHdr(&new)) > 0 )
iffp = AtLeaf(&new);
do {if ( (iffp = GetF1ChunkHdr(&newptr)) > 0 )
iffp = AtLeaf(&newptr);
} while (iffp >= IFF_OKAY);
CloseRGroup(&new);
CloseRGroup(&newptr);
return(iffp == END_MARK ? IFF_OKAY : iffp);
}
/* ---------- GetProp --------------------------------------------------*/
/* Handle a PROP chunk. Print "PROP size subTypeID".
* Then dive into it.*/
IFFP GetProp(listContext) GroupContext *listContext; {
IFFP GetProp(GroupContext *listContext)
{
/*CompilerBug register*/ IFFP iffp;
GroupContext new;
GroupContext newptr;
PutHdr(listContext);
iffp = OpenRGroup(listContext, &new);
iffp = OpenRGroup(listContext, &newptr);
CheckIFFP();
/* PROP reader for Checker. */
((Frame *)listContext->clientFrame)->levels++;
do {if ( (iffp = GetPChunkHdr(&new)) > 0 )
iffp = AtLeaf(&new);
do {if ( (iffp = GetPChunkHdr(&newptr)) > 0 )
iffp = AtLeaf(&newptr);
} while (iffp >= IFF_OKAY);
((Frame *)listContext->clientFrame)->levels--;
CloseRGroup(&new);
CloseRGroup(&newptr);
return(iffp == END_MARK ? IFF_OKAY : iffp);
}
/* ---------- GetCat ---------------------------------------------------*/
/* Handle a CAT chunk. Print "CAT size subTypeID".
* Then dive into it.*/
IFFP GetCat(parent) GroupContext *parent; {
IFFP GetCat(GroupContext *parent)
{
IFFP iffp;
((Frame *)parent->clientFrame)->levels++;

View File

@@ -19,58 +19,60 @@
/* ---------- Read -----------------------------------------------------*/
extern LONG PutID(); /** Added as a diagnostic aid, will remove later ***/
extern int PutID(); /** Added as a diagnostic aid, will remove later ***/
/* ---------- OpenRIFF --------------------------------------------------*/
IFFP OpenRIFF(file0, new0, clientFrame)
BPTR file0; GroupContext *new0; ClientFrame *clientFrame; {
IFFP OpenRIFF(BPTR file0, GroupContext *new0,ClientFrame *clientFrame)
{
register BPTR file = file0;
register GroupContext *new = new0;
register GroupContext *newtmp = new0;
IFFP iffp = IFF_OKAY;
new->parent = NL; /* "whole file" has no parent.*/
new->clientFrame = clientFrame;
new->file = file;
new->position = 0;
new->ckHdr.ckID = new->subtype = NULL_CHUNK;
new->ckHdr.ckSize = new->bytesSoFar = 0;
newtmp->parent = NL; /* "whole file" has no parent.*/
newtmp->clientFrame = clientFrame;
newtmp->file = file;
newtmp->position = 0;
newtmp->ckHdr.ckID = newtmp->subtype = NULL_CHUNK;
newtmp->ckHdr.ckSize = newtmp->bytesSoFar = 0;
/* Set new->bound. AmigaDOS specific code.*/
/* Set newtmp->bound. AmigaDOS specific code.*/
if (file <= 0) return(NO_FILE);
Seek(file, 0L, OFFSET_END); /* Seek to end of file.*/
new->bound = ftell(file);//Seek(file, 0L, OFFSET_CURRENT); /* Pos'n == #bytes in file.*/
if (new->bound < 0) return(DOS_ERROR); /* DOS being absurd.*/
newtmp->bound = ftell(file);//Seek(file, 0L, OFFSET_CURRENT); /* Pos'n == #bytes in file.*/
if (newtmp->bound < 0) return(DOS_ERROR); /* DOS being absurd.*/
Seek(file, 0L, OFFSET_BEGINNING); /* Go to file start.*/
/* Would just do this if Amiga DOS maintained fh_End: */
/* new->bound = (FileHandle *)BADDR(file)->fh_End; */
/* newtmp->bound = (FileHandle *)BADDR(file)->fh_End; */
if ( new->bound < (long)sizeof(ChunkHeader) )
if ( newtmp->bound < (long)sizeof(ChunkHeader) )
iffp = NOT_IFF;
return(iffp);
}
/* ---------- OpenRGroup -----------------------------------------------*/
IFFP OpenRGroup(parent0, new0) GroupContext *parent0, *new0; {
IFFP OpenRGroup(GroupContext* parent0,GroupContext* new0)
{
register GroupContext *parent = parent0;
register GroupContext *new = new0;
register GroupContext *newtmp = new0;
IFFP iffp = IFF_OKAY;
new->parent = parent;
new->clientFrame = parent->clientFrame;
new->file = parent->file;
new->position = parent->position;
new->bound = parent->position + ChunkMoreBytes(parent);
new->ckHdr.ckID = new->subtype = NULL_CHUNK;
new->ckHdr.ckSize = new->bytesSoFar = 0;
newtmp->parent = parent;
newtmp->clientFrame = parent->clientFrame;
newtmp->file = parent->file;
newtmp->position = parent->position;
newtmp->bound = parent->position + ChunkMoreBytes(parent);
newtmp->ckHdr.ckID = newtmp->subtype = NULL_CHUNK;
newtmp->ckHdr.ckSize = newtmp->bytesSoFar = 0;
if ( new->bound > parent->bound || IS_ODD(new->bound) )
if ( newtmp->bound > parent->bound || IS_ODD(newtmp->bound) )
iffp = BAD_IFF;
return(iffp);
}
/* ---------- CloseRGroup -----------------------------------------------*/
IFFP CloseRGroup(context) GroupContext *context; {
register LONG position;
IFFP CloseRGroup(GroupContext *context)
{
register int position;
if (context->parent == NL) {
} /* Context for whole file.*/
@@ -86,9 +88,7 @@ IFFP CloseRGroup(context) GroupContext *context; {
/* Skip over bytes in a context. Won't go backwards.*/
/* Updates context->position but not context->bytesSoFar.*/
/* This implementation is AmigaDOS specific.*/
IFFP SkipFwd(context, bytes)
GroupContext *context;
LONG bytes;
IFFP SkipFwd(GroupContext *context,int bytes)
{
IFFP iffp = IFF_OKAY;
@@ -130,18 +130,18 @@ int endianSwap32(int val)
/* ---------- GetChunkHdr ----------------------------------------------*/
ID GetChunkHdr(GroupContext *context0)
int GetChunkHdr(GroupContext *context0)
{
register GroupContext *context = context0;
register IFFP iffp;
LONG remaining;
int remaining;
/* Skip remainder of previous chunk & padding. */
iffp = SkipFwd(context,
ChunkMoreBytes(context) + IS_ODD(context->ckHdr.ckSize));
CheckIFFP();
/* Set up to read the new header. */
/* Set up to read the newtmp header. */
context->ckHdr.ckID = BAD_IFF; /* Until we know it's okay, mark it BAD.*/
context->subtype = NULL_CHUNK;
context->bytesSoFar = 0;
@@ -189,7 +189,7 @@ ID GetChunkHdr(GroupContext *context0)
context->position += (long)sizeof(ChunkHeader);
remaining -= (long)sizeof(ChunkHeader);
/* Non-positive ID values are illegal and used for error codes.*/
/* Non-positive int values are illegal and used for error codes.*/
/* We could check for other illegal IDs...*/
if (context->ckHdr.ckID <= 0 )
context->ckHdr.ckID = BAD_IFF;
@@ -201,14 +201,14 @@ ID GetChunkHdr(GroupContext *context0)
context->ckHdr.ckID = BAD_IFF;
}
/* Automatically read the LIST, FORM, PROP, or CAT subtype ID */
/* Automatically read the LIST, FORM, PROP, or CAT subtype int */
else {
if (context->ckHdr.ckID == LIST ||
context->ckHdr.ckID == FORM ||
context->ckHdr.ckID == PROP ||
context->ckHdr.ckID == CAT) {
iffp = IFFReadBytes(context, (BYTE *)&context->subtype,
(long)sizeof(ID));
(long)sizeof(int));
if (iffp != IFF_OKAY )
context->ckHdr.ckID = iffp;
}
@@ -218,10 +218,7 @@ ID GetChunkHdr(GroupContext *context0)
}
/* ---------- IFFReadBytes ---------------------------------------------*/
IFFP IFFReadBytes(context, buffer, nBytes)
GroupContext *context;
BYTE *buffer;
LONG nBytes;
IFFP IFFReadBytes(GroupContext *context,BYTE *buffer, int nBytes)
{
register IFFP iffp = IFF_OKAY;
@@ -250,14 +247,12 @@ IFFP SkipGroup( GroupContext* context)
} /* Nothing to do, thanks to GetChunkHdr */
/* ---------- ReadIFF --------------------------------------------------*/
IFFP ReadIFF(file, clientFrame)
BPTR file;
ClientFrame *clientFrame;
IFFP ReadIFF(BPTR file,ClientFrame *clientFrame)
{
/*CompilerBug register*/ IFFP iffp;
GroupContext context;
iffp = OpenRIFF(file, &context);
iffp = OpenRIFF(file, &context,clientFrame);
context.clientFrame = clientFrame;
if (iffp == IFF_OKAY) {
@@ -274,15 +269,13 @@ ClientFrame *clientFrame;
}
CloseRGroup(&context);
if (iffp > 0 ) /* Make sure we don't return an ID.*/
if (iffp > 0 ) /* Make sure we don't return an int.*/
iffp = NOT_IFF; /* GetChunkHdr should've caught this.*/
return(iffp);
}
/* ---------- ReadIList ------------------------------------------------*/
IFFP ReadIList(parent, clientFrame)
GroupContext *parent;
ClientFrame *clientFrame;
IFFP ReadIList(GroupContext *parent,ClientFrame *clientFrame)
{
GroupContext listContext;
IFFP iffp;
@@ -327,15 +320,15 @@ ClientFrame *clientFrame;
/* ---------- ReadICat -------------------------------------------------*/
/* By special arrangement with the ReadIList implement'n, this is trivial.*/
IFFP ReadICat(parent) GroupContext *parent; {
IFFP ReadICat(GroupContext *parent)
{
return( ReadIList(parent, parent->clientFrame));//NL) );
}
/* ---------- GetFChunkHdr ---------------------------------------------*/
ID GetFChunkHdr(context)
GroupContext *context;
int GetFChunkHdr(GroupContext *context)
{
register ID id;
register int id;
id = GetChunkHdr(context);
if (id == PROP)
@@ -344,8 +337,9 @@ GroupContext *context;
}
/* ---------- GetF1ChunkHdr ---------------------------------------------*/
ID GetF1ChunkHdr(context) GroupContext *context; {
register ID id;
int GetF1ChunkHdr(GroupContext *context)
{
register int id;
register ClientFrame *clientFrame = context->clientFrame;
id = GetChunkHdr(context);
@@ -365,10 +359,9 @@ ID GetF1ChunkHdr(context) GroupContext *context; {
}
/* ---------- GetPChunkHdr ---------------------------------------------*/
ID GetPChunkHdr(context)
GroupContext *context;
int GetPChunkHdr(GroupContext *context)
{
register ID id;
register int id;
id = GetChunkHdr(context);
if (id == LIST || id == FORM || id == PROP || id == CAT )

View File

@@ -23,17 +23,18 @@
/* ---------- OpenWIFF -------------------------------------------------*/
IFFP OpenWIFF(file, new0, limit) BPTR file; GroupContext *new0; LONG limit; {
register GroupContext *new = new0;
IFFP OpenWIFF(BPTR file, GroupContext *new0, int limit)
{
register GroupContext *newtmp = new0;
register IFFP iffp = IFF_OKAY;
new->parent = NULL;
new->clientFrame = NULL;
new->file = file;
new->position = 0;
new->bound = limit;
new->ckHdr.ckID = NULL_CHUNK; /* indicates no current chunk */
new->ckHdr.ckSize = new->bytesSoFar = 0;
newtmp->parent = NULL;
newtmp->clientFrame = NULL;
newtmp->file = file;
newtmp->position = 0;
newtmp->bound = limit;
newtmp->ckHdr.ckID = NULL_CHUNK; /* indicates no current chunk */
newtmp->ckHdr.ckSize = newtmp->bytesSoFar = 0;
if (0 > Seek(file, 0, OFFSET_BEGINNING)) /* Go to start of the file.*/
iffp = DOS_ERROR;
@@ -43,46 +44,48 @@ IFFP OpenWIFF(file, new0, limit) BPTR file; GroupContext *new0; LONG limit; {
}
/* ---------- StartWGroup ----------------------------------------------*/
IFFP StartWGroup(parent, groupType, groupSize, subtype, new)
GroupContext *parent, *new; ID groupType, subtype; LONG groupSize; {
IFFP StartWGroup( GroupContext* parent, int groupType,int groupSize,int subtype, GroupContext * newtmp)
{
register IFFP iffp;
iffp = PutCkHdr(parent, groupType, groupSize);
IfIffp( IFFWriteBytes(parent, (BYTE *)&subtype, sizeof(ID)) );
IfIffp( OpenWGroup(parent, new) );
IfIffp( IFFWriteBytes(parent, (char *)&subtype, sizeof(int)) );
IfIffp( OpenWGroup(parent, newtmp) );
return(iffp);
}
/* ---------- OpenWGroup -----------------------------------------------*/
IFFP OpenWGroup(parent0, new0) GroupContext *parent0, *new0; {
IFFP OpenWGroup(GroupContext *parent0,GroupContext* new0)
{
register GroupContext *parent = parent0;
register GroupContext *new = new0;
register LONG ckEnd;
register GroupContext *newtmp = new0;
register int ckEnd;
register IFFP iffp = IFF_OKAY;
new->parent = parent;
new->clientFrame = parent->clientFrame;
new->file = parent->file;
new->position = parent->position;
new->bound = parent->bound;
new->ckHdr.ckID = NULL_CHUNK;
new->ckHdr.ckSize = new->bytesSoFar = 0;
newtmp->parent = parent;
newtmp->clientFrame = parent->clientFrame;
newtmp->file = parent->file;
newtmp->position = parent->position;
newtmp->bound = parent->bound;
newtmp->ckHdr.ckID = NULL_CHUNK;
newtmp->ckHdr.ckSize = newtmp->bytesSoFar = 0;
if ( Known(parent->ckHdr.ckSize) ) {
ckEnd = new->position + ChunkMoreBytes(parent);
if ( new->bound == szNotYetKnown || new->bound > ckEnd )
new->bound = ckEnd;
ckEnd = newtmp->position + ChunkMoreBytes(parent);
if ( newtmp->bound == szNotYetKnown || newtmp->bound > ckEnd )
newtmp->bound = ckEnd;
};
if ( parent->ckHdr.ckID == NULL_CHUNK || /* not currently writing a chunk*/
IS_ODD(new->position) ||
(Known(new->bound) && IS_ODD(new->bound)) )
IS_ODD(newtmp->position) ||
(Known(newtmp->bound) && IS_ODD(newtmp->bound)) )
iffp = CLIENT_ERROR;
return(iffp);
}
/* ---------- CloseWGroup ----------------------------------------------*/
IFFP CloseWGroup(old0) GroupContext *old0; {
IFFP CloseWGroup(GroupContext *old0)
{
register GroupContext *old = old0;
if ( old->ckHdr.ckID != NULL_CHUNK ) /* didn't close the last chunk */
@@ -99,7 +102,8 @@ IFFP CloseWGroup(old0) GroupContext *old0; {
}
/* ---------- EndWGroup ------------------------------------------------*/
IFFP EndWGroup(old) GroupContext *old; {
IFFP EndWGroup(GroupContext *old)
{
register GroupContext *parent = old->parent;
register IFFP iffp;
@@ -110,8 +114,8 @@ IFFP EndWGroup(old) GroupContext *old; {
}
/* ---------- PutCk ----------------------------------------------------*/
IFFP PutCk(context, ckID, ckSize, data)
GroupContext *context; ID ckID; LONG ckSize; BYTE *data; {
IFFP PutCk(GroupContext *context, int ckID,int ckSize,char *data)
{
register IFFP iffp = IFF_OKAY;
if ( ckSize == szNotYetKnown )
@@ -123,14 +127,14 @@ GroupContext *context; ID ckID; LONG ckSize; BYTE *data; {
}
/* ---------- PutCkHdr -------------------------------------------------*/
IFFP PutCkHdr(context0, ckID, ckSize)
GroupContext *context0; ID ckID; LONG ckSize; {
IFFP PutCkHdr(GroupContext *context0, int ckID, int ckSize)
{
register GroupContext *context = context0;
LONG minPSize = sizeof(ChunkHeader); /* physical chunk >= minPSize bytes*/
int minPSize = sizeof(ChunkHeader); /* physical chunk >= minPSize bytes*/
/* CLIENT_ERROR if we're already inside a chunk or asked to write
* other than one FORM, LIST, or CAT at the top level of a file */
/* Also, non-positive ID values are illegal and used for error codes.*/
/* Also, non-positive int values are illegal and used for error codes.*/
/* (We could check for other illegal IDs...)*/
if ( context->ckHdr.ckID != NULL_CHUNK || ckID <= 0 )
return(CLIENT_ERROR);
@@ -157,7 +161,7 @@ GroupContext *context0; ID ckID; LONG ckSize; {
context->bytesSoFar = 0;
{
context->ckHdr.ckSize = endianSwap32(context->ckHdr.ckSize);
if (0 > GWrite(context->file, &context->ckHdr, sizeof(ChunkHeader)))
if (0 > fwrite(&context->ckHdr, 1,sizeof(ChunkHeader),context->file))
return(DOS_ERROR);
context->ckHdr.ckSize = endianSwap32(context->ckHdr.ckSize);
}
@@ -166,8 +170,8 @@ GroupContext *context0; ID ckID; LONG ckSize; {
}
/* ---------- IFFWriteBytes ---------------------------------------------*/
IFFP IFFWriteBytes(context0, data, nBytes)
GroupContext *context0; BYTE *data; LONG nBytes; {
IFFP IFFWriteBytes(GroupContext *context0, char *data, int nBytes)
{
register GroupContext *context = context0;
if ( context->ckHdr.ckID == NULL_CHUNK || /* not in a chunk */
@@ -180,7 +184,7 @@ GroupContext *context0; BYTE *data; LONG nBytes; {
return(CLIENT_ERROR);
}
if (0 > GWrite(context->file, data, nBytes))
if (0 > fwrite(data, 1,nBytes,context->file))
return(DOS_ERROR);
context->bytesSoFar += nBytes;
@@ -189,7 +193,8 @@ GroupContext *context0; BYTE *data; LONG nBytes; {
}
/* ---------- PutCkEnd -------------------------------------------------*/
IFFP PutCkEnd(context0) GroupContext *context0; {
IFFP PutCkEnd(GroupContext *context0)
{
register GroupContext *context = context0;
WORD zero = 0; /* padding source */
@@ -198,7 +203,7 @@ IFFP PutCkEnd(context0) GroupContext *context0; {
if ( context->ckHdr.ckSize == szNotYetKnown ) {
/* go back and set the chunk size to bytesSoFar */
int offset = context->bytesSoFar+sizeof(LONG);
int offset = context->bytesSoFar+sizeof(int);
if ( 0 > GSeek(context->file,
-(offset),
OFFSET_CURRENT))
@@ -207,7 +212,7 @@ IFFP PutCkEnd(context0) GroupContext *context0; {
} else
{
context->bytesSoFar = endianSwap32(context->bytesSoFar);
if (0 > GWrite(context->file, &context->bytesSoFar, sizeof(LONG)))
if (0 > fwrite(&context->bytesSoFar, 1,sizeof(int),context->file))
return (DOS_ERROR);
context->bytesSoFar = endianSwap32(context->bytesSoFar);
if (0 > GSeek(context->file, context->bytesSoFar, OFFSET_CURRENT) )
@@ -226,7 +231,7 @@ IFFP PutCkEnd(context0) GroupContext *context0; {
* overwritten the context, if we're on an odd position there must
* be room for a pad byte. */
if ( IS_ODD(context->bytesSoFar) ) {
if ( 0 > GWrite(context->file, &zero, 1) )
if ( 0 > fwrite(&zero, 1,1,context->file) )
return(DOS_ERROR);
context->position += 1;
};