ubjsc  0.5
Enterprise-grade C library for manipulating UBJSON
ubjs_library.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Tomasz Sieprawski
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  **/
28 #ifndef HAVE_UBJS_LIBRARY
29 #define HAVE_UBJS_LIBRARY
30 
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #endif
35 
36 #include <ubjs_common.h>
37 
46 typedef void *(*ubjs_library_alloc_f)(unsigned long len);
47 
55 typedef void (*ubjs_library_free_f)(void *ptr);
56 
62 
67 typedef struct ubjs_library ubjs_library;
68 
74 
80 
86 
92 
98 
104 
111 typedef void (*ubjs_glue_value_free)(void *pvalue);
112 
120  ubjs_glue_array_builder **pthis);
121 
131 
143  ubjs_glue_value_free value_free);
144 
161 typedef ubjs_result (*ubjs_glue_array_builder_set_length_f)(ubjs_glue_array_builder *this,
162  unsigned int length);
163 
177 typedef ubjs_result (*ubjs_glue_array_builder_set_item_size_f)(ubjs_glue_array_builder *this,
178  unsigned int item_size);
179 
186 typedef ubjs_result (*ubjs_glue_array_builder_build_f)(ubjs_glue_array_builder *this,
187  ubjs_glue_array **parr);
188 
198 
208 typedef ubjs_result (*ubjs_glue_array_get_length)(ubjs_glue_array *this, unsigned int *pvalue);
209 
219 typedef ubjs_result (*ubjs_glue_array_get_first)(ubjs_glue_array *this, void **pvalue);
220 
230 typedef ubjs_result (*ubjs_glue_array_get_last)(ubjs_glue_array *this, void **pvalue);
231 
242 typedef ubjs_result (*ubjs_glue_array_get_at)(ubjs_glue_array *this, unsigned int index,
243  void **pvalue);
244 
252 typedef ubjs_result (*ubjs_glue_array_add_first)(ubjs_glue_array *this, void *value);
253 
261 typedef ubjs_result (*ubjs_glue_array_add_last)(ubjs_glue_array *this, void *value);
262 
271 typedef ubjs_result (*ubjs_glue_array_add_at)(ubjs_glue_array *this, unsigned int index,
272  void *value);
273 
280 typedef ubjs_result (*ubjs_glue_array_delete_first)(ubjs_glue_array *this);
281 
288 typedef ubjs_result (*ubjs_glue_array_delete_last)(ubjs_glue_array *this);
289 
297 typedef ubjs_result (*ubjs_glue_array_delete_at)(ubjs_glue_array *this, unsigned int index);
298 
299 
307 typedef ubjs_result (*ubjs_glue_array_iterate)(ubjs_glue_array *this,
308  ubjs_glue_array_iterator **piterator);
309 
317 
326  void **pvalue);
327 
336 typedef ubjs_result (*ubjs_glue_array_iterator_free)(ubjs_glue_array_iterator **pthis);
337 
345  ubjs_glue_dict_builder **pthis);
346 
356 
368  ubjs_glue_value_free value_free);
369 
386 typedef ubjs_result (*ubjs_glue_dict_builder_set_length_f)(ubjs_glue_dict_builder *this,
387  unsigned int length);
388 
402 typedef ubjs_result (*ubjs_glue_dict_builder_set_item_size_f)(ubjs_glue_dict_builder *this,
403  unsigned int item_size);
404 
411 typedef ubjs_result (*ubjs_glue_dict_builder_build_f)(ubjs_glue_dict_builder *this,
412  ubjs_glue_dict **pdict);
413 
423 
433 typedef ubjs_result (*ubjs_glue_dict_get_length)(ubjs_glue_dict *this, unsigned int *pvalue);
434 
446 typedef ubjs_result (*ubjs_glue_dict_get)(ubjs_glue_dict *this, unsigned int key_length,
447  char *key, void **pvalue);
448 
458 typedef ubjs_result (*ubjs_glue_dict_set)(ubjs_glue_dict *this, unsigned int key_length,
459  char *key, void *value);
460 
469 typedef ubjs_result (*ubjs_glue_dict_delete)(ubjs_glue_dict *this, unsigned int key_length,
470  char *key);
471 
479 typedef ubjs_result (*ubjs_glue_dict_iterate)(ubjs_glue_dict *this,
480  ubjs_glue_dict_iterator **piterator);
481 
489 
498  unsigned int *plen);
499 
507 typedef ubjs_result (*ubjs_glue_dict_iterator_copy_key)(ubjs_glue_dict_iterator *this, char *text);
508 
516 typedef ubjs_result (*ubjs_glue_dict_iterator_get_value)(ubjs_glue_dict_iterator *this,
517  void **pvalue);
518 
527 typedef ubjs_result (*ubjs_glue_dict_iterator_free)(ubjs_glue_dict_iterator **pthis);
528 
533 struct ubjs_glue_array_builder
534 {
538  void *userdata;
539 
542 
545 
548 
551 
554 };
555 
560 struct ubjs_glue_array
561 {
564 
566  void *userdata;
567 
570 
573 
576 
579 
582 
585 
588 
591 
594 
597 
600 
603 };
604 
605 
610 struct ubjs_glue_array_iterator
611 {
613  ubjs_glue_array *array;
614 
616  void *userdata;
617 
620 
623 
626 };
627 
632 struct ubjs_glue_dict_builder
633 {
637  void *userdata;
638 
641 
644 
647 
650 
653 };
654 
659 struct ubjs_glue_dict
660 {
663 
665  void *userdata;
666 
669 
672 
675 
678 
681 
684 };
685 
686 
691 struct ubjs_glue_dict_iterator
692 {
694  ubjs_glue_dict *object;
695 
697  void *userdata;
698 
701 
704 
707 
710 
713 };
714 
719 struct ubjs_library_builder;
720 
733 
746 
758  ubjs_library_alloc_f alloc_f);
759 
771  ubjs_library_builder *this,
773 
789 
804  ubjs_library_builder *this,
806 
821  ubjs_library **plib);
822 
828 {
831 
834 
837 
840 };
841 
856 
866 UBJS_EXPORT ubjs_result ubjs_library_free(ubjs_library **pthis);
867 
868 #ifdef __cplusplus
869 }
870 #endif
871 
872 #endif
ubjs_glue_array_get_at get_at_f
Definition: ubjs_library.h:581
void *(* ubjs_library_alloc_f)(unsigned long len)
Allocation functor.
Definition: ubjs_library.h:46
ubjs_glue_array_iterator_get get_f
Definition: ubjs_library.h:625
ubjs_glue_dict_iterator_get_key_length get_key_length_f
Definition: ubjs_library.h:706
ubjs_result(* ubjs_glue_dict_iterator_copy_key)(ubjs_glue_dict_iterator *this, char *text)
Copies the key to specified array.
Definition: ubjs_library.h:507
ubjs_result(* ubjs_glue_array_iterate)(ubjs_glue_array *this, ubjs_glue_array_iterator **piterator)
Creates an iterator over glued array.
Definition: ubjs_library.h:307
ubjs_result(* ubjs_glue_array_builder_set_value_free_f)(ubjs_glue_array_builder *this, ubjs_glue_value_free value_free)
Sets the value free callback method.
Definition: ubjs_library.h:142
ubjs_glue_array_free free_f
Definition: ubjs_library.h:569
Definitions of common types, ubjson markers and some utilities.
UBJS_EXPORT ubjs_result ubjs_library_builder_new(ubjs_library_builder **pthis)
Initializes the library handle builder.
Definition: ubjs_library.c:32
ubjs_result(* ubjs_glue_array_iterator_next)(ubjs_glue_array_iterator *this)
Tries to iterate to next item.
Definition: ubjs_library.h:316
ubjs_result(* ubjs_glue_array_builder_build_f)(ubjs_glue_array_builder *this, ubjs_glue_array **parr)
Callback that creates a new array glue based on what was passed to builder.
Definition: ubjs_library.h:186
void * userdata
Definition: ubjs_library.h:665
ubjs_result(* ubjs_glue_array_get_first)(ubjs_glue_array *this, void **pvalue)
Gets the value of first item.
Definition: ubjs_library.h:219
ubjs_glue_array_get_length get_length_f
Definition: ubjs_library.h:572
ubjs_result(* ubjs_glue_dict_delete)(ubjs_glue_dict *this, unsigned int key_length, char *key)
Deletes a key, with its value.
Definition: ubjs_library.h:469
ubjs_result(* ubjs_glue_array_builder_set_length_f)(ubjs_glue_array_builder *this, unsigned int length)
Sets the predicted length for the array.
Definition: ubjs_library.h:161
ubjs_glue_array_delete_at delete_at_f
Definition: ubjs_library.h:599
ubjs_result(* ubjs_glue_dict_iterator_next)(ubjs_glue_dict_iterator *this)
Tries to iterate to next item.
Definition: ubjs_library.h:488
ubjs_glue_dict_set set_f
Definition: ubjs_library.h:677
UBJS_EXPORT ubjs_result ubjs_library_new_stdlib(ubjs_library **pthis)
Initializes the library handle using stdlib&#39;s malloc() and free().
Definition: ubjs_library.c:162
UBJS_EXPORT ubjs_result ubjs_library_builder_set_free_f(ubjs_library_builder *this, ubjs_library_free_f free_f)
Sets the deallocation functor for to use for everything.
Definition: ubjs_library.c:79
ubjs_result(* ubjs_glue_array_delete_last)(ubjs_glue_array *this)
Delete the last item.
Definition: ubjs_library.h:288
ubjs_result(* ubjs_glue_array_delete_at)(ubjs_glue_array *this, unsigned int index)
Delete the item at index.
Definition: ubjs_library.h:297
ubjs_glue_array_builder_new_f glue_array_builder
Builder for array glue.
Definition: ubjs_library.h:836
ubjs_result(* ubjs_glue_array_add_at)(ubjs_glue_array *this, unsigned int index, void *value)
Add the value of item at an index.
Definition: ubjs_library.h:271
ubjs_library * lib
Definition: ubjs_library.h:536
ubjs_glue_dict_builder_set_length_f set_length_f
Definition: ubjs_library.h:646
ubjs_result(* ubjs_glue_array_builder_new_f)(ubjs_library *lib, ubjs_glue_array_builder **pthis)
Creates a new array glue builder.
Definition: ubjs_library.h:119
ubjs_library * lib
Definition: ubjs_library.h:635
ubjs_glue_array_builder_set_item_size_f set_item_size_f
Definition: ubjs_library.h:550
ubjs_glue_dict_builder_set_item_size_f set_item_size_f
Definition: ubjs_library.h:649
ubjs_result(* ubjs_glue_array_builder_free_f)(ubjs_glue_array_builder **)
Frees the array glue builder.
Definition: ubjs_library.h:130
Library handle.
Definition: ubjs_library.h:827
ubjs_glue_dict_iterator_next next_f
Definition: ubjs_library.h:703
ubjs_glue_dict * object
Definition: ubjs_library.h:694
ubjs_glue_array_add_first add_first_f
Definition: ubjs_library.h:584
ubjs_glue_array_add_at add_at_f
Definition: ubjs_library.h:590
ubjs_glue_dict_iterator_get_value get_value_f
Definition: ubjs_library.h:712
ubjs_glue_dict_get get_f
Definition: ubjs_library.h:674
ubjs_result(* ubjs_glue_dict_free)(ubjs_glue_dict **)
Frees the dictionary glue.
Definition: ubjs_library.h:422
ubjs_result(* ubjs_glue_dict_builder_set_length_f)(ubjs_glue_dict_builder *this, unsigned int length)
Sets the predicted length for the dictionary.
Definition: ubjs_library.h:386
ubjs_result(* ubjs_glue_array_get_last)(ubjs_glue_array *this, void **pvalue)
Gets the value of last item.
Definition: ubjs_library.h:230
void * userdata
Definition: ubjs_library.h:697
Glue to an array.
Definition: ubjs_library.h:560
ubjs_glue_dict_iterate iterate_f
Definition: ubjs_library.h:683
ubjs_result(* ubjs_glue_dict_get)(ubjs_glue_dict *this, unsigned int key_length, char *key, void **pvalue)
Gets the value under a key.
Definition: ubjs_library.h:446
ubjs_result(* ubjs_glue_array_iterator_free)(ubjs_glue_array_iterator **pthis)
Frees the array iterator glue.
Definition: ubjs_library.h:336
void(* ubjs_library_free_f)(void *ptr)
Deallocation functor.
Definition: ubjs_library.h:55
ubjs_glue_dict_delete delete_f
Definition: ubjs_library.h:680
UBJS_EXPORT ubjs_result ubjs_library_free(ubjs_library **pthis)
Deinitializes the library handle.
Definition: ubjs_library.c:177
ubjs_glue_dict_get_length get_length_f
Definition: ubjs_library.h:671
void * userdata
Definition: ubjs_library.h:538
ubjs_result(* ubjs_glue_dict_iterate)(ubjs_glue_dict *this, ubjs_glue_dict_iterator **piterator)
Creates an iterator over glued dictionary.
Definition: ubjs_library.h:479
ubjs_result(* ubjs_glue_array_add_last)(ubjs_glue_array *this, void *value)
Add the value at end.
Definition: ubjs_library.h:261
ubjs_glue_array_iterate iterate_f
Definition: ubjs_library.h:602
ubjs_library * lib
Definition: ubjs_library.h:662
ubjs_glue_array_delete_last delete_last_f
Definition: ubjs_library.h:596
ubjs_result(* ubjs_glue_dict_builder_new_f)(ubjs_library *lib, ubjs_glue_dict_builder **pthis)
Creates a new dictionary glue builder.
Definition: ubjs_library.h:344
ubjs_result
Enum that defines the result of a method.
Definition: ubjs_common.h:73
ubjs_result(* ubjs_glue_dict_iterator_free)(ubjs_glue_dict_iterator **pthis)
Frees the dictionary iterator glue.
Definition: ubjs_library.h:527
ubjs_glue_dict_iterator_copy_key copy_key_f
Definition: ubjs_library.h:709
ubjs_glue_dict_builder_set_value_free_f set_value_free_f
Definition: ubjs_library.h:643
ubjs_glue_array_get_first get_first_f
Definition: ubjs_library.h:575
UBJS_EXPORT ubjs_result ubjs_library_builder_free(ubjs_library_builder **pthis)
Frees the library handle builder.
Definition: ubjs_library.c:51
ubjs_library * lib
Definition: ubjs_library.h:563
ubjs_glue_array_builder_set_length_f set_length_f
Definition: ubjs_library.h:547
Glue to array iterator.
Definition: ubjs_library.h:610
struct ubjs_library_builder ubjs_library_builder
Library handle builder.
Definition: ubjs_library.h:61
Builder for dictionary glues.
Definition: ubjs_library.h:632
Glue to dictionary iterator.
Definition: ubjs_library.h:691
ubjs_glue_array_builder_free_f free_f
Definition: ubjs_library.h:541
ubjs_library_free_f free_f
Free callback.
Definition: ubjs_library.h:833
ubjs_library_alloc_f alloc_f
Allocation callback.
Definition: ubjs_library.h:830
ubjs_glue_dict_iterator_free free_f
Definition: ubjs_library.h:700
ubjs_glue_array_add_last add_last_f
Definition: ubjs_library.h:587
ubjs_result(* ubjs_glue_array_delete_first)(ubjs_glue_array *this)
Delete the first item.
Definition: ubjs_library.h:280
ubjs_result(* ubjs_glue_dict_builder_set_value_free_f)(ubjs_glue_dict_builder *this, ubjs_glue_value_free value_free)
Sets the value free callback method.
Definition: ubjs_library.h:367
ubjs_result(* ubjs_glue_array_free)(ubjs_glue_array **)
Frees the array glue.
Definition: ubjs_library.h:197
ubjs_result(* ubjs_glue_array_get_length)(ubjs_glue_array *this, unsigned int *pvalue)
Gets the length of the array under a glue.
Definition: ubjs_library.h:208
ubjs_result(* ubjs_glue_array_iterator_get)(ubjs_glue_array_iterator *this, void **pvalue)
Gets current item&#39;s value.
Definition: ubjs_library.h:325
UBJS_EXPORT ubjs_result ubjs_library_builder_set_glue_array_builder(ubjs_library_builder *this, ubjs_glue_array_builder_new_f builder)
Sets the array glue builder. This allows to select a different implementation of actual key-value sto...
Definition: ubjs_library.c:91
ubjs_result(* ubjs_glue_array_get_at)(ubjs_glue_array *this, unsigned int index, void **pvalue)
Gets the value of item under an index.
Definition: ubjs_library.h:242
ubjs_result(* ubjs_glue_dict_set)(ubjs_glue_dict *this, unsigned int key_length, char *key, void *value)
Sets the value under a key.
Definition: ubjs_library.h:458
void * userdata
Definition: ubjs_library.h:616
ubjs_result(* ubjs_glue_dict_iterator_get_value)(ubjs_glue_dict_iterator *this, void **pvalue)
Gets current value.
Definition: ubjs_library.h:516
ubjs_result(* ubjs_glue_dict_get_length)(ubjs_glue_dict *this, unsigned int *pvalue)
Gets the length of the dictionary under a glue.
Definition: ubjs_library.h:433
ubjs_result(* ubjs_glue_dict_builder_free_f)(ubjs_glue_dict_builder **)
Frees the dictionary glue builder.
Definition: ubjs_library.h:355
ubjs_glue_dict_builder_build_f build_f
Definition: ubjs_library.h:652
ubjs_glue_dict_builder_new_f glue_dict_builder
Builder for dictionary glue.
Definition: ubjs_library.h:839
void * userdata
Definition: ubjs_library.h:637
ubjs_glue_array_iterator_free free_f
Definition: ubjs_library.h:619
ubjs_glue_array_builder_build_f build_f
Definition: ubjs_library.h:553
ubjs_glue_array_iterator_next next_f
Definition: ubjs_library.h:622
void * userdata
Definition: ubjs_library.h:566
ubjs_glue_dict_builder_free_f free_f
Definition: ubjs_library.h:640
ubjs_result(* ubjs_glue_dict_builder_set_item_size_f)(ubjs_glue_dict_builder *this, unsigned int item_size)
Sets the predicted item size.
Definition: ubjs_library.h:402
Builder for array glues.
Definition: ubjs_library.h:533
ubjs_result(* ubjs_glue_array_add_first)(ubjs_glue_array *this, void *value)
Add the value at beginning.
Definition: ubjs_library.h:252
ubjs_glue_array * array
Definition: ubjs_library.h:613
ubjs_glue_dict_free free_f
Definition: ubjs_library.h:668
ubjs_result(* ubjs_glue_array_builder_set_item_size_f)(ubjs_glue_array_builder *this, unsigned int item_size)
Sets the predicted item size.
Definition: ubjs_library.h:177
UBJS_EXPORT ubjs_result ubjs_library_builder_set_alloc_f(ubjs_library_builder *this, ubjs_library_alloc_f alloc_f)
Sets the allocation functor for to use for everything.
Definition: ubjs_library.c:67
ubjs_glue_array_delete_first delete_first_f
Definition: ubjs_library.h:593
void(* ubjs_glue_value_free)(void *pvalue)
Frees the value held in a glue. Note that this is a generic callback, not related to actual ubjs_prmt...
Definition: ubjs_library.h:111
UBJS_EXPORT ubjs_result ubjs_library_builder_build(ubjs_library_builder *this, ubjs_library **plib)
Builds the library and returns it.
Definition: ubjs_library.c:115
ubjs_result(* ubjs_glue_dict_builder_build_f)(ubjs_glue_dict_builder *this, ubjs_glue_dict **pdict)
Callback that creates a new dictionary glue based on what was passed to builder.
Definition: ubjs_library.h:411
ubjs_glue_array_get_last get_last_f
Definition: ubjs_library.h:578
ubjs_result(* ubjs_glue_dict_iterator_get_key_length)(ubjs_glue_dict_iterator *this, unsigned int *plen)
Gets current key&#39;s length.
Definition: ubjs_library.h:497
Glue to a dictionary.
Definition: ubjs_library.h:659
ubjs_glue_array_builder_set_value_free_f set_value_free_f
Definition: ubjs_library.h:544
UBJS_EXPORT ubjs_result ubjs_library_builder_set_glue_dict_builder(ubjs_library_builder *this, ubjs_glue_dict_builder_new_f builder)
Sets the dict glue builder. This allows to select a different implementation of actual key-value stor...
Definition: ubjs_library.c:103