-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuffer.cpp
More file actions
63 lines (59 loc) · 976 Bytes
/
Buffer.cpp
File metadata and controls
63 lines (59 loc) · 976 Bytes
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
//
// Created by rjd67 on 2021/3/2.
//
#include <cstring>
#include <unistd.h>
#include "Buffer.h"
constexpr int BUFFER_SIZE = 8192;
Buffer::Buffer():
read_idx_(0),
write_idx_(0),
buffer_(BUFFER_SIZE)
{
}
int Buffer::Readable() const
{
return write_idx_ - read_idx_;
}
int Buffer::Writeable() const
{
return BUFFER_SIZE - write_idx_;
}
char* Buffer::ReadBegin()
{
return &buffer_[read_idx_];
}
char* Buffer::WriteBegin()
{
return &buffer_[write_idx_];
}
void Buffer::AddReadIdx(int idx)
{
read_idx_ += idx;
}
void Buffer::AddWriteIdx(int idx)
{
write_idx_ += idx;
}
void Buffer::RemoveReadData()
{
int readable = Readable();
memcpy(&buffer_[0], ReadBegin(),
readable);
read_idx_ = 0;
write_idx_ = readable;
}
char* Buffer::Find(char c)
{
return static_cast<char*>(memchr(ReadBegin(), c,
Readable()));
}
ssize_t Buffer::ReadDataFromFd(int fd)
{
ssize_t ret = read(fd, WriteBegin(), Writeable());
if (ret > 0)
{
AddWriteIdx(ret);
}
return ret;
}