git » fp-git.git » commit 4bcc626

Fixed memory leaks. Removed deprecated defines and struct members.

author ecalot
2005-06-08 23:39:03 UTC
committer ecalot
2005-06-08 23:39:03 UTC
parent 7a813393b2523a24253f1a878deae9c0b0e439ca

Fixed memory leaks. Removed deprecated defines and struct members.

PR/src/Makefile +1 -1
PR/src/include/dat.h +2 -5
PR/src/include/list.h +2 -1
PR/src/include/reslist.h +1 -1
PR/src/include/types.h +1 -1
PR/src/lib/actions/export.c +1 -1
PR/src/lib/actions/import.c +1 -1
PR/src/lib/formats/bmp.c +1 -0
PR/src/lib/formats/pal.c +0 -1
PR/src/lib/formats/plv.c +3 -0
PR/src/lib/formats/wav.c +2 -0
PR/src/lib/layers/autodetect.c +0 -1
PR/src/lib/layers/dat.c +1 -13
PR/src/lib/layers/idlist.c +0 -1
PR/src/lib/layers/list.c +3 -1
PR/src/lib/layers/reslist.c +13 -4
PR/src/lib/pr.c +2 -3
PR/src/lib/xml/tree.c +0 -1
PR/src/lib/xml/unknown.c +0 -1
PR/src/xml/resources.xml +1 -1

diff --git a/PR/src/Makefile b/PR/src/Makefile
index 1cb4e51..3c8a63e 100644
--- a/PR/src/Makefile
+++ b/PR/src/Makefile
@@ -134,7 +134,7 @@ $(XMLFILE): xml/resources.xml
 	$(CC) $(OPTIONS) -c lib/layers/reslist.c -o $@
 
 .compress.o: lib/compression/main.c
-	$(INFO) Compiling compression module...
+	$(INFO) Compiling main compression library...
 	$(CC) $(OPTIONS) -c lib/compression/main.c -o .compress.o -o $@
 
 .lzg_compress.o: lib/compression/lzg_compress.c
diff --git a/PR/src/include/dat.h b/PR/src/include/dat.h
index ca2afa0..3e6423c 100644
--- a/PR/src/include/dat.h
+++ b/PR/src/include/dat.h
@@ -34,8 +34,6 @@ dat.h: Princed Resources : DAT library headers
 #ifndef _DAT_H_
 #define _DAT_H_
 
