author | ecalot
<ecalot> 2005-06-10 13:41:10 UTC |
committer | ecalot
<ecalot> 2005-06-10 13:41:10 UTC |
parent | e98c8432f0a15448e2aabcdb9585a3e2d6342de3 |
PR/src/console/main.c | +15 | -12 |
PR/src/include/common.h | +15 | -11 |
PR/src/include/en.lang.pr.h | +10 | -7 |
PR/src/lib/actions/classify.c | +4 | -3 |
PR/src/lib/actions/import.c | +9 | -10 |
PR/src/lib/formats/pal.c | +2 | -0 |
PR/src/lib/layers/dat.c | +15 | -2 |
PR/src/lib/layers/disk.c | +14 | -6 |
PR/src/lib/xml/parse.c | +1 | -1 |
diff --git a/PR/src/console/main.c b/PR/src/console/main.c index fdd447b..8848f97 100644 --- a/PR/src/console/main.c +++ b/PR/src/console/main.c @@ -66,9 +66,8 @@ int main (int argc, char **argv) { char* extension =NULL; char* resFile =NULL; char* file; - /*char* exportErrors[] =PR_TEXT_EXPORT_ARRAY; - char* classifyErrors[] =PR_TEXT_CLASSIFY_ARRAY; - char* importErrors[] =PR_TEXT_IMPORT_ARRAY;*/ + char* errors[] =PR_TEXT_ERRORS; + char* types[] =PR_TEXT_TYPES; int c,result; int optionflag=0; @@ -197,25 +196,29 @@ int main (int argc, char **argv) { /* import */ fprintf(outputStream,PR_TEXT_TASK_COMPILE,file,dirName); result=prImportDatOpt(file,dirName,resFile,optionflag,dat,extension); - /* if (result>0) { fprintf(outputStream,PR_TEXT_RESULT_ERR,result,result); } else { - fprintf(outputStream,PR_TEXT_RESULT,importErrors[-result],result); + fprintf(outputStream,PR_TEXT_RESULT,errors[-result],result); } - */ - fprintf(outputStream,"result: %d\n",result); } else if (hasFlag(export_flag)) { /* export */ - /*fprintf(outputStream,PR_TEXT_TASK_EXTRACT,file,dirName);*/ + fprintf(outputStream,PR_TEXT_TASK_EXTRACT,file,dirName); result=prExportDatOpt(file,dirName,resFile,optionflag,dat,datAuthor,extension); - /*fprintf(outputStream,PR_TEXT_RESULT,exportErrors[-result],result);*/ - fprintf(outputStream,"result: %d\n",result); + if (result>0) { + fprintf(outputStream,PR_TEXT_EXPORT_OK,result,result); + } else { + fprintf(outputStream,PR_TEXT_RESULT,errors[-result],result); + } } else { /* classify */ + fprintf(outputStream,PR_TEXT_TASK_CLASSIFY,file); result=prClassifyDat(file); - /*fprintf(outputStream,PR_TEXT_RESULT,classifyErrors[result+2],result);*/ - fprintf(outputStream,"result: %d\n",result); + if (result>0) { + fprintf(outputStream,"Result: Detected as %s (%d)\n",types[result],result); + } else { + fprintf(outputStream,PR_TEXT_RESULT,errors[-result],result); + } } free(file); free(datfile); diff --git a/PR/src/include/common.h b/PR/src/include/common.h index ebe9cce..a35c085 100644 --- a/PR/src/include/common.h +++ b/PR/src/include/common.h @@ -176,18 +176,22 @@ PARSING_OPTRAW\ #define hasFlag(a) (optionflag&(a)) #define setFlag(a) optionflag|=(a) -#define PR_RESULT_SUCCESS 0 #define PR_RESULT_ERR_EXTRACTION -1 -#define PR_RESULT_ERR_FILE_DAT_NOTFOUND -2 -#define PR_RESULT_ERR_FILE_DAT_NOTOPEN -3 -#define PR_RESULT_ERR_FILE_DAT_NOTOPEN_W -3 -#define PR_RESULT_ERR_FILE_NOTFOUND -4 -#define PR_RESULT_ERR_FILE_NOTOPEN -5 -#define PR_RESULT_ERR_INVALID_DAT -6 -#define PR_RESULT_ERR_MEMORY -7 -#define PR_RESULT_ERR_XML_ATTR -8 -#define PR_RESULT_ERR_XML_FILE -9 -#define PR_RESULT_ERR_XML_PARSING -10 +#define PR_RESULT_ERR_FILE_DAT_NOT_OPEN_NOTFOUND -2 +#define PR_RESULT_ERR_FILE_DAT_NOT_OPEN_WASDIR -3 +#define PR_RESULT_ERR_FILE_DAT_NOT_READ_ACCESS -4 +#define PR_RESULT_ERR_FILE_DAT_NOT_WRITE_ACCESS -5 +#define PR_RESULT_ERR_FILE_NOT_OPEN_NOTFOUND -6 +#define PR_RESULT_ERR_FILE_NOT_OPEN_WASDIR -7 +#define PR_RESULT_ERR_FILE_NOT_READ_ACCESS -8 +#define PR_RESULT_ERR_FILE_NOT_WRITE_ACCESS -9 +#define PR_RESULT_ERR_INVALID_DAT -10 +#define PR_RESULT_ERR_MEMORY -11 +#define PR_RESULT_ERR_XML_ATTR -12 +#define PR_RESULT_ERR_XML_FILE -13 +#define PR_RESULT_ERR_XML_PARSING -14 +#define PR_RESULT_SUCCESS 0 + #define PR_RESULT_TYPE_1BINARY 1 #define PR_RESULT_TYPE_1GRAPHIC_BAD 2 #define PR_RESULT_TYPE_1GRAPHIC_PAL 3 diff --git a/PR/src/include/en.lang.pr.h b/PR/src/include/en.lang.pr.h index f1f05d7..edeb4a8 100644 --- a/PR/src/include/en.lang.pr.h +++ b/PR/src/include/en.lang.pr.h @@ -69,6 +69,7 @@ pr.h: Princed Resources : English language strings #define PR_TEXT_IMPORT_PLV_WARN "Warning: PLV file may be corrupt\n" #define PR_TEXT_EXPORT_WORKING "'%s' successfully exported\n" +#define PR_TEXT_EXPORT_OK "%d files successfully exported (%d)\n" #define PR_TEXT_EXPORT_ERROR "'%s' has errors, aborting...\n" #define PR_TEXT_EXPORT_BMP_WARN "Warning: Extracted file may be corrupt\n" @@ -76,20 +77,22 @@ pr.h: Princed Resources : English language strings | Text Output Arrays | \***************************************************************/ -#define PR_TEXT_EXPORT_ARRAY {\ -"Ok",\ +#define PR_TEXT_ERRORS {\ +"Success",\ +"-1","-2","-3","-4","-5","-6","-7","-8","-9","-10",\ +"-11","-12","-13","-14","-15","-16","-17","-18","-19","-20"} + + /* "Error accessing a file",\ "Memory error in extraction",\ "Invalid DAT file",\ "XML Parsing error",\ "Memory error in parsing",\ "XML Attribute not recognized",\ -"XML File not found"} +"XML File not found"}*/ -#define PR_TEXT_CLASSIFY_ARRAY {\ -"Memory error",\ -"File not found or no access error",\ -"Not a valid DAT file",\ +#define PR_TEXT_TYPES {\ +NULL,\ "Levels file",\ "Graphic DAT file without palette",\ "Waves/Digital sound file",\ diff --git a/PR/src/lib/actions/classify.c b/PR/src/lib/actions/classify.c index ffd0d67..327620e 100644 --- a/PR/src/lib/actions/classify.c +++ b/PR/src/lib/actions/classify.c @@ -43,7 +43,7 @@ tasks.c: Princed Resources : Classify a DAT file | Get the type of a DAT file | \***************************************************************/ -#define READ_ERROR {mReadCloseDatFile();return 0;} +#define READ_ERROR {mReadCloseDatFile();return PR_RESULT_ERR_INVALID_DAT;} /* TODO: centralize all return values in defines. Change the interfaces. */ @@ -53,10 +53,10 @@ int prClassifyDat(const char* vFiledat) { unsigned short int numberOfItems; tPopVersion popVersion; tResource res; - int ok; + int error; /* Initialize abstract variables to read this new DAT file */ - if ((ok=mReadBeginDatFile(&numberOfItems,vFiledat))) return ok+1; /* -1 if not found or empty, 0 if invalid */ + if ((error=mReadBeginDatFile(&numberOfItems,vFiledat))) return error; popVersion=mReadGetVersion(); @@ -88,6 +88,7 @@ int prClassify(const char* fileName) { /* let's get it's content and see what it is */ fileSize=mLoadFileArray(fileName,&fileData); + if (fileSize<=0) return fileSize; /* 2) let's compare the size with a .sav size */ if (fileSize==8) { diff --git a/PR/src/lib/actions/import.c b/PR/src/lib/actions/import.c index ab938c4..ba5b97e 100644 --- a/PR/src/lib/actions/import.c +++ b/PR/src/lib/actions/import.c @@ -96,7 +96,7 @@ int fullCompile(const char* vFiledat, const char* vDirExt, tResourceList* r, int const tResource* res; tResource newRes; - if (mWriteBeginDatFile(vFiledat,optionflag)) return PR_RESULT_ERR_FILE_DAT_NOTOPEN_W; /* File couldn't be open */ + if (mWriteBeginDatFile(vFiledat,optionflag)) return PR_RESULT_ERR_FILE_DAT_NOT_WRITE_ACCESS; /* File couldn't be open */ list_firstCursor(r); while ((res=list_getCursor(r))) { @@ -107,7 +107,7 @@ int fullCompile(const char* vFiledat, const char* vDirExt, tResourceList* r, int if (hasFlag(raw_flag)) newRes.type=0; /* compile from raw */ getFileName(vFileext,vDirExt,res,vFiledat,vDatFileName,optionflag,backupExtension); /* the file is in the archive, so i'll add it to the main dat body */ - if ((newRes.size=((unsigned short)mLoadFileArray(vFileext,&newRes.data)))) { + if ((newRes.size=((unsigned short)mLoadFileArray(vFileext,&newRes.data))<=0)) { if (!mAddCompiledFileToDatFile(&newRes,vFileext)) { if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_ERRORS,getFileNameFromPath(vFileext)); error++; @@ -134,7 +134,7 @@ int fullCompile(const char* vFiledat, const char* vDirExt, tResourceList* r, int return error; } -#define RW_ERROR {mRWCloseDatFile(1);return PR_RESULT_ERR_FILE_NOTOPEN;} +#define RW_ERROR {mRWCloseDatFile(1);return error;} int partialCompile(const char* vFiledat, const char* vDirExt, tResourceList* r, int optionflag, const char* vDatFileName,const char* backupExtension) { /* Return values: @@ -144,7 +144,7 @@ int partialCompile(const char* vFiledat, const char* vDirExt, tResourceList* r, positive number: number of missing files */ - int error,ok=0; + int error,errors=0,ok=0; int indexNumber; char vFileext[MAX_FILENAME_SIZE]; unsigned short int numberOfItems; @@ -156,8 +156,7 @@ int partialCompile(const char* vFiledat, const char* vDirExt, tResourceList* r, /* main loop */ for (indexNumber=0;(indexNumber<numberOfItems);indexNumber++) { - if (!mReadFileInDatFile(&res,indexNumber)) - RW_ERROR; /* Read error */ + if ((error=mReadFileInDatFile(&res,indexNumber))) return error; /* Read error */ if (res.id.value==0xFFFF) continue; /* Tammo Jan Bug fix * TODO: move to the dat layer? */ @@ -177,7 +176,7 @@ int partialCompile(const char* vFiledat, const char* vDirExt, tResourceList* r, newRes.type=res.type; if (!mAddCompiledFileToDatFile(&newRes,vFileext)) { if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_ERRORS,getFileNameFromPath(vFileext)); - error++; + errors++; } else { if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_SUCCESS,getFileNameFromPath(vFileext)); ok++; @@ -185,7 +184,7 @@ int partialCompile(const char* vFiledat, const char* vDirExt, tResourceList* r, free(newRes.data); } else { if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_NOT_OPEN,getFileNameFromPath(vFileext)); - error++; + errors++; } } else { /* the file wasn't in the partial list, so I'll re-copy it from the open dat file */ @@ -197,8 +196,8 @@ int partialCompile(const char* vFiledat, const char* vDirExt, tResourceList* r, /* Close dat file */ mRWCloseDatFile(0); - if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_DONE,ok,error); - return error; + if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_DONE,ok,errors); + return errors; } int compile(const char* vFiledat, const char* vDirExt, tResourceList* r, int optionflag, const char* vDatFileName,const char* backupExtension) { diff --git a/PR/src/lib/formats/pal.c b/PR/src/lib/formats/pal.c index 6b638d2..28883f9 100644 --- a/PR/src/lib/formats/pal.c +++ b/PR/src/lib/formats/pal.c @@ -108,6 +108,8 @@ int mFormatImportPal(tResource *res,const char* vFile) { /* Read sample */ sprintf(aux,"%s.more",vFile); sample1=mLoadFileArray(aux,&pals1); + if (sample1<0) return sample1; + if (sample1==100) { pals=pals1; } else { diff --git a/PR/src/lib/layers/dat.c b/PR/src/lib/layers/dat.c index a29323b..9e87da2 100644 --- a/PR/src/lib/layers/dat.c +++ b/PR/src/lib/layers/dat.c @@ -297,7 +297,20 @@ int mReadBeginDatFile(unsigned short int *numberOfItems,const char* vFiledat){ /* Open file */ readDatFileSize=mLoadFileArray(vFiledat,&readDatFile); - if (!readDatFileSize) return PR_RESULT_ERR_FILE_DAT_NOTOPEN; + if (readDatFileSize<=0) { + switch (readDatFileSize) { + case PR_RESULT_ERR_FILE_NOT_READ_ACCESS: + return PR_RESULT_ERR_FILE_DAT_NOT_READ_ACCESS; + case PR_RESULT_ERR_FILE_NOT_OPEN_WASDIR: + return PR_RESULT_ERR_FILE_DAT_NOT_OPEN_WASDIR; + case PR_RESULT_ERR_FILE_NOT_OPEN_NOTFOUND: + return PR_RESULT_ERR_FILE_DAT_NOT_OPEN_NOTFOUND; + case 0: + return PR_RESULT_ERR_INVALID_DAT; + default: + return readDatFileSize; + } + } if (readDatFileSize<=6) { free(readDatFile); return PR_RESULT_ERR_INVALID_DAT; @@ -375,7 +388,7 @@ int mWriteBeginDatFile(const char* vFile, int optionflag) { fwriteshort(&fill,writeDatFile); /* Fill the file with 6 starting null bytes */ return PR_RESULT_SUCCESS; } else { - return PR_RESULT_ERR_FILE_DAT_NOTOPEN_W; + return PR_RESULT_ERR_FILE_DAT_NOT_WRITE_ACCESS; } } diff --git a/PR/src/lib/layers/disk.c b/PR/src/lib/layers/disk.c index 6d151b7..b206ee6 100644 --- a/PR/src/lib/layers/disk.c +++ b/PR/src/lib/layers/disk.c @@ -155,6 +155,7 @@ void addFileToOpenFilesList(const char* fileName,int hasBackup) { if (hasBackup) { newNode->size=mLoadFileArray(fileName,&(newNode->content)); + if (newNode->size<0) newNode->size=0; } else { newNode->size=0; } @@ -406,18 +407,25 @@ int mLoadFileArray(const char* vFile,unsigned char** array) { /* declare variables */ FILE *fp; int aux; - + const char* file=repairFolders(vFile); + whatIs f; + + /* check type */ + f=isDir(file); + if (f==eDirectory) return PR_RESULT_ERR_FILE_NOT_OPEN_WASDIR; + if (f==eNotFound) return PR_RESULT_ERR_FILE_NOT_OPEN_NOTFOUND; + /* Open the file */ - if ((fp=fopen(repairFolders(vFile),"rb"))==NULL) { - return 0; + if ((fp=fopen(file,"rb"))==NULL) { + return PR_RESULT_ERR_FILE_NOT_READ_ACCESS; } else { /* get file size */ fseek(fp,0,SEEK_END); aux=ftell(fp); - if ( !aux || (aux>SIZE_OF_FILE) || ( ((*array=(unsigned char*)malloc(aux+1))==NULL) ) ) { - /* if the file was null or bigger than the max size or couldn't allocate the file in memory */ + if (!aux) {fclose(fp);return 0;} + if ((*array=(unsigned char*)malloc(aux+1))==NULL) { fclose(fp); - return 0; + return PR_RESULT_ERR_MEMORY; /* this is probable to happen in big files! */ } else { /* if the file was successfully open */ fseek(fp,0,SEEK_SET); diff --git a/PR/src/lib/xml/parse.c b/PR/src/lib/xml/parse.c index fd692de..a2d271c 100644 --- a/PR/src/lib/xml/parse.c +++ b/PR/src/lib/xml/parse.c @@ -467,7 +467,7 @@ tTag* parseXmlFile(const char* vFile,int* error) { tTag* father; int size; - if (!(size=mLoadFileArray(vFile,(unsigned char**)(&l)))) { + if (((size=mLoadFileArray(vFile,(unsigned char**)(&l)))<=0)) { *error=PR_RESULT_ERR_XML_FILE; /* File not open */ return NULL; }