csv.h 3.4 KB

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