readrune.c (735B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <errno.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 #include "../utf.h" 8 9 int 10 readrune(const char *file, FILE *fp, Rune *r) 11 { 12 char buf[UTFmax]; 13 int c, i; 14 15 if ((c = fgetc(fp)) == EOF) { 16 if (ferror(fp)) { 17 fprintf(stderr, "%s: read error: %s\n", 18 file, strerror(errno)); 19 exit(1); 20 } 21 return 0; 22 } 23 24 if (c < Runeself) { 25 *r = (Rune)c; 26 return 1; 27 } 28 29 buf[0] = c; 30 for (i = 1; ;) { 31 if ((c = fgetc(fp)) == EOF) { 32 if (ferror(fp)) { 33 fprintf(stderr, "%s: read error: %s\n", 34 file, strerror(errno)); 35 exit(1); 36 } 37 return 0; 38 } 39 buf[i++] = c; 40 if (fullrune(buf, i)) { 41 chartorune(r, buf); 42 break; 43 } 44 } 45 return 1; 46 }