1
/* vim:expandtab:ts=2 sw=2:
3
/* Grafx2 - The Ultimate 256-color bitmap paint program
5
Copyright 2008 Yves Rizoud
6
Copyright 2007 Adrien Destugues
7
Copyright 1996-2001 Sunset Design (Guillaume Dorme & Karl Maritaud)
9
Grafx2 is free software; you can redistribute it and/or
10
modify it under the terms of the GNU General Public License
11
as published by the Free Software Foundation; version 2
14
Grafx2 is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
GNU General Public License for more details.
19
You should have received a copy of the GNU General Public License
20
along with Grafx2; if not, see <http://www.gnu.org/licenses/>
23
// Fonctions de lecture/ecriture file, g�rent les syst�mes big-endian et
26
#define _XOPEN_SOURCE 500
28
#include <SDL_endian.h>
35
#if defined(__amigaos4__)
36
#include <proto/dos.h>
38
#elif defined(__WIN32__)
50
// Renvoie -1 si OK, 0 en cas d'erreur
51
int Read_byte(FILE *file, byte *dest)
53
return fread(dest, 1, 1, file) == 1;
56
// Renvoie -1 si OK, 0 en cas d'erreur
57
int Write_byte(FILE *file, byte b)
59
return fwrite(&b, 1, 1, file) == 1;
62
// Renvoie -1 si OK, 0 en cas d'erreur
63
int Read_bytes(FILE *file, void *dest, size_t size)
65
return fread(dest, 1, size, file) == size;
68
// Renvoie -1 si OK, 0 en cas d'erreur
69
int Write_bytes(FILE *file, void *src, size_t size)
71
return fwrite(src, 1, size, file) == size;
74
// Lit un word (little-endian)
75
// Renvoie -1 si OK, 0 en cas d'erreur
76
int Read_word_le(FILE *file, word *dest)
78
if (fread(dest, 1, sizeof(word), file) != sizeof(word))
80
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
81
*dest = SDL_Swap16(*dest);
85
// Ecrit un word (little-endian)
86
// Renvoie -1 si OK, 0 en cas d'erreur
87
int Write_word_le(FILE *file, word w)
89
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
92
return fwrite(&w, 1, sizeof(word), file) == sizeof(word);
94
// Lit un word (big-endian)
95
// Renvoie -1 si OK, 0 en cas d'erreur
96
int Read_word_be(FILE *file, word *dest)
98
if (fread(dest, 1, sizeof(word), file) != sizeof(word))
100
#if SDL_BYTEORDER != SDL_BIG_ENDIAN
101
*dest = SDL_Swap16(*dest);
105
// Ecrit un word (big-endian)
106
// Renvoie -1 si OK, 0 en cas d'erreur
107
int Write_word_be(FILE *file, word w)
109
#if SDL_BYTEORDER != SDL_BIG_ENDIAN
112
return fwrite(&w, 1, sizeof(word), file) == sizeof(word);
114
// Lit un dword (little-endian)
115
// Renvoie -1 si OK, 0 en cas d'erreur
116
int Read_dword_le(FILE *file, dword *dest)
118
if (fread(dest, 1, sizeof(dword), file) != sizeof(dword))
120
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
121
*dest = SDL_Swap32(*dest);
125
// Ecrit un dword (little-endian)
126
// Renvoie -1 si OK, 0 en cas d'erreur
127
int Write_dword_le(FILE *file, dword dw)
129
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
132
return fwrite(&dw, 1, sizeof(dword), file) == sizeof(dword);
135
// Lit un dword (big-endian)
136
// Renvoie -1 si OK, 0 en cas d'erreur
137
int Read_dword_be(FILE *file, dword *dest)
139
if (fread(dest, 1, sizeof(dword), file) != sizeof(dword))
141
#if SDL_BYTEORDER != SDL_BIG_ENDIAN
142
*dest = SDL_Swap32(*dest);
146
// Ecrit un dword (big-endian)
147
// Renvoie -1 si OK, 0 en cas d'erreur
148
int Write_dword_be(FILE *file, dword dw)
150
#if SDL_BYTEORDER != SDL_BIG_ENDIAN
153
return fwrite(&dw, 1, sizeof(dword), file) == sizeof(dword);
156
// D�termine la position du dernier '/' ou '\\' dans une chaine,
157
// typiquement pour s�parer le nom de file d'un chemin.
158
// Attention, sous Windows, il faut s'attendre aux deux car
159
// par exemple un programme lanc� sous GDB aura comme argv[0]:
160
// d:\Data\C\GFX2\grafx2/grafx2.exe
161
char * Find_last_slash(const char * str)
163
const char * position = NULL;
164
for (; *str != '\0'; str++)
165
if (*str == PATH_SEPARATOR[0]
171
return (char *)position;
173
// R�cup�re la partie "nom de file seul" d'un chemin
174
void Extract_filename(char *dest, const char *source)
176
const char * position = Find_last_slash(source);
179
strcpy(dest,position+1);
183
// R�cup�re la partie "r�pertoire+/" d'un chemin.
184
void Extract_path(char *dest, const char *source)
186
char * position=NULL;
188
Realpath(source,dest);
189
position = Find_last_slash(dest);
191
*(position+1) = '\0';
193
strcat(dest, PATH_SEPARATOR);
196
int File_exists(char * fname)
197
// D�termine si un file pass� en param�tre existe ou non dans le
198
// r�pertoire courant.
203
result=stat(fname,&buf);
205
return(errno!=ENOENT);
210
int Directory_exists(char * directory)
211
// D�termine si un r�pertoire pass� en param�tre existe ou non dans le
212
// r�pertoire courant.
214
DIR* entry; // Structure de lecture des �l�ments
216
if (strcmp(directory,PARENT_DIR)==0)
220
// On va chercher si le r�pertoire existe � l'aide d'un Opendir. S'il
221
// renvoie NULL c'est que le r�pertoire n'est pas accessible...
223
entry=opendir(directory);
234
// Taille de fichier, en octets
235
int File_length(const char * fname)
237
struct stat infos_fichier;
238
if (stat(fname,&infos_fichier))
240
return infos_fichier.st_size;
242
int File_length_file(FILE * file)
244
struct stat infos_fichier;
245
if (fstat(fileno(file),&infos_fichier))
247
return infos_fichier.st_size;
250
void For_each_file(const char * directory_name, void Callback(const char *))
252
// Pour scan de r�pertoire
253
DIR* current_directory; //R�pertoire courant
254
struct dirent* entry; // Structure de lecture des �l�ments
255
char full_filename[MAX_PATH_CHARACTERS];
256
int filename_position;
257
strcpy(full_filename, directory_name);
258
current_directory=opendir(directory_name);
259
if(current_directory == NULL) return; // R�pertoire invalide ...
260
filename_position = strlen(full_filename);
261
if (filename_position==0 || strcmp(full_filename+filename_position-1,PATH_SEPARATOR))
263
strcat(full_filename, PATH_SEPARATOR);
264
filename_position = strlen(full_filename);
266
while ((entry=readdir(current_directory)))
268
struct stat Infos_enreg;
269
strcpy(&full_filename[filename_position], entry->d_name);
270
stat(full_filename,&Infos_enreg);
271
if (S_ISREG(Infos_enreg.st_mode))
273
Callback(full_filename);
276
closedir(current_directory);
279
void Get_full_filename(char * output_name, char * file_name, char * directory_name)
281
strcpy(output_name,directory_name);
282
if (output_name[0] != '\0')
284
// Append a separator at the end of path, if there isn't one already.
285
// This handles the case of directory variables which contain one,
286
// as well as directories like "/" on Unix.
287
if (output_name[strlen(output_name)-1]!=PATH_SEPARATOR[0])
288
strcat(output_name,PATH_SEPARATOR);
290
strcat(output_name,file_name);
293
/// Lock file used to prevent several instances of grafx2 from harming each others' backups
295
HANDLE Lock_file_handle = INVALID_HANDLE_VALUE;
297
int Lock_file_handle = -1;
300
byte Create_lock_file(const char *file_directory)
302
char lock_filename[MAX_PATH_CHARACTERS];
304
strcpy(lock_filename,file_directory);
305
strcat(lock_filename,"gfx2.lck");
308
// Windowzy method for creating a lock file
309
Lock_file_handle = CreateFile(
315
FILE_ATTRIBUTE_NORMAL,
317
if (Lock_file_handle == INVALID_HANDLE_VALUE)
322
// Unixy method for lock file
323
Lock_file_handle = open(lock_filename,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
324
if (Lock_file_handle == -1)
326
// Usually write-protected media
329
if (lockf(Lock_file_handle, F_TLOCK, 0)==-1)
331
close(Lock_file_handle);
332
// Usually write-protected media
339
void Release_lock_file(const char *file_directory)
341
char lock_filename[MAX_PATH_CHARACTERS];
344
if (Lock_file_handle != INVALID_HANDLE_VALUE)
346
CloseHandle(Lock_file_handle);
349
if (Lock_file_handle != -1)
351
close(Lock_file_handle);
352
Lock_file_handle = -1;
357
strcpy(lock_filename,file_directory);
358
strcat(lock_filename,"gfx2.lck");
359
remove(lock_filename);