~ubuntu-branches/ubuntu/maverick/grafx2/maverick

« back to all changes in this revision

Viewing changes to src/io.c

  • Committer: Bazaar Package Importer
  • Author(s): Gürkan Sengün
  • Date: 2010-03-22 12:07:47 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20100322120747-g0jel6vf6mjkc53s
Tags: 2.2-1
* New upstream version, fixes FTBFS with binutils-gold. (Closes: #554742)
* Bump standards version to 3.8.4.
* debian/control: Add liblua5.1-0-dev and pkg-config to build depends.
* debian/rules: Drop dh_desktop call.
* debian/copyright: Update years.
* Switch to dpkg-source format version 3.0 (quilt).
* debian/watch: Added.
* Added patch to fix spelling errors in source code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vim:expandtab:ts=2 sw=2:
 
2
*/
 
3
/*  Grafx2 - The Ultimate 256-color bitmap paint program
 
4
 
 
5
    Copyright 2008 Yves Rizoud
 
6
    Copyright 2007 Adrien Destugues
 
7
    Copyright 1996-2001 Sunset Design (Guillaume Dorme & Karl Maritaud)
 
8
 
 
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
 
12
    of the License.
 
13
 
 
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.
 
18
 
 
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/>
 
21
*/
 
22
 
 
23
// Fonctions de lecture/ecriture file, g�rent les syst�mes big-endian et
 
24
// little-endian.
 
25
 
 
26
#define _XOPEN_SOURCE 500
 
27
 
 
28
#include <SDL_endian.h>
 
29
#include <string.h>
 
30
#include <sys/stat.h>
 
31
#include <errno.h>
 
32
#include <fcntl.h>
 
33
#include <unistd.h>
 
34
 
 
35
#if defined(__amigaos4__)
 
36
    #include <proto/dos.h>
 
37
    #include <dirent.h>
 
38
#elif defined(__WIN32__)
 
39
    #include <dirent.h>
 
40
    #include <windows.h>
 
41
#else
 
42
    #include <dirent.h>
 
43
#endif
 
44
 
 
45
#include "struct.h"
 
46
#include "io.h"
 
47
#include "realpath.h"
 
48
 
 
49
// Lit un octet
 
50
// Renvoie -1 si OK, 0 en cas d'erreur
 
51
int Read_byte(FILE *file, byte *dest)
 
52
{
 
53
  return fread(dest, 1, 1, file) == 1;
 
54
}
 
55
// Ecrit un octet
 
56
// Renvoie -1 si OK, 0 en cas d'erreur
 
57
int Write_byte(FILE *file, byte b)
 
58
{
 
59
  return fwrite(&b, 1, 1, file) == 1;
 
60
}
 
61
// Lit des octets
 
62
// Renvoie -1 si OK, 0 en cas d'erreur
 
63
int Read_bytes(FILE *file, void *dest, size_t size)
 
64
{
 
65
  return fread(dest, 1, size, file) == size;
 
66
}
 
67
// Ecrit des octets
 
68
// Renvoie -1 si OK, 0 en cas d'erreur
 
69
int Write_bytes(FILE *file, void *src, size_t size)
 
70
{
 
71
  return fwrite(src, 1, size, file) == size;
 
72
}
 
73
 
 
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)
 
77
{
 
78
  if (fread(dest, 1, sizeof(word), file) != sizeof(word))
 
79
    return 0;
 
80
  #if SDL_BYTEORDER != SDL_LIL_ENDIAN
 
81
    *dest = SDL_Swap16(*dest);
 
82
  #endif
 
83
  return -1;
 
84
}
 
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)
 
88
{
 
89
  #if SDL_BYTEORDER != SDL_LIL_ENDIAN
 
90
    w = SDL_Swap16(w);
 
91
  #endif
 
92
  return fwrite(&w, 1, sizeof(word), file) == sizeof(word);
 
93
}
 
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)
 
