9 # include <vcl_msvc_warnings.h> 12 static std::ios::openmode modeflags(
char const* mode)
15 return std::ios::openmode(0);
18 if (mode[1] ==
'+' || mode[1] ==
'w')
19 return std::ios::in | std::ios::out | modeflags(mode+2);
21 return std::ios::in | modeflags(mode+1);
26 return std::ios::in | std::ios::out | std::ios::trunc | modeflags(mode+2);
28 return std::ios::out | std::ios::trunc | modeflags(mode+1);
31 std::cerr << std::endl << __FILE__
": DODGY MODE " << mode << std::endl;
32 return std::ios::openmode(0);
35 #define xerr if (true) ; else (std::cerr << "std::fstream#" << id_ << ": ") 40 flags_(modeflags(mode)),
41 f_(fn, flags_ | std::ios::binary),
45 xerr <<
"vil_stream_fstream(\"" << fn <<
"\", \""<<mode<<
"\") = " <<
id_ <<
'\n';
48 std::cerr <<
"vil_stream_fstream::Could not open [" << fn <<
"]\n";
53 #if defined(_WIN32) && VXL_USE_WIN_WCHAR_T 55 flags_(modeflags(mode)),
56 f_(fn, flags_ | std::ios::binary),
61 #endif //defined(_WIN32) && VXL_USE_WIN_WCHAR_T 73 xerr <<
"vil_stream_fstream# " <<
id_ <<
" being deleted\n";
80 assert( n <= std::numeric_limits<std::streamoff>::max() );
82 if (!(
flags_ & std::ios::out)) {
83 std::cerr <<
"vil_stream_fstream: write failed, not a std::ostream\n";
88 xerr <<
"write " << n << std::endl;
89 f_.write((
char const*)buf, (std::streamoff)n);
91 std::cerr << (
"vil_stream_fstream: ERROR: write failed!\n");
102 assert( n <= std::numeric_limits<std::streamoff>::max() );
104 if (!(
flags_ & std::ios::in))
108 xerr <<
"read " << n << std::endl;
109 f_.read((
char *)buf, (std::streamoff)n);
119 if (!
f_.good() && !
f_.bad() &&
f_.eof())
125 if (b < a) {
xerr <<
"urgh!\n";
return numread; }
126 if (numread != n) {
xerr <<
"only read " << numread << std::endl; }
133 if (
flags_ & std::ios::in) {
138 if (
flags_ & std::ios::out) {
151 assert( position <= std::numeric_limits< std::streamoff >::max() );
153 bool fi = (
flags_ & std::ios::in) != 0;
154 bool fo = (
flags_ & std::ios::out) != 0;
157 xerr <<
"seekg and seekp to " << position << std::endl;
159 f_.seekg((std::streamoff)position);
160 f_.seekp((std::streamoff)position);
166 xerr <<
"seek to " << position << std::endl;
168 f_.seekg((std::streamoff)position);
174 xerr <<
"seekp to " << position << std::endl;
175 std::streamoff at =
f_.tellp();
176 if (position != at) {
177 xerr <<
"seekp to " << position <<
", at " << (long)
f_.tellp() << std::endl;
178 f_.seekp((std::streamoff)position);
190 std::streampos curr =
f_.tellg();
191 f_.seekg( 0, std::ios::end );
std::ios::openmode flags_
vil_streampos file_size() const override
Amount of data in the stream.
vil_streampos write(void const *buf, vil_streampos n) override
Write n bytes from buf. Returns number of bytes written.
vil_streampos tell() const override
Return file pointer.
vil_streampos read(void *buf, vil_streampos n) override
Read n bytes into buf. Returns number of bytes read.
A vil_stream implementation using std::fstream.
~vil_stream_fstream() override
void seek(vil_streampos position) override
Goto file pointer.
vil_stream_fstream(char const *filename, char const *mode)