xref: /plan9-contrib/sys/src/libbio/bgetrune.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include	<u.h>
2 #include	<libc.h>
3 #include	<bio.h>
4 
5 long
6 Bgetrune(Biobufhdr *bp)
7 {
8 	int c, i;
9 	Rune rune;
10 	char str[4];
11 
12 	c = Bgetc(bp);
13 	if(c < Runeself) {		/* one char */
14 		bp->runesize = 1;
15 		return c;
16 	}
17 	str[0] = c;
18 
19 	for(i=1;;) {
20 		c = Bgetc(bp);
21 		if(c < 0)
22 			return c;
23 		str[i++] = c;
24 
25 		if(fullrune(str, i)) {
26 			bp->runesize = chartorune(&rune, str);
27 			while(i > bp->runesize) {
28 				Bungetc(bp);
29 				i--;
30 			}
31 			return rune;
32 		}
33 	}
34 }
35 
36 int
37 Bungetrune(Biobufhdr *bp)
38 {
39 
40 	if(bp->state == Bracteof)
41 		bp->state = Bractive;
42 	if(bp->state != Bractive)
43 		return Beof;
44 	bp->icount -= bp->runesize;
45 	bp->runesize = 0;
46 	return 1;
47 }
48