add File::write()

This commit is contained in:
Josh Holtrop 2016-07-24 12:37:34 -04:00
parent 0aad4fd779
commit 1730838921
2 changed files with 25 additions and 0 deletions

View File

@ -89,3 +89,27 @@ bool File::read(uint8_t * buf, size_t size)
return true; return true;
} }
bool File::write(const uint8_t * buf, size_t size)
{
if (size <= 0)
return true;
size_t n_bytes_written = 0u;
for (;;)
{
off_t write_size = ::write(m_fd, &buf[n_bytes_written], size - n_bytes_written);
if (write_size <= 0)
break;
n_bytes_written += (size_t)write_size;
if (n_bytes_written >= size)
break;
}
if (n_bytes_written != size)
{
return false;
}
return true;
}

View File

@ -13,6 +13,7 @@ public:
void close(); void close();
size_t get_size(); size_t get_size();
bool read(uint8_t * buf, size_t size); bool read(uint8_t * buf, size_t size);
bool write(const uint8_t * buf, size_t size);
protected: protected:
int m_fd; int m_fd;