author | ecalot
<ecalot> 2005-03-23 07:26:32 UTC |
committer | ecalot
<ecalot> 2005-03-23 07:26:32 UTC |
parent | 39bcd63cf0b524e72bf632eb6efb57adb9c11d08 |
FP/src/include/disk.h | +1 | -2 |
FP/src/include/resources.h | +0 | -1 |
FP/src/res/disk.c | +304 | -7 |
PR/src/lib/layers/disk.c | +93 | -22 |
diff --git a/FP/src/include/disk.h b/FP/src/include/disk.h index 29df8ab..12ad8ef 100644 --- a/FP/src/include/disk.h +++ b/FP/src/include/disk.h @@ -43,6 +43,7 @@ disk.h: Princed Resources : Disk Access & File handling functions headers /* 64 Kb */ #define SIZE_OF_FILE 256*1024 +#define MAX_FILENAME_SIZE 260 #define DIR_SEPARATOR '/' @@ -72,9 +73,7 @@ int mLoadFileArray(const char* vFile,unsigned char** array); int makebase (const char* p); const char* repairFolders(const char* a); const char* getFileNameFromPath(const char* path); -int recurseDirectory(const char* path,int optionflag, const char* extension,const char* dirName,const char* resFile, const char* datfilename,const char* datAuthor,FILE* output); whatIs isDir(const char *nombre); -int mCopy(const char* strSource, const char* strTarget); #define mRemoveFile(a) remove(repairFolders(a)) /* array2vars*/ diff --git a/FP/src/include/resources.h b/FP/src/include/resources.h index 4e1320e..02425a2 100644 --- a/FP/src/include/resources.h +++ b/FP/src/include/resources.h @@ -37,7 +37,6 @@ resources.h: Free Prince : Resource Handler typedef enum {eImages=2,eWave,eMidi,eLevels}tDataType; -#define MAX_FILENAME_SIZE 260 #define MAX_RES_COUNT 25000 /* Item Types */ diff --git a/FP/src/res/disk.c b/FP/src/res/disk.c index 23f26bb..947785b 100644 --- a/FP/src/res/disk.c +++ b/FP/src/res/disk.c @@ -40,21 +40,46 @@ disk.c: Princed Resources : Disk Access & File handling functions #include "memory.h" #include <string.h> #include "disk.h" -#define IGNORERECURSIVEFUNCTIONS -#ifndef NOLINUX +#ifndef WIN32 #define UNIX #endif #include <sys/types.h> #include <sys/stat.h> -#include "resources.h" +/* #define DISK_DIR_SCANING */ +/* #define DISK_TERM_MANIPULATION */ +#define DISK_ALLWAYS_FORCE + +#ifndef DISK_ALLWAYS_FORCE +#include "pr.h" /* language texts */ +#include "xmlparse.h" /* equalsIgnoreCase */ +#endif #ifndef WIN32 #define defmkdir(a) mkdir (a,(mode_t)0755) + /* Recursive directory scanning needs <dirent> for posix or "direntwin" for windows */ + #ifdef DISK_DIR_SCANING + #include <dirent.h> + #endif + /* Terminal manipulation for unix (to avoid the enter after selecting an option) */ + #ifdef DISK_TERM_MANIPULATION + #include <termios.h> + #include <unistd.h> + #include <fcntl.h> + #endif + #define osIndepGetCharacter() getchar() #else - #include <direct.h> + #include <direct.h> /* mkdir */ #define defmkdir(a) mkdir (a) + #ifdef DISK_DIR_SCANING + #include "direntwin.h" + #endif + #define osIndepGetCharacter() getche() +#endif + +#ifndef DISK_ALLWAYS_FORCE +extern FILE* outputStream; #endif /***************************************************************\ @@ -115,6 +140,182 @@ int makebase(const char* p) { return a; } +#ifndef DISK_ALLWAYS_FORCE +static tOpenFiles* openFilesList=NULL; + +void addFileToOpenFilesList(const char* fileName,int hasBackup) { + /* + Add a recently safe open file to the file pointer dynamic table + using the LIFO philosophy. + */ + + tOpenFiles* newNode; + + + /* Create the new node and fill in the fields */ + newNode=(tOpenFiles*)malloc(sizeof(tOpenFiles)); + newNode->next=openFilesList; + newNode->name=strallocandcopy(fileName); + + if (hasBackup) { + + newNode->size=mLoadFileArray(fileName,&(newNode->content)); + + } else { + + newNode->size=0; + + } + openFilesList=newNode; +} + +void addPointerToOpenFilesList(FILE* fp) { /* TODO: use a define */ + openFilesList->file=fp; +} + +int getFromOpenFilesList(FILE* fp, char** fileName, unsigned char** content, unsigned long int *size) { + tOpenFiles* currentNode; + tOpenFiles* priorNode=NULL; + + /* Search using FILE* file as key */ + if (openFilesList==NULL) return 0; /* Empty list */ + currentNode=openFilesList; + while ((currentNode->file!=fp)&&(currentNode->next!=NULL)) { + priorNode=currentNode; + currentNode=currentNode->next; + } + if (currentNode->file!=fp) return 0; /* Not found */ + + /* Return results */ + *fileName=currentNode->name; + *content=currentNode->content; + *size=currentNode->size; + + /* free node and set prior pointer to the next */ + if (priorNode==NULL) { + openFilesList=currentNode->next; + } else { + priorNode->next=currentNode->next; + } + free(currentNode); + + return 1; +} + +int writeClose(FILE* fp,int dontSave,int optionflag,const char* backupExtension) { + unsigned char* content; + char* fileName; + unsigned long int size; + + if (getFromOpenFilesList(fp,&fileName,&content,&size)) { + if (dontSave) { + fclose(fp); + if (size) { + fp=fopen(fileName,"wb"); + if (fp==NULL) return -1; + fwrite(content,1,size,fp); + fclose(fp); + } else { + remove(fileName); + } + } else { + /* File Existed before and we need to back it up */ + if (hasFlag(backup_flag)) { + char aux[MAX_FILENAME_SIZE]; + static const char defaultbackupExtension[]=DEFAULT_BACKUP_EXTENSION; + /* Set default values if there isn't */ + if (backupExtension==NULL) backupExtension=defaultbackupExtension; + /* generate the file name */ + sprintf(aux,"%s.%s",fileName, backupExtension); + fclose(fp); + fp=fopen(aux,"wb"); + if (fp==NULL) return -2; + fwrite(content,1,size,fp); + fclose(fp); + } + } + + free(fileName); + if (size) free(content); + } + + return 0; +} + +int writeOpen(const char* vFileext, FILE* *fp, int optionflag) { + /* + Opens vFileext for write access + if the path doesn't exist it is created + if the file doesn't exist it is created + if the file does exist it is overwritten + + Sets the file pointer and returns 1 if Ok or 0 if error + + Returns + 0 if error + 1 if ok + */ + const char* file; + whatIs fileType; + static int all=0; + int result; + +#ifdef UNIX +#ifdef DISK_TERM_MANIPULATION + /* This will eliminate the enter after the input */ + struct termios term; + struct termios termOld; + + tcgetattr (STDIN_FILENO, &term); + tcgetattr (STDIN_FILENO, &termOld); /* save original proprieties */ + term.c_lflag &= ~(ICANON); + tcsetattr (STDIN_FILENO, TCSANOW, &term); +#endif +#endif + + /* Create base directory and save file */ + file=repairFolders(vFileext); + + /* Verify if file already exists. */ + fileType=isDir(vFileext); + if (fileType==eDirectory) return 0; + + + if (fileType==eFile) { + /* File exists. We need to ask */ + if ((!(hasFlag(force_flag)))&&(!all)) { + char answer; + printf(PR_TEXT_DISK_REPLACE,getFileNameFromPath(file)); + answer=osIndepGetCharacter(); + printf("\n"); + if (charToUpper(answer)==PR_DISK_REPLACE_NO) return 0; + if (charToUpper(answer)==PR_DISK_REPLACE_ALL) all=1; + } + } else { + makebase(file); + } + + +#ifdef UNIX +#ifdef DISK_TERM_MANIPULATION + /* restoring previous terminal options */ + term=termOld; + tcsetattr (STDIN_FILENO, TCSANOW, &termOld); +#endif +#endif + /* + If the file exists, we need to remember the old content in memory + if not, we need to know the name in case we need to delete it + */ + + + addFileToOpenFilesList(file,hasFlag(backup_flag)); + + if ((result=((*fp=fopen(file,"wb"))!=NULL))) addPointerToOpenFilesList(*fp); + + return result; +} +#else int writeClose(FILE* fp,int dontSave,int optionflag,const char* backupExtension) { unsigned long int size=0; @@ -168,7 +369,7 @@ int writeOpen(const char* vFileext, FILE* *fp, int optionflag) { result=((*fp=fopen(file,"wb"))!=NULL);/* addPointerToOpenFilesList(*fp);*/ return result; } - +#endif int writeData(const unsigned char* data, int ignoreChars, char* vFileext, int size, int optionflag,const char* backupExtension) { /* @@ -214,7 +415,6 @@ int mLoadFileArray(const char* vFile,unsigned char** array) { /* Open the file */ if ((fp=fopen(repairFolders(vFile),"rb"))==NULL) { - fprintf(stderr, "mLoadFileArray: Unable to open file\n"); return 0; } else { /* get file size */ @@ -223,7 +423,6 @@ int mLoadFileArray(const char* vFile,unsigned char** array) { if ( !aux || (aux>SIZE_OF_FILE) || ( ((*array=(unsigned char*)malloc(sizeof(char)*aux))==NULL) ) ) { /* if the file was null or bigger than the max size or couldn't allocate the file in memory */ fclose(fp); - fprintf(stderr, "mLoadFileArray: Wrong size\n"); return 0; } else { /* if the file was successfully open */ @@ -263,3 +462,101 @@ whatIs isDir(const char *path) { return (S_ISDIR(buf.st_mode))?eDirectory:eFile; } +#ifdef DISK_DIR_SCANING + +int recurseDirectory(const char* path,int recursive, void* pass, void (*function)(const char*,void*)) { + /* + Search for all .dat files in the directory + if recursive flag is set search over the subdirectories + 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* recursivePath; + struct dirent* directoryStructure; + DIR* dir; + + /* Opens directory */ + if ((dir = opendir(path))==NULL) { + return -1; + } + + /* Main loop: 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 */ + recursivePath=(char*)malloc(sizeOfPath+2+sizeOfFile); + memcpy(recursivePath,path,sizeOfPath); + recursivePath[sizeOfPath]=DIR_SEPARATOR; + memcpy(recursivePath+sizeOfPath+1,directoryStructure->d_name,sizeOfFile+1); + + if (isDir(recursivePath)==eDirectory&&recursive) { + /* Only recurse if we are in a directory and recursive is true */ + recurseDirectory(recursivePath,recursive,pass,function); + } else { + function(recursivePath,pass); + } + /* Free all allocated memory */ + free(recursivePath); + } + } + closedir(dir); + return 0; +} + +#endif + +#ifdef MACOS +int macfreads (void* bigEndian,FILE* file) { + unsigned short int littleEndian; + unsigned char* lit_e=(unsigned char*)&littleEndian; + unsigned char* big_e=(unsigned char*)bigEndian; + int result=fread(lit_e,2,1,file); + big_e[0]=lit_e[1]; + big_e[1]=lit_e[0]; + return result; +} +int macfreadl (void* bigEndian,FILE* file) { + unsigned long int littleEndian; + unsigned char* lit_e=(unsigned char*)&littleEndian; + unsigned char* big_e=(unsigned char*)bigEndian; + int result=fread(lit_e,4,1,file); + big_e[0]=lit_e[3]; + big_e[1]=lit_e[2]; + big_e[2]=lit_e[1]; + big_e[3]=lit_e[0]; + return result; +} +int macfwrites(const void* var,FILE* file) { + unsigned short int littleEndian; + unsigned short int bigEndian=*(unsigned short int*)(var); + unsigned char* lit_e=(unsigned char*)&littleEndian; + unsigned char* big_e=(unsigned char*)&bigEndian; + lit_e[0]=big_e[1]; + lit_e[1]=big_e[0]; + return fwrite(lit_e,2,1,file); +} + +int macfwritel(const void* var,FILE* file) { + unsigned long int littleEndian; + long int bigEndian=*(long int*)(var); + unsigned char* lit_e=(unsigned char*)&littleEndian; + unsigned char* big_e=(unsigned char*)&bigEndian; + lit_e[0]=big_e[3]; + lit_e[1]=big_e[2]; + lit_e[2]=big_e[1]; + lit_e[3]=big_e[0]; + return fwrite(lit_e,4,1,file); +} + +#endif + diff --git a/PR/src/lib/layers/disk.c b/PR/src/lib/layers/disk.c index f1aa1ab..f5ff968 100644 --- a/PR/src/lib/layers/disk.c +++ b/PR/src/lib/layers/disk.c @@ -39,29 +39,48 @@ disk.c: Princed Resources : Disk Access & File handling functions /* Defines */ #include "memory.h" #include <string.h> -#include "pr.h" #include "disk.h" -#include "xmlparse.h" /* equalsIgnoreCase */ +#ifndef WIN32 +#define UNIX +#endif #include <sys/types.h> #include <sys/stat.h> +#define DISK_DIR_SCANING +#define DISK_TERM_MANIPULATION +/* #define DISK_ALLWAYS_FORCE */ + +#ifndef DISK_ALLWAYS_FORCE +#include "pr.h" /* language texts */ +#include "xmlparse.h" /* equalsIgnoreCase */ +#endif + #ifndef WIN32 - #define defmkdir(a) mkdir (a,(mode_t)0755) - #include <dirent.h> - #include <termios.h> - #include <unistd.h> - #include <fcntl.h> + #define defmkdir(a) mkdir (a,(mode_t)0755) + /* Recursive directory scanning needs <dirent> for posix or "direntwin" for windows */ + #ifdef DISK_DIR_SCANING + #include <dirent.h> + #endif + /* Terminal manipulation for unix (to avoid the enter after selecting an option) */ + #ifdef DISK_TERM_MANIPULATION + #include <termios.h> + #include <unistd.h> + #include <fcntl.h> + #endif #define osIndepGetCharacter() getchar() #else - #include <direct.h> - #include "direntwin.h" - #define defmkdir(a) mkdir (a) - #include <conio.h> + #include <direct.h> /* mkdir */ + #define defmkdir(a) mkdir (a) + #ifdef DISK_DIR_SCANING + #include "direntwin.h" + #endif #define osIndepGetCharacter() getche() #endif +#ifndef DISK_ALLWAYS_FORCE extern FILE* outputStream; +#endif /***************************************************************\ | Disk Access & File handling functions | @@ -72,7 +91,6 @@ const char *repairFolders(const char* a) { int i,k; static char result[MAX_FILENAME_SIZE]; - for (i=0,k=0;(k<MAX_FILENAME_SIZE)&&a[i];) { if (isDirSep(a,i)) { result[k]=DIR_SEPARATOR; @@ -84,9 +102,7 @@ const char *repairFolders(const char* a) { } k++; } - result[k]=0; - return result; } @@ -124,6 +140,7 @@ int makebase(const char* p) { return a; } +#ifndef DISK_ALLWAYS_FORCE static tOpenFiles* openFilesList=NULL; void addFileToOpenFilesList(const char* fileName,int hasBackup) { @@ -244,7 +261,7 @@ int writeOpen(const char* vFileext, FILE* *fp, int optionflag) { int result; #ifdef UNIX -#ifndef IGNORE_TERM_CHANGE +#ifdef DISK_TERM_MANIPULATION /* This will eliminate the enter after the input */ struct termios term; struct termios termOld; @@ -256,7 +273,6 @@ int writeOpen(const char* vFileext, FILE* *fp, int optionflag) { #endif #endif - /* Create base directory and save file */ file=repairFolders(vFileext); @@ -281,7 +297,7 @@ int writeOpen(const char* vFileext, FILE* *fp, int optionflag) { #ifdef UNIX -#ifndef IGNORE_TERM_CHANGE +#ifdef DISK_TERM_MANIPULATION /* restoring previous terminal options */ term=termOld; tcsetattr (STDIN_FILENO, TCSANOW, &termOld); @@ -299,7 +315,61 @@ int writeOpen(const char* vFileext, FILE* *fp, int optionflag) { return result; } +#else +int writeClose(FILE* fp,int dontSave,int optionflag,const char* backupExtension) { + unsigned long int size=0; + + if (dontSave) { + fclose(fp); + if (size) { + fp=fopen(/*fileName*/"/dev/null","wb"); + if (fp==NULL) return -1; + } + } + + return fclose(fp); +} + +int writeOpen(const char* vFileext, FILE* *fp, int optionflag) { + /* + Opens vFileext for write access + if the path doesn't exist it is created + if the file doesn't exist it is created + if the file does exist it is overwritten + + Sets the file pointer and returns 1 if Ok or 0 if error + Returns + 0 if error + 1 if ok + */ + const char* file; + whatIs fileType; + int result; + + /* Create base directory and save file */ + file=repairFolders(vFileext); + + /* Verify if file already exists. */ + fileType=isDir(vFileext); + if (fileType==eDirectory) return 0; + + if (fileType==eFile) { + /* File exists. We need to ask */ + } else { + makebase(file); + } + + /* + If the file exists, we need to remember the old content in memory + if not, we need to know the name in case we need to delete it + */ + +/* addFileToOpenFilesList(file,hasFlag(backup_flag));*/ + result=((*fp=fopen(file,"wb"))!=NULL);/* addPointerToOpenFilesList(*fp);*/ + return result; +} +#endif int writeData(const unsigned char* data, int ignoreChars, char* vFileext, int size, int optionflag,const char* backupExtension) { /* @@ -337,8 +407,6 @@ int mLoadFileArray(const char* vFile,unsigned char** array) { 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. In case the file couldn't be open or memory allocated returns 0. - It will alloc an extra NULL byte at the end of the array so - the file may be treated as a string too. */ /* declare variables */ @@ -352,14 +420,13 @@ int mLoadFileArray(const char* vFile,unsigned char** array) { /* 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 ( !aux || (aux>SIZE_OF_FILE) || ( ((*array=(unsigned char*)malloc(sizeof(char)*aux))==NULL) ) ) { /* if the file was null or bigger than the max size or couldn't allocate the file in memory */ fclose(fp); return 0; } else { /* if the file was successfully open */ fseek(fp,0,SEEK_SET); - (*array)[aux]=0; aux=fread (*array,1,aux,fp); fclose(fp); return aux; @@ -392,9 +459,11 @@ whatIs isDir(const char *path) { struct stat buf; if(stat(path,&buf)==-1) return eNotFound; - return (S_IFDIR&buf.st_mode)?eDirectory:eFile; + return (S_ISDIR(buf.st_mode))?eDirectory:eFile; } +#ifdef DISK_DIR_SCANING + int recurseDirectory(const char* path,int recursive, void* pass, void (*function)(const char*,void*)) { /* Search for all .dat files in the directory @@ -444,6 +513,8 @@ int recurseDirectory(const char* path,int recursive, void* pass, void (*function return 0; } +#endif + #ifdef MACOS int macfreads (void* bigEndian,FILE* file) { unsigned short int littleEndian;