git » fp-git.git » commit 7c2d124

o Added recursive functions to handle file processing

author ecalot
2003-12-09 11:01:44 UTC
committer ecalot
2003-12-09 11:01:44 UTC
parent 96f056b7887f4421974c9b6334d45e249a03cdd2

o Added recursive functions to handle file processing

PR/src/console/main.c +53 -36
PR/src/lib/layers/disk.c +80 -2
PR/src/lib/pr.c +53 -36

diff --git a/PR/src/console/main.c b/PR/src/console/main.c
index 0bce2ad..6130aa7 100644
--- a/PR/src/console/main.c
+++ b/PR/src/console/main.c
@@ -58,18 +58,17 @@ pr.c: Main source file for Princed Resources
 #include "compile.h"
 #include "tasks.h"
 
-#include "memory.h" /* getMemory, free */
+#include "memory.h"    /* getMemory, free */
 #include "disk.h"      /* getFileNameFromPath */
 
 #ifndef DLL
  #ifdef UNIX
-  #include <stduni.h>
+  #include <unistd.h>
  #else
   #include "getopt.h"
  #endif
 #endif
 
-
 //functions
 
 int prExportDat(const char* vDatFile, const char* vDirName, const char* vResFile) {
@@ -132,7 +131,6 @@ x			backup_flag    - backup your files
 	a=parseFile     (vResFile,vDatFileName,r);
 	if (a<0) return a-3; //parsing errors
 	a=extract(vDatFile, vDirName,r,opt,vDatFileName);
-	printf("-3 es error: %d\n",a);
 	return a; //extracting errors/no errors
 }
 
@@ -199,45 +197,61 @@ x			backup_flag    - backup your files
                     M A I N   P R O G R A M
        *******************************************************/
 
