getln2.c
author tomas@localhost
Thu, 01 Nov 2007 14:46:11 +0100
changeset 0 c045670f36e9
permissions -rw-r--r--
Imported queue-fix-1.4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     1
#include "substdio.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     2
#include "stralloc.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     3
#include "byte.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     4
#include "getln.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     5
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     6
int getln2(ss,sa,cont,clen,sep)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     7
register substdio *ss;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     8
register stralloc *sa;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     9
/*@out@*/char **cont;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    10
/*@out@*/unsigned int *clen;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    11
int sep;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    12
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    13
  register char *x;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    14
  register unsigned int i;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    15
  int n;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    16
 
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    17
  if (!stralloc_ready(sa,0)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    18
  sa->len = 0;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    19
 
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    20
  for (;;) {
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    21
    n = substdio_feed(ss);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    22
    if (n < 0) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    23
    if (n == 0) { *clen = 0; return 0; }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    24
    x = substdio_PEEK(ss);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    25
    i = byte_chr(x,n,sep);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    26
    if (i < n) { substdio_SEEK(ss,*clen = i + 1); *cont = x; return 0; }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    27
    if (!stralloc_readyplus(sa,n)) return -1;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    28
    i = sa->len;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    29
    sa->len = i + substdio_get(ss,sa->s + i,n);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    30
  }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    31
}