97
{
 
98
  if (fread(dest, 1, sizeof(word), file) != sizeof(word))
 
99
    return 0;
 
100
  #if SDL_BYTEORDER != SDL_BIG_ENDIAN
 
101
    *dest = SDL_Swap16(*dest);
 
102
  #endif
 
103
  return -1;
 
104
}
 
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)
 
108
{
 
109
  #if SDL_BYTEORDER != SDL_BIG_ENDIAN
 
110
    w = SDL_Swap16(w);
 
111
  #endif
 
112
  return fwrite(&w, 1, sizeof(word), file) == sizeof(word);
 
113
}
 
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)
 
117
{
 
118
  if (fread(dest, 1, sizeof(dword), file) != sizeof(dword))
 
119
    return 0;
 
120
  #if SDL_BYTEORDER != SDL_LIL_ENDIAN
 
121
    *dest = SDL_Swap32(*dest);
 
122
  #endif
 
123
  return -1;
 
124
}
 
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)
 
128
{
 
129
  #if SDL_BYTEORDER != SDL_LIL_ENDIAN
 
130
    dw = SDL_Swap32(dw);
 
131
  #endif
 
132
  return fwrite(&dw, 1, sizeof(dword), file) == sizeof(dword);
 
133
}
 
134
 
 
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)
 
138
{
 
139
  if (fread(dest, 1, sizeof(dword), file) != sizeof(dword))
 
140
    return 0;
 
141
  #if SDL_BYTEORDER != SDL_BIG_ENDIAN
 
142
    *dest = SDL_Swap32(*dest);
 
143
  #endif
 
144
  return -1;
 
145
}
 
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)
 
149
{
 
150
  #if SDL_BYTEORDER != SDL_BIG_ENDIAN
 
151
    dw = SDL_Swap32(dw);
 
152
  #endif
 
153
  return fwrite(&dw, 1, sizeof(dword), file) == sizeof(dword);
 
154
}
 
155
 
 
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)
 
162
{
 
163
  const char * position = NULL;
 
164
  for (; *str != '\0'; str++)
 
165
    if (*str == PATH_SEPARATOR[0]
 
166
#ifdef __WIN32__    
 
167
     || *str == '/'
 
168
#endif
 
169
     )
 
170
      position = str;
 
171
  return (char *)position;
 
172
}
 
173
// R�cup�re la partie "nom de file seul" d'un chemin
 
174
void Extract_filename(char *dest, const char *source)
 
175
{
 
176
  const char * position = Find_last_slash(source);
 
177
 
 
178
  if (position)
 
179
    strcpy(dest,position+1);
 
180
  else
 
181
    strcpy(dest,source);
 
182
}
 
183
// R�cup�re la partie "r�pertoire+/" d'un chemin.
 
184
void Extract_path(char *dest, const char *source)
 
185
{
 
186
  char * position=NULL;
 
187
 
 
188
  Realpath(source,dest);
 
189
  position = Find_last_slash(dest);
 
190
  if (position)
 
191
    *(position+1) = '\0';
 
192
  else
 
193
    strcat(dest, PATH_SEPARATOR);
 
194
}
 
195
 
 
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.
 
199
{
 
200
    struct stat buf;
 
201
    int result;
 
202
 
 
203
    result=stat(fname,&buf);
 
204
    if (result!=0)
 
205
        return(errno!=ENOENT);
 
206
    else
 
207
        return 1;
 
208
 
 
209
}
 
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.
 
213
{
 
214
  DIR* entry;    // Structure de lecture des �l�ments
 
215
 
 
216
  if (strcmp(directory,PARENT_DIR)==0)
 
217
    return 1;
 
218
  else
 
219
  {
 
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...
 
222
 
 
223
    entry=opendir(directory);
 
224
    if (entry==NULL)
 
225
        return 0;
 
226
    else
 
227
    {
 
228
        closedir(entry);
 
229
        return 1;
 
230
    }
 
231
  }
 
232
}
 
