bgetrune.c 634 B

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