vici_builder.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2014 Martin Willi
  3. * Copyright (C) 2014 revosec AG
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. /**
  16. * @defgroup vici_builder vici_builder
  17. * @{ @ingroup vici
  18. */
  19. #ifndef VICI_BUILDER_H_
  20. #define VICI_BUILDER_H_
  21. #include "vici_message.h"
  22. typedef struct vici_builder_t vici_builder_t;
  23. /**
  24. * Build helper for vici message
  25. */
  26. struct vici_builder_t {
  27. /**
  28. * Append a generic message element to message.
  29. *
  30. * The additional arguments are type specific, it may be nothing, a string,
  31. * a chunk value or both.
  32. *
  33. * @param type element type to add
  34. * @param ... additional type specific arguments
  35. */
  36. void (*add)(vici_builder_t *this, vici_type_t type, ...);
  37. /**
  38. * Append a key/value element using a format string.
  39. *
  40. * Instead of passing the type specific value as a chunk, this method
  41. * takes a printf() style format string followed by its arguments. The
  42. * key name for a key/value type is still a fixed string.
  43. *
  44. * @param key key name of the key/value to add
  45. * @param fmt value format string
  46. * @param ... arguments to value format string
  47. */
  48. void (*add_kv)(vici_builder_t *this, char *key, char *fmt, ...);
  49. /**
  50. * Append a message element using a format string and va_list.
  51. *
  52. * Instead of passing the type specific value as a chunk, this method
  53. * takes a printf() style format string followed by its arguments. The
  54. * key name for a key/value type is still a fixed string.
  55. *
  56. * @param key key name of the key/value to add
  57. * @param fmt value format string
  58. * @param args arguments to value format string
  59. */
  60. void (*vadd_kv)(vici_builder_t *this, char *key, char *fmt, va_list args);
  61. /**
  62. * Append a list item element using a format string.
  63. *
  64. * Instead of passing the type specific value as a chunk, this method
  65. * takes a printf() style format string followed by its arguments.
  66. *
  67. * @param fmt value format string
  68. * @param ... arguments to value format string
  69. */
  70. void (*add_li)(vici_builder_t *this, char *fmt, ...);
  71. /**
  72. * Append a list item element using a format string and va_list.
  73. *
  74. * Instead of passing the type specific value as a chunk, this method
  75. * takes a printf() style format string followed by its arguments.
  76. *
  77. * @param fmt value format string
  78. * @param args arguments to value format string
  79. */
  80. void (*vadd_li)(vici_builder_t *this, char *fmt, va_list args);
  81. /**
  82. * Begin a new section.
  83. *
  84. * @param name name of section to begin
  85. */
  86. void (*begin_section)(vici_builder_t *this, char *name);
  87. /**
  88. * End the currently open section.
  89. */
  90. void (*end_section)(vici_builder_t *this);
  91. /**
  92. * Begin a new list.
  93. *
  94. * @param name name of list to begin
  95. */
  96. void (*begin_list)(vici_builder_t *this, char *name);
  97. /**
  98. * End the currently open list.
  99. */
  100. void (*end_list)(vici_builder_t *this);
  101. /**
  102. * Finalize a vici message with all added elements, destroy builder.
  103. *
  104. * @return vici message, NULL on error
  105. */
  106. vici_message_t* (*finalize)(vici_builder_t *this);
  107. /**
  108. * Destroy a vici builder without finalization.
  109. *
  110. * Note that finalize() already destroys the message, and calling destroy()
  111. * is required only if the message does not get finalize()d.
  112. */
  113. void (*destroy)(vici_builder_t *this);
  114. };
  115. /**
  116. * Create a vici_builder instance.
  117. */
  118. vici_builder_t *vici_builder_create();
  119. #endif /** VICI_BUILDER_H_ @}*/