Added rtti-reader and made various cleanups, switching to Shutdown_M for error handling

This commit is contained in:
Fatbag 2012-12-03 12:31:24 -06:00
parent 5444c9aea6
commit 6dddbd2efa
25 changed files with 771 additions and 194 deletions

View file

@ -23,7 +23,21 @@
#include <time.h>
#include "iff.h"
int charmatches(char c, const char * filter){
static FILE * hFile = NULL;
static uint8_t * IFFData = NULL;
static IFFFile IFFFileInfo;
static int iffcreated = 0;
static void Shutdown_M(const char * Message){
fprintf(stderr, "iffexport: error: %s.", Message);
if(iffcreated)
iff_delete(&IFFFileInfo);
free(IFFData);
fclose(hFile);
exit(EXIT_FAILURE);
}
static int charmatches(char c, const char * filter){
while(*filter){
if(c == *filter) return 1;
filter++;
@ -32,18 +46,15 @@ int charmatches(char c, const char * filter){
}
int main(int argc, char *argv[]){
FILE * hFile;
int overwrite = 0;
char *InFile, *OutDirectory;
const char *InFile, *OutDirectory;
size_t FileSize;
uint8_t * IFFData;
clock_t BeginningTime;
unsigned chunkcount, chunk;
unsigned exported = 0;
IFFFile IFFFileInfo;
IFFChunk * ChunkData;
if(argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
if(argc < 3 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
printf("Usage: iffexport [-f] infile outdirectory\n"
"Export the resources of an EA IFF file.\n"
"Use -f to force overwriting without confirmation.\n"
@ -68,50 +79,39 @@ int main(int argc, char *argv[]){
*/
hFile = fopen(InFile, "rb");
if(hFile == NULL){
printf("%sThe specified input file does not exist or could not be opened for reading.", "iffexport: error: ");
return -1;
}
if(hFile == NULL)
Shutdown_M("The specified input file does not exist or could not be opened for reading");
fseek(hFile, 0, SEEK_END);
FileSize = ftell(hFile);
if(FileSize < 24){
printf("%sNot a valid IFF file.", "iffexport: error: ");
return -1;
}
if(FileSize < 24)
Shutdown_M("Not a valid IFF file");
fseek(hFile, 0, SEEK_SET);
IFFData = malloc(FileSize);
if(IFFData == NULL){
printf("%sMemory for this file could not be allocated.", "iffexport: error: ");
return -1;
}
if(!fread(IFFData, FileSize, 1, hFile)){
printf("%sThe input file could not be read.", "iffexport: error: ");
return -1;
}
fclose(hFile);
if(IFFData == NULL)
Shutdown_M("Memory for this file could not be allocated");
if(fread(IFFData, 1, FileSize, hFile) != FileSize)
Shutdown_M("The input file could not be read");
fclose(hFile); hFile = NULL;
/****
** Load header information
*/
if(!iff_create(&IFFFileInfo)){
printf("%sMemory for this file could not be allocated.", "iffexport: error: ");
return -1;
}
if(!iff_read_header(&IFFFileInfo, IFFData, FileSize)){
printf("%sNot a valid IFF file.", "iffexport: error: ");
return -1;
}
if(!iff_create(&IFFFileInfo))
Shutdown_M("Memory for this file could not be allocated");
iffcreated++;
if(!iff_read_header(&IFFFileInfo, IFFData, FileSize))
Shutdown_M("Not a valid IFF file");
/****
** Load entry information
*/
if(!iff_enumerate_chunks(&IFFFileInfo, IFFData+64, FileSize-64)){
printf("%sChunk data is corrupt.", "iffexport: error: ");
return -1;
}
if(!iff_enumerate_chunks(&IFFFileInfo, IFFData+64, FileSize-64))
Shutdown_M("Chunk data is corrupt");
free(IFFData); IFFData = NULL;
chunkcount = IFFFileInfo.ChunkCount;
printf("This IFF file contains %u chunks.\n\nExporting\n", chunkcount);
@ -144,21 +144,19 @@ int main(int argc, char *argv[]){
if(hFile != NULL){
/* File exists */
char c;
fclose(hFile);
fclose(hFile); hFile = NULL;
printf("File \"%s\" exists.\nContinue anyway? (y/n) ", destination);
c = getchar();
if(c != 'y' && c != 'Y'){
printf("\nAborted.");
return -1;
printf("\n");
Shutdown_M("Aborted");
}
}
overwrite++;
}
hFile = fopen(destination, "wb");
if(hFile == NULL){
printf("%sThe output file could not be opened for writing.", "iffexport: error: ");
return -1;
}
if(hFile == NULL)
Shutdown_M("The output file could not be opened for writing");
fwrite(ChunkData->Data, 1, ChunkData->Size, hFile);
fclose(hFile);
@ -168,5 +166,7 @@ int main(int argc, char *argv[]){
printf("\nExported %u of %u chunks in %.2f seconds.", exported, chunkcount,
((float) (clock() - BeginningTime))/CLOCKS_PER_SEC);
iff_delete(&IFFFileInfo);
return 0;
}