/* ojo v2 */ #include #include #include #include #include #define BUFSIZE 12 //#define XDEBUG static unsigned char buf[BUFSIZE + 1]; static int inbuf; static unsigned char * bp; static unsigned char * be; static int doread = 1; int readint(int fd, int * num) { int rv; int gotdigit; int ret = 0; unsigned char * bb; while (1) { if (doread) { rv = read(fd, buf + inbuf, BUFSIZE - inbuf); if (rv <= 0) { #ifdef XDEBUG printf("not loaded a thing\n"); #endif return ret; /* eof albo inny zonk */ } #ifdef XDEBUG printf("loaded %d @ %d\n", rv, inbuf); #endif doread = 0; inbuf += rv; bp = buf; be = buf + inbuf; *be = '\0'; } bb = bp; ret = sscanf(bp, "%d", num); #ifdef XDEBUG printf("sscanf @ %ld: %d (bp[0]: 0x%02x, bp[1]: 0x%02x)\n", bp - buf, rv, bp[0], bp[1]); #endif if (ret == -1) { ret = 0; doread = 1; inbuf = 0; continue; } if (!ret) { break; } gotdigit = 0; while (bp < be) { bp++; if (!gotdigit) { /* ignoruj wszystkie non-digity póki znajdziesz digit */ if (isdigit(*bp) || *bp == '-') { gotdigit = 1; bb = bp; /* xx */ } } else { if (!isdigit(*bp)) break; } } if (bp == be) { /* cos tam bylo, wiec sprobuj przesunac buf */ memmove(buf, bb, be - bb); inbuf = be - bb; doread = 1; #ifdef XDEBUG printf("shifted, in buf: %d\n", inbuf); #endif } else { break; } } return ret; } int main(int ac, char ** av) { int x; while (readint(0, &x)) { printf("found: %d\n", x); } return 0; }