14 #include <stdio_ext.h> 38 static inline size_t fwrite(
const void *ptr,
size_t size,
size_t count, FILE *file_ptr)
40 return ::fwrite(ptr, size, count, file_ptr);
43 static inline size_t fread(
void * ptr,
size_t size,
size_t count, FILE *file_ptr)
45 return ::fread(ptr, size, count, file_ptr);
48 static inline int fgetc(FILE *file_ptr)
50 return ::fgetc(file_ptr);
53 static inline int fputc(
int c, FILE *file_ptr)
55 return ::fputc(c, file_ptr);
71 __fsetlocking(file_ptr, FSETLOCKING_BYCALLER);
74 static inline size_t fwrite(
const void *ptr,
size_t size,
size_t count, FILE *file_ptr)
76 return ::fwrite_unlocked(ptr, size, count, file_ptr);
79 static inline size_t fread(
void * ptr,
size_t size,
size_t count, FILE *file_ptr)
81 return ::fread_unlocked(ptr, size, count, file_ptr);
84 static inline int fgetc(FILE *file_ptr)
86 return ::fgetc_unlocked(file_ptr);
89 static inline int fputc(
int c, FILE *file_ptr)
91 return ::fputc_unlocked(c, file_ptr);
114 typedef SHARED_PTR< BaseFile<IO> >
Ptr;
129 static bool Exists(
const char *file_name)
131 struct stat file_stat;
132 return (stat(file_name, &file_stat) == 0);
142 bool Open(
const char *file_name,
const char *access)
144 assert(file_ptr == NULL);
146 if((file_ptr = fopen(file_name, access)) == NULL)
return false;
148 IO::configure(file_ptr);
160 bool Open(
const string& file_name,
const char *access)
162 return Open(file_name.c_str(), access);
176 assert((file_ptr == NULL) && file.
IsValid());
182 if((file_ptr = fdopen(new_fd, access)) == NULL) {
187 IO::configure(file_ptr);
199 return Open(file_name,
"rb");
208 return Open(file_name.c_str(),
"rb");
217 return Open(file,
"rb");
226 return Open(file_name,
"wb");
235 return Open(file_name.c_str(),
"wb");
244 return Open(file,
"wb");
254 bool Seek(
int offset,
int origin = SEEK_SET)
const 256 assert(file_ptr != NULL);
258 return !fseek(file_ptr, offset, origin);
266 if(file_ptr != NULL) {
277 assert(file_ptr != NULL);
279 return ftell(file_ptr);
287 assert(file_ptr != NULL);
289 return feof(file_ptr);
297 assert(file_ptr != NULL);
299 return fileno(file_ptr);
308 assert(file_ptr != NULL);
310 uint64_t offset = GetOffset();
312 uint64_t final_offset = GetOffset();
313 Seek(offset, SEEK_SET);
323 assert(file_ptr != NULL);
325 return IO::fgetc(file_ptr);
335 template<
typename T>
bool Read(T *value,
int num_bytes =
sizeof(T))
const 337 assert(file_ptr != NULL);
339 return (IO::fread((
void *) value, num_bytes, 1, file_ptr) == 1);
349 template<
typename T>
bool ReadReverse(T *value,
int num_bytes =
sizeof(T))
const 351 assert(file_ptr != NULL);
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;
364 assert(file_ptr != NULL);
366 return IO::fputc(c, file_ptr);
376 template<
typename T>
bool Write(T *value,
int num_bytes =
sizeof(T))
const 378 assert(file_ptr != NULL);
380 return (IO::fwrite((
void *) value, num_bytes, 1, file_ptr) == 1);
390 template<
typename T>
bool WriteReverse(T *value,
int num_bytes =
sizeof(T))
const 392 assert(file_ptr != NULL);
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;
406 return (file_ptr != NULL);
413 operator bool()
const 415 return (file_ptr != NULL);
451 #ifndef _NO_FAST_FILE 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
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