2 #ifdef VIL_USE_FSTREAM64 // only compile this file when needed 8 # include <vcl_msvc_warnings.h> 22 #define _O_RDWR O_RDWR 23 #define _O_RDONLY O_RDONLY 24 #define _O_WRONLY O_WRONLY 26 #define _O_CREAT O_CREAT 27 #define _S_IREAD S_IREAD 28 #define _S_IWRITE S_IWRITE 30 #define _close ::close 31 #define _telli64(fd) ::lseek(fd, 0, SEEK_CUR) 32 #define _lseeki64 ::lseek 34 #define _commit ::fsync 36 #define _write ::write 39 #define xerr if (true) ; else (std::cerr << "std::fstream#" << fd_ << ": ") 41 static int modeflags(
char const* mode)
46 for (
unsigned int i = 0; mode[i] != 0; ++i ) {
49 else if ( mode[i] ==
'w' )
53 if ( read && write )
return _O_RDWR;
54 else if ( read )
return _O_RDONLY;
55 else if ( write )
return _O_WRONLY;
57 std::cerr <<
'\n' << __FILE__
": DODGY MODE " << mode <<
'\n';
62 vil_stream_fstream64::vil_stream_fstream64(
char const* fn,
char const* mode) :
63 mode_( modeflags(mode) )
65 if ( mode_ == O_RDONLY ) {
66 fd_ = _open( fn, mode_ | _O_BINARY );
68 fd_ = _open( fn, mode_ | _O_CREAT | _O_BINARY, _S_IREAD | _S_IWRITE );
71 std::cerr <<
"vil_stream_fstream64::Could not open [" << fn <<
"]\n";
75 #if defined(_WIN32) && VXL_USE_WIN_WCHAR_T 76 vil_stream_fstream64::vil_stream_fstream64(
wchar_t const* fn,
char const* mode):
77 mode_(modeflags(mode))
79 if ( mode_ == O_RDONLY )
80 fd_ = _wopen( fn, mode_ | _O_BINARY );
82 fd_ = _wopen( fn, mode_ | _O_CREAT | _O_BINARY, _S_IREAD | _S_IWRITE );
85 std::cerr <<
"vil_stream_fstream64::Could not open [" << fn <<
"]\n";
88 #endif //defined(_WIN32) && VXL_USE_WIN_WCHAR_T 90 vil_stream_fstream64::~vil_stream_fstream64()
103 if ( !( ( mode_ == _O_WRONLY ) ||
104 ( mode_ == _O_RDWR ) ) )
106 std::cerr <<
"vil_stream_fstream64: write failed, stream not open for write\n";
111 int ret_val = _write( fd_, buf, (
unsigned int)n );
112 if ( ret_val == -1 ){
113 std::cerr << (
"vil_stream_fstream64: ERROR: write failed!\n");
135 if ( !( ( mode_ == _O_RDONLY ) ||
136 ( mode_ == _O_RDWR ) ) )
138 xerr <<
"vil_stream_fstream64: read failed, stream not open for read\n";
142 int ret_val = _read( fd_, buf, (
unsigned int)n );
144 xerr <<
"read failed!\n";
145 else if ( ret_val < n )
146 xerr <<
"only read " << ret_val << std::endl;
154 return _telli64( fd_ );
160 long long ret_val = _lseeki64( fd_, position, SEEK_SET );
161 if ( ret_val == -1L ){
162 xerr <<
"error during seek.";
169 _lseeki64( fd_, 0, SEEK_END );
171 _lseeki64( fd_, curr, SEEK_SET );
175 #endif // VIL_USE_FSTREAM64