~ubuntu-branches/ubuntu/precise/code-saturne/precise

« back to all changes in this revision

Viewing changes to preprocessor/util/ecs_file.h

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2011-11-24 00:00:08 UTC
  • mfrom: (6.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20111124000008-2vo99e38267942q5
Tags: 2.1.0-3
Install a missing file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef __ECS_FILE_H__
 
2
#define __ECS_FILE_H__
 
3
 
 
4
/*============================================================================
 
5
 * Base file wrapper type and associated functions
 
6
 *============================================================================*/
 
7
 
 
8
/*
 
9
  This file is part of Code_Saturne, a general-purpose CFD tool.
 
10
 
 
11
  Copyright (C) 1998-2011 EDF S.A.
 
12
 
 
13
  This program is free software; you can redistribute it and/or modify it under
 
14
  the terms of the GNU General Public License as published by the Free Software
 
15
  Foundation; either version 2 of the License, or (at your option) any later
 
16
  version.
 
17
 
 
18
  This program is distributed in the hope that it will be useful, but WITHOUT
 
19
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
20
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
21
  details.
 
22
 
 
23
  You should have received a copy of the GNU General Public License along with
 
24
  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 
25
  Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
26
*/
 
27
 
 
28
/*----------------------------------------------------------------------------*/
 
29
 
 
30
/*
 
31
 * Obtain definitions such as that of size_t through stddef.h (C99 standard)
 
32
 * if available (preferred method), or through stdlib.h (which defines
 
33
 * malloc() and family and so must define size_t some way) otherwise.
 
34
 */
 
35
 
 
36
#if defined(__STDC_VERSION__)
 
37
#  if (__STDC_VERSION__ == 199901L)
 
38
#    include <stddef.h>
 
39
#  else
 
40
#    include <stdio.h>
 
41
#  endif
 
42
#else
 
43
#  include <stdio.h>
 
44
#endif
 
45
 
 
46
/* ECS headers */
 
47
 
 
48
#include "cs_config.h"
 
49
#include "ecs_def.h"
 
50
 
 
51
/*-----------------------------------------------------------------------------*/
 
52
 
 
53
#ifdef __cplusplus
 
54
extern "C" {
 
55
#if 0
 
56
} /* Fake brace to force Emacs auto-indentation back to column 0 */
 
57
#endif
 
58
#endif /* __cplusplus */
 
59
 
 
60
/*============================================================================
 
61
 * Public types
 
62
 *============================================================================*/
 
63
 
 
64
/* ECS file descriptor */
 
65
 
 
66
typedef struct _ecs_file_t ecs_file_t;
 
67
 
 
68
/* ECS file types */
 
69
 
 
70
typedef enum {
 
71
 
 
72
  ECS_FILE_TYPE_TEXT,          /* Text file */
 
73
  ECS_FILE_TYPE_BINARY,        /* Simple C binary file */
 
74
  ECS_FILE_TYPE_FORTRAN_BINARY /* Common Fortran binary file */
 
75
 
 
76
} ecs_file_type_t;
 
77
 
 
78
/* ECS file modes */
 
79
 
 
80
typedef enum {
 
81
 
 
82
  ECS_FILE_MODE_READ,   /* Read mode */
 
83
  ECS_FILE_MODE_WRITE,  /* Write mode */
 
84
  ECS_FILE_MODE_APPEND  /* Append mode */
 
85
 
 
86
} ecs_file_mode_t;
 
87
 
 
88
/* Offset for ECS file position indicator */
 
89
 
 
90
#if defined(SIZEOF_LONG_LONG)
 
91
typedef long long ecs_file_off_t;
 
92
#else
 
93
typedef long ecs_file_off_t;
 
94
#endif
 
95
 
 
96
/* Possibilities for the third argument of ecs_file_seek() */
 
97
 
 
98
typedef enum {
 
99
 
 
100
  ECS_FILE_SEEK_SET,   /* Seek from beginning of file */
 
101
  ECS_FILE_SEEK_CUR,   /* Seek from current position */
 
102
  ECS_FILE_SEEK_END    /* Seek from end of file */
 
103
 
 
104
} ecs_file_seek_t;
 
105
 
 
106
/*============================================================================
 
107
 * Public function prototypes
 
108
 *============================================================================*/
 
109
 
 
110
/*
 
111
 * Create a `ecs_file_t' file descriptor and open the associated file.
 
112
 *
 
113
 * The associated file is also opened. By default, data is written
 
114
 * or read as big-endian data. This behavior may be modified by
 
115
 * ecs_file_set_swap_endian().
 
116
 *
 
117
 * parameters:
 
118
 *   name: <-- file name.
 
119
 *   mode: <-- file acces mode: read, write, or append.
 
120
 *   type: <-- file type: text, binary, or Fortran binary.
 
121
 *
 
122
 * returns:
 
123
 *   pointer to ecs_file_t file descriptor (NULL in case of failure).
 
124
 */
 
125
 
 
126
ecs_file_t *
 
127
ecs_file_open(const char             *name,
 
128
              const ecs_file_mode_t   mode,
 
129
              const ecs_file_type_t   type);
 
130
 
 
131
/*
 
132
 * Destroy a `ecs_file_t' descriptor and close the associated file.
 
133
 *
 
134
 * The descriptor may only be destroyed if the file was successfully
 
135
 * closed. To force destruction of a ecs_file_t descriptor even
 
136
 * if the associated file was not closed, use (ecs_file_free_force()).
 
137
 *
 
138
 * The associated file is only closed if this was not already the case.
 
139
 *
 
140
 * parameters:
 
141
 *   f: <-- ecs_file_t descriptor.
 
142
 *
 
143
 * returns:
 
144
 *   pointer to ecs_file_t file descriptor (NULL in case of success,
 
145
 *   f in case of failure).
 
146
 */
 
147
 
 
148
ecs_file_t *
 
149
ecs_file_free(ecs_file_t  *f);
 
150
 
 
151
/*
 
152
 * Destroy a `ecs_file_t' descriptor without closing its associated file.
 
153
 *
 
154
 * parameters:
 
155
 *   f:  -> ecs_file_t descriptor.
 
156
 *
 
157
 * returns:
 
158
 *   NULL pointer.
 
159
 */
 
160
 
 
161
ecs_file_t *
 
162
ecs_file_free_descriptor(ecs_file_t *f);
 
163
 
 
164
/*
 
165
 * Open `ecs_file_t' descriptor's associated file.
 
166
 *
 
167
 * If the file is already open, this function does nothing.
 
168
 *
 
169
 * parameters:
 
170
 *  f:    <-- ecs_file_t descriptor.
 
171
 *  mode: <-- file acces mode: read, write, or append.
 
172
 *
 
173
 * returns:
 
174
 *   0 in case of success, system error code in case of failure
 
175
 *   (or Zlib error code in case of Zlib memory allocation problem
 
176
 *   for a gzipped file).
 
177
 */
 
178
 
 
179
int
 
180
ecs_file_open_stream(ecs_file_t       *f,
 
181
                     ecs_file_mode_t   mode);
 
182
 
 
183
/*
 
184
 * Close a ecs_file_t file descriptor's associated file.
 
185
 *
 
186
 * If the file is already closed, this function does nothing.
 
187
 *
 
188
 * parameter:
 
189
 *   f: <-- ecs_file_t descriptor.
 
190
 *
 
191
 * returns:
 
192
 *   0 in case of success, system error code in case of failure
 
193
 *   (or Zlib error code in case of a Zlib specific error
 
194
 *   for a gzipped file).
 
195
 */
 
196
 
 
197
int
 
198
ecs_file_close_stream(ecs_file_t  *f);
 
199
 
 
200
/*
 
201
 * Test the end-of-file indicator for a given file.
 
202
 *
 
203
 * parameter:
 
204
 *   f: <-- ecs_file_t descriptor.
 
205
 *
 
206
 * returns:
 
207
 *   0 if the end-of-file has not been reached, or non-zero
 
208
 *   (1 or feof() return value) otherwise.
 
209
 */
 
210
 
 
211
int
 
212
ecs_file_eof(const ecs_file_t  *f);
 
213
 
 
214
/*
 
215
 * Force write of all user-space buffered data for a given file.
 
216
 *
 
217
 * parameter:
 
218
 *   f: <-- ecs_file_t descriptor.
 
219
 *
 
220
 * returns:
 
221
 *   0 upon successful completion, system error code otherwise.
 
222
 */
 
223
 
 
224
int
 
225
ecs_file_flush(ecs_file_t  *f);
 
226
 
 
227
/*
 
228
 * Obtain the current value of a file's position indicator.
 
229
 *
 
230
 * parameter:
 
231
 *   f: <-- ecs_file_t descriptor.
 
232
 *
 
233
 * returns:
 
234
 *   current value of the file's position indicator, or -1 in case of failure.
 
235
 */
 
236
 
 
237
ecs_file_off_t
 
238
ecs_file_tell(ecs_file_t  *f);
 
239
 
 
240
/*
 
241
 * Sets the file position indicator to the beginning of the file.
 
242
 *
 
243
 * A successful call to this function clears the end-of-file indicator for
 
244
 * this file.
 
245
 *
 
246
 * parameter:
 
247
 *   f: <-- ecs_file_t descriptor.
 
248
 */
 
249
 
 
250
void
 
251
ecs_file_rewind(ecs_file_t  *f);
 
252
 
 
253
/*
 
254
 * This function may call the libc's fseek() function, or Zlib's gzseek()
 
255
 * function. The C 99 standard draft specifies that for a text file, the offset
 
256
 * argument to fseek() should be zero or a value returned by an earlier
 
257
 * successful call to ftell() (here ecs_file_ftell()) on a stream (here a
 
258
 * ecs_file_t structure). Zlib's gzseek() does not support SEEK_END, at least
 
259
 * as of version 1.2.1.
 
260
 *
 
261
 * A successful call to this function clears the end-of-file indicator for
 
262
 * this file.
 
263
 *
 
264
 * parameters:
 
265
 *   f:      <-- ecs_file_t descriptor.
 
266
 *   offset: <-- add to position specified to whence to obtain new position,
 
267
 *               measured in characters from the beginning of the file.
 
268
 *   whence: <-- beginning if ECS_FILE_SEEK_SET, current if ECS_FILE_SEEK_CUR,
 
269
 *               or end-of-file if ECS_FILE_SEEK_END.
 
270
 *
 
271
 * returns:
 
272
 *   0 upon success, nonzero otherwise.
 
273
 */
 
274
 
 
275
int
 
276
ecs_file_seek(ecs_file_t             *f,
 
277
              const ecs_file_off_t    offset,
 
278
              const ecs_file_seek_t   whence);
 
279
 
 
280
/*
 
281
 * Return a file's name.
 
282
 *
 
283
 * parameter:
 
284
 *   f: <-- ecs_file_t descriptor.
 
285
 *
 
286
 * returns:
 
287
 *   pointer to file's name.
 
288
 */
 
289
 
 
290
const char *
 
291
ecs_file_get_name(const ecs_file_t  *f);
 
292
 
 
293
/*
 
294
 * Return a file's type.
 
295
 *
 
296
 * parameter:
 
297
 *   f: <-- ecs_file_t descriptor.
 
298
 *
 
299
 * returns:
 
300
 *   file's type.
 
301
 */
 
302
 
 
303
ecs_file_type_t
 
304
ecs_file_get_type(const ecs_file_t  *f);
 
305
 
 
306
/*
 
307
 * Change a file's type.
 
308
 *
 
309
 * Using this function assumes one is familiar with a file's coding
 
310
 * or structure; use with caution.
 
311
 *
 
312
 * parameters:
 
313
 *   f:    <-> ecs_file_t descriptor.
 
314
 *   type: <-- text, binary, or Fortran binary type descriptor.
 
315
 */
 
316
 
 
317
void
 
318
ecs_file_set_type(ecs_file_t             *f,
 
319
                  const ecs_file_type_t   type);
 
320
 
 
321
/*
 
322
 * Ensure that data is read or written in big-endian
 
323
 * (network standard) format.
 
324
 *
 
325
 * By default, data is written or read in native format (as regards
 
326
 * big-endian or little-endian)..
 
327
 *
 
328
 * parameter:
 
329
 *   f: <-> ecs_file_t descriptor.
 
330
 */
 
331
 
 
332
void
 
333
ecs_file_set_big_endian(ecs_file_t  *f);
 
334
 
 
335
/*
 
336
 * Return a file's byte-swapping behavior.
 
337
 *
 
338
 * parameter:
 
339
 *   f: <-- ecs_file_t descriptor.
 
340
 *
 
341
 * returns:
 
342
 *   0 if file's endianness is the same as the system's, 1 otherwise.
 
343
 */
 
344
 
 
345
int
 
346
ecs_file_get_swap_endian(const ecs_file_t  *f);
 
347
 
 
348
/*
 
349
 * Set a file's byte-swapping behavior.
 
350
 *
 
351
 * Using this function assumes one is familiar with a file's coding
 
352
 * or structure; use with caution.
 
353
 *
 
354
 * parameters:
 
355
 *   f:    <-> ecs_file_t descriptor.
 
356
 *   swap: <-- 1 if bytes must be swapped, 0 therwise.
 
357
 */
 
358
 
 
359
void
 
360
ecs_file_set_swap_endian(ecs_file_t  *f,
 
361
                         const int    swap);
 
362
 
 
363
/*
 
364
 * Test a file's error or EOF condition.
 
365
 *
 
366
 * parameters:
 
367
 *   f:    <-- ecs_file_t descriptor.
 
368
 *   line: <-- file line number if available, or 0.
 
369
 *
 
370
 * returns:
 
371
 *   0 if no error, system error code, or -1 if EOF.
 
372
 */
 
373
 
 
374
int
 
375
ecs_file_read_check_error(const ecs_file_t  *f,
 
376
                          const int          line);
 
377
 
 
378
/*
 
379
 * Formatted output to a text file (as fprintf()).
 
380
 *
 
381
 * parameters:
 
382
 *   f:      <-- ecs_file_t descriptor.
 
383
 *   format: <-- format string, as printf() and family.
 
384
 *   ... :   <-- variable arguments based on format string.
 
385
 *
 
386
 * returns:
 
387
 *   number of characters printed, not counting the trailing '\0'
 
388
 *   used to end output strings
 
389
 */
 
390
 
 
391
int
 
392
ecs_file_printf(const ecs_file_t  *const f,
 
393
                const char        *const format,
 
394
                ...);
 
395
 
 
396
/*
 
397
 * Formatted input from a text file (as fgets()).
 
398
 *
 
399
 * parameters:
 
400
 *   s:    --> buffer to which string is to be read.
 
401
 *   size: <-- maximum number of characters to be read plus one.
 
402
 *   f:    <-- ecs_file_t descriptor.
 
403
 *   line: <-> file line number if available, or NULL.
 
404
 *
 
405
 * returns:
 
406
 *   s on success, NULL on error or when end of file occurs and
 
407
 *   no characters have been read.
 
408
 */
 
409
 
 
410
char *
 
411
ecs_file_gets(char              *s,
 
412
              const int          size,
 
413
              const ecs_file_t  *f,
 
414
              int               *line);
 
415
 
 
416
/*
 
417
 * Formatted input from a text file if possible (as fgets()).
 
418
 *
 
419
 * This function is similar to ecs_file_gets(), but failure to read
 
420
 * a line du to an end-of-file condition is not considered an error with
 
421
 * this variant, which may be used to read text files or sections thereof
 
422
 * of unknown length
 
423
 *
 
424
 * parameters:
 
425
 *   s:    --> buffer to which string is to be read.
 
426
 *   size: <-- maximum number of characters to be read plus one.
 
427
 *   f:    <-- ecs_file_t descriptor.
 
428
 *   line: <-> file line number if available, or NULL.
 
429
 *
 
430
 * returns:
 
431
 *   s on success, NULL on error or when end of file occurs and
 
432
 *   no characters have been read.
 
433
 */
 
434
 
 
435
char *
 
436
ecs_file_gets_try(char              *s,
 
437
                  const int          size,
 
438
                  const ecs_file_t  *f,
 
439
                  int               *line);
 
440
 
 
441
/*
 
442
 * Read a binary C or Fortran type record.
 
443
 *
 
444
 * A Fortran record compatible with most compilers is structured
 
445
 * as follows:
 
446
 *   - a 4-byte integer indicating the number of bytes in the record.
 
447
 *   - the raw data
 
448
 *   - a 4-byte integer indicating the number of bytes in the record.
 
449
 *
 
450
 * A C record contains only the raw data.
 
451
 *
 
452
 * parameters:
 
453
 *   rec:  --> pointer to location receiving data.
 
454
 *   size: <-- size of each item of data in bytes.
 
455
 *   ni:   <-- number of items to read.
 
456
 *   f:    <-- ecs_file_t descriptor.
 
457
 *
 
458
 * returns:
 
459
 *   the number of items (not bytes) sucessfully read; for a Fortran
 
460
 *   record, if the whole record could not be read, returns 0.
 
461
 */
 
462
 
 
463
size_t
 
464
ecs_file_read(void              *rec,
 
465
              const size_t       size,
 
466
              const size_t       ni,
 
467
              const ecs_file_t  *f);
 
468
 
 
469
/*
 
470
 * Read a binary C or Fortran type record.
 
471
 *
 
472
 * This function is similar to ecs_file_read(), but failure to read
 
473
 * a record due to an end-of-file condition is not considered an error with
 
474
 * this variant, which may be used to read records whose presence in the
 
475
 * file is unknown.
 
476
 *
 
477
 * A Fortran record compatible with most compilers is structured
 
478
 * as follows:
 
479
 *   - a 4-byte integer indicating the number of bytes in the record.
 
480
 *   - the raw data
 
481
 *   - a 4-byte integer indicating the number of bytes in the record.
 
482
 *
 
483
 * A C record contains only the raw data.
 
484
 *
 
485
 * parameters:
 
486
 *   rec:  --> pointer to location receiving data.
 
487
 *   size: <-- size of each item of data in bytes.
 
488
 *   ni:   <-- number of items to read.
 
489
 *   f:    <-- ecs_file_t descriptor.
 
490
 *
 
491
 * returns:
 
492
 *   the number of items (not bytes) sucessfully read; for a Fortran
 
493
 *   record, if the whole record could not be read, returns 0.
 
494
 */
 
495
 
 
496
size_t
 
497
ecs_file_read_try(void              *rec,
 
498
                  const size_t       size,
 
499
                  const size_t       ni,
 
500
                  const ecs_file_t  *f);
 
501
 
 
502
/*
 
503
 * Write a binary C or Fortran type record.
 
504
 *
 
505
 * A Fortran record compatible with most compilers is structured
 
506
 * as follows:
 
507
 *   - a 4-byte integer indicating the number of bytes in the record.
 
508
 *   - the raw data
 
509
 *   - a 4-byte integer indicating the number of bytes in the record.
 
510
 *
 
511
 * A C record contains only the raw data.
 
512
 *
 
513
 * parameters:
 
514
 *   rec:  <-- pointer to location containing data.
 
515
 *   size: <-- size of each item of data in bytes.
 
516
 *   ni:   <-- number of items to write.
 
517
 *   f:    <-- ecs_file_t descriptor.
 
518
 *
 
519
 * returns:
 
520
 *   the number of items (not bytes) sucessfully written.
 
521
 */
 
522
 
 
523
size_t
 
524
ecs_file_write(const void        *rec,
 
525
               const size_t       size,
 
526
               const size_t       ni,
 
527
               const ecs_file_t  *f);
 
528
 
 
529
/*
 
530
 * Convert data from "little-endian" to "big-endian" or the reverse.
 
531
 *
 
532
 * The memory areas pointed to by src and dest should overlap either
 
533
 * exactly or not at all.
 
534
 *
 
535
 * parameters:
 
536
 *   dest: --> pointer to converted data location.
 
537
 *   src:  <-- pointer to source data location.
 
538
 *   size: <-- size of each item of data in bytes.
 
539
 *   ni:   <-- number of data items.
 
540
 */
 
541
 
 
542
void
 
543
ecs_file_swap_endian(void          *dest,
 
544
                     const void    *src,
 
545
                     const size_t   size,
 
546
                     const size_t   ni);
 
547
 
 
548
/*
 
549
 * Create a new directory using default permissions.
 
550
 *
 
551
 * This function is similar to the POSIX function mkdir(), except that
 
552
 * it has no "mode" argument: by default, on a POSIX type system,
 
553
 * permissions include read, write, and execute access for the user,
 
554
 * group and others, modified by the users umask value (so with a
 
555
 * typical configuration, the user will have read, write, and execute
 
556
 * pemission, the group and others will only have read and execute
 
557
 * permission, but this behavior may be modified).
 
558
 *
 
559
 * Also, contrary to the usual mkdir(), if the directory already
 
560
 * exists (and is truly a directory), this is considered a success
 
561
 * and not a failure, and 0 is returned: the aim of this function
 
562
 * is to make a directory available, so if it already exists,
 
563
 * this is considered acceptable.
 
564
 *
 
565
 * parameters:
 
566
 *   pathname: <-- name of new directory.
 
567
 *
 
568
 * returns:
 
569
 *   0 on success, -1 if an error occured (in which case errno
 
570
 *   contains the appropriate error code). If the underlying
 
571
 *   system has no mkdir() function or it was not detected
 
572
 *   upon ECS configuration, 1 is returned.
 
573
 */
 
574
 
 
575
int
 
576
ecs_file_mkdir_default(const char  *pathname);
 
577
 
 
578
/*
 
579
 * Check if a file exists and is a regular file.
 
580
 *
 
581
 * parameters:
 
582
 *   name: <-- file name.
 
583
 *
 
584
 * returns:
 
585
 *   1 if file exists and is a regular file, 0 otherwise.
 
586
 */
 
587
 
 
588
int
 
589
ecs_file_isreg(const char  *name);
 
590
 
 
591
/*
 
592
 * Check if a directory exists.
 
593
 *
 
594
 * parameters:
 
595
 *   name: <-- directory name.
 
596
 *
 
597
 * returns:
 
598
 *   1 if directory exists, 0 otherwise.
 
599
 */
 
600
 
 
601
int
 
602
ecs_file_isdir(const char  *name);
 
603
 
 
604
/*
 
605
 * Indicate Zlib version available at run time.
 
606
 *
 
607
 * It may be useful to compare the Zlib version used at compile
 
608
 * and run time in case we use dynamic libraries.
 
609
 *
 
610
 * returns:
 
611
 *   pointer to string indicating Zlib version in use, or NULL
 
612
 *   if Zlib support is not available.
 
613
 */
 
614
 
 
615
const char *
 
616
ecs_file_version_zlib(void);
 
617
 
 
618
/*
 
619
 * Indicate Zlib version available at compilation time.
 
620
 *
 
621
 * It may be useful to compare the Zlib version used at compile
 
622
 * and link time in case we use dynamic libraries.
 
623
 *
 
624
 * returns:
 
625
 *   pointer to string indicating Zlib version at compilation, or NULL
 
626
 *   if Zlib support is not available.
 
627
 */
 
628
 
 
629
const char *
 
630
ecs_file_version_build_zlib(void);
 
631
 
 
632
/*----------------------------------------------------------------------------*/
 
633
 
 
634
#ifdef __cplusplus
 
635
}
 
636
#endif /* __cplusplus */
 
637
 
 
638
#endif /* __ECS_FILE_H__ */