-#define MAX_RES_COUNT            65000 /* TODO grep if this constant still exists */
-
 /* types */
 typedef struct {
 	unsigned short int value;
@@ -49,10 +47,9 @@ typedef struct {
 	unsigned long int  offset; /* Used internally in dat.c to remember the offset */
 	unsigned char      number; /* Used for level number */
 	char               type;
-	char*              path;
-	unsigned char*     palAux;
-	char*              name;
 	char*              desc;
+	char*              name;
+	char*              path;
 	unsigned char*     data;
 	unsigned long      flags;
 }tResource;
diff --git a/PR/src/include/list.h b/PR/src/include/list.h
index 855c72b..81a5f39 100644
--- a/PR/src/include/list.h
+++ b/PR/src/include/list.h
@@ -47,10 +47,11 @@ typedef struct node {
 typedef struct {
 	int size;
 	int (*cmp)(const void*,const void*);
+	void (*free)(void* a);
 	tListNode *cursor,*first;
 }tList;
 
-tList list_create(int dataSize,int dataCmp(const void*,const void*));
+tList list_create(int dataSize,int dataCmp(const void*,const void*),void dataFree(void* a));
 void  list_insert(tList *list,const void* data);
 void  list_drop(tList *list);
 void  list_nextCursor(tList* list);
diff --git a/PR/src/include/reslist.h b/PR/src/include/reslist.h
index a090cd1..d2a7ed9 100644
--- a/PR/src/include/reslist.h
+++ b/PR/src/include/reslist.h
@@ -46,7 +46,7 @@ void printr(const tResource* record); /* for debug purposes */
 int resIdCmp(tResourceId a,tResourceId b);
 int resCmp(const void* a,const void* b);
 void resourceListDrop(tResourceList* r);
-tResourceList resourceListCreate();
+tResourceList resourceListCreate(int isCopy);
 void resourceListAdd(tResourceList* r,const tResource* res);
 void resourceListDebugPrint(tResourceList* r);
 void resourceListAddInfo(tResourceList* r,tResource* res);
diff --git a/PR/src/include/types.h b/PR/src/include/types.h
index a090cd1..d2a7ed9 100644
--- a/PR/src/include/types.h
+++ b/PR/src/include/types.h
@@ -46,7 +46,7 @@ void printr(const tResource* record); /* for debug purposes */
 int resIdCmp(tResourceId a,tResourceId b);
 int resCmp(const void* a,const void* b);
 void resourceListDrop(tResourceList* r);
-tResourceList resourceListCreate();
+tResourceList resourceListCreate(int isCopy);
 void resourceListAdd(tResourceList* r,const tResource* res);
 void resourceListDebugPrint(tResourceList* r);
 void resourceListAddInfo(tResourceList* r,tResource* res);
diff --git a/PR/src/lib/actions/export.c b/PR/src/lib/actions/export.c
index 5eb8f78..5e5a800 100644
--- a/PR/src/lib/actions/export.c
+++ b/PR/src/lib/actions/export.c
@@ -79,7 +79,7 @@ int extract(const char* vFiledat,const char* vDirExt, tResourceList* r, int opti
 	if (mReadBeginDatFile(&numberOfItems,vFiledat)) return -1;
 
 	/* initialize palette buffer */
-	paletteBuffer=resourceListCreate();
+	paletteBuffer=resourceListCreate(1);
 	/* initialize the default palette */
 	memcpy(image.pal,DEFAULT_PALETTE,SIZE_OF_PALETTE);
 	
diff --git a/PR/src/lib/actions/import.c b/PR/src/lib/actions/import.c
index 436da59..a6ee7ba 100644
--- a/PR/src/lib/actions/import.c
+++ b/PR/src/lib/actions/import.c
@@ -126,7 +126,7 @@ int fullCompile(const char* vFiledat, const char* vDirExt, tResourceList* r, int
 					if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_SUCCESS,getFileNameFromPath(vFileext));
 					ok++;
 				}
-				/*free(data);*/
+				free(newRes.data);
 			} else {
 				if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_NOT_OPEN,getFileNameFromPath(vFileext));
 				error++;
diff --git a/PR/src/lib/formats/bmp.c b/PR/src/lib/formats/bmp.c
index 11ced6c..430caa5 100644
--- a/PR/src/lib/formats/bmp.c
+++ b/PR/src/lib/formats/bmp.c
@@ -73,6 +73,7 @@ int mFormatImportBmp(tResource *res) {
 	tImage img;
 
 	if (!mReadBitMap(&img,res->data,res->size)) return 0;
+	free(res->data);
 	mCompressGraphic(&(res->data),&img,(int*)&(res->size));
 	mWriteFileInDatFile(res);
 	free(img.pix);
diff --git a/PR/src/lib/formats/pal.c b/PR/src/lib/formats/pal.c
index 0bf604c..6b638d2 100644
--- a/PR/src/lib/formats/pal.c
+++ b/PR/src/lib/formats/pal.c
@@ -138,7 +138,6 @@ int mFormatImportPal(tResource *res,const char* vFile) {
 	free(res->data);
 	res->data=pal;
 	mWriteFileInDatFile(res);
-	free(pal);
 
 	return 1;
 }
diff --git a/PR/src/lib/formats/plv.c b/PR/src/lib/formats/plv.c
index 6aceef9..5580231 100644
--- a/PR/src/lib/formats/plv.c
+++ b/PR/src/lib/formats/plv.c
@@ -170,6 +170,7 @@ extern FILE* outputStream;
 int mFormatImportPlv(tResource *res) {
 	/* declare variables */
 	unsigned char* pos;
+	unsigned char* posAux;
 	unsigned long int oldSize=res->size;
 
 	/* integrity check 1 */
@@ -190,8 +191,10 @@ int mFormatImportPlv(tResource *res) {
 		fprintf(outputStream,PR_TEXT_IMPORT_PLV_WARN);
 
 	/* save data */
+	posAux=res->data;
 	res->data=pos;
 	mWriteFileInDatFileIgnoreChecksum(res); /* TODO: check res->size-- */
+	res->data=posAux;
 	
 	return 1;
 }
diff --git a/PR/src/lib/formats/wav.c b/PR/src/lib/formats/wav.c
index 2f96d9c..4deddc1 100644
--- a/PR/src/lib/formats/wav.c
+++ b/PR/src/lib/formats/wav.c
@@ -64,6 +64,7 @@ int mFormatExportWav(const unsigned char* data, char *vFileext,unsigned long int
 int mFormatImportWav(tResource *res) {
 	unsigned char wav[]=WAVE_HEADER;
 	int i=sizeof(wav);
+	unsigned char* posAux=res->data;
 
 	if (res->size<=i) return 0;
 	res->size-=(--i);
@@ -71,6 +72,7 @@ int mFormatImportWav(tResource *res) {
 	(res->data)[sizeof(wav)-1]=1; /* First character must be a 0x01 (wav type in dat) */
 	res->data+=sizeof(wav)-1;
 	if (i==-1) mWriteFileInDatFile(res);
+	res->data=posAux;
 	return 1;
 }
 
diff --git a/PR/src/lib/layers/autodetect.c b/PR/src/lib/layers/autodetect.c
index 38e8694..925184f 100644
--- a/PR/src/lib/layers/autodetect.c
+++ b/PR/src/lib/layers/autodetect.c
@@ -45,7 +45,6 @@ resources.c: Princed Resources : Resource Handler
 #include "disk.h"
 #include "memory.h"
 #include "resources.h"
-#include "dat.h" /* MAX_RES_COUNT */
 #include "compress.h"
 
 extern FILE* outputStream;
diff --git a/PR/src/lib/layers/dat.c b/PR/src/lib/layers/dat.c
index e775a38..95521d5 100644
--- a/PR/src/lib/layers/dat.c
+++ b/PR/src/lib/layers/dat.c
@@ -382,7 +382,7 @@ int mWriteBeginDatFile(const char* vFile, int optionflag) {
 	*/
 	if (writeOpen(vFile,&writeDatFile,optionflag|backup_flag)) {
 		short fill=0;
-		resIndex=resourceListCreate();
+		resIndex=resourceListCreate(1);
 		fwriteshort(&fill,writeDatFile); /* Fill the file with 6 starting null bytes */
 		fwriteshort(&fill,writeDatFile); /* Fill the file with 6 starting null bytes */
 		fwriteshort(&fill,writeDatFile); /* Fill the file with 6 starting null bytes */
@@ -391,18 +391,6 @@ int mWriteBeginDatFile(const char* vFile, int optionflag) {
 		return 0;
 	}
 }
-/*
-void mWriteInitResource(tResource** res) {
-	if ((*res)==NULL) {
-		(*res)=(tResource*)malloc(sizeof(tResource));
-		(*res)->path=NULL;
-		(*res)->palAux=NULL;
-		(*res)->desc=NULL;
-		(*res)->name=NULL;
-	}
-	(*res)->offset=(unsigned long)ftell(writeDatFile);
-}
-*/
 
 void dat_write(const tResource* res,unsigned long off) {
 	tResource insert;
diff --git a/PR/src/lib/layers/idlist.c b/PR/src/lib/layers/idlist.c
index 38e8694..925184f 100644
--- a/PR/src/lib/layers/idlist.c
+++ b/PR/src/lib/layers/idlist.c
@@ -45,7 +45,6 @@ resources.c: Princed Resources : Resource Handler
 #include "disk.h"
 #include "memory.h"
 #include "resources.h"
-#include "dat.h" /* MAX_RES_COUNT */
 #include "compress.h"
 
 extern FILE* outputStream;
diff --git a/PR/src/lib/layers/list.c b/PR/src/lib/layers/list.c
index 89cfde0..10a44af 100644
--- a/PR/src/lib/layers/list.c
+++ b/PR/src/lib/layers/list.c
@@ -36,10 +36,11 @@ list.c: Princed Resources : Ordered Read-Only list implementarion
 #include <string.h> /* memcpy */
 #include "list.h"
 
-tList list_create(int dataSize,int dataCmp(const void*,const void*)) {
+tList list_create(int dataSize,int dataCmp(const void*,const void*),void dataFree(void* a)) {
 	tList r;
 	r.size=dataSize;
 	r.cmp=dataCmp;
+	r.free=dataFree;
 	r.cursor=NULL;
 	r.first=NULL;
 	return r;
@@ -83,6 +84,7 @@ void list_drop(tList *list) {
 	list->cursor=list->first;
 	while (list->cursor) {
 		aux=list->cursor->next;
+		list->free(list->cursor->data);
 		free(list->cursor->data);
 		free(list->cursor);
 		list->cursor=aux;
diff --git a/PR/src/lib/layers/reslist.c b/PR/src/lib/layers/reslist.c
index 8a688d5..42aec1b 100644
--- a/PR/src/lib/layers/reslist.c
+++ b/PR/src/lib/layers/reslist.c
@@ -34,6 +34,7 @@ reslist.c: Princed Resources : Ordered Read-Only list implementarion
 /* Defines */
 #include "reslist.h"
 #include <string.h> /* strncmp */
+#include "memory.h" /* freeAllocation */
 #include <stdio.h> /* debug */
 
 /* resource list layer (that uses the abstract list layer primitives) */
@@ -50,12 +51,22 @@ int resCmp(const void* a,const void* b) {
 	return resIdCmp(((tResource*)a)->id,((tResource*)b)->id);
 }
 
+void resFree(void* a) {
+	tResource* res=a;
+	freeAllocation(res->desc);
+	freeAllocation(res->name);
+	freeAllocation(res->path);
+}
+
+void resFreeDummy(void* a) {
+}
+
 void resourceListDrop(tResourceList* r) {
 	list_drop(r);
 }
 
-tResourceList resourceListCreate() {
-	return list_create(sizeof(tResource),resCmp);
+tResourceList resourceListCreate(int isCopy) {
+	return list_create(sizeof(tResource),resCmp,isCopy?resFreeDummy:resFree);
 }
 
 void resourceListAdd(tResourceList* r,const tResource* res) {
@@ -85,7 +96,6 @@ void resourceListDebugPrint(tResourceList* r) {
 void resourceListAddInfo(tResourceList* r,tResource* res) {
 	if (!list_moveCursor(r,res)) {
 		res->path=NULL;
-		res->palAux=NULL;
 		res->desc=NULL;
 		res->name=NULL;
 		res->palette.value=0;
@@ -96,7 +106,6 @@ void resourceListAddInfo(tResourceList* r,tResource* res) {
 		const tResource* resInfo=list_getCursor(r);
 		/* copy only the list information */
 		res->path=resInfo->path;
-		res->palAux=resInfo->palAux;
 		res->desc=resInfo->desc;
 		res->name=resInfo->name;
 		res->palette=resInfo->palette;
diff --git a/PR/src/lib/pr.c b/PR/src/lib/pr.c
index 918618b..6bad3a6 100644
--- a/PR/src/lib/pr.c
+++ b/PR/src/lib/pr.c
@@ -54,7 +54,6 @@ pr.c: Main source file for Princed Resources library
 #include "common.h"
 
 #include "compress.h"
-#include "dat.h" /* MAX_RES_COUNT */
 
 #include "export.h"
 #include "import.h"
@@ -126,7 +125,7 @@ int prExportDatOpt(const char* vDatFile, const char* vDirName, const char* vResF
 	char* currentDatFileName;
 	char* currentDatFile;
 
-	r=resourceListCreate();
+	r=resourceListCreate(0);
 
 	currentDatFile=strallocandcopy(vDatFile);
 
@@ -206,7 +205,7 @@ int prImportDatOpt(const char* vDatFile, const char* vDirName, const char* vResF
 	char* currentDatFileName;
 	char* currentDatFile;
 
-	r=resourceListCreate();
+	r=resourceListCreate(0);
 
 	currentDatFile=strallocandcopy(vDatFile);
 
diff --git a/PR/src/lib/xml/tree.c b/PR/src/lib/xml/tree.c
index 38e8694..925184f 100644
--- a/PR/src/lib/xml/tree.c
+++ b/PR/src/lib/xml/tree.c
@@ -45,7 +45,6 @@ resources.c: Princed Resources : Resource Handler
 #include "disk.h"
 #include "memory.h"
 #include "resources.h"
-#include "dat.h" /* MAX_RES_COUNT */
 #include "compress.h"
 
 extern FILE* outputStream;
diff --git a/PR/src/lib/xml/unknown.c b/PR/src/lib/xml/unknown.c
index 38e8694..925184f 100644
--- a/PR/src/lib/xml/unknown.c
+++ b/PR/src/lib/xml/unknown.c
@@ -45,7 +45,6 @@ resources.c: Princed Resources : Resource Handler
 #include "disk.h"
 #include "memory.h"
 #include "resources.h"
-#include "dat.h" /* MAX_RES_COUNT */
 #include "compress.h"
 
 extern FILE* outputStream;
diff --git a/PR/src/xml/resources.xml b/PR/src/xml/resources.xml
index dc0e6ac..6cd7062 100644
--- a/PR/src/xml/resources.xml
+++ b/PR/src/xml/resources.xml
@@ -768,7 +768,7 @@ resources.xml: Princed Resources : Resource tree
    <folder name="fighting" path="fighting" itemtype="image" palette="700">
     <item value="700" path="sword.pal" itemtype="palette">Unknown Resource number 700</item>
     <item value="701" path="sword01.bmp">Image sword01 (701)</item>
-    <item value="702" path="sword02">Image sword02 (702)</item>
+    <item value="702" path="sword02.bmp">Image sword02 (702)</item>
     <item value="703" path="sword03.bmp">Image sword03 (703)</item>
     <item value="704" path="sword04.bmp">Image sword04 (704)</item>
     <item value="705" path="sword05.bmp">Image sword05 (705)</item>