forked from chunyi1994/cppevent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.cpp
84 lines (72 loc) · 1.46 KB
/
buffer.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "buffer.h"
namespace cppevent{
Buffer::Buffer():data_(1024), writeIndex_(0), readIndex_(0){
data_[0] = '\0';
}
size_t Buffer::size() const
{
return writeIndex_ - readIndex_;
}
void Buffer::append(const char* msg, size_t len)
{
if(writeableSize() < len){
data_.resize(2 * std::max(len,data_.size()));
}
::memcpy(writePtr(), msg, len);
writeIndex_ += len;
data_[writeIndex_] = '\0';
}
void Buffer::append(const std::string& msg)
{
append(msg.c_str(), msg.length());
}
void Buffer::clear()
{
writeIndex_ = 0;
readIndex_ = 0;
data_[0] = '\0';
}
std::string Buffer::read(size_t len)
{
len = std::min(size(), len);
std::string ret(readPtr(), len);
readIndex_ += len;
if(size() == 0){
clear();
}
return ret;
}
bool Buffer::readLine(std::string& line)
{
return readLine(line, '\n');
}
bool Buffer:: readLine(std::string &str, char br)
{
if(size() == 0){
return false;
}
size_t i = readIndex_;
while(i < writeIndex_){
if(data_[i] == br){
break;
}
i++;
}
str = read(i + 1 - readIndex_);
return true;
}
const char* Buffer::readPtr() const
{
auto iter = readIndex_ + data_.begin();
return static_cast<const char *>(&*iter);
}
char* Buffer::writePtr()
{
auto iter = writeIndex_ + data_.begin();
return static_cast<char *>(&*iter);
}
size_t Buffer::writeableSize() const
{
return data_.size() - writeIndex_;
}
}