233
 
 
234
// Taille de fichier, en octets
 
235
int File_length(const char * fname)
 
236
{
 
237
    struct stat infos_fichier;
 
238
    if (stat(fname,&infos_fichier))
 
239
        return 0;
 
240
    return infos_fichier.st_size;
 
241
}
 
242
int File_length_file(FILE * file)
 
243
{
 
244
    struct stat infos_fichier;
 
245
    if (fstat(fileno(file),&infos_fichier))
 
246
        return 0;
 
247
    return infos_fichier.st_size;
 
248
}
 
249
 
 
250
void For_each_file(const char * directory_name, void Callback(const char *))
 
251
{
 
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))
 
262
  {
 
263
    strcat(full_filename, PATH_SEPARATOR);
 
264
    filename_position = strlen(full_filename);
 
265
  }
 
266
  while ((entry=readdir(current_directory)))
 
267
  {
 
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))
 
272
    {
 
273
      Callback(full_filename);
 
274
    }
 
275
  }
 
276
  closedir(current_directory);
 
277
}
 
278
 
 
279
void Get_full_filename(char * output_name, char * file_name, char * directory_name)
 
280
{
 
281
  strcpy(output_name,directory_name);
 
282
  if (output_name[0] != '\0')
 
283
  {
 
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);
 
289
  }
 
290
  strcat(output_name,file_name);
 
291
}
 
292
 
 
293
/// Lock file used to prevent several instances of grafx2 from harming each others' backups
 
294
#ifdef __WIN32__
 
295
HANDLE Lock_file_handle = INVALID_HANDLE_VALUE;
 
296
#else
 
297
int Lock_file_handle = -1;
 
298
#endif
 
299
 
 
300
byte Create_lock_file(const char *file_directory)
 
301
{
 
302
  char lock_filename[MAX_PATH_CHARACTERS];
 
303
  
 
304
  strcpy(lock_filename,file_directory);
 
305
  strcat(lock_filename,"gfx2.lck");
 
306
  
 
307
  #ifdef __WIN32__
 
308
  // Windowzy method for creating a lock file
 
309
  Lock_file_handle = CreateFile(
 
310
    lock_filename,
 
311
    GENERIC_WRITE,
 
312
    0, // No sharing
 
313
    NULL,
 
314
    OPEN_ALWAYS,
 
315
    FILE_ATTRIBUTE_NORMAL,
 
316
    NULL);
 
317
  if (Lock_file_handle == INVALID_HANDLE_VALUE)
 
318
  {
 
319
    return -1;
 
320
  }
 
321
  #else
 
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)
 
325
  {
 
326
    // Usually write-protected media
 
327
    return -1;
 
328
  }
 
329
  if (lockf(Lock_file_handle, F_TLOCK, 0)==-1)
 
330
  {
 
331
    close(Lock_file_handle);
 
332
    // Usually write-protected media
 
333
    return -1;
 
334
  }
 
335
  #endif
 
336
  return 0;
 
337
}
 
338
 
 
339
void Release_lock_file(const char *file_directory)
 
340
{
 
341
  char lock_filename[MAX_PATH_CHARACTERS];
 
342
    
 
343
  #ifdef __WIN32__
 
344
  if (Lock_file_handle != INVALID_HANDLE_VALUE)
 
345
  {
 
346
    CloseHandle(Lock_file_handle);
 
347
  }
 
348
  #else
 
349
  if (Lock_file_handle != -1)
 
350
  {
 
351
    close(Lock_file_handle);
 
352
    Lock_file_handle = -1;
 
353
  }  
 
354
  #endif
 
355
  
 
356
  // Actual deletion
 
357
  strcpy(lock_filename,file_directory);
 
358
  strcat(lock_filename,"gfx2.lck");
 
359
  remove(lock_filename);
 
360
}