Skip to content

Commit

Permalink
COMMON: Add WriteFile::size()
Browse files Browse the repository at this point in the history
Returns the number of bytes written to the current file in total.
  • Loading branch information
DrMcCoy committed Nov 28, 2016
1 parent 63c2c11 commit 74bd31e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/common/writefile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Common {
WriteFile::WriteFile() : _handle(0) {
}

WriteFile::WriteFile(const UString &fileName) : _handle(0) {
WriteFile::WriteFile(const UString &fileName) : _handle(0), _size(0) {
if (!open(fileName))
throw Exception("Can't open file \"%s\" for writing", fileName.c_str());
}
Expand Down Expand Up @@ -73,6 +73,7 @@ void WriteFile::close() {
std::fclose(_handle);

_handle = 0;
_size = 0;
}

bool WriteFile::isOpen() const {
Expand All @@ -92,7 +93,15 @@ size_t WriteFile::write(const void *dataPtr, size_t dataSize) {
return 0;

assert(dataPtr);
return std::fwrite(dataPtr, 1, dataSize, _handle);

const size_t written = std::fwrite(dataPtr, 1, dataSize, _handle);
_size += written;

return written;
}

size_t WriteFile::size() const {
return _size;
}

} // End of namespace Common
5 changes: 5 additions & 0 deletions src/common/writefile.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ class WriteFile : boost::noncopyable, public WriteStream {

size_t write(const void *dataPtr, size_t dataSize);

/** Return the number of bytes written to the current file in total. */
size_t size() const;

protected:
std::FILE *_handle; ///< The actual file handle.

size_t _size;
};

} // End of namespace Common
Expand Down

0 comments on commit 74bd31e

Please sign in to comment.