-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefillContent.cpp
More file actions
55 lines (44 loc) · 1.26 KB
/
refillContent.cpp
File metadata and controls
55 lines (44 loc) · 1.26 KB
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
/*
refillContent.cpp
Implementation file for the refillContent function
*/
#include "refillContent.hpp"
#include <errno.h>
#include <sys/types.h>
#if !defined(_MSC_VER)
#include <sys/uio.h>
#include <unistd.h>
#define READ read
#else
#include <BaseTsd.h>
#include <io.h>
typedef SSIZE_T ssize_t;
#define READ _read
#endif
const int BLOCK_SIZE = 4096;
const int BUFFER_SIZE = 16 * 16 * BLOCK_SIZE;
/*
Refill the content preserving the existing data.
@param[in, out] content View of the content
@return Number of bytes read
@retval 0 EOF
@retval -1 Read error
*/
[[nodiscard]] int refillContent(std::string_view& content) {
// initialize the internal buffer at first use
static char buffer[BUFFER_SIZE];
// preserve prefix of unprocessed characters to start of the buffer
std::copy(content.cbegin(), content.cend(), buffer);
// read in multiple of whole blocks
ssize_t bytesRead = 0;
while (((bytesRead = READ(0, (buffer + content.size()),
BUFFER_SIZE - BLOCK_SIZE)) == -1) && (errno == EINTR)) {
}
if (bytesRead == -1) {
// error in read
return -1;
}
// set content to the start of the buffer
content = std::string_view(buffer, content.size() + bytesRead);
return bytesRead;
}