blob: 5c24f526d9e86d2fb8be1ac0e2b7fd69d55b1796 (
plain) (
blame)
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
|
#include <stdio.h>
#include <string.h>
#include "macros.h"
#include "mbio.h"
#include "mbstring.h"
#include "rune.h"
#define RETURN_INVAL \
do { \
*ch = RUNE_ERROR; \
return 3; \
} while (false)
int
freadrune(rune *ch, FILE *stream)
{
int c, n = 0;
char8_t buf[U8_LEN_MAX];
if ((c = fgetc(stream)) == EOF)
goto eof_or_err;
buf[0] = (char8_t)c;
n = u8byte1(c) ? 0 : u8byte2(c) ? 1 : u8byte3(c) ? 2 : u8byte4(c) ? 3 : 4;
if (n == 0) {
*ch = buf[0];
return 1;
} else if (n == 4)
RETURN_INVAL;
for (int i = 0; i < n; i++) {
if ((c = fgetc(stream)) == EOF)
goto eof_or_err;
if (!u8bytec(c))
RETURN_INVAL;
buf[i + 1] = c;
}
return u8tor(ch, buf);
eof_or_err:
if (ferror(stream))
return MBERR;
if (n == 0)
return MBEOF;
RETURN_INVAL;
}
|