File: ibuf_gets.c

package info (click to toggle)
bglibs 2.04%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,468 kB
  • sloc: ansic: 15,821; perl: 674; sh: 63; makefile: 29
file content (44 lines) | stat: -rw-r--r-- 834 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "ibuf.h"

/** Read a line from the \c ibuf into a C string. */
int ibuf_gets(ibuf* in, char* data, unsigned datalen, char boundary)
{
  iobuf* io;
  int ch;
  
  io = &in->io;
  in->count = 0;
  if (ibuf_eof(in) || ibuf_error(in) || ibuf_timedout(in)) return 0;
  while (datalen > 1) {
    if (io->bufstart >= io->buflen && !ibuf_refill(in)) {
      if (ibuf_eof(in)) break;
      return 0;
    }
    in->count++;
    ch = io->buffer[io->bufstart++];
    *data++ = ch;
    if (ch == boundary) break;
  }
  *data = 0;
  return 1;
}

#ifdef SELFTEST_MAIN
#include <unistd.h>

ibuf in = {
  { -1, "a\0bcd\0efgh", 10, 10, 0, 0, 0, 0, 0 },
  0, (ibuf_fn)read
};

MAIN
{
  char buf[999];
  while (ibuf_gets(&in, buf, sizeof buf, 0))
    obuf_putf(&outbuf, "u{:}s{\n}", in.count, buf);
}
#endif
#ifdef SELFTEST_EXP
2:a
4:bcd
#endif