-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread4.c
52 lines (37 loc) · 781 Bytes
/
read4.c
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
#include "lib/helpers.h"
/**
* The read4 API is defined in the parent class Reader4.
* int read4(char *buf4);
*/
int read4(char *buf4);
#define READ4_FULL_LEN 4
/**
* @param buf Destination buffer
* @param n Number of characters to read
* @return The number of actual characters read
*/
int _read(char* buf, int len) {
int org_len = len, cn = 0;
if (len <= 0)
return 0;
while (len > 0)
{
if (len < READ4_FULL_LEN)
{
char *tmp = calloc(READ4_FULL_LEN + 1, sizeof(char));
int tmp_len = read4(tmp), i = 0;
cn = Min(tmp_len, len);
for (i = 0; i < cn; ++i)
buf[i] = tmp[i];
len -= cn;
free(tmp);
break;
}
cn = read4(buf);
buf += cn;
len -= cn;
if (cn < READ4_FULL_LEN)
break;
}
return org_len - len;
}