git » fp-git.git » master » tree

[master] / PR / src / lib / layers / disk.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*  Princed V3 - Prince of Persia Level Editor for PC Version
    Copyright (C) 2003 Princed Development Team

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    The authors of this program may be contacted at http://forum.princed.com.ar
*/

/*
disk.c: Princed Resources : Disk Access & File handling functions
������
 Copyright 2003 Princed Development Team
  Created: 29 Oct 2003

  Author: Enrique Calot <ecalot.cod@princed.com.ar>
  Version: 1.00 (2003-Oct-29)

  Modified by: Enrique Calot <ecalot.cod@princed.com.ar>
  Version: 1.10 (2003-Dec-03)
  Modified by: Santiago Zamora <drkzight@users.sourceforge.net>
  Version: 1.20 (2004-Jan-06)

 Note:
  DO NOT remove this copyright notice
*/

/* Defines */
#include "memory.h" /* strallocandcopy */
#include <string.h>
#include "disk.h"
#ifndef WIN32
#define UNIX
#endif

#include <sys/types.h>
#include <sys/stat.h>

#ifndef DISK_ALLWAYS_FORCE
#include "common.h" /* language texts */
#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> /* mkdir */
#define defmkdir(a) mkdir (a)
#ifdef DISK_DIR_SCANING
#include "direntwin.h"
#endif
#include <conio.h> /* getche */
#define osIndepGetCharacter() getche()
#endif

#ifndef DISK_ALLWAYS_FORCE
extern FILE* outputStream;
#endif

typedef struct tOpenFiles {
	struct tOpenFiles* next;
	FILE* file;
	char* name;
	tBinary content;
/*	unsigned char* content;
	long int size;*/
}tOpenFiles;

/***************************************************************\
|              Disk Access & File handling functions            |
\***************************************************************/

/* Repair folders */
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;
			i++;
			while (isDirSep(a,i)) i++;
		} else {
			result[k]=a[i];
			i++;
		}
		k++;
	}
	result[k]=0;
	return result;
}

/* Create base directory of a file */
int makebase(const char* p) {
	/*
		Creates the base directory of the given file "p"
		Returns 0 if created
		Returns -1 if an error occurred

		Note: if the base directory already exists it will return -1!
	*/

	/* Declare variables */
	static char old[MAX_FILENAME_SIZE];
	int i,a=0,equal=1;
	int size;
	char* aux;

	/* Initialize variables */
	size=(strlen(p)+1);
	aux=(char*)malloc(size);

	/* Make directories */
	for (i=0;i<size;i++) {
		aux[i]=0;
		equal=equal&&(old[i]==p[i]);
		if ((!equal)&&(p[i]==DIR_SEPARATOR)) a=defmkdir(aux);
		old[i]=p[i];
		aux[i]=p[i];
	}

	free(aux);
	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->content=mLoadFileArray(fileName);
		if (newNode->content.size<0) newNode->content.size=0;
	} else {
		newNode->content.size=0;
	}
	openFilesList=newNode;
}

#define addPointerToOpenFilesList(fp) openFilesList->file=fp;

int getFromOpenFilesList(FILE* fp, char** fileName, tBinary *content) {
	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) {
	tBinary content;
	char* fileName;

	if (getFromOpenFilesList(fp,&fileName,&content)) {
		if (dontSave) {
			fclose(fp);
			if (content.size) {
				fp=fopen(fileName,"wb");
				if (fp==NULL) return -1;
				fwrite(content.data,1,content.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.data,1,content.size,fp);
				fclose(fp);
			} else {
				fclose(fp);
			}
		}

		free(fileName);
		if (content.size) free(content.data);
	}

	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;

		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, const char* vFileext, int size, int optionflag,const char* backupExtension) { /* TODO: use tBinary */
	/*
		Creates vFileext and saves data in it. In case the directory doesn't
		exist it will be created.

		Data is read from ignoreChars to size.
		Example:
			if data="123456789", ignoreChars=3, size=8
			saved file will contain "45678"

		Returns
		 0 if error
		 1 if ok
	*/

	/* Declare variables */
	FILE* target;
	char  ok;

	/* Verify parameters */
	size-=ignoreChars;
	if (size<0) return 0;
	/*if (size==0) return 1; * Wrote 0 bytes */

	/* Save file */
	ok=writeOpen(vFileext,&target,optionflag);
	ok=ok&&((!size)||fwrite(data+ignoreChars,size,1,target));
	ok=ok&&(!writeCloseOk(target,optionflag,backupExtension));
	return ok;
}

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.
		In case the file couldn't be open or memory allocated returns 0.
	*/

	/* declare variables */
	FILE *fp;
	const char* file=repairFolders(vFile);
	whatIs f;
	tBinary ret;

	/* check type */
	f=isDir(file);
	if (f==eDirectory) {
		ret.size=PR_RESULT_F_FILE_NOT_OPEN_WASDIR;
		ret.data=NULL;
		return ret;
	}
	if (f==eNotFound) {
		ret.size=PR_RESULT_F_FILE_NOT_OPEN_NOTFOUND;
		ret.data=NULL;
		return ret;
	}

	/* Open the file */
	if ((fp=fopen(file,"rb"))==NULL) {
		ret.size=PR_RESULT_F_FILE_NOT_READ_ACCESS;
		ret.data=NULL;
		return ret;
	} else {
		/* get file size */
		fseek(fp,0,SEEK_END);
		ret.size=ftell(fp);
		if (!ret.size) {
			fclose(fp);
			ret.data=NULL;
			return ret;
		}
		if ((ret.data=(unsigned char*)malloc(ret.size+1))==NULL) {
			fclose(fp);
			ret.size=PR_RESULT_F_MEMORY;
			ret.data=NULL;
			return ret; /* this could happen in big files! */
		} else {
			/* if the file was successfully open */
			fseek(fp,0,SEEK_SET);
			ret.size=fread (ret.data,1,ret.size,fp);
			ret.data[ret.size]=0;
			fclose(fp);
			return ret;
		}
	}
}

const char* getFileNameFromPath(const char* path) {
	/*
		If you give a path you get the filename,
		if you give a filename, you get the same filename
	*/
	int size;
	size=strlen(path);
	while (size) {
		if (isDirSep(path,size)) {
			return path+size+1;
		}
		size--;
	}
	return path;
}

whatIs isDir(const char *path) {
	/*
		eDirectory if path is a directory
		eNotFound if path isn't a directory or doesn't exist
		eFile if it is a file
	*/
	struct stat buf;

	if(stat(path,&buf)==-1) return eNotFound;
	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 recourse 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