Scrap readrune(), introducing fgetrune()

Interface as proposed by cls, but internally rewritten after a few
considerations.
The code is much shorter and to the point, aligning itself with other
standard functions. It should also be much faster, which is not bad.
This commit is contained in:
FRIGN
2015-02-11 20:13:43 +01:00
parent 4888bae455
commit a5ae899a48
10 changed files with 45 additions and 57 deletions

34
libutf/fgetrune.c Normal file
View File

@@ -0,0 +1,34 @@
/* 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 *p, FILE *fp)
{
char buf[UTFmax];
int i;
for (i = 0; i < UTFmax && (buf[i] = fgetc(fp)) != EOF && ++i ;)
if (charntorune(p, buf, i) > 0)
break;
if (ferror(fp))
return -1;
return i;
}
int
efgetrune(Rune *p, FILE *fp, const char *file)
{
int ret;
if ((ret = fgetrune(p, fp)) < 0) {
fprintf(stderr, "fgetrune %s: %s\n", file, strerror(errno));
exit(1);
}
return ret;
}

View File

@@ -1,47 +0,0 @@
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../utf.h"
int
readrune(const char *file, FILE *fp, Rune *r)
{
char buf[UTFmax];
int c, i;
if ((c = fgetc(fp)) == EOF) {
if (ferror(fp)) {
fprintf(stderr, "%s: read error: %s\n",
file, strerror(errno));
exit(1);
}
return 0;
}
if (c < Runeself) {
*r = (Rune)c;
return 1;
}
buf[0] = c;
for (i = 1; i < UTFmax; ) {
if ((c = fgetc(fp)) == EOF) {
if (ferror(fp)) {
fprintf(stderr, "%s: read error: %s\n",
file, strerror(errno));
exit(1);
}
*r = Runeerror;
return i;
}
buf[i++] = c;
if (fullrune(buf, i)) {
chartorune(r, buf);
break;
}
}
return i;
}