csvtest.c 799 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. csvtest - reads CSV data from stdin and output properly formed equivalent
  3. useful for testing the library
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #include <csv.h>
  10. static int put_comma;
  11. void cb1 (void *s, size_t i, void *p) {
  12. if (put_comma)
  13. putc(',', stdout);
  14. csv_fwrite(stdout, s, i);
  15. put_comma = 1;
  16. }
  17. void cb2 (int c, void *p) {
  18. put_comma = 0;
  19. putc('\n', stdout);
  20. }
  21. int main (void) {
  22. struct csv_parser p;
  23. int i;
  24. char c;
  25. csv_init(&p, 0);
  26. while ((i=getc(stdin)) != EOF) {
  27. c = i;
  28. if (csv_parse(&p, &c, 1, cb1, cb2, NULL) != 1) {
  29. fprintf(stderr, "Error: %s\n", csv_strerror(csv_error(&p)));
  30. exit(EXIT_FAILURE);
  31. }
  32. }
  33. csv_fini(&p, cb1, cb2, NULL);
  34. csv_free(&p);
  35. return EXIT_SUCCESS;
  36. }