Factor out readrune and writerune
This commit is contained in:
46
libutf/readrune.c
Normal file
46
libutf/readrune.c
Normal file
@@ -0,0 +1,46 @@
|
||||
/* 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; ;) {
|
||||
if ((c = fgetc(fp)) == EOF) {
|
||||
if (ferror(fp)) {
|
||||
fprintf(stderr, "%s: read error: %s\n",
|
||||
file, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
buf[i++] = c;
|
||||
if (fullrune(buf, i)) {
|
||||
chartorune(r, buf);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
23
libutf/writerune.c
Normal file
23
libutf/writerune.c
Normal file
@@ -0,0 +1,23 @@
|
||||
/* See LICENSE file for copyright and license details. */
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../utf.h"
|
||||
|
||||
void
|
||||
writerune(Rune *r)
|
||||
{
|
||||
char buf[UTFmax];
|
||||
int n;
|
||||
|
||||
if ((n = runetochar(buf, r)) > 0) {
|
||||
fwrite(buf, n, 1, stdout);
|
||||
if (ferror(stdout)) {
|
||||
fprintf(stderr, "stdout: write error: %s\n",
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user