bgetrune.c - 9base - revived minimalist port of Plan 9 userland to Unix
 (HTM) git clone git://git.suckless.org/9base
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       bgetrune.c (635B)
       ---
            1 #include        "lib9.h"
            2 #include        <bio.h>
            3 #include        <utf.h>
            4 
            5 long
            6 Bgetrune(Biobuf *bp)
            7 {
            8         int c, i;
            9         Rune rune;
           10         char str[UTFmax];
           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(Biobuf *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 }