# HG changeset patch # User mas01cr # Date 1194531527 0 # Node ID cdd441dcc9a82c9c0f252d25c2099c5aeabb1536 # Parent ecfa25f72b7e8edf1c846d16f6b58580c091a057 Better error detection (and messages) on invalid feature files Factor out commonality of dealing with the input file (in audioDB::initTables and audioDB::batchinsert) into initInputFile(); call it appropriately. diff -r ecfa25f72b7e -r cdd441dcc9a8 audioDB.cpp --- a/audioDB.cpp Wed Nov 07 12:03:28 2007 +0000 +++ b/audioDB.cpp Thu Nov 08 14:18:47 2007 +0000 @@ -472,35 +472,48 @@ timesTable = (double *) (db + dbH->timesTableOffset); } +void audioDB::initInputFile (const char *inFile) { + if (inFile) { + if ((infid = open(inFile, O_RDONLY)) < 0) { + error("can't open input file for reading", inFile, "open"); + } + + if (fstat(infid, &statbuf) < 0) { + error("fstat error finding size of input", inFile, "fstat"); + } + + if(dbH->dim == 0 && dbH->length == 0) { // empty database + // initialize with input dimensionality + if(read(infid, &dbH->dim, sizeof(unsigned)) != sizeof(unsigned)) { + error("short read of input file", inFile); + } + if(dbH->dim == 0) { + error("dimensionality of zero in input file", inFile); + } + } else { + unsigned test; + if(read(infid, &test, sizeof(unsigned)) != sizeof(unsigned)) { + error("short read of input file", inFile); + } + if(dbH->dim == 0) { + error("dimensionality of zero in input file", inFile); + } + if(dbH->dim != test) { + cerr << "error: expected dimension: " << dbH->dim << ", got : " << test <dim == 0 && dbH->length == 0) // empty database - // initialize with input dimensionality - read(infid, &dbH->dim, sizeof(unsigned)); - else { - unsigned test; - read(infid, &test, sizeof(unsigned)); - if(dbH->dim != test) { - cerr << "error: expected dimension: " << dbH->dim << ", got : " << test <length+statbuf.st_size-sizeof(int))) - error("No more room in database","insert failed: reason database is full."); + error("Insert failed: no more room in database", inFile); if(!key) key=inFile; @@ -675,36 +688,12 @@ if(filesIn->eof()) break; - // open the input file - if (thisFile && (infid = open (thisFile, O_RDONLY)) < 0) - error("can't open feature file for reading", thisFile, "open"); - - // find size of input file - if (thisFile && fstat (infid,&statbuf) < 0) - error("fstat error finding size of input", "", "fstat"); + initInputFile(thisFile); // Check that there is room for at least 1 more file if((char*)timesTable<((char*)dataBuf+(dbH->length+statbuf.st_size-sizeof(int)))) - error("No more room in database","insert failed: reason database is full."); + error("batchinsert failed: no more room in database", thisFile); - if(thisFile) - if(dbH->dim==0 && dbH->length==0) // empty database - read(infid,&dbH->dim,sizeof(unsigned)); // initialize with input dimensionality - else { - unsigned test; - read(infid,&test,sizeof(unsigned)); - if(dbH->dim!=test){ - cerr << "error: expected dimension: " << dbH->dim << ", got :" << test <