-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsimplebuffer.h
64 lines (50 loc) · 1.52 KB
/
simplebuffer.h
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
#pragma once
#ifndef SIMPLESTRING_H
#define SIMPLESTRING_H
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
class SimpleBuffer
{
public:
SimpleBuffer();
~SimpleBuffer();
SimpleBuffer(const SimpleBuffer &) = delete;
SimpleBuffer & operator=(const SimpleBuffer &) = delete;
/* return 0, close(peerFd) or shoutdown(peerFd)
return -1, error on fd
return 1, read sucess */
int ReadFromFd(int fd);
/* return 0, have writen all data in the buffer to fd, shoule remove EPOLLOUT event
return -1, error on fd
return 1, shoule continue to write */
int WriteToFd(int fd);
const char * Buffer();
// the index of useful data is [m_readOffset, m_writeOffset)
size_t BufferSize();
void ReadFromBuffer(size_t nRead);
void WriteToBuffer(const char * data, size_t writeSize);
// note that the format string will not append into the buffer if the length is larger than 1024
void Append(const char * format, ...);
// return -1, not found
// else return the index of the char c
ssize_t Find(const char c);
// return -1, not found
// else return the index of the first char of src
// FIX ME, change to KMP algorithm
ssize_t Find(const char * src);
// note that strlen(src) must > 0
bool EndWith(const char * src);
bool EndWithBackEndLength(const char * src, size_t length);
void DropEnd(size_t n);
private:
static const size_t kReadOrWriteFromFdSize;
char * m_buffer;
size_t m_allocSize;
size_t m_readOffset;
size_t m_writeOffset;
};
#endif