ESA JPIP server  0.1
file.h
Go to the documentation of this file.
1 #ifndef _DATA_FILE_H_
2 #define _DATA_FILE_H_
3 
4 
5 #include <stdio.h>
6 #include <assert.h>
7 #include <stdint.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <string>
12 
13 #ifndef _NO_FAST_FILE
14  #include <stdio_ext.h>
15 #endif
16 
17 #include "tr1_compat.h"
18 
19 
20 namespace data
21 {
22 
23  using namespace std;
24 
25 
32  struct LockedAccess
33  {
34  static inline void configure(FILE *file_ptr)
35  {
36  }
37 
38  static inline size_t fwrite(const void *ptr, size_t size, size_t count, FILE *file_ptr)
39  {
40  return ::fwrite(ptr, size, count, file_ptr);
41  }
42 
43  static inline size_t fread(void * ptr, size_t size, size_t count, FILE *file_ptr)
44  {
45  return ::fread(ptr, size, count, file_ptr);
46  }
47 
48  static inline int fgetc(FILE *file_ptr)
49  {
50  return ::fgetc(file_ptr);
51  }
52 
53  static inline int fputc(int c, FILE *file_ptr)
54  {
55  return ::fputc(c, file_ptr);
56  }
57  };
58 
59 
66  #ifndef _NO_FAST_FILE
68  {
69  static inline void configure(FILE *file_ptr)
70  {
71  __fsetlocking(file_ptr, FSETLOCKING_BYCALLER);
72  }
73 
74  static inline size_t fwrite(const void *ptr, size_t size, size_t count, FILE *file_ptr)
75  {
76  return ::fwrite_unlocked(ptr, size, count, file_ptr);
77  }
78 
79  static inline size_t fread(void * ptr, size_t size, size_t count, FILE *file_ptr)
80  {
81  return ::fread_unlocked(ptr, size, count, file_ptr);
82  }
83 
84  static inline int fgetc(FILE *file_ptr)
85  {
86  return ::fgetc_unlocked(file_ptr);
87  }
88 
89  static inline int fputc(int c, FILE *file_ptr)
90  {
91  return ::fputc_unlocked(c, file_ptr);
92  }
93  };
94  #endif
95 
96 
108  template<class IO> class BaseFile
109  {
110  public:
114  typedef SHARED_PTR< BaseFile<IO> > Ptr;
115 
116 
121  {
122  file_ptr = NULL;
123  }
124 
129  static bool Exists(const char *file_name)
130  {
131  struct stat file_stat;
132  return (stat(file_name, &file_stat) == 0);
133  }
134 
142  bool Open(const char *file_name, const char *access)
143  {
144  assert(file_ptr == NULL);
145 
146  if((file_ptr = fopen(file_name, access)) == NULL) return false;
147  else {
148  IO::configure(file_ptr);
149  return true;
150  }
151  }
152 
160  bool Open(const string& file_name, const char *access)
161  {
162  return Open(file_name.c_str(), access);
163  }
164 
174  template<class IO2> bool Open(const BaseFile<IO2>& file, const char *access)
175  {
176  assert((file_ptr == NULL) && file.IsValid());
177 
178  int new_fd = -1;
179 
180  if((new_fd = dup(file.GetDescriptor())) < 0) return false;
181  else {
182  if((file_ptr = fdopen(new_fd, access)) == NULL) {
183  close(new_fd);
184  return false;
185 
186  } else {
187  IO::configure(file_ptr);
188  return true;
189  }
190  }
191  }
192 
193  /*
194  * Calls the function <code>Open</code> with the <code>"rb"
195  * </code> access mode (reading).
196  */
197  bool OpenForReading(const char *file_name)
198  {
199  return Open(file_name, "rb");
200  }
201 
202  /*
203  * Calls the function <code>Open</code> with the <code>"rb"
204  * </code> access mode (reading).
205  */
206  bool OpenForReading(const string& file_name)
207  {
208  return Open(file_name.c_str(), "rb");
209  }
210 
211  /*
212  * Calls the function <code>Open</code> with the <code>"rb"
213  * </code> access mode (reading).
214  */
215  template<class IO2> bool OpenForReading(const BaseFile<IO2>& file)
216  {
217  return Open(file, "rb");
218  }
219 
220  /*
221  * Calls the function <code>Open</code> with the <code>"wb"
222  * </code> access mode (writing).
223  */
224  bool OpenForWriting(const char *file_name)
225  {
226  return Open(file_name, "wb");
227  }
228 
229  /*
230  * Calls the function <code>Open</code> with the <code>"wb"
231  * </code> access mode (writing).
232  */
233  bool OpenForWriting(const string& file_name)
234  {
235  return Open(file_name.c_str(), "wb");
236  }
237 
238  /*
239  * Calls the function <code>Open</code> with the <code>"wb"
240  * </code> access mode (writing).
241  */
242  template<class IO2> bool OpenForWriting(const BaseFile<IO2>& file)
243  {
244  return Open(file, "wb");
245  }
246 
254  bool Seek(int offset, int origin = SEEK_SET) const
255  {
256  assert(file_ptr != NULL);
257 
258  return !fseek(file_ptr, offset, origin);
259  }
260 
264  void Close()
265  {
266  if(file_ptr != NULL) {
267  fclose(file_ptr);
268  file_ptr = NULL;
269  }
270  }
271 
275  uint64_t GetOffset() const
276  {
277  assert(file_ptr != NULL);
278 
279  return ftell(file_ptr);
280  }
281 
285  int IsEOF() const
286  {
287  assert(file_ptr != NULL);
288 
289  return feof(file_ptr);
290  }
291 
295  int GetDescriptor() const
296  {
297  assert(file_ptr != NULL);
298 
299  return fileno(file_ptr);
300  }
301 
306  uint64_t GetSize() const
307  {
308  assert(file_ptr != NULL);
309 
310  uint64_t offset = GetOffset();
311  Seek(0, SEEK_END);
312  uint64_t final_offset = GetOffset();
313  Seek(offset, SEEK_SET);
314 
315  return final_offset;
316  }
317 
321  int ReadByte() const
322  {
323  assert(file_ptr != NULL);
324 
325  return IO::fgetc(file_ptr);
326  }
327 
335  template<typename T> bool Read(T *value, int num_bytes = sizeof(T)) const
336  {
337  assert(file_ptr != NULL);
338 
339  return (IO::fread((void *) value, num_bytes, 1, file_ptr) == 1);
340  }
341 
349  template<typename T> bool ReadReverse(T *value, int num_bytes = sizeof(T)) const
350  {
351  assert(file_ptr != NULL);
352 
353  for (char *ptr = ((char *) value) + (num_bytes - 1); num_bytes-- > 0; ptr--)
354  if (IO::fread((void *) ptr, 1, 1, file_ptr) != 1) return false;
355 
356  return true;
357  }
358 
362  int WriteByte(int c) const
363  {
364  assert(file_ptr != NULL);
365 
366  return IO::fputc(c, file_ptr);
367  }
368 
376  template<typename T> bool Write(T *value, int num_bytes = sizeof(T)) const
377  {
378  assert(file_ptr != NULL);
379 
380  return (IO::fwrite((void *) value, num_bytes, 1, file_ptr) == 1);
381  }
382 
390  template<typename T> bool WriteReverse(T *value, int num_bytes = sizeof(T)) const
391  {
392  assert(file_ptr != NULL);
393 
394  for (char *ptr = ((char *) value) + (num_bytes - 1); num_bytes-- > 0; ptr--)
395  if (IO::fwrite((void *) ptr, 1, 1, file_ptr) != 1) return false;
396 
397  return true;
398  }
399 
404  bool IsValid() const
405  {
406  return (file_ptr != NULL);
407  }
408 
413  operator bool() const
414  {
415  return (file_ptr != NULL);
416  }
417 
421  virtual ~BaseFile()
422  {
423  Close();
424  }
425 
426  private:
430  FILE *file_ptr;
431  };
432 
433 
442 
443 
451  #ifndef _NO_FAST_FILE
453  #endif
454 
455 }
456 
457 #endif /* _DATA_FILE_H_ */
virtual ~BaseFile()
The destructor closes the file.
Definition: file.h:421
bool OpenForWriting(const char *file_name)
Definition: file.h:224
uint64_t GetSize() const
Return the current size of the file, without modifying the file position.
Definition: file.h:306
bool Write(T *value, int num_bytes=sizeof(T)) const
Writes a value to the file.
Definition: file.h:376
FILE * file_ptr
File pointer.
Definition: file.h:430
BaseFile< UnlockedAccess > FastFile
Specialization of the class BaseFile with unlocked access.
Definition: file.h:452
static void configure(FILE *file_ptr)
Definition: file.h:34
static int fputc(int c, FILE *file_ptr)
Definition: file.h:89
uint64_t GetOffset() const
Returns the current file position.
Definition: file.h:275
bool Seek(int offset, int origin=SEEK_SET) const
Changes the current position of the file.
Definition: file.h:254
Contains a set of classes to easy the handling of data and files, as well as the serialization.
Definition: data.h:9
bool OpenForWriting(const BaseFile< IO2 > &file)
Definition: file.h:242
int GetDescriptor() const
Returns the file descriptor.
Definition: file.h:295
bool IsValid() const
Returns true if the file pointer is not NULL.
Definition: file.h:404
static size_t fread(void *ptr, size_t size, size_t count, FILE *file_ptr)
Definition: file.h:79
static bool Exists(const char *file_name)
Returns true if the given file exists.
Definition: file.h:129
STL namespace.
bool Open(const string &file_name, const char *access)
Opens a file with a specific access mode.
Definition: file.h:160
bool OpenForReading(const char *file_name)
Definition: file.h:197
Struct for wrapping the basic FILE locked functions for reading and writing defined in stdio...
Definition: file.h:32
static int fgetc(FILE *file_ptr)
Definition: file.h:84
Struct for wrapping the basic FILE unlocked functions for reading and writing defined in stdio_exts...
Definition: file.h:67
BaseFile()
Initialized the internal file pointer to NULL.
Definition: file.h:120
bool Open(const BaseFile< IO2 > &file, const char *access)
Opens a file with a specific access mode given an already opened File object.
Definition: file.h:174
bool Read(T *value, int num_bytes=sizeof(T)) const
Reads a value from the file.
Definition: file.h:335
int WriteByte(int c) const
Writes a byte to the file.
Definition: file.h:362
void Close()
Closes the file.
Definition: file.h:264
int IsEOF() const
Returns the EOF status (feof) of the file.
Definition: file.h:285
bool OpenForReading(const string &file_name)
Definition: file.h:206
int ReadByte() const
Reads a byte from the file.
Definition: file.h:321
This is a wrapper class for the FILE functions that provides all the functionality to handle files sa...
Definition: file.h:108
static int fputc(int c, FILE *file_ptr)
Definition: file.h:53
bool Open(const char *file_name, const char *access)
Opens a file with a specific access mode.
Definition: file.h:142
bool WriteReverse(T *value, int num_bytes=sizeof(T)) const
Writes a value to the file in reverse order.
Definition: file.h:390
BaseFile< LockedAccess > File
Specialization of the class BaseFile with locked access.
Definition: file.h:441
static int fgetc(FILE *file_ptr)
Definition: file.h:48
bool ReadReverse(T *value, int num_bytes=sizeof(T)) const
Reads a value from the file in reverse order.
Definition: file.h:349
static size_t fwrite(const void *ptr, size_t size, size_t count, FILE *file_ptr)
Definition: file.h:38
static size_t fread(void *ptr, size_t size, size_t count, FILE *file_ptr)
Definition: file.h:43
bool OpenForWriting(const string &file_name)
Definition: file.h:233
static size_t fwrite(const void *ptr, size_t size, size_t count, FILE *file_ptr)
Definition: file.h:74
static void configure(FILE *file_ptr)
Definition: file.h:69
SHARED_PTR< BaseFile< IO > > Ptr
Safe pointer to this class.
Definition: file.h:114
bool OpenForReading(const BaseFile< IO2 > &file)
Definition: file.h:215