File: csvtest.c

package info (click to toggle)
libcsv 3.0.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,436 kB
  • ctags: 226
  • sloc: sh: 10,209; ansic: 1,070; makefile: 32
file content (47 lines) | stat: -rw-r--r-- 799 bytes parent folder | download | duplicates (4)
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
45
46
47
/*
csvtest - reads CSV data from stdin and output properly formed equivalent
          useful for testing the library
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <csv.h>

static int put_comma;

void cb1 (void *s, size_t i, void *p) {
  if (put_comma)
    putc(',', stdout);
  csv_fwrite(stdout, s, i);
  put_comma = 1;
}

void cb2 (int c, void *p) {
  put_comma = 0;
  putc('\n', stdout);
}

int main (void) {
  struct csv_parser p;
  int i;
  char c;

  csv_init(&p, 0);

  while ((i=getc(stdin)) != EOF) {
    c = i;
    if (csv_parse(&p, &c, 1, cb1, cb2, NULL) != 1) {
      fprintf(stderr, "Error: %s\n", csv_strerror(csv_error(&p)));
      exit(EXIT_FAILURE);
    }
  }

  csv_fini(&p, cb1, cb2, NULL);
  csv_free(&p);

  return EXIT_SUCCESS;
}