2 // (C) 2002-2004 Royce Mitchell III
3 // Dually licensed under BSD & LGPL
9 # include <stdio.h> // fgetc
12 # include <sys/stat.h>
22 typedef __int64 fileoff_t
;
23 typedef unsigned __int64 filesize_t
;
25 typedef __off64_t fileoff_t
;
26 typedef __size64_t filesize_t
;
33 File ( const std::string
& filename
, const char* mode
) : _f(0)
35 open ( filename
, mode
);
38 File ( const std::wstring
& filename
, const wchar_t* mode
) : _f(0)
40 open ( filename
, mode
);
43 File ( const char* filename
, const char* mode
) : _f(0)
45 open ( filename
, mode
);
48 File ( const wchar_t* filename
, const wchar_t* mode
) : _f(0)
50 open ( filename
, mode
);
58 bool open ( const std::string
& filename
, const char* mode
)
61 return ( _f
= fopen ( filename
.c_str(), mode
) ) != 0;
64 bool open ( const std::wstring
& filename
, const wchar_t* mode
)
67 return ( _f
= _wfopen ( filename
.c_str(), mode
) ) != 0;
70 bool open ( const char* filename
, const char* mode
)
73 return ( _f
= fopen ( filename
, mode
) ) != 0;
76 bool open ( const wchar_t* filename
, const wchar_t* mode
)
79 return ( _f
= _wfopen ( filename
, mode
) ) != 0;
82 fileoff_t
seek ( fileoff_t offset
);
91 return fputc ( c
, _f
) != EOF
;
94 std::string
getline ( bool strip_crlf
= false );
95 std::wstring
wgetline ( bool strip_crlf
= false );
97 // this function searches for the next end-of-line and puts all data it
98 // finds until then in the 'line' parameter.
100 // call continuously until the function returns false ( no more data )
101 bool next_line ( std::string
& line
, bool strip_crlf
);
102 bool next_line ( std::wstring
& line
, bool strip_crlf
);
104 bool read ( void* data
, unsigned len
)
106 return len
== fread ( data
, 1, len
, _f
);
109 bool write ( const void* data
, unsigned len
)
111 return len
== fwrite ( data
, 1, len
, _f
);
114 bool write ( const std::string
& data
)
116 return data
.length() == fwrite ( data
.c_str(), 1, data
.length(), _f
);
119 bool write ( const std::wstring
& data
)
121 return data
.length() == fwrite ( data
.c_str(), sizeof(data
[0]), data
.length(), _f
);
124 filesize_t
length() const;
128 bool isopened() const
140 return feof(_f
) ? true : false;
153 void printf ( const char* fmt
, ... );
154 void printf ( const wchar_t* fmt
, ... );
156 static bool LoadIntoString ( std::string
& s
, const std::string
& filename
);
157 static bool LoadIntoString ( std::string
& s
, const std::wstring
& filename
);
158 static bool SaveFromString ( const std::string
& filename
, const std::string
& s
, bool binary
);
159 static bool SaveFromString ( const std::wstring
& filename
, const std::string
& s
, bool binary
);
160 static bool SaveFromBuffer ( const std::string
& filename
, const char* buf
, size_t buflen
, bool binary
);
161 static bool SaveFromBuffer ( const std::wstring
& filename
, const char* buf
, size_t buflen
, bool binary
);
163 static std::string
TempFileName ( const std::string
& prefix
);
164 static std::wstring
TempFileName ( const std::wstring
& prefix
);
168 const File
& operator = ( const File
& ) { return *this; }