~sircmpwn/scdoc

ref: 9f74b6a0bf9794081b755274bbddd15deb6a10aa scdoc/src/utf8_fgetch.c -rw-r--r-- 413 bytes
9f74b6a0Drew DeVault Parse section headings 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdint.h>
#include <stdio.h>
#include "unicode.h"

uint32_t utf8_fgetch(FILE *f) {
	char buffer[UTF8_MAX_SIZE];
	int c = fgetc(f);
	if (c == EOF) {
		return UTF8_INVALID;
	}
	buffer[0] = (char)c;
	int size = utf8_size(buffer);
	if (size > 1) {
		int amt = fread(&buffer[1], 1, size - 1, f);
		if (amt != size - 1) {
			return UTF8_INVALID;
		}
	}
	const char *ptr = buffer;
	return utf8_decode(&ptr);
}