-
-
-//Main program
-#ifndef DLL
-void syntax() {
-	printf(PARSING_HELP);
+void syntax(FILE* output) {
+	fprintf(output,PARSING_HELP);
 }
 
-int prMain(int option, const char* extension,const char* dirName,const char* resFile,const char* datfile, const char* datfilename,const char* datAuthor) {
+int prMain(int* pOption, const char* extension,const char* dirName,const char* resFile,const char* datfile, const char* datfilename,const char* datAuthor,FILE* output) {
 
 	//declare variables
 	int returnValue=1;
 
 	//Do CGI tasks
-	if (option&cgi_flag) {
-		option&=(~classify_flag);
-		printf(PR_CGI_TEXT1);
-		if (option==cgi_flag) {
-			printf(PR_CGI_TEXT2,prVerifyDatType(datfile));
+	if (optionflag&cgi_flag) {
+		optionflag&=(~classify_flag);
+		if (!(optionflag&first_flag)) {
+			fprintf(output,PR_CGI_TEXT1);
+			optionflag|=first_flag;
+		}
+		if (optionflag==cgi_flag) {
+			fprintf(output,PR_CGI_TEXT2,prVerifyDatType(datfile));
 			return 1;
 		} else {
-			option=help_flag;
+			optionflag=help_flag;
 		}
 	}
 
-	printf(PR_ABOUT);
+	//Show about
+	if (!(optionflag&first_flag)) {
+		fprintf(output,PR_ABOUT);
+		optionflag|=first_flag;
+	}
+
+	//Recursive testing
+	if (datfile==NULL) {
+		fprintf(output,"Scanning dat files in current directory\n");
+		recurseDirectory(".",pOption,extension,dirName,resFile,datfile,datfilename,datAuthor,output);
+		return 1;
+	} else if (isDir(datfile)) {
+		fprintf(output,"Scanning dat files un given directory\n");
+		recurseDirectory(datfile,pOption,extension,dirName,resFile,datfile,datfilename,datAuthor,output);
+		return 1;
+	}
+
 
 	//If bad syntax or help screen requested
-	if (option&help_flag) {
-		syntax();
+	if (optionflag&help_flag) {
+		syntax(output);
 		return -1;
 	}
 
-	if ((datfile==NULL)||option&version_flag) { //TODO use the recursive option and/or generic datfile
+	//If version asked, stop
+	if (optionflag&version_flag) {
 		return -1;
 	}
 
 	//do selected tasks
-	if (option&export_flag) {
+	if (optionflag&export_flag) {
 		char* array[]={
 			"Ok",
 			"Error accessing the file DAT", /* DAT or extracted */
@@ -247,10 +261,10 @@ int prMain(int option, const char* extension,const char* dirName,const char* res
 			"Memory error in parsing",
 			"XML Attribute not recognized",
 			"XML File not found"};
-		printf("Extracting '%s' to '%s' with %04x\r\n",datfile,dirName,option);
-		returnValue=prExportDatOpt(datfile,dirName,resFile,option,NULL,datAuthor,extension);
-		printf(PR_TEXT_RESULT,array[-returnValue],returnValue);
-	}	else if (option&classify_flag) {
+		fprintf(output,"Extracting '%s' to '%s' with %04x\r\n",datfile,dirName,optionflag);
+		returnValue=prExportDatOpt(datfile,dirName,resFile,optionflag,NULL,datAuthor,extension);
+		fprintf(output,PR_TEXT_RESULT,array[-returnValue],returnValue);
+	}	else if (optionflag&classify_flag) {
 		char* array[]={
 			"Memory error",
 			"File not found or no access error",
@@ -264,10 +278,10 @@ int prMain(int option, const char* extension,const char* dirName,const char* res
 			"PC Speaker dat file",
 			"\0","\0","\0",
 			"Pop2 dat files"};
-		printf("Classifing '%s'\r\n",datfile);
+		fprintf(output,"Classifing '%s'\r\n",datfile);
 		returnValue=prVerifyDatType(datfile);
-		printf(PR_TEXT_RESULT,array[2+returnValue],returnValue);
-	}	else if (option&import_flag) {
+		fprintf(output,PR_TEXT_RESULT,array[2+returnValue],returnValue);
+	}	else if (optionflag&import_flag) {
 		char* array[]={
 			"File succesfully compiled",
 			"DAT File couldn't be open for writing",
@@ -275,20 +289,23 @@ int prMain(int option, const char* extension,const char* dirName,const char* res
 			"No memory",
 			"XML Attribute not recognized",
 			"XML File not found"};
-		printf("Compiling '%s' from '%s' with %04x\r\n",datfile,dirName,option);
-		returnValue=prImportDatOpt(datfile,dirName,resFile,option,NULL,extension);
+		fprintf(output,"Compiling '%s' from '%s' with %04x\r\n",datfile,dirName,optionflag);
+		returnValue=prImportDatOpt(datfile,dirName,resFile,optionflag,NULL,extension);
 		if (returnValue<=0) {
-			printf(PR_TEXT_RESULT,array[-returnValue],returnValue);
+			fprintf(output,PR_TEXT_RESULT,array[-returnValue],returnValue);
 		} else {
-			printf(PR_TEXT_RESULT_ERR,returnValue);
+			fprintf(output,PR_TEXT_RESULT_ERR,returnValue);
 		}
 	} else {
-		syntax();
+		syntax(output);
 		return -1;
 	}
 	return returnValue;
 }
 
+//Main program
+#ifndef DLL
+
 int main (int argc, char **argv) {
 	//declare variables
 	char  datFileName[MAX_FILENAME_SIZE]; //TODO add
@@ -342,7 +359,7 @@ int main (int argc, char **argv) {
 					flag|=raw_flag;
 					break;
 				case 'R':
-					flag|=force_flag;
+					flag|=recursive_flag;
 					break;
 				case 't': {
 					int size;
@@ -373,7 +390,7 @@ int main (int argc, char **argv) {
 	if (!flag) flag=help_flag;
 
 	//Run main program
-	prMain(flag,extension,dirName,resFile,datFilePath,datFileName,datAuthor);
+	prMain(&flag,extension,dirName,resFile,datFilePath,datFileName,datAuthor,stdout);
 
 	//Free memory and exit
 	if (datAuthor!=NULL) free(datAuthor);
diff --git a/PR/src/lib/layers/disk.c b/PR/src/lib/layers/disk.c
index b0a25a8..d542016 100644
--- a/PR/src/lib/layers/disk.c
+++ b/PR/src/lib/layers/disk.c
@@ -39,12 +39,16 @@ disk.c: Princed Resources : Disk Access & File handling functions
 #include <string.h>
 #include "pr.h"
 #include "disk.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
 #ifdef UNIX
-	#include <sys/types.h>
-	#include <sys/stat.h>
 	#define defmkdir(a) mkdir (a,(mode_t)0755)
+	#include <dirent.h>
 #else
 	#include <direct.h>
+	#include "dirent.h"
 	#define defmkdir(a) mkdir (a)
 #endif
 
@@ -257,4 +261,78 @@ const char* getFileNameFromPath(const char* path) {
 	return path;
 }
 
+int isDir(const char *path) {
+	/*
+		1 if nombre is a directory
+		0 if nombre isn't a directory or doesn't exist
+	*/
+	struct stat buf;
+
+	if(stat(path,&buf)==-1) return 0;
+	return (S_IFDIR&buf.st_mode);
+}
+
+int recurseDirectory(const char* path,int* pOption, const char* extension,const char* dirName,const char* resFile,const char* datfile, const char* datfilename,const char* datAuthor,FILE* output) {
+	/*
+		Searchs for all .dat files in the directory
+		if recursive flag is set searchs over the dubdirectories
+		if verbose flag is set shows some messages in the screen
+		when .dat files are found it runs prMain form each of them
+	*/
 
+	//Declare variables
+	char*          recursive;
+	struct dirent* directoryStructure;
+	DIR*           dir;
+
+	//Opens directory
+	if ((dir = opendir(path))==NULL) {
+		return 0;
+	}
+
+	//Shows some messages
+	if ((optionflag&recursive_flag)&&(optionflag&verbose_flag)) { //Only recurse if recursive and verbose flags are set
+		printf("Processing '%s'...\n",path);
+	}
+
+	//Main cicle: while there are still more files left
+	while ((directoryStructure = readdir(dir))!=NULL) {
+		if /* Don't look over the system directories */
+			(!(
+				!strcmp(directoryStructure->d_name,".")||
+				!strcmp(directoryStructure->d_name,"..")
+		)) {
+			//Declare variables
+			int sizeOfPath=strlen(path);
+			int sizeOfFile=strlen(directoryStructure->d_name);
+
+			//Generate recursive path
+			recursive=getMemory(sizeOfPath+1+sizeOfFile);
+			memcpy(recursive,path,sizeOfPath);
+			recursive[sizeOfPath]=DIR_SEPARATOR;
+			memcpy(recursive+sizeOfPath+1,directoryStructure->d_name,sizeOfFile+1);
+
+			/*
+				If recursive path is a directory and recursive flag is set recurse into it
+				if recursive path is a directory and recursive flag wasn't set, just ignore
+				if recursive path is not a directory and is a dat file, do prMain
+				if recursive path is not a directory and is not a dat file, ignore
+			*/
+			if (isDir(recursive)) {
+				if (optionflag&recursive_flag) { //Only recurse if recursive flag is set
+					recurseDirectory(recursive,pOption,extension,dirName,resFile,datfile,datfilename,datAuthor,output);
+				}
+			} else {
+				char aux[]=".dat";
+				if (sizeOfFile>4) {
+					if (!strcmp(aux,directoryStructure->d_name+sizeOfFile-4)) {
+						prMain(pOption,extension,dirName,resFile,recursive,directoryStructure->d_name,datAuthor,output);
+					}
+				}
+			}
+			//Free all allocated mem
+			free(recursive);
+		}
+	}
+	return 1;
+}
diff --git a/PR/src/lib/pr.c b/PR/src/lib/pr.c
index 0bce2ad..6130aa7 100644
--- a/PR/src/lib/pr.c
+++ b/PR/src/lib/pr.c
@@ -58,18 +58,17 @@ pr.c: Main source file for Princed Resources
 #include "compile.h"
 #include "tasks.h"
 
-#include "memory.h" /* getMemory, free */
+#include "memory.h"    /* getMemory, free */
 #include "disk.h"      /* getFileNameFromPath */
 
 #ifndef DLL
  #ifdef UNIX
-  #include <stduni.h>
+  #include <unistd.h>
  #else
   #include "getopt.h"
  #endif
 #endif
 
-
 //functions
 
 int prExportDat(const char* vDatFile, const char* vDirName, const char* vResFile) {
@@ -132,7 +131,6 @@ x			backup_flag    - backup your files
 	a=parseFile     (vResFile,vDatFileName,r);
 	if (a<0) return a-3; //parsing errors
 	a=extract(vDatFile, vDirName,r,opt,vDatFileName);
-	printf("-3 es error: %d\n",a);
 	return a; //extracting errors/no errors
 }
 
@@ -199,45 +197,61 @@ x			backup_flag    - backup your files
                     M A I N   P R O G R A M
        *******************************************************/
 
-
-
-//Main program
-#ifndef DLL
-void syntax() {
-	printf(PARSING_HELP);
+void syntax(FILE* output) {
+	fprintf(output,PARSING_HELP);
 }
 
-int prMain(int option, const char* extension,const char* dirName,const char* resFile,const char* datfile, const char* datfilename,const char* datAuthor) {
+int prMain(int* pOption, const char* extension,const char* dirName,const char* resFile,const char* datfile, const char* datfilename,const char* datAuthor,FILE* output) {
 
 	//declare variables
 	int returnValue=1;
 
 	//Do CGI tasks
-	if (option&cgi_flag) {
-		option&=(~classify_flag);
-		printf(PR_CGI_TEXT1);
-		if (option==cgi_flag) {
-			printf(PR_CGI_TEXT2,prVerifyDatType(datfile));
+	if (optionflag&cgi_flag) {
+		optionflag&=(~classify_flag);
+		if (!(optionflag&first_flag)) {
+			fprintf(output,PR_CGI_TEXT1);
+			optionflag|=first_flag;
+		}
+		if (optionflag==cgi_flag) {
+			fprintf(output,PR_CGI_TEXT2,prVerifyDatType(datfile));
 			return 1;
 		} else {
-			option=help_flag;
+			optionflag=help_flag;
 		}
 	}
 
-	printf(PR_ABOUT);
+	//Show about
+	if (!(optionflag&first_flag)) {
+		fprintf(output,PR_ABOUT);
+		optionflag|=first_flag;
+	}
+
+	//Recursive testing
+	if (datfile==NULL) {
+		fprintf(output,"Scanning dat files in current directory\n");
+		recurseDirectory(".",pOption,extension,dirName,resFile,datfile,datfilename,datAuthor,output);
+		return 1;
+	} else if (isDir(datfile)) {
+		fprintf(output,"Scanning dat files un given directory\n");
+		recurseDirectory(datfile,pOption,extension,dirName,resFile,datfile,datfilename,datAuthor,output);
+		return 1;
+	}
+
 
 	//If bad syntax or help screen requested
-	if (option&help_flag) {
-		syntax();
+	if (optionflag&help_flag) {
+		syntax(output);
 		return -1;
 	}
 
-	if ((datfile==NULL)||option&version_flag) { //TODO use the recursive option and/or generic datfile
+	//If version asked, stop
+	if (optionflag&version_flag) {
 		return -1;
 	}
 
 	//do selected tasks
-	if (option&export_flag) {
+	if (optionflag&export_flag) {
 		char* array[]={
 			"Ok",
 			"Error accessing the file DAT", /* DAT or extracted */
@@ -247,10 +261,10 @@ int prMain(int option, const char* extension,const char* dirName,const char* res
 			"Memory error in parsing",
 			"XML Attribute not recognized",
 			"XML File not found"};
-		printf("Extracting '%s' to '%s' with %04x\r\n",datfile,dirName,option);
-		returnValue=prExportDatOpt(datfile,dirName,resFile,option,NULL,datAuthor,extension);
-		printf(PR_TEXT_RESULT,array[-returnValue],returnValue);
-	}	else if (option&classify_flag) {
+		fprintf(output,"Extracting '%s' to '%s' with %04x\r\n",datfile,dirName,optionflag);
+		returnValue=prExportDatOpt(datfile,dirName,resFile,optionflag,NULL,datAuthor,extension);
+		fprintf(output,PR_TEXT_RESULT,array[-returnValue],returnValue);
+	}	else if (optionflag&classify_flag) {
 		char* array[]={
 			"Memory error",
 			"File not found or no access error",
@@ -264,10 +278,10 @@ int prMain(int option, const char* extension,const char* dirName,const char* res
 			"PC Speaker dat file",
 			"\0","\0","\0",
 			"Pop2 dat files"};
-		printf("Classifing '%s'\r\n",datfile);
+		fprintf(output,"Classifing '%s'\r\n",datfile);
 		returnValue=prVerifyDatType(datfile);
-		printf(PR_TEXT_RESULT,array[2+returnValue],returnValue);
-	}	else if (option&import_flag) {
+		fprintf(output,PR_TEXT_RESULT,array[2+returnValue],returnValue);
+	}	else if (optionflag&import_flag) {
 		char* array[]={
 			"File succesfully compiled",
 			"DAT File couldn't be open for writing",
@@ -275,20 +289,23 @@ int prMain(int option, const char* extension,const char* dirName,const char* res
 			"No memory",
 			"XML Attribute not recognized",
 			"XML File not found"};
-		printf("Compiling '%s' from '%s' with %04x\r\n",datfile,dirName,option);
-		returnValue=prImportDatOpt(datfile,dirName,resFile,option,NULL,extension);
+		fprintf(output,"Compiling '%s' from '%s' with %04x\r\n",datfile,dirName,optionflag);
+		returnValue=prImportDatOpt(datfile,dirName,resFile,optionflag,NULL,extension);
 		if (returnValue<=0) {
-			printf(PR_TEXT_RESULT,array[-returnValue],returnValue);
+			fprintf(output,PR_TEXT_RESULT,array[-returnValue],returnValue);
 		} else {
-			printf(PR_TEXT_RESULT_ERR,returnValue);
+			fprintf(output,PR_TEXT_RESULT_ERR,returnValue);
 		}
 	} else {
-		syntax();
+		syntax(output);
 		return -1;
 	}
 	return returnValue;
 }
 
+//Main program
+#ifndef DLL
+
 int main (int argc, char **argv) {
 	//declare variables
 	char  datFileName[MAX_FILENAME_SIZE]; //TODO add
@@ -342,7 +359,7 @@ int main (int argc, char **argv) {
 					flag|=raw_flag;
 					break;
 				case 'R':
-					flag|=force_flag;
+					flag|=recursive_flag;
 					break;
 				case 't': {
 					int size;
@@ -373,7 +390,7 @@ int main (int argc, char **argv) {
 	if (!flag) flag=help_flag;
 
 	//Run main program
-	prMain(flag,extension,dirName,resFile,datFilePath,datFileName,datAuthor);
+	prMain(&flag,extension,dirName,resFile,datFilePath,datFileName,datAuthor,stdout);
 
 	//Free memory and exit
 	if (datAuthor!=NULL) free(datAuthor);