sccvt.c++ 3.73 KB
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <bstring.h>

#include "instfile.h"
#include "sccvt.h"

static char usage[] = "[-verbose] infile";

main(int argc, char *argv[])
{
    FileHeader          fhdr;
    FILE                *ifile;
    FILE                *objFile;
    FILE                *wavFile;
    int                 done=0;
    extern char         *optarg;
    extern int          optind;
    int                 errflg=0;
    int                 c;
    int                 list;
    int                 verbose=0;
    
    /*
     * process command line arguments
     */
    while ((c = getopt(argc, argv, "vdl")) != EOF) {        
        switch (c) {
            case 'd':
                break;
            case 'l':
                list = 1;
                break;

            case 'v':
                verbose = 1;
                break;
                
            case '?':
                errflg++;
                break;
        }
    }
    
    if (errflg) {
        fprintf(stderr, "%s %s\n", argv[0], usage);
    }

    char *fname = argv[optind];
    if ((ifile = fopen(fname, "r")) == NULL){
        fprintf(stderr,"Couldn't open %s\n",argv[1]);
        exit(1);
    }
    
    char *tmp = (char *)malloc(strlen(fname) + strlen(".bnk") + 1);
    strcpy(tmp, fname);
    strcat(tmp, ".bnk");
    printf("objFile = %s\n", tmp);
    objFile = fopen(tmp, "w");
    assert(objFile);
    free(tmp);
    
    tmp = (char *)malloc(strlen(fname) + strlen(".tbl") + 1);
    strcpy(tmp, fname);
    strcat(tmp, ".tbl");
    printf("wavFile = %s\n", tmp);
    wavFile = fopen(tmp, "w");
    assert(objFile);
    free(tmp);
    
    fread(&fhdr, sizeof(FileHeader), 1, ifile);

    if (0) {
        printf("Type = %.4s\n", &fhdr.ckId);
        printf("size = %x\n", fhdr.ckSize);
        printf("Format = %.4s\n", &fhdr.format);
    }
    
    fclose(ifile);    
    
    SCInstrumentFile *file;

    ICObjTab *objtab = new ICObjTab();
    ICSymTab *symtab = new ICSymTab();
    ICWavTab *wavtab = new ICWavTab();
    
    ICBankFile *b = new ICBankFile();
    assert(b);
    objtab->Append(b);
    
    ICBank *bank = new ICBank();
    assert(bank);
    objtab->Append(bank);
    b->AddBank(bank, 0);
    
    switch (fhdr.format) {
        case ('SCsd'):
        case ('SCin'):
            file = new SCInstrumentFile(fname, "r");
            if (!file)
                exit(-1);
            break;
            
        case ('MixD'):
            break;

        default:
            printf("error: unrecognized file format %.4s in %s\n",
                   &fhdr.format, fname);
            exit(-1);
            break;
    }

    bank->AddProgram(file->GetICInst(), 0);
    
    file->SetObjTab(objtab);
    file->SetWavTab(wavtab);
    file->SetSymTab(symtab);
    
    file->Parse();

    if (list) {
        file->ListFiles();
    }

    wavtab->LayOut();
    objtab->LayOut();
    
    if (verbose) {
        objtab->Print();
        wavtab->Print();
    }
    
    objtab->Write(objFile);
    wavtab->Write(wavFile);
    
//    file->Close();    
}

char *p2cstr(char *s)
{
    int  i;
    int  size = *s;
    char *d = (char *)malloc(size+1);

    for (i = 0; i < size; i++) {
        d[i] = s[i+1];
    }

    d[i] = 0;

    return d;
}

char *p2cstri(char *s)
{
    int  i;
    int  size = *s;

    for (i = 0; i < size; i++) {
        s[i] = s[i+1];
    }

    s[i] = 0;

    return s;
}

        
void printBuffer(char *b, int size)
{
    int         i,j;
    
    for (i = 0; i < size/8; i++) {
        printf("\t");
        for (j = 0; j < 8; j++) {
            printf("%.2x ", b[8*i +j]);
        }
        printf("\n");
    }

    printf("\t");
    for (i = 8*i; i < size; i++) {
        printf("%.2x ", b[i]);
    }
    printf("\n");
}