libcsv.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef LIBCSV_H__
  2. #define LIBCSV_H__
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #ifdef __cplusplus
  6. extern "C"
  7. {
  8. #endif
  9. #define CSV_MAJOR 3
  10. #define CSV_MINOR 0
  11. #define CSV_RELEASE 3
  12. /* Error Codes */
  13. #define CSV_SUCCESS 0
  14. #define CSV_EPARSE 1 /* Parse error in strict mode */
  15. #define CSV_ENOMEM 2 /* Out of memory while increasing buffer size */
  16. #define CSV_ETOOBIG 3 /* Buffer larger than SIZE_MAX needed */
  17. #define CSV_EINVALID 4 /* Invalid code,should never be received from csv_error */
  18. /* parser options */
  19. #define CSV_STRICT 1 /* enable strict mode */
  20. #define CSV_REPALL_NL 2 /* report all unquoted carriage returns and linefeeds */
  21. #define CSV_STRICT_FINI 4 /* causes csv_fini to return CSV_EPARSE if last field is quoted and doesn't containg ending quote */
  22. #define CSV_APPEND_NULL 8 /* Ensure that all fields are null-terminated */
  23. #define CSV_EMPTY_IS_NULL 16 /* Pass null pointer to cb1 function when empty, unquoted fields are encountered */
  24. /* Character values */
  25. #define CSV_TAB 0x09
  26. #define CSV_SPACE 0x20
  27. #define CSV_CR 0x0d
  28. #define CSV_LF 0x0a
  29. #define CSV_COMMA 0x2c
  30. #define CSV_QUOTE 0x22
  31. struct csv_parser
  32. {
  33. int pstate; /* Parser state */
  34. int quoted; /* Is the current field a quoted field? */
  35. size_t spaces; /* Number of continious spaces after quote or in a non-quoted field */
  36. unsigned char *entry_buf; /* Entry buffer */
  37. size_t entry_pos; /* Current position in entry_buf (and current size of entry) */
  38. size_t entry_size; /* Size of entry buffer */
  39. int status; /* Operation status */
  40. unsigned char options;
  41. unsigned char quote_char;
  42. unsigned char delim_char;
  43. int (*is_space)(unsigned char);
  44. int (*is_term)(unsigned char);
  45. size_t blk_size;
  46. void *(*malloc_func)(size_t);
  47. void *(*realloc_func)(void *, size_t);
  48. void (*free_func)(void *);
  49. };
  50. /* Function Prototypes */
  51. int csv_init(struct csv_parser *p, unsigned char options);
  52. int csv_fini(struct csv_parser *p, void (*cb1)(void *, size_t, void *), void (*cb2)(int, void *), void *data);
  53. void csv_free(struct csv_parser *p);
  54. int csv_error(struct csv_parser *p);
  55. char *csv_strerror(int error);
  56. size_t csv_parse(struct csv_parser *p, const void *s, size_t len, void (*cb1)(void *, size_t, void *), void (*cb2)(int, void *), void *data);
  57. size_t csv_write(void *dest, size_t dest_size, const void *src, size_t src_size);
  58. int csv_fwrite(FILE *fp, const void *src, size_t src_size);
  59. size_t csv_write2(void *dest, size_t dest_size, const void *src, size_t src_size, unsigned char quote);
  60. int csv_fwrite2(FILE *fp, const void *src, size_t src_size, unsigned char quote);
  61. int csv_get_opts(struct csv_parser *p);
  62. int csv_set_opts(struct csv_parser *p, unsigned char options);
  63. void csv_set_delim(struct csv_parser *p, unsigned char c);
  64. void csv_set_quote(struct csv_parser *p, unsigned char c);
  65. unsigned char csv_get_delim(struct csv_parser *p);
  66. unsigned char csv_get_quote(struct csv_parser *p);
  67. void csv_set_space_func(struct csv_parser *p, int (*f)(unsigned char));
  68. void csv_set_term_func(struct csv_parser *p, int (*f)(unsigned char));
  69. void csv_set_realloc_func(struct csv_parser *p, void *(*)(void *, size_t));
  70. void csv_set_free_func(struct csv_parser *p, void (*)(void *));
  71. void csv_set_blk_size(struct csv_parser *p, size_t);
  72. size_t csv_get_buffer_size(struct csv_parser *p);
  73. #ifdef __cplusplus
  74. }
  75. #endif
  76. #endif