Skip to content

Commit

Permalink
COMMON: Add an abstract base for seekable write streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Nostritius committed Jun 20, 2018
1 parent 10d2910 commit d5d4393
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/common/writestream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,30 @@ void WriteStream::writeString(const UString &str) {
throw Exception(kWriteError);
}

SeekableWriteStream::SeekableWriteStream() {

}

SeekableWriteStream::~SeekableWriteStream() {

}

size_t SeekableWriteStream::evalSeek(ptrdiff_t offset, SeekableWriteStream::Origin whence, size_t pos, size_t begin,
size_t size) {
switch (whence) {
case kOriginEnd:
offset = size + offset;
XOREOS_FALLTHROUGH;
case kOriginBegin:
return begin + offset;
case kOriginCurrent:
return pos + offset;

default:
break;
}

throw Exception("Invalid whence (%d)", (int) whence);
}

} // End of namespace Common
58 changes: 58 additions & 0 deletions src/common/writestream.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,64 @@ class WriteStream {
void writeString(const UString &str);
};

class SeekableWriteStream : public WriteStream {
public:
/** The position a seeking offset takes as a base. */
enum Origin {
kOriginBegin = 0, ///< Seek from the begin of the stream.
kOriginCurrent = 1, ///< Seek from the current position of the stream.
kOriginEnd = 2, ///< Seek from the end of the stream.
kOriginMAX ///< For range checks.
};

SeekableWriteStream();
~SeekableWriteStream();

/** Obtains the current value of the stream position indicator of the stream.
*
* @return the current position indicator, or kPositionInvalid if an error occurred.
*/
virtual size_t pos() const = 0;

/** Obtains the total size of the stream, measured in bytes.
* If this value is unknown or can not be computed, kSizeInvalid is returned.
*
* @return the size of the stream, or kSizeInvalid if an error occurred.
*/
virtual size_t size() const = 0;

/** Sets the stream position indicator for the stream. The new position,
* measured in bytes, is obtained by adding offset bytes to the position
* specified by whence. If whence is set to kOriginBegin, kOriginCurrent, or
* kOriginEnd, the offset is relative to the start of the file, the current
* position indicator, or end-of-file, respectively.
*
* On error, or when trying to seek outside the stream, a kSeekError
* exception is thrown.
*
* @param offset the relative offset in bytes.
* @param whence the seek reference: kOriginBegin, kOriginCurrent or kOriginEnd.
* @return the previous position of the stream, before seeking.
*/
virtual size_t seek(ptrdiff_t offset, Origin whence = kOriginBegin) = 0;

/** Skip the specified number of bytes, adding that offset to the current
* position in the stream.
*
* On error, or when trying to skip outside the stream, a kSeekError
* exception is thrown.
*
* @param offset the number of bytes to skip.
* @return the previous position of the stream, before skipping.
*/
virtual size_t skip(ptrdiff_t offset) {
return seek(offset, kOriginCurrent);
}

/** Evaluate the seek offset relative to whence into a position from the beginning. */
static size_t evalSeek(ptrdiff_t offset, Origin whence, size_t pos, size_t begin, size_t size);
};

} // End of namespace Common

#endif // COMMON_WRITESTREAM_H

0 comments on commit d5d4393

Please sign in to comment.