getln2.c (787B)
1 #include "substdio.h" 2 #ifdef USING_SKALIBS 3 //#include <skalibs/buffer.h> 4 #include <skalibs/stralloc.h> 5 #else 6 #include "stralloc.h" 7 #endif 8 #include "byte.h" 9 #include "getln.h" 10 11 12 int getln2(substdio *ss, stralloc *sa, 13 /*@out@*/char **cont, /*@out@*/unsigned int *clen, 14 int sep) 15 { 16 char *x; 17 unsigned int i; 18 19 if (!stralloc_ready(sa,0)) return -1; 20 sa->len = 0; 21 22 for (;;) { 23 ssize_t n, m; 24 n = substdio_feed(ss); 25 if (n < 0) return -1; 26 if (n == 0) { *clen = 0; return 0; } 27 x = substdio_PEEK(ss); 28 i = byte_chr(x,n,sep); 29 if (i < n) { substdio_SEEK(ss,*clen = i + 1); *cont = x; return 0; } 30 if (!stralloc_readyplus(sa,n)) return -1; 31 i = sa->len; 32 m = substdio_get(ss,sa->s + i,n); 33 if (m != -1) 34 sa->len = i + m; 35 } 36 }