summaryrefslogtreecommitdiff
path: root/util/sbase/libutf/fgetrune.c
blob: 8cd78c642134d3dcb66686a64a9c3db009cd5f53 (plain)
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
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../utf.h"

int
fgetrune(Rune *r, FILE *fp)
{
	char buf[UTFmax];
	int  i = 0, c;

	while (i < UTFmax && (c = fgetc(fp)) != EOF) {
		buf[i++] = c;
		if (charntorune(r, buf, i) > 0)
			break;
	}
	if (ferror(fp))
		return -1;

	return i;
}

int
efgetrune(Rune *r, FILE *fp, const char *file)
{
	int ret;

	if ((ret = fgetrune(r, fp)) < 0) {
		fprintf(stderr, "fgetrune %s: %s\n", file, strerror(errno));
		exit(1);
	}
	return ret;
}