/ Published in: C++
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include <stdio.h> #include <unistd.h> #include <string.h> #include <assert.h> #include <inttypes.h> typedef unsigned int uint_t; class RawReader { static const uint_t RBUF_SIZE = 8192; char rbuf[RBUF_SIZE]; char *rlim, *rpos; int rrlen, reof; char* read_more(char *rpos) { int l, s, t; char *p; if (reof) return rpos; l = rrlen - (rpos - rbuf); if (rpos != rbuf) memmove(rbuf, rpos, l); rpos = rbuf; p = rbuf + l; t = sizeof(rbuf) - l; s = read(0, p, t); if (s < 0) s = 0; rrlen = l + s; rlim = rpos + rrlen; if (s <= 0) { reof = 1; } return rpos; } public: void init(void) { rrlen = 0; reof = 0; rlim = rbuf; rpos = rbuf; } inline int rnextf() { return *rpos++; } inline int rnext() { if (rlim - rpos <= 0) rpos = read_more(rpos); return (rlim - rpos > 0) ? rnextf() : -1; } inline uint32_t getu32() { if (rlim - rpos < 12) rpos = read_more(rpos); uint32_t v; do { v = rnextf() - '0'; } while (v >= 10); uint32_t res = v; for (;;) { v = rnextf() - '0'; if (v >= 10) break; res = res * 10 + v; } return res; } };