1
0

bof.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * on the rights to use, copy, modify, merge, publish, distribute, sub
  8. * license, and/or sell copies of the Software, and to permit persons to whom
  9. * the Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  21. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Jerome Glisse
  25. */
  26. #ifndef BOF_H
  27. #define BOF_H
  28. #include <stdio.h>
  29. #include <stdint.h>
  30. #define BOF_TYPE_STRING 0
  31. #define BOF_TYPE_NULL 1
  32. #define BOF_TYPE_BLOB 2
  33. #define BOF_TYPE_OBJECT 3
  34. #define BOF_TYPE_ARRAY 4
  35. #define BOF_TYPE_INT32 5
  36. struct bof;
  37. typedef struct bof {
  38. struct bof **array;
  39. unsigned centry;
  40. unsigned nentry;
  41. unsigned refcount;
  42. FILE *file;
  43. uint32_t type;
  44. uint32_t size;
  45. uint32_t array_size;
  46. void *value;
  47. long offset;
  48. } bof_t;
  49. extern int bof_file_flush(bof_t *root);
  50. extern bof_t *bof_file_new(const char *filename);
  51. extern int bof_object_dump(bof_t *object, const char *filename);
  52. /* object */
  53. extern bof_t *bof_object(void);
  54. extern bof_t *bof_object_get(bof_t *object, const char *keyname);
  55. extern int bof_object_set(bof_t *object, const char *keyname, bof_t *value);
  56. /* array */
  57. extern bof_t *bof_array(void);
  58. extern int bof_array_append(bof_t *array, bof_t *value);
  59. extern bof_t *bof_array_get(bof_t *bof, unsigned i);
  60. extern unsigned bof_array_size(bof_t *bof);
  61. /* blob */
  62. extern bof_t *bof_blob(unsigned size, void *value);
  63. extern unsigned bof_blob_size(bof_t *bof);
  64. extern void *bof_blob_value(bof_t *bof);
  65. /* string */
  66. extern bof_t *bof_string(const char *value);
  67. /* int32 */
  68. extern bof_t *bof_int32(int32_t value);
  69. extern int32_t bof_int32_value(bof_t *bof);
  70. /* common functions */
  71. extern void bof_decref(bof_t *bof);
  72. extern void bof_incref(bof_t *bof);
  73. extern bof_t *bof_load_file(const char *filename);
  74. extern int bof_dump_file(bof_t *bof, const char *filename);
  75. extern void bof_print(bof_t *bof);
  76. static inline int bof_is_object(bof_t *bof){return (bof->type == BOF_TYPE_OBJECT);}
  77. static inline int bof_is_blob(bof_t *bof){return (bof->type == BOF_TYPE_BLOB);}
  78. static inline int bof_is_null(bof_t *bof){return (bof->type == BOF_TYPE_NULL);}
  79. static inline int bof_is_int32(bof_t *bof){return (bof->type == BOF_TYPE_INT32);}
  80. static inline int bof_is_array(bof_t *bof){return (bof->type == BOF_TYPE_ARRAY);}
  81. static inline int bof_is_string(bof_t *bof){return (bof->type == BOF_TYPE_STRING);}
  82. #endif