blob: 47780aef4d4a8b72f69f69e8ee3cf4d932e72a31 [file] [log] [blame]
Keith Millerd8cbaab2024-10-30 16:45:461/*
2 * Copyright (C) 2014 Filip Pizlo. All rights reserved.
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
5 * are met:
6 * 1. Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 *
12 * THIS SOFTWARE IS PROVIDED BY FILIP PIZLO ``AS IS'' AND ANY
13 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
15 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FILIP PIZLO OR
16 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
17 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
19 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
20 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "tsf_internal.h"
26#include "tsf_define_helpers.h"
27#include <stdio.h>
28#include <stdlib.h>
29
30static void aoe_failure(void) {
31 fprintf(stderr, "Error defining TSF types: %s\n", tsf_get_error());
32 abort();
33}
34
35tsf_type_t *tsf_type_create_aoe(tsf_type_kind_t kind_code) {
36 tsf_type_t *result = tsf_type_create(kind_code);
37 if (result == NULL) {
38 aoe_failure();
39 }
40 return result;
41}
42
43tsf_type_t *tsf_type_create_array_aoe(tsf_type_t *ele_type) {
44 tsf_type_t *result = tsf_type_create_array(ele_type);
45 if (result == NULL) {
46 aoe_failure();
47 }
48 return result;
49}
50
51tsf_type_t *tsf_type_create_struct_with_native_size_aoe(size_t size) {
52 tsf_type_t *result = tsf_type_create(TSF_TK_STRUCT);
53 if (result == NULL) {
54 aoe_failure();
55 }
56 if (!tsf_native_struct_type_set_size(&result, size)) {
57 aoe_failure();
58 }
59 return result;
60}
61
62tsf_type_t *tsf_type_create_struct_with_native_size_and_destructor_aoe(size_t size, void (*dest)(void*)) {
63 tsf_type_t *result = tsf_type_create_struct_with_native_size_aoe(size);
64 if (!tsf_native_struct_type_set_destructor(&result, dest)) {
65 aoe_failure();
66 }
67 return result;
68}
69
70tsf_type_t *tsf_type_create_choice_with_native_map_aoe(
71 tsf_type_t **type, tsf_bool_t in_place, size_t value_offset, size_t data_offset, size_t size) {
72 tsf_type_t *result = tsf_type_create(TSF_TK_CHOICE);
73 if (result == NULL) {
74 aoe_failure();
75 }
76 if (!tsf_native_choice_type_map(&result, in_place, value_offset, data_offset, size)) {
77 aoe_failure();
78 }
79 return result;
80}
81
82void tsf_type_set_comment_aoe(tsf_type_t **type, const char *comment) {
83 if (!tsf_type_set_comment(type, comment)) {
84 aoe_failure();
85 }
86}
87
88void tsf_struct_type_append_with_native_map_aoe(
89 tsf_type_t **type, const char *name, tsf_type_t *ele_type, size_t offset) {
90 if (!tsf_struct_type_append(type, name, ele_type) ||
91 !tsf_native_struct_type_map(type, name, offset)) {
92 aoe_failure();
93 }
94}
95
96void tsf_struct_type_append_primitive_with_native_map_aoe(
97 tsf_type_t **type, const char *name, tsf_type_kind_t type_kind, size_t offset) {
98 tsf_struct_type_append_with_native_map_aoe(
99 type, name, tsf_type_create_aoe(type_kind), offset);
100}
101
102void tsf_struct_type_append_from_callback_and_dup_with_native_map_aoe(
103 tsf_type_t **type, const char *name, tsf_type_t *(*callback)(void), size_t offset) {
104 tsf_struct_type_append_with_native_map_aoe(
105 type, name, tsf_type_dup(callback()), offset);
106}
107
108void tsf_struct_type_append_with_native_map_with_comment_aoe(
109 tsf_type_t **type, const char *name, tsf_type_t *ele_type, size_t offset,
110 const char *comment) {
111 tsf_struct_type_append_with_native_map_aoe(type, name, ele_type, offset);
112 if (!tsf_struct_type_set_comment(type, name, comment)) {
113 aoe_failure();
114 }
115}
116
117void tsf_struct_type_append_primitive_with_native_map_with_comment_aoe(
118 tsf_type_t **type, const char *name, tsf_type_kind_t type_kind, size_t offset,
119 const char *comment) {
120 tsf_struct_type_append_with_native_map_with_comment_aoe(
121 type, name, tsf_type_create_aoe(type_kind), offset, comment);
122}
123
124void tsf_struct_type_append_from_callback_and_dup_with_native_map_with_comment_aoe(
125 tsf_type_t **type, const char *name, tsf_type_t *(*callback)(void), size_t offset,
126 const char *comment) {
127 tsf_struct_type_append_with_native_map_with_comment_aoe(
128 type, name, tsf_type_dup(callback()), offset, comment);
129}
130
131void tsf_choice_type_append_aoe(tsf_type_t **type, const char *name, tsf_type_t *ele_type) {
132 if (!tsf_choice_type_append(type, name, ele_type)) {
133 aoe_failure();
134 }
135}
136
137void tsf_choice_type_append_primitive_aoe(
138 tsf_type_t **type, const char *name, tsf_type_kind_t type_kind) {
139 tsf_choice_type_append_aoe(type, name, tsf_type_create_aoe(type_kind));
140}
141
142void tsf_choice_type_append_from_callback_and_dup_aoe(
143 tsf_type_t **type, const char *name, tsf_type_t *(*callback)(void)) {
144 tsf_choice_type_append_aoe(type, name, tsf_type_dup(callback()));
145}
146
147void tsf_choice_type_append_with_comment_aoe(
148 tsf_type_t **type, const char *name, tsf_type_t *ele_type, const char *comment) {
149 tsf_choice_type_append_aoe(type, name, ele_type);
150 if (!tsf_choice_type_set_comment(type, name, comment)) {
151 aoe_failure();
152 }
153}
154
155void tsf_choice_type_append_primitive_with_comment_aoe(
156 tsf_type_t **type, const char *name, tsf_type_kind_t type_kind, const char *comment) {
157 tsf_choice_type_append_with_comment_aoe(
158 type, name, tsf_type_create_aoe(type_kind), comment);
159}
160
161void tsf_choice_type_append_from_callback_and_dup_with_comment_aoe(
162 tsf_type_t **type, const char *name, tsf_type_t *(*callback)(void), const char *comment) {
163 tsf_choice_type_append_with_comment_aoe(
164 type, name, tsf_type_dup(callback()), comment);
165}
166
167void tsf_struct_type_set_optional_aoe(tsf_type_t **type, const char *name, tsf_bool_t optional) {
168 if (!tsf_struct_type_set_optional(type, name, optional)) {
169 aoe_failure();
170 }
171}
172
173void tsf_type_set_name_aoe(tsf_type_t **type, const char *name) {
174 if (!tsf_type_set_name(type, name)) {
175 aoe_failure();
176 }
177}
178
179void tsf_type_set_version_aoe(tsf_type_t **type, uint32_t major, uint32_t minor, uint32_t patch) {
180 if (!tsf_type_set_version(type, major, minor, patch)) {
181 aoe_failure();
182 }
183}
184
185tsf_bool_t tsf_typed_data_write(tsf_stream_file_output_t *out, tsf_genrtr_t *generator, void *data) {
186 tsf_buffer_t buf;
187 char inlineBuffer[256];
188 tsf_bool_t result;
189
190 tsf_buffer_initialize_custom(&buf, inlineBuffer, sizeof(inlineBuffer));
191
192 if (!tsf_generator_generate_existing_buffer(generator, data, &buf)) {
193 return tsf_false;
194 }
195
196 result = tsf_stream_file_output_write(out, &buf);
197 tsf_buffer_destroy_custom(&buf);
198 return result;
199}
200
201void *tsf_typed_data_read(tsf_stream_file_input_t *in, tsf_parser_t *parser) {
202 tsf_buffer_t buf;
203 char inlineBuffer[256];
204 void* result;
205
206 tsf_buffer_initialize_custom(&buf, inlineBuffer, sizeof(inlineBuffer));
207
208 if (!tsf_stream_file_input_read_existing_buffer(in, &buf)) {
209 return NULL;
210 }
211
212 result = tsf_parser_parse(parser, &buf);
213 tsf_buffer_destroy_custom(&buf);
214 return result;
215}
216
217tsf_bool_t tsf_typed_data_read_into(tsf_stream_file_input_t *in, tsf_parser_t *parser, void *result) {
218 tsf_buffer_t buf;
219 char inlineBuffer[256];
220 tsf_bool_t success;
221
222 tsf_buffer_initialize_custom(&buf, inlineBuffer, sizeof(inlineBuffer));
223
224 if (!tsf_stream_file_input_read_existing_buffer(in, &buf)) {
225 return tsf_false;
226 }
227
228 success = tsf_parser_parse_into(parser, &buf, result, NULL);
229 tsf_buffer_destroy_custom(&buf);
230 return success;
231}
232