Debian Codegraph @

c/include/proton/codec.h

raw
1 #ifndef PROTON_CODEC_H
2 #define PROTON_CODEC_H 1
3
4 /*
5 *
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements. See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership. The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License. You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied. See the License for the
20 * specific language governing permissions and limitations
21 * under the License.
22 *
23 */
24
25 #include <proton/import_export.h>
26 #include <proton/object.h>
27 #include <proton/types.h>
28 #include <proton/error.h>
29 #include <proton/type_compat.h>
30 #include <stdarg.h>
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 /**
37 * @file
38 *
39 * @copybrief codec
40 */
41
42 /**
43 * Identifies an AMQP type.
44 *
45 * @ingroup amqp_types
46 */
47 typedef enum {
48 /**
49 * The NULL AMQP type.
50 */
51 PN_NULL = 1,
52
53 /**
54 * The boolean AMQP type.
55 */
56 PN_BOOL = 2,
57
58 /**
59 * The unsigned byte AMQP type. An 8 bit unsigned integer.
60 */
61 PN_UBYTE = 3,
62
63 /**
64 * The byte AMQP type. An 8 bit signed integer.
65 */
66 PN_BYTE = 4,
67
68 /**
69 * The unsigned short AMQP type. A 16 bit unsigned integer.
70 */
71 PN_USHORT = 5,
72
73 /**
74 * The short AMQP type. A 16 bit signed integer.
75 */
76 PN_SHORT = 6,
77
78 /**
79 * The unsigned int AMQP type. A 32 bit unsigned integer.
80 */
81 PN_UINT = 7,
82
83 /**
84 * The signed int AMQP type. A 32 bit signed integer.
85 */
86 PN_INT = 8,
87
88 /**
89 * The char AMQP type. A 32 bit unicode character.
90 */
91 PN_CHAR = 9,
92
93 /**
94 * The ulong AMQP type. An unsigned 64 bit integer.
95 */
96 PN_ULONG = 10,
97
98 /**
99 * The long AMQP type. A signed 64 bit integer.
100 */
101 PN_LONG = 11,
102
103 /**
104 * The timestamp AMQP type. A signed 64 bit value measuring
105 * milliseconds since the epoch.
106 */
107 PN_TIMESTAMP = 12,
108
109 /**
110 * The float AMQP type. A 32 bit floating point value.
111 */
112 PN_FLOAT = 13,
113
114 /**
115 * The double AMQP type. A 64 bit floating point value.
116 */
117 PN_DOUBLE = 14,
118
119 /**
120 * The decimal32 AMQP type. A 32 bit decimal floating point value.
121 */
122 PN_DECIMAL32 = 15,
123
124 /**
125 * The decimal64 AMQP type. A 64 bit decimal floating point value.
126 */
127 PN_DECIMAL64 = 16,
128
129 /**
130 * The decimal128 AMQP type. A 128 bit decimal floating point value.
131 */
132 PN_DECIMAL128 = 17,
133
134 /**
135 * The UUID AMQP type. A 16 byte UUID.
136 */
137 PN_UUID = 18,
138
139 /**
140 * The binary AMQP type. A variable length sequence of bytes.
141 */
142 PN_BINARY = 19,
143
144 /**
145 * The string AMQP type. A variable length sequence of unicode
146 * characters.
147 */
148 PN_STRING = 20,
149
150 /**
151 * The symbol AMQP type. A variable length sequence of unicode
152 * characters.
153 */
154 PN_SYMBOL = 21,
155
156 /**
157 * A described AMQP type.
158 */
159 PN_DESCRIBED = 22,
160
161 /**
162 * An AMQP array. A monomorphic sequence of other AMQP values.
163 */
164 PN_ARRAY = 23,
165
166 /**
167 * An AMQP list. A polymorphic sequence of other AMQP values.
168 */
169 PN_LIST = 24,
170
171 /**
172 * An AMQP map. A polymorphic container of other AMQP values formed
173 * into key/value pairs.
174 */
175 PN_MAP = 25,
176
177 /**
178 * A special invalid type value that is returned when no valid type
179 * is available.
180 */
181 PN_INVALID = -1
182 } pn_type_t;
183
184 /**
185 * Return a string name for an AMQP type.
186 *
187 * @ingroup amqp_types
188 * @param type an AMQP type
189 * @return the string name of the given type
190 */
191 PN_EXTERN const char *pn_type_name(pn_type_t type);
192
193 /**
194 * A discriminated union that holds any scalar AMQP value. The type
195 * field indicates the AMQP type of the value, and the union may be
196 * used to access the value for a given type.
197 *
198 * @ingroup api_types
199 */
200 typedef struct {
201 /**
202 * Indicates the type of value the atom is currently pointing to.
203 * See ::pn_type_t for details on AMQP types.
204 */
205 pn_type_t type;
206 union {
207 /**
208 * Valid when type is ::PN_BOOL.
209 */
210 bool as_bool;
211
212 /**
213 * Valid when type is ::PN_UBYTE.
214 */
215 uint8_t as_ubyte;
216
217 /**
218 * Valid when type is ::PN_BYTE.
219 */
220 int8_t as_byte;
221
222 /**
223 * Valid when type is ::PN_USHORT.
224 */
225 uint16_t as_ushort;
226
227 /**
228 * Valid when type is ::PN_SHORT.
229 */
230 int16_t as_short;
231
232 /**
233 * Valid when type is ::PN_UINT.
234 */
235 uint32_t as_uint;
236
237 /**
238 * Valid when type is ::PN_INT.
239 */
240 int32_t as_int;
241
242 /**
243 * Valid when type is ::PN_CHAR.
244 */
245 pn_char_t as_char;
246
247 /**
248 * Valid when type is ::PN_ULONG.
249 */
250 uint64_t as_ulong;
251
252 /**
253 * Valid when type is ::PN_LONG.
254 */
255 int64_t as_long;
256
257 /**
258 * Valid when type is ::PN_TIMESTAMP.
259 */
260 pn_timestamp_t as_timestamp;
261
262 /**
263 * Valid when type is ::PN_FLOAT.
264 */
265 float as_float;
266
267 /**
268 * Valid when type is ::PN_DOUBLE.
269 */
270 double as_double;
271
272 /**
273 * Valid when type is ::PN_DECIMAL32.
274 */
275 pn_decimal32_t as_decimal32;
276
277 /**
278 * Valid when type is ::PN_DECIMAL64.
279 */
280 pn_decimal64_t as_decimal64;
281
282 /**
283 * Valid when type is ::PN_DECIMAL128.
284 */
285 pn_decimal128_t as_decimal128;
286
287 /**
288 * Valid when type is ::PN_UUID.
289 */
290 pn_uuid_t as_uuid;
291
292 /**
293 * Valid when type is ::PN_BINARY or ::PN_STRING or ::PN_SYMBOL.
294 * When the type is ::PN_STRING the field will point to utf8
295 * encoded unicode. When the type is ::PN_SYMBOL, the field will
296 * point to 7-bit ASCII. In the latter two cases, the bytes
297 * pointed to are *not* necessarily null terminated.
298 */
299 pn_bytes_t as_bytes;
300 } u;
301 } pn_atom_t;
302
303 /**
304 * A type that holds scalar AMQP values that are valid for
305 * message id and correlation id fields.
306 *
307 * This is actually another name for ::pn_atom_t as it is just a
308 * restricted version of that type.
309 *
310 * @ingroup api_types
311 */
312 typedef pn_atom_t pn_msgid_t;
313
314 /**
315 * @addtogroup data
316 * @{
317 */
318
319 /**
320 * An AMQP Data object.
321 *
322 * A pn_data_t object provides an interface for decoding, extracting,
323 * creating, and encoding arbitrary AMQP data. A pn_data_t object
324 * contains a tree of AMQP values. Leaf nodes in this tree correspond
325 * to scalars in the AMQP type system such as @link ::PN_INT ints
326 * @endlink or @link ::PN_STRING strings @endlink. Non-leaf nodes in
327 * this tree correspond to compound values in the AMQP type system
328 * such as @link ::PN_LIST lists @endlink, @link ::PN_MAP maps
329 * @endlink, @link ::PN_ARRAY arrays @endlink, or @link ::PN_DESCRIBED
330 * described @endlink values. The root node of the tree is the
331 * pn_data_t object itself and can have an arbitrary number of
332 * children.
333 *
334 * A pn_data_t object maintains the notion of the current node and the
335 * current parent node. Siblings are ordered within their parent.
336 * Values are accessed and/or added by using the ::pn_data_next(),
337 * ::pn_data_prev(), ::pn_data_enter(), and ::pn_data_exit()
338 * operations to navigate to the desired location in the tree and
339 * using the supplied variety of pn_data_put_* / pn_data_get_*
340 * operations to access or add a value of the desired type.
341 *
342 * The pn_data_put_* operations will always add a value _after_ the
343 * current node in the tree. If the current node has a next sibling
344 * the pn_data_put_* operations will overwrite the value on this node.
345 * If there is no current node or the current node has no next sibling
346 * then one will be added. The pn_data_put_* operations always set the
347 * added/modified node to the current node. The pn_data_get_*
348 * operations read the value of the current node and do not change
349 * which node is current.
350 *
351 * The following types of scalar values are supported:
352 *
353 * - ::PN_NULL
354 * - ::PN_BOOL
355 * - ::PN_UBYTE
356 * - ::PN_USHORT
357 * - ::PN_SHORT
358 * - ::PN_UINT
359 * - ::PN_INT
360 * - ::PN_ULONG
361 * - ::PN_LONG
362 * - ::PN_FLOAT
363 * - ::PN_DOUBLE
364 * - ::PN_BINARY
365 * - ::PN_STRING
366 * - ::PN_SYMBOL
367 *
368 * The following types of compound values are supported:
369 *
370 * - ::PN_DESCRIBED
371 * - ::PN_ARRAY
372 * - ::PN_LIST
373 * - ::PN_MAP
374 */
375 typedef struct pn_data_t pn_data_t;
376
377 /**
378 * Construct a pn_data_t object with the supplied initial capacity. A
379 * pn_data_t will grow automatically as needed, so an initial capacity
380 * of 0 is permitted.
381 *
382 * @param capacity the initial capacity
383 * @return the newly constructed pn_data_t
384 */
385 PN_EXTERN pn_data_t *pn_data(size_t capacity);
386
387 /**
388 * Free a pn_data_t object.
389 *
390 * @param data a pn_data_t object or NULL
391 */
392 PN_EXTERN void pn_data_free(pn_data_t *data);
393
394 /**
395 * Access the current error code for a given pn_data_t.
396 *
397 * @param data a pn_data_t object
398 * @return the current error code
399 */
400 PN_EXTERN int pn_data_errno(pn_data_t *data);
401
402 /**
403 * Access the current error for a given pn_data_t.
404 *
405 * Every pn_data_t has an error descriptor that is created with the
406 * pn_data_t and dies with the pn_data_t. The error descriptor is
407 * updated whenever an operation fails. The ::pn_data_error() function
408 * may be used to access a pn_data_t's error descriptor.
409 *
410 * @param data a pn_data_t object
411 * @return a pointer to the pn_data_t's error descriptor
412 */
413 PN_EXTERN pn_error_t *pn_data_error(pn_data_t *data);
414
415 /**
416 * @cond INTERNAL
417 */
418 PN_EXTERN int pn_data_vfill(pn_data_t *data, const char *fmt, va_list ap);
419 PN_EXTERN int pn_data_fill(pn_data_t *data, const char *fmt, ...);
420 PN_EXTERN int pn_data_vscan(pn_data_t *data, const char *fmt, va_list ap);
421 PN_EXTERN int pn_data_scan(pn_data_t *data, const char *fmt, ...);
422 /**
423 * @endcond
424 */
425
426 /**
427 * Clears a pn_data_t object.
428 *
429 * A cleared pn_data_t object is equivalent to a newly constructed
430 * one.
431 *
432 * @param data the pn_data_t object to clear
433 */
434 PN_EXTERN void pn_data_clear(pn_data_t *data);
435
436 /**
437 * Returns the total number of nodes contained in a pn_data_t object.
438 * This includes all parents, children, siblings, grandchildren, etc.
439 * In other words the count of all ancestors and descendants of the
440 * current node, along with the current node if there is one.
441 *
442 * @param data a pn_data_t object
443 * @return the total number of nodes in the pn_data_t object
444 */
445 PN_EXTERN size_t pn_data_size(pn_data_t *data);
446
447 /**
448 * Clears current node pointer and sets the parent to the root node.
449 * Clearing the current node sets it _before_ the first node, calling
450 * ::pn_data_next() will advance to the first node.
451 */
452 PN_EXTERN void pn_data_rewind(pn_data_t *data);
453
454 /**
455 * Advances the current node to its next sibling and returns true. If
456 * there is no next sibling the current node remains unchanged and
457 * false is returned.
458 *
459 * @param data a pn_data_t object
460 * @return true iff the current node was changed
461 */
462 PN_EXTERN bool pn_data_next(pn_data_t *data);
463
464 /**
465 * Moves the current node to its previous sibling and returns true. If
466 * there is no previous sibling the current node remains unchanged and
467 * false is returned.
468 *
469 * @param data a pn_data_t object
470 * @return true iff the current node was changed
471 */
472 PN_EXTERN bool pn_data_prev(pn_data_t *data);
473
474 /**
475 * Sets the parent node to the current node and clears the current
476 * node. Clearing the current node sets it _before_ the first child,
477 * calling ::pn_data_next() advances to the first child. This
478 * operation will return false if there is no current node or if the
479 * current node is not a compound type.
480 *
481 * @param data a pn_data_object
482 * @return true iff the pointers to the current/parent nodes are changed
483 */
484 PN_EXTERN bool pn_data_enter(pn_data_t *data);
485
486 /**
487 * Sets the current node to the parent node and the parent node to its
488 * own parent. This operation will return false if there is no current
489 * node or parent node.
490 *
491 * @param data a pn_data object
492 * @return true iff the pointers to the current/parent nodes are
493 * changed
494 */
495 PN_EXTERN bool pn_data_exit(pn_data_t *data);
496
497 /**
498 * @cond INTERNAL
499 */
500 PN_EXTERN bool pn_data_lookup(pn_data_t *data, const char *name);
501 /**
502 * @endcond
503 */
504
505 /**
506 * Access the type of the current node. Returns PN_INVALID if there is no
507 * current node.
508 *
509 * @param data a data object
510 * @return the type of the current node
511 */
512 PN_EXTERN pn_type_t pn_data_type(pn_data_t *data);
513
514 /**
515 * Prints the contents of a pn_data_t object using ::pn_data_format()
516 * to stdout.
517 *
518 * @param data a pn_data_t object
519 * @return zero on success or an error on failure
520 */
521 PN_EXTERN int pn_data_print(pn_data_t *data);
522
523 /**
524 * Formats the contents of a pn_data_t object in a human readable way
525 * and writes them to the indicated location. The size pointer must
526 * hold the amount of free space following the bytes pointer, and upon
527 * success will be updated to indicate how much space has been used.
528 *
529 * @param data a pn_data_t object
530 * @param bytes a buffer to write the output to
531 * @param size a pointer to the size of the buffer
532 * @return zero on success, or an error on failure
533 */
534 PN_EXTERN int pn_data_format(pn_data_t *data, char *bytes, size_t *size);
535
536 /**
537 * Writes the contents of a data object to the given buffer as an AMQP
538 * data stream.
539 *
540 * @param data the data object to encode
541 * @param bytes the buffer for encoded data
542 * @param size the size of the buffer
543 *
544 * @return the size of the encoded data on success or an error code on failure
545 */
546 PN_EXTERN ssize_t pn_data_encode(pn_data_t *data, char *bytes, size_t size);
547
548 /**
549 * Returns the number of bytes needed to encode a data object.
550 *
551 * @param data the data object
552 *
553 * @return the size of the encoded data or an error code if data is invalid.
554 */
555 PN_EXTERN ssize_t pn_data_encoded_size(pn_data_t *data);
556
557 /**
558 * Decodes a single value from the contents of the AMQP data stream
559 * into the current data object. Note that if the pn_data_t object is
560 * pointing to a current node, the decoded value will overwrite the
561 * current one. If the pn_data_t object has no current node then a
562 * node will be appended to the current parent. If there is no current
563 * parent then a node will be appended to the pn_data_t itself.
564 *
565 * Upon success, this operation returns the number of bytes consumed
566 * from the AMQP data stream. Upon failure, this operation returns an
567 * error code.
568 *
569 * @param data a pn_data_t object
570 * @param bytes a pointer to an encoded AMQP data stream
571 * @param size the size of the encoded AMQP data stream
572 * @return the number of bytes consumed from the AMQP data stream or an error code
573 */
574 PN_EXTERN ssize_t pn_data_decode(pn_data_t *data, const char *bytes, size_t size);
575
576 /**
577 * Puts an empty list value into a pn_data_t. Elements may be filled
578 * by entering the list node using ::pn_data_enter() and using
579 * pn_data_put_* to add the desired contents. Once done,
580 * ::pn_data_exit() may be used to return to the current level in the
581 * tree and put more values.
582 *
583 * @code
584 * pn_data_t *data = pn_data(0);
585 * ...
586 * pn_data_put_list(data);
587 * pn_data_enter(data);
588 * pn_data_put_int(data, 1);
589 * pn_data_put_int(data, 2);
590 * pn_data_put_int(data, 3);
591 * pn_data_exit(data);
592 * ...
593 * @endcode
594 *
595 * @param data a pn_data_t object
596 * @return zero on success or an error code on failure
597 */
598 PN_EXTERN int pn_data_put_list(pn_data_t *data);
599
600 /**
601 * Puts an empty map value into a pn_data_t. Elements may be filled by
602 * entering the map node and putting alternating key value pairs.
603 *
604 * @code
605 * pn_data_t *data = pn_data(0);
606 * ...
607 * pn_data_put_map(data);
608 * pn_data_enter(data);
609 * pn_data_put_string(data, pn_bytes(3, "key"));
610 * pn_data_put_string(data, pn_bytes(5, "value"));
611 * pn_data_exit(data);
612 * ...
613 * @endcode
614 *
615 * @param data a pn_data_t object
616 * @return zero on success or an error code on failure
617 */
618 PN_EXTERN int pn_data_put_map(pn_data_t *data);
619
620 /**
621 * Puts an empty array value into a pn_data_t. Elements may be filled
622 * by entering the array node and putting the element values. The
623 * values must all be of the specified array element type. If an array
624 * is described then the first child value of the array is the
625 * descriptor and may be of any type.
626 *
627 * @code
628 * pn_data_t *data = pn_data(0);
629 * ...
630 * pn_data_put_array(data, false, PN_INT);
631 * pn_data_enter(data);
632 * pn_data_put_int(data, 1);
633 * pn_data_put_int(data, 2);
634 * pn_data_put_int(data, 3);
635 * pn_data_exit(data);
636 * ...
637 * pn_data_put_array(data, True, Data.DOUBLE);
638 * pn_data_enter(data);
639 * pn_data_put_symbol(data, "array-descriptor");
640 * pn_data_put_double(data, 1.1);
641 * pn_data_put_double(data, 1.2);
642 * pn_data_put_double(data, 1.3);
643 * pn_data_exit(data);
644 * ...
645 * @endcode
646 *
647 * @param data a pn_data_t object
648 * @param described specifies whether the array is described
649 * @param type the type of the array
650 *
651 * @return zero on success or an error code on failure
652 */
653 PN_EXTERN int pn_data_put_array(pn_data_t *data, bool described, pn_type_t type);
654
655 /**
656 * Puts a described value into a pn_data_t object. A described node
657 * has two children, the descriptor and the value. These are specified
658 * by entering the node and putting the desired values.
659 *
660 * @code
661 * pn_data_t *data = pn_data(0);
662 * ...
663 * pn_data_put_described(data);
664 * pn_data_enter(data);
665 * pn_data_put_symbol(data, pn_bytes(16, "value-descriptor"));
666 * pn_data_put_string(data, pn_bytes(9, "the value"));
667 * pn_data_exit(data);
668 * ...
669 * @endcode
670 *
671 * @param data a pn_data_t object
672 * @return zero on success or an error code on failure
673 */
674 PN_EXTERN int pn_data_put_described(pn_data_t *data);
675
676 /**
677 * Puts a ::PN_NULL value.
678 *
679 * @param data a pn_data_t object
680 * @return zero on success or an error code on failure
681 */
682 PN_EXTERN int pn_data_put_null(pn_data_t *data);
683
684 /**
685 * Puts a ::PN_BOOL value.
686 *
687 * @param data a pn_data_t object
688 * @param b the value
689 * @return zero on success or an error code on failure
690 */
691 PN_EXTERN int pn_data_put_bool(pn_data_t *data, bool b);
692
693 /**
694 * Puts a ::PN_UBYTE value.
695 *
696 * @param data a pn_data_t object
697 * @param ub the value
698 * @return zero on success or an error code on failure
699 */
700 PN_EXTERN int pn_data_put_ubyte(pn_data_t *data, uint8_t ub);
701
702 /**
703 * Puts a ::PN_BYTE value.
704 *
705 * @param data a pn_data_t object
706 * @param b the value
707 * @return zero on success or an error code on failure
708 */
709 PN_EXTERN int pn_data_put_byte(pn_data_t *data, int8_t b);
710
711 /**
712 * Puts a ::PN_USHORT value.
713 *
714 * @param data a pn_data_t object
715 * @param us the value
716 * @return zero on success or an error code on failure
717 */
718 PN_EXTERN int pn_data_put_ushort(pn_data_t *data, uint16_t us);
719
720 /**
721 * Puts a ::PN_SHORT value.
722 *
723 * @param data a pn_data_t object
724 * @param s the value
725 * @return zero on success or an error code on failure
726 */
727 PN_EXTERN int pn_data_put_short(pn_data_t *data, int16_t s);
728
729 /**
730 * Puts a ::PN_UINT value.
731 *
732 * @param data a pn_data_t object
733 * @param ui the value
734 * @return zero on success or an error code on failure
735 */
736 PN_EXTERN int pn_data_put_uint(pn_data_t *data, uint32_t ui);
737
738 /**
739 * Puts a ::PN_INT value.
740 *
741 * @param data a pn_data_t object
742 * @param i the value
743 * @return zero on success or an error code on failure
744 */
745 PN_EXTERN int pn_data_put_int(pn_data_t *data, int32_t i);
746
747 /**
748 * Puts a ::PN_CHAR value.
749 *
750 * @param data a pn_data_t object
751 * @param c the value
752 * @return zero on success or an error code on failure
753 */
754 PN_EXTERN int pn_data_put_char(pn_data_t *data, pn_char_t c);
755
756 /**
757 * Puts a ::PN_ULONG value.
758 *
759 * @param data a pn_data_t object
760 * @param ul the value
761 * @return zero on success or an error code on failure
762 */
763 PN_EXTERN int pn_data_put_ulong(pn_data_t *data, uint64_t ul);
764
765 /**
766 * Puts a ::PN_LONG value.
767 *
768 * @param data a pn_data_t object
769 * @param l the value
770 * @return zero on success or an error code on failure
771 */
772 PN_EXTERN int pn_data_put_long(pn_data_t *data, int64_t l);
773
774 /**
775 * Puts a ::PN_TIMESTAMP value.
776 *
777 * @param data a pn_data_t object
778 * @param t the value
779 * @return zero on success or an error code on failure
780 */
781 PN_EXTERN int pn_data_put_timestamp(pn_data_t *data, pn_timestamp_t t);
782
783 /**
784 * Puts a ::PN_FLOAT value.
785 *
786 * @param data a pn_data_t object
787 * @param f the value
788 * @return zero on success or an error code on failure
789 */
790 PN_EXTERN int pn_data_put_float(pn_data_t *data, float f);
791
792 /**
793 * Puts a ::PN_DOUBLE value.
794 *
795 * @param data a pn_data_t object
796 * @param d the value
797 * @return zero on success or an error code on failure
798 */
799 PN_EXTERN int pn_data_put_double(pn_data_t *data, double d);
800
801 /**
802 * Puts a ::PN_DECIMAL32 value.
803 *
804 * @param data a pn_data_t object
805 * @param d the value
806 * @return zero on success or an error code on failure
807 */
808 PN_EXTERN int pn_data_put_decimal32(pn_data_t *data, pn_decimal32_t d);
809
810 /**
811 * Puts a ::PN_DECIMAL64 value.
812 *
813 * @param data a pn_data_t object
814 * @param d the value
815 * @return zero on success or an error code on failure
816 */
817 PN_EXTERN int pn_data_put_decimal64(pn_data_t *data, pn_decimal64_t d);
818
819 /**
820 * Puts a ::PN_DECIMAL128 value.
821 *
822 * @param data a pn_data_t object
823 * @param d the value
824 * @return zero on success or an error code on failure
825 */
826 PN_EXTERN int pn_data_put_decimal128(pn_data_t *data, pn_decimal128_t d);
827
828 /**
829 * Puts a ::PN_UUID value.
830 *
831 * @param data a pn_data_t object
832 * @param u the value
833 * @return zero on success or an error code on failure
834 */
835 PN_EXTERN int pn_data_put_uuid(pn_data_t *data, pn_uuid_t u);
836
837 /**
838 * Puts a ::PN_BINARY value. The bytes referenced by the pn_bytes_t
839 * argument are copied and stored inside the pn_data_t object.
840 *
841 * @param data a pn_data_t object
842 * @param bytes the value
843 * @return zero on success or an error code on failure
844 */
845 PN_EXTERN int pn_data_put_binary(pn_data_t *data, pn_bytes_t bytes);
846
847 /**
848 * Puts a ::PN_STRING value. The bytes referenced by the pn_bytes_t
849 * argument are copied and stored inside the pn_data_t object.
850 *
851 * @param data a pn_data_t object
852 * @param string utf8 encoded unicode
853 * @return zero on success or an error code on failure
854 */
855 PN_EXTERN int pn_data_put_string(pn_data_t *data, pn_bytes_t string);
856
857 /**
858 * Puts a ::PN_SYMBOL value. The bytes referenced by the pn_bytes_t
859 * argument are copied and stored inside the pn_data_t object.
860 *
861 * @param data a pn_data_t object
862 * @param symbol ascii encoded symbol
863 * @return zero on success or an error code on failure
864 */
865 PN_EXTERN int pn_data_put_symbol(pn_data_t *data, pn_bytes_t symbol);
866
867 /**
868 * Puts any scalar value value.
869 *
870 * @param data a pn_data_t object
871 * @param atom the value
872 * @return zero on success or an error code on failure
873 */
874 PN_EXTERN int pn_data_put_atom(pn_data_t *data, pn_atom_t atom);
875
876 /**
877 * If the current node is a list, return the number of elements,
878 * otherwise return zero. List elements can be accessed by entering
879 * the list.
880 *
881 * @code
882 * ...
883 * size_t count = pn_data_get_list(data);
884 * pn_data_enter(data);
885 * for (size_t i = 0; i < count; i++) {
886 * if (pn_data_next(data)) {
887 * switch (pn_data_type(data)) {
888 * case PN_STRING:
889 * ...
890 * break;
891 * case PN_INT:
892 * ...
893 * break;
894 * }
895 * }
896 * pn_data_exit(data);
897 * ...
898 * @endcode
899 .*
900 * @param data a pn_data_t object
901 * @return the size of a list node
902 */
903 PN_EXTERN size_t pn_data_get_list(pn_data_t *data);
904
905 /**
906 * If the current node is a map, return the number of child elements,
907 * otherwise return zero. Key value pairs can be accessed by entering
908 * the map.
909 *
910 * @code
911 * ...
912 * size_t count = pn_data_get_map(data);
913 * pn_data_enter(data);
914 * for (size_t i = 0; i < count/2; i++) {
915 * // read key
916 * if (pn_data_next(data)) {
917 * switch (pn_data_type(data)) {
918 * case PN_STRING:
919 * ...
920 * break;
921 * ...
922 * }
923 * }
924 * ...
925 * // read value
926 * if (pn_data_next(data)) {
927 * switch (pn_data_type(data)) {
928 * case PN_INT:
929 * ...
930 * break;
931 * ...
932 * }
933 * }
934 * ...
935 * }
936 * pn_data_exit(data);
937 * ...
938 * @endcode
939 *
940 * @param data a pn_data_t object
941 * @return the number of child elements of a map node
942 */
943 PN_EXTERN size_t pn_data_get_map(pn_data_t *data);
944
945 /**
946 * If the current node is an array, return the number of elements in
947 * the array, otherwise return 0. Array data can be accessed by
948 * entering the array. If the array is described, the first child node
949 * will be the descriptor, and the remaining count child nodes
950 * will be the elements of the array.
951 *
952 * @code
953 * ...
954 * size_t count = pn_data_get_array(data);
955 * bool described = pn_data_is_array_described(data);
956 * pn_type_t type = pn_data_get_array_type(data);
957 *
958 * pn_data_enter(data);
959 *
960 * if (described && pn_data_next(data)) {
961 * // the descriptor could be another type, but let's assume it's a symbol
962 * pn_bytes_t descriptor = pn_data_get_symbol(data);
963 * }
964 *
965 * for (size_t i = 0; i < count; i++) {
966 * if (pn_data_next(data)) {
967 * // all elements will be values of the array type retrieved above
968 * ...
969 * }
970 * }
971 * pn_data_exit(data);
972 * ...
973 * @endcode
974 *
975 * @param data a pn_data_t object
976 * @return the number of elements of an array node
977 */
978 PN_EXTERN size_t pn_data_get_array(pn_data_t *data);
979
980 /**
981 * Returns true if the current node points to a described array.
982 *
983 * @param data a pn_data_t object
984 * @return true if the current node points to a described array
985 */
986 PN_EXTERN bool pn_data_is_array_described(pn_data_t *data);
987
988 /**
989 * Return the array type if the current node points to an array,
990 * PN_INVALID otherwise.
991 *
992 * @param data a pn_data_t object
993 * @return the element type of an array node
994 */
995 PN_EXTERN pn_type_t pn_data_get_array_type(pn_data_t *data);
996
997 /**
998 * Checks if the current node is a described value. The descriptor and
999 * value may be accessed by entering the described value node.
1000 *
1001 * @code
1002 * ...
1003 * // read a symbolically described string
1004 * if (pn_data_is_described(data)) {
1005 * pn_data_enter(data);
1006 * pn_data_next(data);
1007 * assert(pn_data_type(data) == PN_SYMBOL);
1008 * pn_bytes_t symbol = pn_data_get_symbol(data);
1009 * pn_data_next(data);
1010 * assert(pn_data_type(data) == PN_STRING);
1011 * pn_bytes_t utf8 = pn_data_get_string(data);
1012 * pn_data_exit(data);
1013 * }
1014 * ...
1015 * @endcode
1016 *
1017 * @param data a pn_data_t object
1018 * @return true if the current node is a described type
1019 */
1020 PN_EXTERN bool pn_data_is_described(pn_data_t *data);
1021
1022 /**
1023 * Checks if the current node is a ::PN_NULL.
1024 *
1025 * @param data a pn_data_t object
1026 * @return true iff the current node is ::PN_NULL
1027 */
1028 PN_EXTERN bool pn_data_is_null(pn_data_t *data);
1029
1030 /**
1031 * If the current node is a ::PN_BOOL, returns its value.
1032 *
1033 * @param data a pn_data_t object
1034 */
1035 PN_EXTERN bool pn_data_get_bool(pn_data_t *data);
1036
1037 /**
1038 * If the current node is a ::PN_UBYTE, return its value, otherwise
1039 * return 0.
1040 *
1041 * @param data a pn_data_t object
1042 */
1043 PN_EXTERN uint8_t pn_data_get_ubyte(pn_data_t *data);
1044
1045 /**
1046 * If the current node is a signed byte, returns its value, returns 0
1047 * otherwise.
1048 *
1049 * @param data a pn_data_t object
1050 */
1051 PN_EXTERN int8_t pn_data_get_byte(pn_data_t *data);
1052
1053 /**
1054 * If the current node is an unsigned short, returns its value,
1055 * returns 0 otherwise.
1056 *
1057 * @param data a pn_data_t object
1058 */
1059 PN_EXTERN uint16_t pn_data_get_ushort(pn_data_t *data);
1060
1061 /**
1062 * If the current node is a signed short, returns its value, returns 0
1063 * otherwise.
1064 *
1065 * @param data a pn_data_t object
1066 */
1067 PN_EXTERN int16_t pn_data_get_short(pn_data_t *data);
1068
1069 /**
1070 * If the current node is an unsigned int, returns its value, returns
1071 * 0 otherwise.
1072 *
1073 * @param data a pn_data_t object
1074 */
1075 PN_EXTERN uint32_t pn_data_get_uint(pn_data_t *data);
1076
1077 /**
1078 * If the current node is a signed int, returns its value, returns 0
1079 * otherwise.
1080 *
1081 * @param data a pn_data_t object
1082 */
1083 PN_EXTERN int32_t pn_data_get_int(pn_data_t *data);
1084
1085 /**
1086 * If the current node is a char, returns its value, returns 0
1087 * otherwise.
1088 *
1089 * @param data a pn_data_t object
1090 */
1091 PN_EXTERN pn_char_t pn_data_get_char(pn_data_t *data);
1092
1093 /**
1094 * If the current node is an unsigned long, returns its value, returns
1095 * 0 otherwise.
1096 *
1097 * @param data a pn_data_t object
1098 */
1099 PN_EXTERN uint64_t pn_data_get_ulong(pn_data_t *data);
1100
1101 /**
1102 * If the current node is an signed long, returns its value, returns 0
1103 * otherwise.
1104 *
1105 * @param data a pn_data_t object
1106 */
1107 PN_EXTERN int64_t pn_data_get_long(pn_data_t *data);
1108
1109 /**
1110 * If the current node is a timestamp, returns its value, returns 0
1111 * otherwise.
1112 *
1113 * @param data a pn_data_t object
1114 */
1115 PN_EXTERN pn_timestamp_t pn_data_get_timestamp(pn_data_t *data);
1116
1117 /**
1118 * If the current node is a float, returns its value, returns 0
1119 * otherwise.
1120 *
1121 * @param data a pn_data_t object
1122 */
1123 PN_EXTERN float pn_data_get_float(pn_data_t *data);
1124
1125 /**
1126 * If the current node is a double, returns its value, returns 0
1127 * otherwise.
1128 *
1129 * @param data a pn_data_t object
1130 */
1131 PN_EXTERN double pn_data_get_double(pn_data_t *data);
1132
1133 /**
1134 * If the current node is a decimal32, returns its value, returns 0
1135 * otherwise.
1136 *
1137 * @param data a pn_data_t object
1138 */
1139 PN_EXTERN pn_decimal32_t pn_data_get_decimal32(pn_data_t *data);
1140
1141 /**
1142 * If the current node is a decimal64, returns its value, returns 0
1143 * otherwise.
1144 *
1145 * @param data a pn_data_t object
1146 */
1147 PN_EXTERN pn_decimal64_t pn_data_get_decimal64(pn_data_t *data);
1148
1149 /**
1150 * If the current node is a decimal128, returns its value, returns 0
1151 * otherwise.
1152 *
1153 * @param data a pn_data_t object
1154 */
1155 PN_EXTERN pn_decimal128_t pn_data_get_decimal128(pn_data_t *data);
1156
1157 /**
1158 * If the current node is a UUID, returns its value, returns None
1159 * otherwise.
1160 *
1161 * @param data a pn_data_t object
1162 * @return a uuid value
1163 */
1164 PN_EXTERN pn_uuid_t pn_data_get_uuid(pn_data_t *data);
1165
1166 /**
1167 * If the current node is binary, returns its value, returns ""
1168 * otherwise. The pn_bytes_t returned will point to memory held inside
1169 * the pn_data_t. When the pn_data_t is cleared or freed, this memory
1170 * will be reclaimed.
1171 *
1172 * @param data a pn_data_t object
1173 */
1174 PN_EXTERN pn_bytes_t pn_data_get_binary(pn_data_t *data);
1175
1176 /**
1177 * If the current node is a string, returns its value, returns ""
1178 * otherwise. The pn_bytes_t returned will point to memory held inside
1179 * the pn_data_t. When the pn_data_t is cleared or freed, this memory
1180 * will be reclaimed.
1181 *
1182 * @param data a pn_data_t object
1183 * @return a pn_bytes_t pointing to utf8
1184 */
1185 PN_EXTERN pn_bytes_t pn_data_get_string(pn_data_t *data);
1186
1187 /**
1188 * If the current node is a symbol, returns its value, returns ""
1189 * otherwise. The pn_bytes_t returned will point to memory held inside
1190 * the pn_data_t. When the pn_data_t is cleared or freed, this memory
1191 * will be reclaimed.
1192 *
1193 * @param data a pn_data_t object
1194 * @return a pn_bytes_t pointing to ascii
1195 */
1196 PN_EXTERN pn_bytes_t pn_data_get_symbol(pn_data_t *data);
1197
1198 /**
1199 * If the current node is a symbol, string, or binary, return the
1200 * bytes representing its value. The pn_bytes_t returned will point to
1201 * memory held inside the pn_data_t. When the pn_data_t is cleared or
1202 * freed, this memory will be reclaimed.
1203 *
1204 * @param data a pn_data_t object
1205 * @return a pn_bytes_t pointing to the node's value
1206 */
1207 PN_EXTERN pn_bytes_t pn_data_get_bytes(pn_data_t *data);
1208
1209 /**
1210 * If the current node is a scalar value, return it as a pn_atom_t.
1211 *
1212 * @param data a pn_data_t object
1213 * @return the value of the current node as pn_atom_t
1214 */
1215 PN_EXTERN pn_atom_t pn_data_get_atom(pn_data_t *data);
1216
1217 /**
1218 * Copy the contents of another pn_data_t object. Any values in the
1219 * data object will be lost.
1220 *
1221 * @param data a pn_data_t object
1222 * @param src the source pn_data_t to copy from
1223 * @return zero on success or an error code on failure
1224 */
1225 PN_EXTERN int pn_data_copy(pn_data_t *data, pn_data_t *src);
1226
1227 /**
1228 * Append the contents of another pn_data_t object.
1229 *
1230 * @param data a pn_data_t object
1231 * @param src the source pn_data_t to append from
1232 * @return zero on success or an error code on failure
1233 */
1234 PN_EXTERN int pn_data_append(pn_data_t *data, pn_data_t *src);
1235
1236 /**
1237 * Append up to _n_ values from the contents of another pn_data_t
1238 * object.
1239 *
1240 * @param data a pn_data_t object
1241 * @param src the source pn_data_t to append from
1242 * @param limit the maximum number of values to append
1243 * @return zero on success or an error code on failure
1244 */
1245 PN_EXTERN int pn_data_appendn(pn_data_t *data, pn_data_t *src, int limit);
1246
1247 /**
1248 * Modify a pn_data_t object to behave as if the current node is the
1249 * root node of the tree. This impacts the behaviour of
1250 * ::pn_data_rewind(), ::pn_data_next(), ::pn_data_prev(), and
1251 * anything else that depends on the navigational state of the
1252 * pn_data_t object. Use ::pn_data_widen() to reverse the effect of
1253 * this operation.
1254 *
1255 * @param data a pn_data_t object
1256 */
1257 PN_EXTERN void pn_data_narrow(pn_data_t *data);
1258
1259 /**
1260 * Reverse the effect of ::pn_data_narrow().
1261 *
1262 * @param data a pn_data_t object
1263 */
1264 PN_EXTERN void pn_data_widen(pn_data_t *data);
1265
1266 /**
1267 * Returns a handle for the current navigational state of a pn_data_t
1268 * so that it can be later restored using ::pn_data_restore().
1269 *
1270 * @param data a pn_data_t object
1271 * @return a handle for the current navigational state
1272 */
1273 PN_EXTERN pn_handle_t pn_data_point(pn_data_t *data);
1274
1275 /**
1276 * Restores a prior navigational state that was saved using
1277 * ::pn_data_point(). If the data object has been modified in such a
1278 * way that the prior navigational state cannot be restored, then this
1279 * will return false and the navigational state will remain unchanged,
1280 * otherwise it will return true.
1281 *
1282 * @param data a pn_data_t object
1283 * @param point a handle referencing the saved navigational state
1284 * @return true iff the prior navigational state was restored
1285 */
1286 PN_EXTERN bool pn_data_restore(pn_data_t *data, pn_handle_t point);
1287
1288 /**
1289 * Dumps a debug representation of the internal state of the pn_data_t
1290 * object that includes its navigational state to stdout for debugging
1291 * purposes.
1292 *
1293 * @param data a pn_data_t object that is behaving in a confusing way
1294 */
1295 PN_EXTERN void pn_data_dump(pn_data_t *data);
1296
1297 /**
1298 * @}
1299 */
1300
1301 #ifdef __cplusplus
1302 }
1303 #endif
1304
1305 #endif /* codec.h */