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