2 * Copyright 2009-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
13 BFileIO::BFileIO(FILE* file
, bool takeOverOwnership
)
16 fOwnsFile(takeOverOwnership
)
23 if (fOwnsFile
&& fFile
!= NULL
)
29 BFileIO::Read(void* buffer
, size_t size
)
32 ssize_t bytesRead
= fread(buffer
, 1, size
, fFile
);
33 return bytesRead
>= 0 ? bytesRead
: errno
;
38 BFileIO::Write(const void* buffer
, size_t size
)
41 ssize_t bytesRead
= fwrite(buffer
, 1, size
, fFile
);
42 return bytesRead
>= 0 ? bytesRead
: errno
;
47 BFileIO::ReadAt(off_t position
, void* buffer
, size_t size
)
49 // save the old position and seek to the requested one
50 off_t oldPosition
= _Seek(position
, SEEK_SET
);
55 ssize_t result
= BFileIO::Read(buffer
, size
);
58 fseeko(fFile
, oldPosition
, SEEK_SET
);
65 BFileIO::WriteAt(off_t position
, const void* buffer
, size_t size
)
67 // save the old position and seek to the requested one
68 off_t oldPosition
= _Seek(position
, SEEK_SET
);
73 ssize_t result
= BFileIO::Write(buffer
, size
);
76 fseeko(fFile
, oldPosition
, SEEK_SET
);
83 BFileIO::Seek(off_t position
, uint32 seekMode
)
85 if (fseeko(fFile
, position
, seekMode
) < 0)
88 return BFileIO::Position();
93 BFileIO::Position() const
95 off_t result
= ftello(fFile
);
96 return result
>= 0 ? result
: errno
;
101 BFileIO::SetSize(off_t size
)
103 return B_UNSUPPORTED
;
108 BFileIO::GetSize(off_t
* _size
) const
110 // save the current position and seek to the end
111 off_t position
= _Seek(0, SEEK_END
);
115 // get the size (position at end) and seek back
116 off_t size
= _Seek(position
, SEEK_SET
);
126 BFileIO::_Seek(off_t position
, uint32 seekMode
) const
128 // save the current position
129 off_t oldPosition
= ftello(fFile
);
133 // seek to the requested position
134 if (fseeko(fFile
, position
, seekMode
) < 0)