Bgetfield.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "../common/common.h"
  5. #include "tr2post.h"
  6. int
  7. isspace(Rune r)
  8. {
  9. return r==' ' || r=='\t' || r=='\n' || r=='\r' || r=='\f';
  10. }
  11. int
  12. Bskipws(Biobufhdr *bp) {
  13. int r, sindex = 0;
  14. /* skip over initial white space */
  15. do {
  16. r = Bgetrune(bp);
  17. if (r == '\n')
  18. inputlineno++;
  19. sindex++;
  20. } while (r>=0 && isspace(r));
  21. if (r<0)
  22. return(-1);
  23. else if (!isspace(r)) {
  24. Bungetrune(bp);
  25. --sindex;
  26. }
  27. return sindex;
  28. }
  29. int
  30. asc2dig(char c, int base) {
  31. if (c >= '0' && c <= '9')
  32. if (base == 8 && c > '7')
  33. return(-1);
  34. else
  35. return(c - '0');
  36. if (base == 16)
  37. if (c >= 'a' && c <= 'f')
  38. return(10 + c - 'a');
  39. else if (c >= 'A' && c <= 'F')
  40. return(10 + c - 'A');
  41. return(-1);
  42. }
  43. /*
  44. * get a string of type: "d" for decimal integer, "u" for unsigned,
  45. * "s" for string", "c" for char,
  46. * return the number of characters gotten for the field. If nothing
  47. * was gotten and the end of file was reached, a negative value
  48. * from the Bgetrune is returned.
  49. */
  50. int
  51. Bgetfield(Biobufhdr *bp, int type, void *thing, int size) {
  52. int base = 10;
  53. int dig;
  54. int negate = 0;
  55. int sindex = 0, i, j, n = 0;
  56. long r;
  57. Rune R;
  58. unsigned u = 0;
  59. BOOLEAN bailout = FALSE;
  60. char c[UTFmax];
  61. /* skip over initial white space */
  62. if (Bskipws(bp) < 0)
  63. return(-1);
  64. r = 0;
  65. switch (type) {
  66. case 'd':
  67. while (!bailout && (r = Bgetrune(bp))>=0) {
  68. switch (sindex++) {
  69. case 0:
  70. switch (r) {
  71. case '-':
  72. negate = 1;
  73. continue;
  74. case '+':
  75. continue;
  76. case '0':
  77. base = 8;
  78. continue;
  79. default:
  80. break;
  81. }
  82. break;
  83. case 1:
  84. if ((r == 'x' || r == 'X') && base == 8) {
  85. base = 16;
  86. continue;
  87. }
  88. break;
  89. }
  90. if ((dig = asc2dig(r, base)) == -1)
  91. bailout = TRUE;
  92. else
  93. n = dig + (n * base);
  94. }
  95. if (r < 0)
  96. return(-1);
  97. *(int *)thing = (negate)?-n:n;
  98. Bungetrune(bp);
  99. break;
  100. case 'u':
  101. while (!bailout && (r = Bgetrune(bp))>=0) {
  102. switch (sindex++) {
  103. case 0:
  104. if (*c == '0') {
  105. base = 8;
  106. continue;
  107. }
  108. break;
  109. case 1:
  110. if ((r == 'x' || r == 'X') && base == 8) {
  111. base = 16;
  112. continue;
  113. }
  114. break;
  115. }
  116. if ((dig = asc2dig(r, base)) == -1)
  117. bailout = TRUE;
  118. else
  119. u = dig + (n * base);
  120. }
  121. *(int *)thing = u;
  122. if (r < 0)
  123. return(-1);
  124. Bungetrune(bp);
  125. break;
  126. case 's':
  127. j = 0;
  128. while (size > j+UTFmax && (r = Bgetrune(bp)) >= 0 &&
  129. !isspace(r)) {
  130. R = r;
  131. i = runetochar(&(((char *)thing)[j]), &R);
  132. j += i;
  133. sindex++;
  134. }
  135. ((char *)thing)[j] = '\0';
  136. if (r < 0)
  137. return(-1);
  138. Bungetrune(bp);
  139. break;
  140. case 'r':
  141. if ((r = Bgetrune(bp))>=0) {
  142. *(Rune *)thing = r;
  143. sindex++;
  144. return(sindex);
  145. }
  146. if (r <= 0)
  147. return(-1);
  148. Bungetrune(bp);
  149. break;
  150. default:
  151. return(-2);
  152. }
  153. if (r < 0 && sindex == 0)
  154. return(r);
  155. else if (bailout && sindex == 1) {
  156. return(0);
  157. } else
  158. return(sindex);
  159. }