llpkgc.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #ifndef INCLUDE_LLPKGC_API_H_
  4. #define INCLUDE_LLPKGC_API_H_
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. #include <stddef.h>
  9. #include "llpkgc__internal.h"
  10. #if defined(_MSC_VER)
  11. #define LLPKGC_EXPORT __declspec(dllexport)
  12. #else
  13. #define LLPKGC_EXPORT __attribute__((visibility("default")))
  14. #endif
  15. typedef llpkgc__internal_t llpkgc_t;
  16. typedef struct llpkgc_settings_s llpkgc_settings_t;
  17. typedef int (*llpkgc_data_cb)(llpkgc_t*, const char* at, size_t length);
  18. typedef int (*llpkgc_cb)(llpkgc_t*);
  19. struct llpkgc_settings_s {
  20. /* Possible return values 0, -1, PCE_USER */
  21. llpkgc_data_cb on_key;
  22. llpkgc_data_cb on_value_literal;
  23. llpkgc_data_cb on_value_variable;
  24. /* Possible return values 0, -1, `PCE_PAUSED` */
  25. llpkgc_cb on_line_begin;
  26. llpkgc_cb on_keyword_complete;
  27. llpkgc_cb on_variable_complete;
  28. llpkgc_cb on_value_literal_complete;
  29. llpkgc_cb on_value_variable_complete;
  30. llpkgc_cb on_value_complete;
  31. llpkgc_cb on_pkgc_complete;
  32. };
  33. enum llpkgc_errno {
  34. PCE_OK = 0,
  35. PCE_INTERNAL = 1,
  36. PCE_PAUSED = 2,
  37. PCE_USER = 3,
  38. PCE_UNFINISHED = 4,
  39. };
  40. typedef enum llpkgc_errno llpkgc_errno_t;
  41. /* Initialize the parser with user settings.
  42. *
  43. * NOTE: lifetime of `settings` has to be at least the same as the lifetime of
  44. * the `parser` here. In practice, `settings` has to be either a static
  45. * variable or be allocated with `malloc`, `new`, etc.
  46. */
  47. LLPKGC_EXPORT
  48. void llpkgc_init(llpkgc_t* parser, const llpkgc_settings_t* settings);
  49. /* Reset an already initialized parser back to the start state, preserving the
  50. * existing callback settings and user data.
  51. */
  52. LLPKGC_EXPORT
  53. void llpkgc_reset(llpkgc_t* parser);
  54. /* Initialize the settings object */
  55. LLPKGC_EXPORT
  56. void llpkgc_settings_init(llpkgc_settings_t* settings);
  57. /* Parse full or partial pc data, invoking user callbacks along the way.
  58. *
  59. * If any of `llpkgc_data_cb` returns errno not equal to `PCE_OK` - the parsing
  60. * interrupts, and such errno is returned from `llpkgc_execute()`. If
  61. * `PCE_PAUSED` was used as an errno, the execution can be resumed with
  62. * `llpkgc_resume()` call.
  63. *
  64. * NOTE: if this function ever returns a non-pause type error, it will continue
  65. * to return the same error upon each successive call up until `llpkgc_init()`
  66. * or `llpkgc_reset()` are called.
  67. */
  68. LLPKGC_EXPORT
  69. llpkgc_errno_t llpkgc_execute(llpkgc_t* parser, const char* data, size_t len);
  70. /* This method should be called when the input has reached EOF
  71. *
  72. * This method will invoke `on_pkgc_complete()` callback if the file was
  73. * terminated safely. Otherwise an error code will be returned.
  74. */
  75. LLPKGC_EXPORT
  76. llpkgc_errno_t llpkgc_finish(llpkgc_t* parser);
  77. /* Make further calls of `llpkgc_execute()` return `PCE_PAUSED` and set
  78. * appropriate error reason.
  79. *
  80. * Important: do not call this from user callbacks! User callbacks must return
  81. * `PCE_PAUSED` if pausing is required.
  82. */
  83. LLPKGC_EXPORT
  84. void llpkgc_pause(llpkgc_t* parser);
  85. /* Might be called to resume the execution after the pause in user's callback.
  86. * See `llpkgc_execute()` above for details.
  87. *
  88. * Call this only if `llpkgc_execute()` returns `PCE_PAUSED`.
  89. */
  90. LLPKGC_EXPORT
  91. void llpkgc_resume(llpkgc_t* parser);
  92. /* Returns the latest return error */
  93. LLPKGC_EXPORT
  94. llpkgc_errno_t llpkgc_get_errno(const llpkgc_t* parser);
  95. /* Returns the verbal explanation of the latest returned error.
  96. *
  97. * Note: User callback should set error reason when returning the error. See
  98. * `llpkgc_set_error_reason()` for details.
  99. */
  100. LLPKGC_EXPORT
  101. const char* llpkgc_get_error_reason(const llpkgc_t* parser);
  102. /* Assign verbal description to the returned error. Must be called in user
  103. * callbacks right before returning the errno.
  104. *
  105. * Note: `PCE_USER` error code might be useful in user callbacks.
  106. */
  107. LLPKGC_EXPORT
  108. void llpkgc_set_error_reason(llpkgc_t* parser, const char* reason);
  109. /* Returns the pointer to the last parsed byte before the returned error. The
  110. * pointer is relative to the `data` argument of `llpkgc_execute()`.
  111. *
  112. * Note: this method might be useful for counting the number of parsed bytes.
  113. */
  114. LLPKGC_EXPORT
  115. const char* llpkgc_get_error_pos(const llpkgc_t* parser);
  116. /* Returns textual name of error code */
  117. LLPKGC_EXPORT
  118. const char* llpkgc_errno_name(llpkgc_errno_t err);
  119. #ifdef __cplusplus
  120. } /* extern "C" */
  121. #endif
  122. #endif /* INCLUDE_LLPKGC_API_H_ */