dmap_parser.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #ifndef dmap_parser_h
  2. #define dmap_parser_h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <sys/types.h>
  9. #define DMAP_VERSION_MAJOR 1
  10. #define DMAP_VERSION_MINOR 2
  11. #define DMAP_VERSION_PATCH 1
  12. #define DMAP_VERSION (DMAP_VERSION_MAJOR * 1000000 + \
  13. DMAP_VERSION_MINOR * 1000 + \
  14. DMAP_VERSION_PATCH)
  15. /*
  16. * Callbacks invoked during parsing.
  17. *
  18. * @param ctx The context pointer specified in the dmap_settings structure.
  19. * @param code The content code from the message.
  20. * @param name The name associated with the content code, if known. If there is
  21. * no known name this parameter contains the same value as the code
  22. * parameter.
  23. */
  24. typedef void (*dmap_dict_cb) (void *ctx, const char *code, const char *name);
  25. typedef void (*dmap_int32_cb) (void *ctx, const char *code, const char *name, int32_t value);
  26. typedef void (*dmap_int64_cb) (void *ctx, const char *code, const char *name, int64_t value);
  27. typedef void (*dmap_uint32_cb) (void *ctx, const char *code, const char *name, uint32_t value);
  28. typedef void (*dmap_uint64_cb) (void *ctx, const char *code, const char *name, uint64_t value);
  29. typedef void (*dmap_data_cb) (void *ctx, const char *code, const char *name, const char *buf, size_t len);
  30. typedef struct {
  31. /* Callbacks to indicate the start and end of dictionary fields. */
  32. dmap_dict_cb on_dict_start;
  33. dmap_dict_cb on_dict_end;
  34. /* Callbacks for field data. */
  35. dmap_int32_cb on_int32;
  36. dmap_int64_cb on_int64;
  37. dmap_uint32_cb on_uint32;
  38. dmap_uint64_cb on_uint64;
  39. dmap_uint32_cb on_date;
  40. dmap_data_cb on_string;
  41. dmap_data_cb on_data;
  42. /** A context pointer passed to each callback function. */
  43. void *ctx;
  44. } dmap_settings;
  45. /**
  46. * Returns the library version number.
  47. *
  48. * The version number format is (major * 1000000) + (minor * 1000) + patch.
  49. * For example, the value for version 1.2.3 is 1002003.
  50. */
  51. int dmap_version(void);
  52. /**
  53. * Returns the library version as a string.
  54. */
  55. const char *dmap_version_string(void);
  56. /**
  57. * Returns the name associated with the provided content code, or NULL if there
  58. * is no known name.
  59. *
  60. * For example, if given the code "minm" this function returns "dmap.itemname".
  61. */
  62. const char *dmap_name_from_code(const char *code);
  63. /**
  64. * Parses a DMAP message buffer using the provided settings.
  65. *
  66. * @param settings A dmap_settings structure populated with the callbacks to
  67. * invoke during parsing.
  68. * @param buf Pointer to a DMAP message buffer. The buffer must contain a
  69. * complete message.
  70. * @param len The length of the DMAP message buffer.
  71. *
  72. * @return 0 if parsing was successful, or -1 if an error occurred.
  73. */
  74. int dmap_parse(const dmap_settings *settings, const char *buf, size_t len);
  75. #ifdef __cplusplus
  76. }
  77. #endif
  78. #endif