git » fp-git.git » commit 3f6e252

Modified loadArray to support tBinary

author ecalot
2006-02-20 04:03:32 UTC
committer ecalot
2006-02-20 04:03:32 UTC
parent 1f7b667aa0048717a67f8d690554d848240c023a

Modified loadArray to support tBinary

PR/src/lib/actions/classify.c +7 -7
PR/src/lib/formats/pal.c +4 -4
PR/src/lib/layers/dat.c +15 -16
PR/src/lib/layers/disk.c +44 -25

diff --git a/PR/src/lib/actions/classify.c b/PR/src/lib/actions/classify.c
index feab167..0e9b62f 100644
--- a/PR/src/lib/actions/classify.c
+++ b/PR/src/lib/actions/classify.c
@@ -85,7 +85,7 @@ int prClassify(const char* fileName) {
 
 	if (!result) { /* it's not a DAT file*/
 		/*long int fileContent.size;
-		unsigned char* fileData;*/
+		unsigned char* fileContent.data;*/
 		tBinary fileContent;
 
 		/* let's get it's content and see what it is */
@@ -96,7 +96,7 @@ int prClassify(const char* fileName) {
 		if (fileContent.size==8) {
 			int framesLeft;
 			/* check that the frames (seconds/12) are in the range [0*12,60*12) */
-			framesLeft=fileData[2]|fileData[3]<<8;
+			framesLeft=fileContent.data[2]|fileContent.data[3]<<8;
 			if (framesLeft<60*12)
 				result=30; /* SAV file */
 		}
@@ -105,13 +105,13 @@ int prClassify(const char* fileName) {
 		if (fileContent.size==176) {
 			int records;
 			/* check that the number of stored records are 6 or less */
-			records=fileData[0]|fileData[1]<<8;
+			records=fileContent.data[0]|fileContent.data[1]<<8;
 			if (records<=6) {
 				result=31; /* HOF file */
 				while (records) {
 					int framesLeft;
 					/* wrong seconds left format for this record will invalidate the whole file */
-					framesLeft=fileData[29*records-2]|fileData[29*records-1]<<8;
+					framesLeft=fileContent.data[29*records-2]|fileContent.data[29*records-1]<<8;
 					if (framesLeft>=60*12) result=0;
 					records--;
 				}
@@ -119,7 +119,7 @@ int prClassify(const char* fileName) {
 		}
 
 		/* 4) as the last resource, check if it is an EXE file */
-		if (!result && fileContent.size>2 && fileData[0]=='M' && fileData[1]=='Z') {
+		if (!result && fileContent.size>2 && fileContent.data[0]=='M' && fileContent.data[1]=='Z') {
 			static tExeClassification x[]={
 				/* install.pdm         : 41 */ {717181985,4233},
 				/* prince.exe v1.0 THG : 42 */ {622612442,123335},
@@ -131,7 +131,7 @@ int prClassify(const char* fileName) {
 			/* Now I'll try to recognize some known EXE files */
 			/* calculate checksum */
 			for (i=0;i<fileContent.size;i++) {
-				checkSum+=fileData[i]<<((3-(i%4))*8);
+				checkSum+=fileContent.data[i]<<((3-(i%4))*8);
 			}
 #ifdef DEBUG_GETCHECKSUM
 			printf("{%lu,%ld},\n",checkSum,fileContent.size);
@@ -145,7 +145,7 @@ int prClassify(const char* fileName) {
 				}
 		}
 
-		free(fileData);
+		free(fileContent.data);
 	}
 
 	return result;
diff --git a/PR/src/lib/formats/pal.c b/PR/src/lib/formats/pal.c
index a3e9c58..178e0cf 100644
--- a/PR/src/lib/formats/pal.c
+++ b/PR/src/lib/formats/pal.c
@@ -72,12 +72,12 @@ int mFormatImportPal(tPalette* p, int* bits, const char* vFile) {
 	/* declare variables */
 	unsigned char* pals;
 	unsigned char* pals1;
-	unsigned char  pals2[400]/*PAL_SAMPLE*/;
+	/*unsigned char  pals2[400 *PAL_SAMPLE*/
 	unsigned char  palh [30];
 	unsigned char* pal;
 	unsigned char* pal2;
 /*	char* data2;*/
-	char aux[MAX_FILENAME_SIZE];
+	/*char aux[MAX_FILENAME_SIZE];*/
 /*	int r;
 	int g;
 	int b;*/
@@ -93,7 +93,7 @@ int mFormatImportPal(tPalette* p, int* bits, const char* vFile) {
 	if (i!=sizeof(palh)) return 0; /* false: palette differs with headers */
 
 	/* Read sample */
-	sprintf(aux,"%s.more",vFile);
+/*	sprintf(aux,"%s.more",vFile);
 	sample1=mLoadFileArray(aux,&pals1);
 	if (sample1<0) return sample1;
 
@@ -102,7 +102,7 @@ int mFormatImportPal(tPalette* p, int* bits, const char* vFile) {
 	} else {
 		pals=pals2;
 	}
-
+*/
 	/* Allocate palette */
 	pal=malloc(100);
 
diff --git a/PR/src/lib/layers/dat.c b/PR/src/lib/layers/dat.c
index 8f8e1a0..1c8536a 100644
--- a/PR/src/lib/layers/dat.c
+++ b/PR/src/lib/layers/dat.c
@@ -64,8 +64,7 @@ typedef struct {
 	int* pointer;
 } tValuePointer;
 
-static unsigned char* readDatFile;
-static int            readDatFileSize;
+static tBinary        readDatFile;
 static tIndexCursor   readIndexCursor;
 static const char*    textPop1="pop1";
 
@@ -405,8 +404,8 @@ int dat_readRes(tResource* res, tIndexCursor indexCursor) {
 
 	/*res->size++; * add the checksum */
 
-	res->content.data=readDatFile+res->offset+1; /* ignore the checksum */
-	return checkSum(readDatFile+res->offset,res->content.size+1);
+	res->content.data=readDatFile.data+res->offset+1; /* ignore the checksum */
+	return checkSum(readDatFile.data+res->offset,res->content.size+1);
 /*printf("reading resource: %d:%4s at %d order=%d\n",res->id.value,res->id.index,res->offset,res->id.order);*/
 }
 
@@ -430,7 +429,7 @@ tPopVersion mReadGetVersion() {
 
 void mReadCloseDatFile() {
 	dat_releaseCursor(&readIndexCursor);
-	free(readDatFile);
+	free(readDatFile.data);
 }
 
 int mReadBeginDatFile(unsigned short int *numberOfItems,const char* vFiledat){
@@ -444,9 +443,9 @@ int mReadBeginDatFile(unsigned short int *numberOfItems,const char* vFiledat){
 	unsigned short int indexSize;
 
 	/* Open file */
-	readDatFileSize=mLoadFileArray(vFiledat,&readDatFile);
-	if (readDatFileSize<=0) {
-		switch (readDatFileSize) {
+	readDatFile=mLoadFileArray(vFiledat);
+	if (readDatFile.size<=0) {
+		switch (readDatFile.size) {
 		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:
@@ -456,26 +455,26 @@ int mReadBeginDatFile(unsigned short int *numberOfItems,const char* vFiledat){
 		case PR_RESULT_SUCCESS:
 			return PR_RESULT_ERR_INVALID_DAT;
 		default:
-			return readDatFileSize;
+			return readDatFile.size;
 		}
 	}
-	if (readDatFileSize<=6) {
-		free(readDatFile);
+	if (readDatFile.size<=6) {
+		free(readDatFile.data);
 		return PR_RESULT_ERR_INVALID_DAT;
 	}
 
 	/* read header  */
-	indexOffset=array2long(readDatFile);
-	indexSize=array2short(readDatFile+4);
+	indexOffset=array2long(readDatFile.data);
+	indexSize=array2short(readDatFile.data+4);
 
 	/* verify DAT format: the index offset belongs to the file and the file size is the index size plus the index offset */
-	if ((indexOffset>readDatFileSize)&&((indexOffset+indexSize)!=readDatFileSize)) {
-		free(readDatFile);
+	if ((indexOffset>readDatFile.size)&&((indexOffset+indexSize)!=readDatFile.size)) {
+		free(readDatFile.data);
 		return PR_RESULT_ERR_INVALID_DAT; /* this is not a valid prince DAT file */
 	}
 
 	/* create cursor */
-	readIndexCursor=dat_createCursor(readDatFile+indexOffset,indexSize,numberOfItems);
+	readIndexCursor=dat_createCursor(readDatFile.data+indexOffset,indexSize,numberOfItems);
 
 	/* pop version check */
 	if (!dat_readCursorGetVersion(readIndexCursor)) return PR_RESULT_ERR_INVALID_DAT;
diff --git a/PR/src/lib/layers/disk.c b/PR/src/lib/layers/disk.c
index f5c526c..5a23cdd 100644
--- a/PR/src/lib/layers/disk.c
+++ b/PR/src/lib/layers/disk.c
@@ -82,8 +82,9 @@ typedef struct tOpenFiles {
 	struct tOpenFiles* next;
 	FILE* file;
 	char* name;
-	unsigned char* content;
-	long int size;
+	tBinary content;
+/*	unsigned char* content;
+	long int size;*/
 }tOpenFiles;
 
 /***************************************************************\
@@ -160,17 +161,17 @@ void addFileToOpenFilesList(const char* fileName,int hasBackup) {
 	newNode->name=strallocandcopy(fileName);
 
 	if (hasBackup) {
-		newNode->size=mLoadFileArray(fileName,&(newNode->content));
-		if (newNode->size<0) newNode->size=0;
+		newNode->content=mLoadFileArray(fileName);
+		if (newNode->content.size<0) newNode->content.size=0;
 	} else {
-		newNode->size=0;
+		newNode->content.size=0;
 	}
 	openFilesList=newNode;
 }
 
 #define addPointerToOpenFilesList(fp) openFilesList->file=fp;
 
-int getFromOpenFilesList(FILE* fp, char** fileName, unsigned char** content, unsigned long int *size) {
+int getFromOpenFilesList(FILE* fp, char** fileName, tBinary *content) {
 	tOpenFiles* currentNode;
 	tOpenFiles* priorNode=NULL;
 
@@ -186,7 +187,7 @@ int getFromOpenFilesList(FILE* fp, char** fileName, unsigned char** content, uns
 	/* Return results */
 	*fileName=currentNode->name;
 	*content=currentNode->content;
-	*size=currentNode->size;
+/*	*size=currentNode->size;*/
 
 	/* free node and set prior pointer to the next */
 	if (priorNode==NULL) {
@@ -200,17 +201,16 @@ int getFromOpenFilesList(FILE* fp, char** fileName, unsigned char** content, uns
 }
 
 int writeClose(FILE* fp,int dontSave,int optionflag,const char* backupExtension) {
-	unsigned char* content;
+	tBinary content;
 	char* fileName;
-	unsigned long int size;
 
-	if (getFromOpenFilesList(fp,&fileName,&content,&size)) {
+	if (getFromOpenFilesList(fp,&fileName,&content)) {
 		if (dontSave) {
 			fclose(fp);
-			if (size) {
+			if (content.size) {
 				fp=fopen(fileName,"wb");
 				if (fp==NULL) return -1;
-				fwrite(content,1,size,fp);
+				fwrite(content.data,1,content.size,fp);
 				fclose(fp);
 			} else {
 				remove(fileName);
@@ -227,7 +227,7 @@ int writeClose(FILE* fp,int dontSave,int optionflag,const char* backupExtension)
 				fclose(fp);
 				fp=fopen(aux,"wb");
 				if (fp==NULL) return -2;
-				fwrite(content,1,size,fp);
+				fwrite(content.data,1,content.size,fp);
 				fclose(fp);
 			} else {
 				fclose(fp);
@@ -235,7 +235,7 @@ int writeClose(FILE* fp,int dontSave,int optionflag,const char* backupExtension)
 		}
 
 		free(fileName);
-		if (size) free(content);
+		if (content.size) free(content.data);
 	}
 
 	return 0;
@@ -398,7 +398,7 @@ int writeData(const unsigned char* data, int ignoreChars, const char* vFileext,
 	return ok;
 }
 
-int mLoadFileArray(const char* vFile,unsigned char** array) {
+tBinary mLoadFileArray(const char* vFile) {
 	/*
 		Using the string in vFile, it opens the file and returns the
 		number of bytes	in it and the content of the file in array.
@@ -407,33 +407,52 @@ int mLoadFileArray(const char* vFile,unsigned char** array) {
 
 	/* declare variables */
 	FILE *fp;
-	int  aux;
+	int  aux; /* TODO: replace by ret.size */
 	const char* file=repairFolders(vFile);
 	whatIs f;
+	tBinary ret;
 
 	/* 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;
+	if (f==eDirectory) {
+		ret.size=PR_RESULT_ERR_FILE_NOT_OPEN_WASDIR;
+		ret.data=NULL;
+		return ret;
+	}
+	if (f==eNotFound) {
+		ret.size=PR_RESULT_ERR_FILE_NOT_OPEN_NOTFOUND;
+		ret.data=NULL;
+		return ret;
+	}
 
 	/* Open the file */
 	if ((fp=fopen(file,"rb"))==NULL) {
-		return PR_RESULT_ERR_FILE_NOT_READ_ACCESS;
+		ret.size=PR_RESULT_ERR_FILE_NOT_READ_ACCESS;
+		ret.data=NULL;
+		return ret;
 	} else {
 		/* get file size */
 		fseek(fp,0,SEEK_END);
 		aux=ftell(fp);
-		if (!aux) {fclose(fp);return 0;}
-		if ((*array=(unsigned char*)malloc(aux+1))==NULL) {
+		if (!aux) {
+			fclose(fp);
+			ret.size=0;
+			ret.data=NULL;
+			return ret;
+		}
+		if ((ret.data=(unsigned char*)malloc(aux+1))==NULL) {
 			fclose(fp);
-			return PR_RESULT_ERR_MEMORY; /* this is probable to happen in big files! */
+			ret.size=PR_RESULT_ERR_MEMORY;
+			ret.data=NULL;
+			return ret; /* this could happen in big files! */
 		} else {
 			/* if the file was successfully open */
 			fseek(fp,0,SEEK_SET);
-			aux=fread (*array,1,aux,fp);
-			(*array)[aux]=0;
+			aux=fread (ret.data,1,aux,fp);
+			ret.data[aux]=0;
 			fclose(fp);
-			return aux;
+			ret.size=aux;
+			return ret;
 		}
 	}
 }