o Solved partial importing bug
authorecalot <ecalot>
Sun, 21 Mar 2004 23:30:04 +0000 (23:30 +0000)
committerecalot <ecalot>
Sun, 21 Mar 2004 23:30:04 +0000 (23:30 +0000)
o Added partial importing to the compile function

PR/src/console/main.c
PR/src/lib/actions/classify.c
PR/src/lib/actions/export.c
PR/src/lib/actions/import.c
PR/src/lib/layers/autodetect.c
PR/src/lib/layers/dat.c
PR/src/lib/layers/idlist.c
PR/src/lib/pr.c
PR/src/lib/xml/tree.c
PR/src/lib/xml/unknown.c

index bb408f41108c69a4d818cb2ba84356ce89a1d808..2390b8a6c64acaf7e9d8907a44c0e41882aae050 100644 (file)
@@ -204,10 +204,11 @@ int prImportDatOpt(const char* vDatFile, const char* vDirName, const char* vResF
 
                Return values:
                        -1 DAT File couldn't be open for writing
-                       -2 XML Parse error
-                       -3 No memory
-                       -4 XML Attribute not recognized
-                       -5 XML File not found
+                       -2 DAT file not found or invalid in partial importation
+                       -3 XML Parse error
+                       -4 No memory
+                       -5 XML Attribute not recognized
+                       -6 XML File not found
                        00 File successfully imported
                        positive number: number of missing files
        */
@@ -229,7 +230,7 @@ int prImportDatOpt(const char* vDatFile, const char* vDirName, const char* vResF
        a=parseFile(vResFile,currentDatFileName,r);
        if (a<0) {
                /* parsing errors */
-               a-=1;
+               a-=2;
        } else {
                /* importing errors/no errors */
                a=compile (vDatFile, vDirName,r,opt,currentDatFileName,backupExtension);
index 402931440d8ba95e91ac59adfc99a195225a6997..31a9d50a98f77663ded1c323258dbdbc77a5e7f1 100644 (file)
@@ -32,7 +32,7 @@ tasks.c: Princed Resources : Classify a DAT file
 */
 
 #include <stdio.h>
-#include "tasks.h" 
+#include "tasks.h"
 #include "memory.h"
 #include "resources.h"
 #include "dat.h"
@@ -48,8 +48,9 @@ tasks.c: Princed Resources : Classify a DAT file
 
 extern int pop1;
 
+#define READ_ERROR {mReadCloseDatFile();return 0;}
+
 int prVerifyDatType(const char* vFiledat) {
-       int                error;
        int                indexNumber;
        long int           id;
        unsigned char*     data;
@@ -58,16 +59,17 @@ int prVerifyDatType(const char* vFiledat) {
        unsigned short int numberOfItems;
 
        /* Initialize abstract variables to read this new DAT file */
-       if (error=mReadBeginDatFile(&numberOfItems,vFiledat)) return error;
+       if (!mReadBeginDatFile(&numberOfItems,vFiledat)) return -1;
 
        /* main loop */
        for (indexNumber=0;(indexNumber<numberOfItems)&&(type==RES_TYPE_BINARY);indexNumber++) {
                id=mReadGetFileInDatFile(indexNumber,&data,&size);
-               if (id<0) return 0; /* Read error */
+               if (id<0) READ_ERROR; /* Read error */
                if (id==0xFFFF) continue; /* Tammo Jan Bug fix */
-               if (id>=MAX_RES_COUNT) return 0; /* A file with an ID out of range will be treated as invalid */
+               if (id>=MAX_RES_COUNT) READ_ERROR; /* A file with an ID out of range will be treated as invalid */
                type=verifyHeader(data,size);
        }
+
        mReadCloseDatFile();
        return pop1?type:(type+10);
 }
index c187d66bcc3a5fc4986edd356a1818cdc2d3c898..cf75a5ff6e9ca0533439697da791824b4b942142 100644 (file)
@@ -74,7 +74,7 @@ for (id=0;id<MAX_RES_COUNT;id++) {\
 int extract(const char* vFiledat,const char* vDirExt, tResource* r[], int optionflag, const char* vDatFileName, const char* vDatAuthor,const char* backupExtension) {
        char               vFileext[MAX_FILENAME_SIZE];
        int                indexNumber;
-       int                ok;
+       int                ok=1;
        long int           id;
        tImage             image; /* this is used to make a persistent palette */
        unsigned char*     data;
@@ -83,13 +83,12 @@ int extract(const char* vFiledat,const char* vDirExt, tResource* r[], int option
        unsigned short int paletteId=0;
 
        /* Initialize abstract variables to read this new DAT file */
-       if (ok=mReadBeginDatFile(&numberOfItems,vFiledat)) return ok;
+       if (!mReadBeginDatFile(&numberOfItems,vFiledat)) return -1;
 
        /* Initializes the palette list */
        initializePaletteList;
 
        /* main loop */
-       ok=1;
        for (indexNumber=0;ok&&(indexNumber<numberOfItems);indexNumber++) {
                id=mReadGetFileInDatFile(indexNumber,&data,&size);
 
index 07cbd3052fb40351300f1c7040ea009bd50a0fda..084b9cfa864aa83c4a5b2015219db1f523e2c67b 100644 (file)
@@ -93,7 +93,7 @@ for (id=0;id<MAX_RES_COUNT;id++) {\
 |                   M A I N   F U N C T I O N                   |
 \***************************************************************/
 
-int compile(const char* vFiledat, const char* vDirExt, tResource* r[], int optionflag, const char* vDatFileName,const char* backupExtension) {
+int fullCompile(const char* vFiledat, const char* vDirExt, tResource* r[], int optionflag, const char* vDatFileName,const char* backupExtension) {
        /*
                Return values:
                        -1 File couldn't be open for writing
@@ -117,15 +117,15 @@ int compile(const char* vFiledat, const char* vDirExt, tResource* r[], int optio
                        if ((r[id]->size=((unsigned short)mLoadFileArray(vFileext,&data)))) {
                                mWriteInitResource(r+id);
                                if (!mAddCompiledFileToDatFile(data,r[id],vFileext)) {
-                                       if (hasFlag(verbose_flag)) printf(PR_TEXT_IMPORT_ERRORS,getFileNameFromPath(vFileext));
+                                       if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_ERRORS,getFileNameFromPath(vFileext));
                                        error++;
                                } else {
-                                       if (hasFlag(verbose_flag)) printf(PR_TEXT_IMPORT_SUCCESS,getFileNameFromPath(vFileext));
+                                       if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_SUCCESS,getFileNameFromPath(vFileext));
                                        ok++;
                                }
                                free(data);
                        } else {
-                               if (hasFlag(verbose_flag)) printf(PR_TEXT_IMPORT_NOT_OPEN,getFileNameFromPath(vFileext));
+                               if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_NOT_OPEN,getFileNameFromPath(vFileext));
                                error++;
                        }
                }
@@ -140,7 +140,16 @@ int compile(const char* vFiledat, const char* vDirExt, tResource* r[], int optio
        return error;
 }
 
+#define RW_ERROR {mRWCloseDatFile(1);return 0;}
 int partialCompile(const char* vFiledat, const char* vDirExt, tResource* r[], int optionflag, const char* vDatFileName,const char* backupExtension) {
+       /*
+               Return values:
+                       -2 Previous DAT file was invalid
+                       -1 File couldn't be open for writing
+                       00 File successfully compiled
+                       positive number: number of missing files
+       */
+
        char vFileext[MAX_FILENAME_SIZE];
        int                error,ok=0;
        int                indexNumber;
@@ -150,43 +159,53 @@ int partialCompile(const char* vFiledat, const char* vDirExt, tResource* r[], in
        unsigned short int numberOfItems;
 
        /* Initialize abstract variables to read this new DAT file */
-       if (error=mRWBeginDatFile(vFiledat,&numberOfItems)) return error;
+       if ((error=mRWBeginDatFile(vFiledat,&numberOfItems,optionflag))) return error;
 
        /* main loop */
        for (indexNumber=0;(indexNumber<numberOfItems);indexNumber++) {
                id=mReadGetFileInDatFile(indexNumber,&data,&size);
-               if (id<0) return 0; /* Read error */
+               if (id<0) RW_ERROR; /* Read error */ /* TODO BUG: return doesn't close file */
                if (id==0xFFFF) continue; /* Tammo Jan Bug fix */
-               if (id>=MAX_RES_COUNT) return 0; /* A file with an ID out of range will be treated as invalid */
+               if (id>=MAX_RES_COUNT) RW_ERROR; /* A file with an ID out of range will be treated as invalid */
 
                mWriteInitResource(r+id);
                if (r[id]&&isInThePartialList(r[id]->path,id)) { /* If the resource was specified */
                        if (hasFlag(raw_flag)) r[id]->type=0; /* compile from raw */
                        getFileName(vFileext,vDirExt,r[id],(unsigned short)id,vFiledat,vDatFileName,optionflag,backupExtension);
                        /* the file is in the archive, so i'll add it to the main dat body */
-                       if ((r[id]->size=((unsigned short)mLoadFileArray(vFileext,&data)))) {
+                       if ((r[id]->size=((unsigned long)mLoadFileArray(vFileext,&data)))) {
                                if (!mAddCompiledFileToDatFile(data,r[id],vFileext)) {
-                                       if (hasFlag(verbose_flag)) printf(PR_TEXT_IMPORT_ERRORS,getFileNameFromPath(vFileext));
+                                       if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_ERRORS,getFileNameFromPath(vFileext));
                                        error++;
                                } else {
-                                       if (hasFlag(verbose_flag)) printf(PR_TEXT_IMPORT_SUCCESS,getFileNameFromPath(vFileext));
+                                       if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_SUCCESS,getFileNameFromPath(vFileext));
                                        ok++;
                                }
                                free(data);
                        } else {
-                               if (hasFlag(verbose_flag)) printf(PR_TEXT_IMPORT_NOT_OPEN,getFileNameFromPath(vFileext));
+                               if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_NOT_OPEN,getFileNameFromPath(vFileext));
                                error++;
                        }
                } else {
+                       r[id]->size=size-1;
                        mWriteSetFileInDatFileIgnoreChecksum(data,size);
                }
        }
-       /* Free allocated resources and dynamic strings */
-       freeResources;
 
        /* Close dat file */
        mRWCloseDatFile(0);
 
+       /* Free allocated resources and dynamic strings */
+       freeResources;
+
        if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_DONE,ok,error);
        return error;
 }
+
+int compile(const char* vFiledat, const char* vDirExt, tResource* r[], int optionflag, const char* vDatFileName,const char* backupExtension) {
+       if (partialListActive()) {
+               return partialCompile(vFiledat,vDirExt,r,optionflag,vDatFileName,backupExtension);
+       } else {
+               return fullCompile(vFiledat,vDirExt,r,optionflag,vDatFileName,backupExtension);
+       }
+}
index a49cd3418168bec6b367ee5e885e1889e596eafc..d1164982056a509efa23953d3c7ccc714884050d 100644 (file)
@@ -112,6 +112,10 @@ const char* getExtDesc(int type) {
 
 static tResIdList partialList;
 
+int partialListActive() {
+       return partialList.count;
+}
+
 void parseGivenPath(char* path) {
        /*
                PRE:  partialList.list was not allocated
index 605121abffa0a3f6eeeac80ca40f5a132a04f12b..28b59cf42c465dc6b6dd8b561abdc272160c57c1 100644 (file)
@@ -58,13 +58,10 @@ void mReadCloseDatFile() {
 }
 
 int mReadBeginDatFile(unsigned short int *numberOfItems,const char* vFiledat){
-       /* 
+       /*
                Return Values:
-                       -1 Wrong format
-        *
-        *
-        *
-        *
+                       0 Wrong Format or file not found
+                       1 Ok
        */
 
        int ok;
@@ -72,8 +69,8 @@ int mReadBeginDatFile(unsigned short int *numberOfItems,const char* vFiledat){
 
        /* Open file */
        readDatFileSize=mLoadFileArray(vFiledat,&readDatFile);
-       if (!readDatFileSize) return -1;
-       if (readDatFileSize<=6) return -1;
+       if (!readDatFileSize) return 0;
+       if (readDatFileSize<=6) {free(readDatFile);return 0;}
 
        readDatFilePoint=readDatFile;
 
@@ -84,7 +81,7 @@ int mReadBeginDatFile(unsigned short int *numberOfItems,const char* vFiledat){
 
        if ((indexOffset>readDatFileSize)&&((indexOffset+indexSize)!=readDatFileSize)) {
                free(readDatFile);
-               return -1; /* this is not a valid prince dat file */
+               return 0; /* this is not a valid prince dat file */
        }
 
        indexPointer=readDatFile+indexOffset;
@@ -100,18 +97,19 @@ int mReadBeginDatFile(unsigned short int *numberOfItems,const char* vFiledat){
        }
        recordSize=pop1?8:11;
 
-       return 0;
+       return 1;
 }
 
 int mReadGetFileInDatFile(int k,unsigned char* *data,unsigned long  int *size) {
-       int ok=1;
+       int ok=1; /* TODO: rename mReadGet* for mRead* and mWriteSet for mWrite */
        unsigned short int id;
 
        /* for each archived file the index is read */
        id=    (indexPointer[ofk+k*recordSize])+(indexPointer[ofk+k*recordSize+1]<<8);
        offset=(indexPointer[ofk+k*recordSize+2])+(indexPointer[ofk+k*recordSize+3]<<8)+(indexPointer[ofk+k*recordSize+4]<<16)+(indexPointer[ofk+k*recordSize+5]<<24);
        *size= (indexPointer[ofk+k*recordSize+6])+(indexPointer[ofk+k*recordSize+7]<<8)+1;
-       if (!pop1) ok=(indexPointer[ofk+k*recordSize+8]==0x40)&&(!indexPointer[ofk+k*recordSize+9])&&(!indexPointer[ofk+k*recordSize+10]);
+       if ((!pop1)&&(!(indexPointer[ofk+k*recordSize+8]==0x40)&&(!indexPointer[ofk+k*recordSize+9])&&(!indexPointer[ofk+k*recordSize+10]))) return -1;
+       if (offset+indexSize>readDatFileSize) return -1;
        *data=readDatFile+offset;
        return ok?id:-1;
 }
@@ -127,7 +125,7 @@ int mReadInitResource(tResource** res,const unsigned char* data,long size) {
                (*res)->palette=0;
                (*res)->number=0;
                (*res)->size=(unsigned short int)size;
-               (*res)->offset=(unsigned short)offset;
+               (*res)->offset=(unsigned short)offset; /* TODO delete this line */
                (*res)->type=verifyHeader(data,(unsigned short int)size);
        } else { /* If resource type is invalid or 0, the type will be decided by PR */
                if (!((*res)->type)) (*res)->type=verifyHeader(data,(unsigned short int)size);
@@ -145,6 +143,11 @@ int mWriteBeginDatFile(const char* vFile, int optionflag) {
        /*
                Opens safely a dat file for writing mode and
                reserves space for the headers
+
+               Return Values:
+                        1 Ok
+                        0 File couldn't be open
+
        */
 
        if (writeOpen(vFile,&writeDatFile,optionflag|backup_flag)) {
@@ -156,6 +159,9 @@ int mWriteBeginDatFile(const char* vFile, int optionflag) {
 }
 
 void mWriteInitResource(tResource** res) {
+       if ((*res)==NULL) {
+               (*res)=(tResource*)malloc(sizeof(tResource));
+       }
        (*res)->offset=(unsigned long)ftell(writeDatFile);
 }
 
@@ -216,3 +222,22 @@ void mWriteCloseDatFile(tResource* r[],int dontSave,int optionflag, const char*
        writeClose(writeDatFile,dontSave,optionflag,backupExtension);
 }
 
+/***************************************************************\
+|                       DAT R/W primitives                      |
+\***************************************************************/
+
+int mRWBeginDatFile(const char* vFile, unsigned short int *numberOfItems, int optionflag) {
+       if (!mReadBeginDatFile(numberOfItems,vFile)) return -2;
+       if (!mWriteBeginDatFile(vFile,optionflag)) {
+               mReadCloseDatFile();
+               return -1;
+       }
+       return 0;
+}
+
+
+
+
+
+
+
index a49cd3418168bec6b367ee5e885e1889e596eafc..d1164982056a509efa23953d3c7ccc714884050d 100644 (file)
@@ -112,6 +112,10 @@ const char* getExtDesc(int type) {
 
 static tResIdList partialList;
 
+int partialListActive() {
+       return partialList.count;
+}
+
 void parseGivenPath(char* path) {
        /*
                PRE:  partialList.list was not allocated
index bb408f41108c69a4d818cb2ba84356ce89a1d808..2390b8a6c64acaf7e9d8907a44c0e41882aae050 100644 (file)
@@ -204,10 +204,11 @@ int prImportDatOpt(const char* vDatFile, const char* vDirName, const char* vResF
 
                Return values:
                        -1 DAT File couldn't be open for writing
-                       -2 XML Parse error
-                       -3 No memory
-                       -4 XML Attribute not recognized
-                       -5 XML File not found
+                       -2 DAT file not found or invalid in partial importation
+                       -3 XML Parse error
+                       -4 No memory
+                       -5 XML Attribute not recognized
+                       -6 XML File not found
                        00 File successfully imported
                        positive number: number of missing files
        */
@@ -229,7 +230,7 @@ int prImportDatOpt(const char* vDatFile, const char* vDirName, const char* vResF
        a=parseFile(vResFile,currentDatFileName,r);
        if (a<0) {
                /* parsing errors */
-               a-=1;
+               a-=2;
        } else {
                /* importing errors/no errors */
                a=compile (vDatFile, vDirName,r,opt,currentDatFileName,backupExtension);
index a49cd3418168bec6b367ee5e885e1889e596eafc..d1164982056a509efa23953d3c7ccc714884050d 100644 (file)
@@ -112,6 +112,10 @@ const char* getExtDesc(int type) {
 
 static tResIdList partialList;
 
+int partialListActive() {
+       return partialList.count;
+}
+
 void parseGivenPath(char* path) {
        /*
                PRE:  partialList.list was not allocated
index a49cd3418168bec6b367ee5e885e1889e596eafc..d1164982056a509efa23953d3c7ccc714884050d 100644 (file)
@@ -112,6 +112,10 @@ const char* getExtDesc(int type) {
 
 static tResIdList partialList;
 
+int partialListActive() {
+       return partialList.count;
+}
+
 void parseGivenPath(char* path) {
        /*
                PRE:  partialList.list was not allocated