Go Back

0 /*

1 * ISC License

2 * Copyright (c) 2023 RMF <rawmonk@firemail.cc>

3 */

4 #include <stdio.h>

5 #include <stdlib.h>

6 #include <stdint.h>

7 #include <unistd.h>

8 #include "macro.h"

9 #include "utf8.h"

10 #include "wcwidth.h"

11 #define PAGE_INTERNAL

12 #include "page.h"

13 #include "request.h"

14 #define PARSER_INTERNAL

15 #include "parser.h"

16

17 int renderable(uint32_t codepoint) {

18 return !(codepoint == 0xFEFF || codepoint == 127 ||

19 ((mk_wcwidth(codepoint) < 1 || codepoint < ' ') &&

20 codepoint != '\n' && codepoint != '\t'));

21 }

22

23 int readnext(int fd, uint32_t *ch, size_t *pos, size_t length) {

24

25 char buf[16] = {0};

26 int len;

27

28 if (read(fd, buf, 1) != 1) return -1;

29 len = utf8_char_length(*buf);

30 if ((size_t)len >= sizeof(buf)) return -1;

31 if (*pos + len > length) {

32 if (*pos + 1 < length) {

33 size_t bytes = length - *pos - 1;

34 if (vread(fd, buf, bytes)) return -1;

35 }

36 *ch = '\0';

37 *pos = length;

38 return 0;

39 }

40 if (len > 1 && vread(fd, &buf[1], len - 1)) return -1;

41 if (len > 0) *pos += len;

42 utf8_char_to_unicode(ch, buf);

43

44 return 0;

45 }

46

47 int vread(int fd, void *buf, size_t nbytes) {

48 ssize_t len;

49 char *ptr = buf;

50 while (nbytes) {

51 len = read(fd, ptr, nbytes);

52 if (len < 1) return -1;

53 nbytes -= len;

54 ptr += len;

55 }

56 return 0;

57 }

58