forked from uchan-nos/mikanos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.cpp
41 lines (36 loc) · 809 Bytes
/
file.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
#include "file.hpp"
#include <cstdio>
#include <vector>
size_t PrintToFD(FileDescriptor& fd, const char* format, ...) {
constexpr int BUFFER_SIZE = 128;
va_list ap;
int result;
char s[BUFFER_SIZE];
va_start(ap, format);
result = vsnprintf(s, BUFFER_SIZE, format, ap);
va_end(ap);
if (result < BUFFER_SIZE) {
fd.Write(s, result);
return result;
}
std::vector<char> s2(result + 1);
va_start(ap, format);
vsnprintf(&s2[0], result + 1, format, ap);
va_end(ap);
fd.Write(&s2[0], result);
return result;
}
size_t ReadDelim(FileDescriptor& fd, char delim, char* buf, size_t len) {
size_t i = 0;
for (; i < len - 1; ++i) {
if (fd.Read(&buf[i], 1) == 0) {
break;
}
if (buf[i] == delim) {
++i;
break;
}
}
buf[i] = '\0';
return i;
}