/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying1file LICENSE.rst or https://cmake.org/licensing for details. */23#ifndef INCLUDE_LLPKGC_API_H_4#define INCLUDE_LLPKGC_API_H_5#ifdef __cplusplus6extern "C" {7#endif8#include <stddef.h>910#include "llpkgc__internal.h"1112#if defined(_MSC_VER)13#define LLPKGC_EXPORT __declspec(dllexport)14#else15#define LLPKGC_EXPORT __attribute__((visibility("default")))16#endif1718typedef llpkgc__internal_t llpkgc_t;19typedef struct llpkgc_settings_s llpkgc_settings_t;2021typedef int (*llpkgc_data_cb)(llpkgc_t*, const char* at, size_t length);22typedef int (*llpkgc_cb)(llpkgc_t*);2324struct llpkgc_settings_s {25/* Possible return values 0, -1, PCE_USER */26llpkgc_data_cb on_key;27llpkgc_data_cb on_value_literal;28llpkgc_data_cb on_value_variable;2930/* Possible return values 0, -1, `PCE_PAUSED` */31llpkgc_cb on_line_begin;32llpkgc_cb on_keyword_complete;33llpkgc_cb on_variable_complete;34llpkgc_cb on_value_literal_complete;35llpkgc_cb on_value_variable_complete;36llpkgc_cb on_value_complete;37llpkgc_cb on_pkgc_complete;38};3940enum llpkgc_errno {41PCE_OK = 0,42PCE_INTERNAL = 1,43PCE_PAUSED = 2,44PCE_USER = 3,45PCE_UNFINISHED = 4,46};47typedef enum llpkgc_errno llpkgc_errno_t;4849/* Initialize the parser with user settings.50*51* NOTE: lifetime of `settings` has to be at least the same as the lifetime of52* the `parser` here. In practice, `settings` has to be either a static53* variable or be allocated with `malloc`, `new`, etc.54*/55LLPKGC_EXPORT56void llpkgc_init(llpkgc_t* parser, const llpkgc_settings_t* settings);5758/* Reset an already initialized parser back to the start state, preserving the59* existing callback settings and user data.60*/61LLPKGC_EXPORT62void llpkgc_reset(llpkgc_t* parser);6364/* Initialize the settings object */65LLPKGC_EXPORT66void llpkgc_settings_init(llpkgc_settings_t* settings);6768/* Parse full or partial pc data, invoking user callbacks along the way.69*70* If any of `llpkgc_data_cb` returns errno not equal to `PCE_OK` - the parsing71* interrupts, and such errno is returned from `llpkgc_execute()`. If72* `PCE_PAUSED` was used as an errno, the execution can be resumed with73* `llpkgc_resume()` call.74*75* NOTE: if this function ever returns a non-pause type error, it will continue76* to return the same error upon each successive call up until `llpkgc_init()`77* or `llpkgc_reset()` are called.78*/79LLPKGC_EXPORT80llpkgc_errno_t llpkgc_execute(llpkgc_t* parser, const char* data, size_t len);8182/* This method should be called when the input has reached EOF83*84* This method will invoke `on_pkgc_complete()` callback if the file was85* terminated safely. Otherwise an error code will be returned.86*/87LLPKGC_EXPORT88llpkgc_errno_t llpkgc_finish(llpkgc_t* parser);8990/* Make further calls of `llpkgc_execute()` return `PCE_PAUSED` and set91* appropriate error reason.92*93* Important: do not call this from user callbacks! User callbacks must return94* `PCE_PAUSED` if pausing is required.95*/96LLPKGC_EXPORT97void llpkgc_pause(llpkgc_t* parser);9899/* Might be called to resume the execution after the pause in user's callback.100* See `llpkgc_execute()` above for details.101*102* Call this only if `llpkgc_execute()` returns `PCE_PAUSED`.103*/104LLPKGC_EXPORT105void llpkgc_resume(llpkgc_t* parser);106107/* Returns the latest return error */108LLPKGC_EXPORT109llpkgc_errno_t llpkgc_get_errno(const llpkgc_t* parser);110111/* Returns the verbal explanation of the latest returned error.112*113* Note: User callback should set error reason when returning the error. See114* `llpkgc_set_error_reason()` for details.115*/116LLPKGC_EXPORT117const char* llpkgc_get_error_reason(const llpkgc_t* parser);118119/* Assign verbal description to the returned error. Must be called in user120* callbacks right before returning the errno.121*122* Note: `PCE_USER` error code might be useful in user callbacks.123*/124LLPKGC_EXPORT125void llpkgc_set_error_reason(llpkgc_t* parser, const char* reason);126127/* Returns the pointer to the last parsed byte before the returned error. The128* pointer is relative to the `data` argument of `llpkgc_execute()`.129*130* Note: this method might be useful for counting the number of parsed bytes.131*/132LLPKGC_EXPORT133const char* llpkgc_get_error_pos(const llpkgc_t* parser);134135/* Returns textual name of error code */136LLPKGC_EXPORT137const char* llpkgc_errno_name(llpkgc_errno_t err);138139#ifdef __cplusplus140} /* extern "C" */141#endif142#endif /* INCLUDE_LLPKGC_API_H_ */143144145