pbc.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef PROTOBUF_C_H
  2. #define PROTOBUF_C_H
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. #define PBC_ARRAY_CAP 64
  6. #define PBC_NOEXIST -1
  7. #define PBC_INT 1
  8. #define PBC_REAL 2
  9. #define PBC_BOOL 3
  10. #define PBC_ENUM 4
  11. #define PBC_STRING 5
  12. #define PBC_MESSAGE 6
  13. #define PBC_FIXED64 7
  14. #define PBC_FIXED32 8
  15. #define PBC_BYTES 9
  16. #define PBC_INT64 10
  17. #define PBC_UINT 11
  18. #define PBC_UNKNOWN 12
  19. #define PBC_REPEATED 128
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. typedef struct _pbc_array { char _data[PBC_ARRAY_CAP]; } pbc_array[1];
  24. struct pbc_slice {
  25. void *buffer;
  26. int len;
  27. };
  28. struct pbc_pattern;
  29. struct pbc_env;
  30. struct pbc_rmessage;
  31. struct pbc_wmessage;
  32. struct pbc_env * pbc_new(void);
  33. void pbc_delete(struct pbc_env *);
  34. int pbc_register(struct pbc_env *, struct pbc_slice * slice);
  35. int pbc_type(struct pbc_env *, const char * type_name , const char * key , const char ** type);
  36. const char * pbc_error(struct pbc_env *);
  37. // callback api
  38. union pbc_value {
  39. struct {
  40. uint32_t low;
  41. uint32_t hi;
  42. } i;
  43. double f;
  44. struct pbc_slice s;
  45. struct {
  46. int id;
  47. const char * name;
  48. } e;
  49. };
  50. typedef void (*pbc_decoder)(void *ud, int type, const char * type_name, union pbc_value *v, int id, const char *key);
  51. int pbc_decode(struct pbc_env * env, const char * type_name , struct pbc_slice * slice, pbc_decoder f, void *ud);
  52. // message api
  53. struct pbc_rmessage * pbc_rmessage_new(struct pbc_env * env, const char * type_name , struct pbc_slice * slice);
  54. void pbc_rmessage_delete(struct pbc_rmessage *);
  55. uint32_t pbc_rmessage_integer(struct pbc_rmessage * , const char *key , int index, uint32_t *hi);
  56. double pbc_rmessage_real(struct pbc_rmessage * , const char *key , int index);
  57. const char * pbc_rmessage_string(struct pbc_rmessage * , const char *key , int index, int *sz);
  58. struct pbc_rmessage * pbc_rmessage_message(struct pbc_rmessage *, const char *key, int index);
  59. int pbc_rmessage_size(struct pbc_rmessage *, const char *key);
  60. int pbc_rmessage_next(struct pbc_rmessage *, const char **key);
  61. struct pbc_wmessage * pbc_wmessage_new(struct pbc_env * env, const char *type_name);
  62. void pbc_wmessage_delete(struct pbc_wmessage *);
  63. // for negative integer, pass -1 to hi
  64. int pbc_wmessage_integer(struct pbc_wmessage *, const char *key, uint32_t low, uint32_t hi);
  65. int pbc_wmessage_real(struct pbc_wmessage *, const char *key, double v);
  66. int pbc_wmessage_string(struct pbc_wmessage *, const char *key, const char * v, int len);
  67. struct pbc_wmessage * pbc_wmessage_message(struct pbc_wmessage *, const char *key);
  68. void * pbc_wmessage_buffer(struct pbc_wmessage *, struct pbc_slice * slice);
  69. // array api
  70. int pbc_array_size(pbc_array);
  71. uint32_t pbc_array_integer(pbc_array array, int index, uint32_t *hi);
  72. double pbc_array_real(pbc_array array, int index);
  73. struct pbc_slice * pbc_array_slice(pbc_array array, int index);
  74. void pbc_array_push_integer(pbc_array array, uint32_t low, uint32_t hi);
  75. void pbc_array_push_slice(pbc_array array, struct pbc_slice *);
  76. void pbc_array_push_real(pbc_array array, double v);
  77. struct pbc_pattern * pbc_pattern_new(struct pbc_env * , const char * message, const char *format, ...);
  78. void pbc_pattern_delete(struct pbc_pattern *);
  79. // return unused bytes , -1 for error
  80. int pbc_pattern_pack(struct pbc_pattern *, void *input, struct pbc_slice * s);
  81. // <0 for error
  82. int pbc_pattern_unpack(struct pbc_pattern *, struct pbc_slice * s , void * output);
  83. void pbc_pattern_set_default(struct pbc_pattern * , void *data);
  84. void pbc_pattern_close_arrays(struct pbc_pattern *, void *data);
  85. int pbc_enum_id(struct pbc_env *env, const char *enum_type, const char *enum_name);
  86. #ifdef __cplusplus
  87. }
  88. #endif
  89. #endif