/*

    STRIPIFF.c

    allows to filter chunks out of a FORM-IFF-file

    © 1990 by Ølli
              Landsberge 5
              4322 Sprockhövel
              West Germany

    this source is for the Lettuce-C 5.04

*/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>


#define readlong() fread((char*)&lb,4,1,inf)
#define writelong(val) lb=val; outsize+=4; if(!fwrite((char*)&lb,4,1,outf)) { printf("Write error!\n"); fclose(outf); unlink(outname); exit(20); }

/* Protos */

int main(int,char**);
void exit(int);
void copychunk(void);
void skipchunk(void);


/* Global stuff */

FILE *inf,*outf;
char outname[256];
long lb=0,xymox=0,outsize=0;
long ofchunk[128];


/* Voila: the functions */

void skipchunk()
{
    readlong();
    fseek(inf,lb,1);
    printf("removed\n");
}

void copychunk()
{
    int c;
    writelong(lb);
    readlong();
    writelong(lb);
    if(lb&1) lb++;
    for(c=0; c<lb; c++) {
        fputc(fgetc(inf),outf);
    }
    outsize+=lb;
    printf("copied\n");
}

int main(argc,argv)
int argc;
char **argv;
{
    int exclude=0,c;
    int chunkp=2,numchunk=0;
    char ar[4];

    printf("StripIFF > Form-IFF Chunk-Filter < © 1990 by Ølli\n");
    if(argc<2||*argv[1]=='?') {
        printf("Filters chunks out of FORM-IFF files\nUsage: StripIFF iff-file [-onewfile] [-e] chunk-names\n'-e' declares chunk-names to be excluded from the file\n\
Example: 'StripIFF test.iff -e CMAP' will create a file 'test.stripped' with all CMAP-chunks removed\n");
        exit(0);
    }
    if(*argv[2]=='-') {
        chunkp++;
        if(argv[2][1]=='o') strcpy(outname,&argv[2][2]);
        else if(argv[2][1]=='e') exclude=1;
        else {
            printf("Unknown option: %s\n",argv[2]);
            exit(20);
        }
    }
    if(*argv[3]=='-') {
        chunkp++;
        if(argv[3][1]=='e') exclude=1;
        else {
            printf("Unknown option: %s\n",argv[3]);
            exit(20);
        }
    }
    if(!outname[0]) {
        strsfn(argv[1],0,0,outname,0);
        strcat(outname,".stripped");
    }
    for(;chunkp<argc;chunkp++) {
        ar[0]=*argv[chunkp];
        ar[1]=argv[chunkp][1];
        ar[2]=argv[chunkp][2];
        ar[3]=argv[chunkp][3];
        ofchunk[numchunk++]=*(long*)ar;
    }
    if(!(inf=fopen(argv[1],"r"))) {
        printf("Unable to open input file!\n");
        exit(20);
    }
    readlong();
    if(lb!='FORM') {
        printf("File is not a FORM-IFF!\n");
        exit(20);
    }
    readlong();
    if(!(outf=fopen(outname,"w"))) {
        printf("Unable to open output file!\n");
        exit(20);
    }
    writelong('FORM');
    writelong(0);
    readlong();
    writelong(lb);
    while(readlong()) {
        printf("found chunk '%s', ",&lb);
        for(c=0; c<numchunk; c++) {
            if(lb==ofchunk[c]) {
                if(exclude) skipchunk();
                else copychunk();
                goto processed;
            }
        }
        if(exclude) copychunk();
        else skipchunk();
processed:;
    }
    fseek(outf,4,0);
    writelong(outsize-8);
    printf("processing completed; final output file size = %d\n",outsize-4);
    exit(0);
}

