Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_yaml.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * AFW YAML Miscellaneous Functions
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
14 #include "afw.h"
15 #include "afw_yaml.h"
16 #include "afw_content_type_impl.h"
18 
19 
20 /* Declares and rti/inf defines for interface afw_extension */
21 #define AFW_IMPLEMENTATION_ID "afw_yaml"
22 #define AFW_IMPLEMENTATION_VERSION AFW_YAML_VERSION_STRING
25 
26 
27 /* Media types supported by this afw_content_type implementation. */
28 static const afw_utf8_t impl_media_types[] = {
29  AFW_UTF8_LITERAL(AFW_YAML_Q_CONTENT_TYPE)
30 };
31 
32 /* Raw begin object list. */
33 static const afw_memory_t impl_raw_begin_object_list = {
34  (const afw_byte_t *)"[\n",
35  sizeof("[\n") - 1
36 };
37 
38 /* Raw object separator. */
39 static const afw_memory_t impl_raw_object_separator = {
40  (const afw_byte_t *)",\n",
41  sizeof(",\n") - 1
42 };
43 
44 /* Raw last object separator. */
45 static const afw_memory_t impl_raw_last_object_separator = {
46  (const afw_byte_t *)"\n",
47  sizeof("\n") - 1
48 };
49 
50 /* Raw end object list. */
51 static const afw_memory_t impl_raw_end_object_list = {
52  (const afw_byte_t *)"]\n",
53  sizeof("[\n") - 1
54 };
55 
56 /* Content type singleton instance for this implementation. */
57 static const afw_content_type_t impl_afw_content_type =
58 {
59  &impl_afw_content_type_inf,
60  AFW_UTF8_LITERAL("yaml"),
61  sizeof(impl_media_types) / sizeof(afw_utf8_t),
62  &impl_media_types[0]
63 };
64 
65 
66 /* Convert a value to yaml and write it. Defined in afw_yaml_from_value. */
67 extern void afw_yaml_internal_write_value(
68  const afw_value_t *value,
69  const afw_object_options_t *options,
70  void * context,
71  afw_write_cb_t callback,
72  const afw_pool_t *p, afw_xctx_t *xctx);
73 
74 
83 const afw_content_type_t *
85 {
86  return &impl_afw_content_type;
87 }
88 
89 
90 
91 /* Register xml support. */
93 {
95 }
96 
97 
98 /* Define extension implementation. */
100 
101 
102 /*
103  * Implementation of method initialize for interface afw_extension.
104  */
105 const afw_extension_t *
106 impl_afw_extension_initialize(
107  const afw_extension_t * instance,
108  const afw_object_t * properties,
109  const afw_pool_t * p,
110  afw_xctx_t *xctx)
111 {
112  /*
113  * IMPORTANT:
114  *
115  * Make sure associated _AdaptiveManifest_ is up to date,
116  * especially the "registers" property.
117  *
118  */
119 
120  /* Call the register function for this extension. */
121  afw_yaml_register(xctx);
122 
123  /* Return extension instance. */
124  return &impl_extension;
125 }
126 
127 
128 /*
129  * Implementation of method release of interface afw_extension.
130  */
131 void
132 impl_afw_extension_release(
133  const afw_extension_t * instance,
134  afw_xctx_t *xctx)
135 {
136 }
137 
138 static const afw_utf8_z_t * impl_u8z_to_yaml(
139  const afw_utf8_z_t *s, afw_xctx_t *xctx)
140 {
141  const afw_utf8_z_t *c;
142  apr_array_header_t *a;
143  char *u;
144 
145  if (!s) return "null";
146 
147  a = apr_array_make(afw_pool_get_apr_pool(xctx->p), 128, sizeof(char));
148 
149  /* Add characters to array with proper escaping. */
150  for (c = s; *c; c++) {
151 
152  /* If control character, use \u00xx escape. */
153  if (*c < 32) {
154  u = apr_psprintf(afw_pool_get_apr_pool(xctx->p), "\\u%02x", *c);
155  while (*u) {
156  APR_ARRAY_PUSH(a, char) = *u;
157  u++;
158  }
159  }
160 
161  /*
162  * If not control char, add char to array.
163  */
164  else {
165  APR_ARRAY_PUSH(a, char) = *c;
166  }
167 
168  }
169 
170  /* Add starting quote and zero terminate. */
171  APR_ARRAY_PUSH(a, char) = 0;
172 
173  return (const afw_utf8_z_t *)a->elts;
174 }
175 
177 {
178  afw_error_t *error = xctx->error;
179 
180  return afw_utf8_printf(xctx->p, xctx,
181  "\"status\": \"error\",\n"
182  "\"errorCode\": %d,\n"
183  "\"errorCodeId\": %s,\n"
184  "\"sourceFile\": %s,\n"
185  "\"function\": %s,\n"
186  "\"lineNumber\": %d,\n"
187  "\"rvSourceId\": %s,\n"
188  "\"rv\": %d,\n"
189  "\"rvDecoded\": %s,\n"
190  "\"message\": %s\n",
191  error->code,
192  afw_error_code_id_z(error),
193  impl_u8z_to_yaml(afw_error_source_file(error), xctx),
194  error->source_z,
195  impl_u8z_to_yaml(error->rv_source_id_z, xctx),
196  error->rv,
197  impl_u8z_to_yaml(error->rv_decoded_z, xctx),
198  impl_u8z_to_yaml(error->message_z, xctx)
199  );
200 }
201 
202 
203 
204 /*
205  * Implementation of method to_value_p of interface afw_content_type.
206  */
207 const afw_value_t *
208 impl_afw_content_type_raw_to_value(
209  const afw_content_type_t * instance,
210  const afw_memory_t * raw,
211  const afw_utf8_t * source_location,
212  const afw_pool_t * p,
213  afw_xctx_t *xctx)
214 {
215  return afw_yaml_to_value(raw, NULL, p, xctx);
216 }
217 
218 
219 
220 /*
221  * Implementation of method raw_to_object of interface afw_content_type.
222  */
223 const afw_object_t *
224 impl_afw_content_type_raw_to_object (
225  const afw_content_type_t * instance,
226  const afw_memory_t * raw,
227  const afw_utf8_t * source_location,
228  const afw_utf8_t * adaptor_id,
229  const afw_utf8_t * object_type_id,
230  const afw_utf8_t * object_id,
231  afw_boolean_t cede_p,
232  const afw_pool_t * p,
233  afw_xctx_t *xctx)
234 {
235  return afw_yaml_to_object(raw, source_location,
236  adaptor_id, object_type_id, object_id, cede_p, p, xctx);
237 }
238 
239 
240 
241 /*
242  * Implementation of method write_value of interface afw_content_type.
243  */
244 void
245 impl_afw_content_type_write_value(
246  const afw_content_type_t * instance,
247  const afw_value_t * value,
248  const afw_object_options_t *options,
249  void * context,
250  afw_write_cb_t callback,
251  const afw_pool_t *p, afw_xctx_t *xctx)
252 {
253  afw_yaml_internal_write_value(value, options, context, callback,
254  p, xctx);
255 }
256 
257 
258 
259 /*
260  * Implementation of method create_object_list_writer of interface afw_content_type.
261  */
263 impl_afw_content_type_create_object_list_writer(
264  const afw_content_type_t * instance,
265  const afw_object_options_t *options,
266  void * context,
267  afw_write_cb_t callback,
268  const afw_pool_t *p, afw_xctx_t *xctx)
269 {
270  return afw_content_type_impl_create_object_list_writer(
271  instance, options, context, callback,
272  &impl_raw_begin_object_list,
273  &impl_raw_object_separator,
274  &impl_raw_last_object_separator,
275  &impl_raw_end_object_list,
276  p, xctx);
277 }
Adaptive Framework Core API.
Helpers for afw_content_type* implementation development.
Interface afw_interface implementation declares.
Interface afw_interface implementation declares.
const afw_content_type_t * afw_yaml_content_type_get()
Get the content type instance for FIXME.
Definition: afw_yaml.c:84
Header file for Adaptive Framework YAML.
Adaptive Framework Version (afw_yaml_) header.
#define AFW_UTF8_LITERAL(A_STRING)
String literal initializer.
Definition: afw_common.h:582
_Bool afw_boolean_t
Definition: afw_common.h:373
unsigned char afw_byte_t
A byte of memory (unsigned).
Definition: afw_common.h:208
afw_utf8_octet_t afw_utf8_z_t
NFC normalized UTF-8 null terminated string.
Definition: afw_common.h:523
afw_size_t(* afw_write_cb_t)(void *context, const void *buffer, afw_size_t size, const afw_pool_t *p, afw_xctx_t *xctx)
Typedef for write callback function.
Definition: afw_common.h:1226
afw_content_type_register(const afw_content_type_t *content_type, afw_xctx_t *xctx)
Register content type with its id and all of its media types.
#define AFW_ENVIRONMENT_DEFINE_EXTENSION_IMPL()
Macro to put in each afw_extension implementation.
afw_error_code_id_z(const afw_error_t *error)
Returns error->code id.
Definition: afw_error.c:1062
#define afw_error_source_file(error)
Returns value of error->source_z after last '/ 'or '\'.
Definition: afw_error.h:879
#define afw_pool_get_apr_pool(instance)
Call method get_apr_pool of interface afw_pool.
afw_utf8_printf(const afw_pool_t *p, afw_xctx_t *xctx, const afw_utf8_z_t *format,...)
Create a utf-8 string using a c format string in specified pool.
Definition: afw_utf8.c:459
void afw_yaml_register(afw_xctx_t *xctx)
Register YAML support.
Definition: afw_yaml.c:92
const afw_value_t * afw_yaml_to_value(const afw_memory_t *yaml, const afw_utf8_t *path, const afw_pool_t *p, afw_xctx_t *xctx)
Convert YAML to an adaptive value.
const afw_utf8_t * afw_yaml_from_error(afw_xctx_t *xctx)
Convert error from xctx to a YAML object.
Definition: afw_yaml.c:176
const afw_object_t * afw_yaml_to_object(const afw_memory_t *yaml, const afw_utf8_t *source_location, const afw_utf8_t *adaptor_id, const afw_utf8_t *object_type_id, const afw_utf8_t *object_id, afw_boolean_t cede_p, const afw_pool_t *p, afw_xctx_t *xctx)
Convert from YAML to adaptive object.
Interface afw_content_type_object_list_writer public struct.
Interface afw_content_type public struct.
Adaptive Framework Error.
Definition: afw_error.h:65
afw_error_code_t code
Error code.
Definition: afw_error.h:74
const afw_utf8_z_t * rv_source_id_z
This is the source of the non-zero rv.
Definition: afw_error.h:86
const afw_utf8_z_t * message_z
Message.
Definition: afw_error.h:80
int rv
If non-zero, this is rc, rv, or any int value related to error.
Definition: afw_error.h:112
const afw_utf8_z_t * source_z
File:line in source error was thrown.
Definition: afw_error.h:77
const afw_utf8_z_t * rv_decoded_z
Human readable decode of rv.
Definition: afw_error.h:89
Interface afw_extension public struct.
Definition: afw_interface.h:55
Struct for memory pointer and size.
Definition: afw_common.h:505
Struct for object processing options.
Interface afw_object public struct.
Interface afw_pool public struct.
NFC normalized UTF-8 string.
Definition: afw_common.h:545
Interface afw_value public struct.
Interface afw_xctx public struct.