forked from derat/xsettingsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_writer.cc
67 lines (51 loc) · 1.56 KB
/
data_writer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2009 Daniel Erat <[email protected]>
// All rights reserved.
#include "data_writer.h"
#include <algorithm>
#include <cstring>
using std::min;
namespace xsettingsd {
DataWriter::DataWriter(char* buffer, size_t buf_len)
: buffer_(buffer),
buf_len_(buf_len),
bytes_written_(0) {
}
bool DataWriter::WriteBytes(const char* data, size_t bytes_to_write) {
if (bytes_to_write > buf_len_ - bytes_written_)
return false;
memcpy(buffer_ + bytes_written_,
data,
min(bytes_to_write, buf_len_ - bytes_written_));
bytes_written_ += bytes_to_write;
return true;
}
bool DataWriter::WriteInt8(int8_t num) {
if (sizeof(int8_t) > buf_len_ - bytes_written_)
return false;
*(reinterpret_cast<int8_t*>(buffer_ + bytes_written_)) = num;
bytes_written_ += sizeof(int8_t);
return true;
}
bool DataWriter::WriteInt16(int16_t num) {
if (sizeof(int16_t) > buf_len_ - bytes_written_)
return false;
*(reinterpret_cast<int16_t*>(buffer_ + bytes_written_)) = num;
bytes_written_ += sizeof(int16_t);
return true;
}
bool DataWriter::WriteInt32(int32_t num) {
if (sizeof(int32_t) > buf_len_ - bytes_written_)
return false;
*(reinterpret_cast<int32_t*>(buffer_ + bytes_written_)) = num;
bytes_written_ += sizeof(int32_t);
return true;
}
bool DataWriter::WriteZeros(size_t bytes_to_write) {
if (bytes_to_write > buf_len_ - bytes_written_)
return false;
bzero(buffer_ + bytes_written_,
min(bytes_to_write, buf_len_ - bytes_written_));
bytes_written_ += bytes_to_write;
return true;
}
} // namespace xsettingsd