Guest User

Untitled

a guest
Jan 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <inttypes.h>
  6.  
  7. typedef unsigned int uint_t;
  8.  
  9. class RawReader
  10. {
  11. static const uint_t D = 10;
  12. static const uint_t RBUF_SIZE = 8192;
  13. char rbuf[RBUF_SIZE];
  14.  
  15. char *rlim, *rpos;
  16. int rrlen, reof;
  17.  
  18. char* read_more(char *rpos)
  19. {
  20. int l, s, t;
  21. char *p;
  22.  
  23. if (reof)
  24. return rpos;
  25.  
  26. l = rrlen - (rpos - rbuf);
  27. if (rpos != rbuf)
  28. memmove(rbuf, rpos, l);
  29. rpos = rbuf;
  30. p = rbuf + l;
  31. t = sizeof(rbuf) - l;
  32. s = read(0, p, t);
  33. if (s < 0)
  34. s = 0;
  35. rrlen = l + s;
  36. rlim = rpos + rrlen;
  37. if (s <= 0) {
  38. reof = 1;
  39. }
  40.  
  41. return rpos;
  42. }
  43.  
  44. public:
  45. void init(void)
  46. {
  47. rrlen = 0;
  48. reof = 0;
  49. rlim = rbuf;
  50. rpos = rbuf;
  51. }
  52.  
  53. inline int rnextf() { return *rpos++; }
  54.  
  55. inline int rnext()
  56. {
  57. if (rlim - rpos <= 0)
  58. rpos = read_more(rpos);
  59. return (rlim - rpos > 0) ? rnextf() : -1;
  60. }
  61.  
  62. inline uint32_t getu32()
  63. {
  64. if (rlim - rpos < 12)
  65. rpos = read_more(rpos);
  66.  
  67. uint32_t v;
  68. do {
  69. v = rnextf() - '0';
  70. } while (v >= D);
  71.  
  72. uint32_t res = v;
  73.  
  74. for (;;) {
  75. v = rnextf() - '0';
  76. if (v >= D)
  77. break;
  78. res = res * D + v;
  79. }
  80.  
  81. return res;
  82. }
  83.  
  84. };
Add Comment
Please, Sign In to add comment