2 // (C) 2002-2004 Royce Mitchell III
3 // Dually licensed under BSD & LGPL
6 #pragma warning ( disable : 4786 )
14 #define nelem(x) ( sizeof(x) / sizeof(x[0]) )
17 typedef File::filesize_t filesize_t
;
18 typedef File::fileoff_t fileoff_t
;
21 fileoff_t
File::seek ( fileoff_t offset
)
24 if ( _f
->_flag
& _IOWRT
) // is there pending output?
27 // reset "buffered input" variables
31 // make sure we're going forward
32 if ( _f
->_flag
& _IORW
)
33 _f
->_flag
&= ~(_IOREAD
|_IOWRT
);
35 return _lseeki64 ( _fileno(_f
), offset
, SEEK_SET
);
37 return lseek64 ( fileno(_f
), offset
, SEEK_SET
);
41 std::string
File::getline ( bool strip_crlf
/*= false*/ )
48 fgets ( buf
, nelem(buf
)-1, _f
);
52 if ( strchr ( "\r\n", buf
[strlen(buf
)-1] ) )
55 if ( strip_crlf
&& s
.size() )
57 char* p
= strpbrk ( &s
[0], "\r\n" );
67 std::wstring
File::wgetline ( bool strip_crlf
/*= false*/ )
74 fgetws ( buf
, nelem(buf
)-1, _f
);
78 if ( wcschr ( L
"\r\n", buf
[wcslen(buf
)-1] ) )
81 if ( strip_crlf
&& s
.size() )
83 wchar_t* p
= wcspbrk ( &s
[0], L
"\r\n" );
87 s
.resize ( (p
-&s
[0])/sizeof(wchar_t) );
93 // this function searches for the next end-of-line and puts all data it
94 // finds until then in the 'line' parameter.
96 // call continuously until the function returns false ( no more data )
97 bool File::next_line ( std::string
& line
, bool strip_crlf
)
99 line
= getline(strip_crlf
);
100 // indicate that we're done *if*:
101 // 1) there's no more data, *and*
102 // 2) we're at the end of the file
103 return line
.size()>0 || !eof();
106 bool File::next_line ( std::wstring
& line
, bool strip_crlf
)
108 line
= wgetline ( strip_crlf
);
109 return line
.size()>0 || !eof();
115 bool mycallback ( const std::string& line, int line_number, long lparam )
117 std::cout << line << std::endl;
118 return true; // continue enumeration
121 File f ( "file.txt", "rb" ); // open file for binary read-only ( i.e. "rb" )
122 f.enum_lines ( mycallback, 0, true );
125 /*bool File::enum_lines ( bool (*callback)(const std::string& line, int line_number, long lparam), long lparam, bool strip_crlf )
130 std::string s = getline(strip_crlf);
139 if ( !(*callback) ( s, line_number, lparam ) )
144 filesize_t
File::length() const
147 return _filelengthi64 ( _fileno(_f
) );
149 struct stat64 file_stat
;
150 verify(fstat64(fileno(_f
), &file_stat
) == 0);
151 return file_stat
.st_size
;
164 void File::printf ( const char* fmt
, ... )
171 done
= vfprintf ( _f
, fmt
, arg
);
175 /*static*/ bool File::LoadIntoString ( std::string
& s
, const std::string
& filename
)
177 File
in ( filename
, "rb" );
178 if ( !in
.isopened() )
180 filesize_t flen
= in
.length();
181 size_t len
= size_t(flen
);
183 return false; // file too big...
184 s
.resize ( len
+ 1 );
185 if ( !in
.read ( &s
[0], len
) )
192 /*static*/ bool File::LoadIntoString ( std::string
& s
, const std::wstring
& filename
)
194 File
in ( filename
, L
"rb" );
195 if ( !in
.isopened() )
197 filesize_t flen
= in
.length();
198 size_t len
= size_t(flen
);
200 return false; // file too big...
201 s
.resize ( len
+ 1 );
202 if ( !in
.read ( &s
[0], len
) )
209 /*static*/ bool File::SaveFromString ( const std::string
& filename
, const std::string
& s
, bool binary
)
211 File
out ( filename
, binary
? "wb" : "w" );
212 if ( !out
.isopened() )
214 out
.write ( s
.c_str(), s
.size() );
218 /*static*/ bool File::SaveFromString ( const std::wstring
& filename
, const std::string
& s
, bool binary
)
220 File
out ( filename
, binary
? L
"wb" : L
"w" );
221 if ( !out
.isopened() )
223 out
.write ( s
.c_str(), s
.size() );
227 /*static*/ bool File::SaveFromBuffer ( const std::string
& filename
, const char* buf
, size_t buflen
, bool binary
)
229 File
out ( filename
, binary
? "wb" : "w" );
230 if ( !out
.isopened() )
232 out
.write ( buf
, buflen
);
236 /*static*/ bool File::SaveFromBuffer ( const std::wstring
& filename
, const char* buf
, size_t buflen
, bool binary
)
238 File
out ( filename
, binary
? L
"wb" : L
"w" );
239 if ( !out
.isopened() )
241 out
.write ( buf
, buflen
);
245 /*static*/ std::string
File::TempFileName ( const std::string
& prefix
)
248 std::string
s ( _tempnam ( ".", prefix
.c_str() ) );
256 /*static*/ std::wstring
File::TempFileName ( const std::wstring
& prefix
)
259 std::wstring
s ( _wtempnam ( L
".", prefix
.c_str() ) );
263 return std::wstring(L
"");