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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
#include "commtst.h"
#include <stdint.h>
i32_t
fsize (FILE *strm)
{
fseek (strm, 0, SEEK_SET);
fseek (strm, 0, SEEK_END);
long sz = ftell (strm);
assert (sz > 0);
fseek (strm, 0, SEEK_SET);
return sz;
}
void
read_atmost (void *src, char *rbuf, const size_t nmost, size_t *nread)
{
#ifdef is_win
HANDLE *hdlp = (HANDLE *) src;
#else
int *fdp = (int *) src;
#endif
size_t nread_accm = 0;
size_t nleft = nmost;
while (nleft > 0)
{
ssize_t nbuffered = nmost - nleft;
#ifdef is_win
ssize_t nread_cur = 0;
assT (ReadFile
(*hdlp, rbuf + nbuffered, (DWORD)nleft, (LPDWORD) (&nread_cur),
nullptr));
#else
ssize_t nread_cur = read (*fdp, rbuf + nbuffered, nleft);
#endif
if (nread_cur <= 0)
break;
nread_accm += nread_cur;
nleft -= nread_cur;
}
*nread = nread_accm;
}
void
recv_atmost (
#if is_unix
int csock,
#elif is_win
SOCKET csock,
#endif
char *rbuf, const size_t nmost, size_t *nread)
{
size_t nread_accm = 0;
size_t nleft = nmost;
while (nleft > 0)
{
ssize_t nbuffered = nmost - nleft;
ssize_t nread_cur = recv (csock, rbuf + nbuffered, (int)nleft, 0);
if (nread_cur <= 0)
break;
nread_accm += nread_cur;
nleft -= nread_cur;
}
*nread = nread_accm;
}
i32_t
read_u8str (const char8_t *const filename, char8_t *const destbuf)
{
FILE *file;
#if is_win
fopen_s (&file, (const char*) filename, "rb,ccs=UTF-8");
#else
file = fopen ((const char*) filename, "rb");
#endif
assert (file != nullptr);
i32_t fsz = fsize (file);
size_t bytes_read = fread (destbuf, 1, fsz, file);
assert (bytes_read > 0);
// null-terminate the buffer
destbuf[bytes_read] = 0;
// printf("File content: %s\n", destbuf);
fclose (file);
return 0;
}
i32_t
read_u8str_sz (const char8_t *const filename, char8_t *const destbuf,
size_t *nread)
{
FILE *file;
#if is_win
fopen_s (&file, (const char*) filename, "rb,ccs=UTF-8");
#else
file = fopen ((const char*) filename, "rb");
#endif
assTcb (file != nullptr,
{
fprintf (stderr, "filename:%s\n", filename);
});
i32_t fsz = fsize (file);
size_t bytes_read = fread (destbuf, 1, fsz, file);
assert (bytes_read > 0);
// null-terminate the buffer
destbuf[bytes_read] = 0;
*nread = bytes_read;
// printf("File content: %s\n", destbuf);
fclose (file);
return 0;
}
void
zero_trailing_white_char (char8_t *buf, const size_t len)
{
for (size_t i = len - 1; i != 0; i--) // TODO ckd_add
{
if (buf[i] == '\n' || buf[i] == '\r' || buf[i] == '\t' || buf[i] == ' ')
buf[i] = 0;
else
break;
}
}
void
zero_trailing_white_char2 (char8_t *buf, size_t *len)
{
for (size_t i = *len - 1; i != 0; i--) // TODO ckd_add
{
if (buf[i] == '\n' || buf[i] == '\r' || buf[i] == '\t' || buf[i] == ' ')
{
buf[i] = 0;
*len -= 1;
}
else
break;
}
}
#ifdef is_unix
FILE *
unix_fd2strm_r (int fd)
{
return fdopen (fd, "r");
}
#endif
#ifdef is_win
FILE *
win_handle2strm (HANDLE h)
{
int fd = _open_osfhandle ((intptr_t) h, _O_RDONLY);
assT (fd > 0);
FILE *file = _fdopen (fd, "r+");
assT (file != nullptr);
return file;
}
#endif
#ifdef is_win
int
win_handle2fd (HANDLE h)
{
int fd = _open_osfhandle ((intptr_t) h, _O_RDONLY);
assT (fd > 0);
return fd;
}
#endif
|