### Install Dependencies via Bash Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Commands to install Meson and Ninja build systems using pip, which are prerequisites for building Libcdict. ```bash pip3 install meson pip3 install ninja ``` -------------------------------- ### Makefile for Fortran cdict Example Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md A Makefile to compile the Fortran cdict example program. It specifies the Fortran compiler, library paths, include directories, and compilation flags. It also defines the target executable and source files. ```makefile # Makefile FC = gfortran # Need to add this path to $LD_LIBRARY_PATH before it can run LIB_DIRS = -Llib/ LIBS = -lcdict INC_DIRS = -Iinclude/cdict/ FCFLAGS = -g -fcheck=all -Wall $(INC_DIRS) TARGET = run # Ordering here determines compilation order SRCS = include/cdict/cdict_fortran_interface.f90 cdict_example.f90 all: $(TARGET) $(TARGET): $(SRCS) $(FC) $(FCFLAGS) $(SRCS) -o $(TARGET) $(LIB_DIRS) $(LIBS) clean: rm -f $(TARGET) ``` -------------------------------- ### Clone and Build Libcdict via Bash Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Steps to clone the Libcdict repository and build it using Meson and Ninja, specifying a release build type and installation directory. ```bash git clone https://gitlab.com/rob.izzard/libcdict.git meson setup --prefix=$HOME --buildtype=release builddir cd builddir ninja install ``` -------------------------------- ### Full Fortran cdict Example Program Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md A complete Fortran program (`cdict_example.f90`) demonstrating various cdict operations including creation, setting values, pointers, nesting, copying, JSON output, and freeing memory. It utilizes the `libcdict_m` module. ```fortran ! cdict_example.f90 program cdict_example use libcdict_m use, intrinsic :: iso_c_binding implicit none type(c_ptr) :: dict0, dict1, dictc integer(c_int) :: int_key, int_val ! Using 'target' here to demonstrate setting pointers real(c_float), target :: float_key, float_val real(c_float) :: nest_key, nest_val real(c_double) :: double_key, double_val character(kind=c_char, len=10) :: string_key ! Creating new cdicts dict0 = CDict_new() dict1 = CDict_new(dict0) ! Setting key->value pairs float_key = 1.0 float_val = 10.0 call CDict_set(dict0, float_key, float_val) double_key = 2.0 double_val = 20.0 call CDict_set(dict0, double_key, double_val) int_key = 1 int_val = 10 call CDict_set(dict0, int_key, int_val) ! Setting a pointer call CDict_set(dict1, c_loc(float_key), c_loc(float_val)) ! Setting a cdict string_key = "First key" call CDict_set(dict1, trim(string_key), dict0) ! Nesting key->value pairs nest_key = 100.0 nest_val = 1000.0 call CDict_nest(dict0, nest_key, nest_val, float_val) call CDict_nest(dict0, nest_key, nest_val, float_val, nest_val) ! Copying a cdict dictc = CDict_new() call CDict_copy(dict0, dictc) ! JSON output call CDict_print_JSON(dict0, CDICT_JSON_SORT) call CDict_print_JSON(dict1, CDICT_JSON_NO_SORT) call CDict_print_JSON(dictc, CDICT_JSON_NO_SORT) ! Stats call CDict_stats(dict0) call CDict_stats(dict1) call CDict_stats(dictc) ! Freeing cdicts call CDict_free(dict0) call CDict_free(dict1) call CDict_free(dictc) end program cdict_example ``` -------------------------------- ### Set and Get Metadata from Cdict Entry in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Shows how to use CDict_set_metadata and CDict_get_metadata for setting and retrieving metadata from a cdict entry. ```c CDict_set_metadata(entry, metadata, metadata_free_function); ``` ```c CDict_get_metadata(entry); ``` -------------------------------- ### Get Fortran cdict Statistics Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Provides the Fortran code snippet to print the statistics of a cdict. The `CDict_stats()` function is used for this purpose. ```fortran type(c_ptr) :: c c = CDict_new() call CDict_stats(c) ``` -------------------------------- ### Debugging Utilities for CDICT in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Provides examples of using libcdict's debugging API. CDict_assert can be used to validate conditions, triggering an error if false. CDict_stats outputs cdict performance statistics, and setting `cdict->vb = TRUE` enables verbose debugging output. ```c CDict_assert(x); CDict_stats(cdict); cdict->vb = TRUE; ``` -------------------------------- ### Get nested cdict data pointer in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Retrieves a pointer to data stored at a nested location in a cdict. The pointer will be NULL if the data does not exist. Requires casting for pointer types. ```c double * x = CDict_nest_get_data_pointer(nestcdict, "nested", "location", "of", "scalar double"); ``` ```c struct cdict_t * nested_cdict = *(struct cdict_t **) CDict_nest_get_data_pointer(parent_cdict, "nested", "location"); ``` -------------------------------- ### Get nested cdict data value in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Directly retrieves the value of data stored at a nested location in a cdict. Raises an error if the data does not exist. Suitable for scalar types. ```c double x; x = CDict_nest_get_data_value(nestcdict, x, "nested", "location", ...); ``` -------------------------------- ### Fortran Build Rules for Binary C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Defines compilation and linking rules for Fortran source files in the Binary C project. It specifies how object files are created from Fortran sources and how the final target executable is linked using a Fortran compiler, libraries, and directory flags. Includes a clean rule to remove generated files. ```makefile OBJS = $(addsuffix .o,$(basename $(SRCS))) $(TARGET) : $(OBJS) $(FC) $(LIB_DIRS) -o $@ $^ $(LIBS) %.o: %.f90 $(FC) -c $(FCFLAGS) -o $@ $< clean: rm -f *.mod *.o $(TARGET) ``` -------------------------------- ### cdict-config Usage Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Demonstrates the usage of the cdict-config command-line utility for retrieving configuration information. ```bash $ cdict-config ``` ```bash $ cdict-config --version ``` ```bash $ cdict-config --tests ``` -------------------------------- ### Creating a New Fortran cdict Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Demonstrates the creation of a new cdict object in Fortran using the `CDict_new` function. It shows how to create an empty cdict and how to create one with a parent cdict. ```fortran type(c_ptr) :: c c = CDict_new() type(c_ptr) :: c, p p = CDict_new() c = CDict_new(p) ``` -------------------------------- ### Loading JSON Data in Python Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Demonstrates how to load JSON data in Python, either from a string or a file using the `json` module. This is the first step in processing exported libcdict data. ```python # read from a string data = json.loads(string) # or read from a file data = json.load(file) ``` -------------------------------- ### Set Metadata with Formats in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Demonstrates how to set metadata including formats using CDict_set_with_types_formats_and_metadata. Includes a metadata free function to release dynamically allocated resources. ```c CDict_set_with_types_formats_and_metadata(cdict, key,keytype,keyformat, value,valuetype,valueformat, metadata,metadata_free_function, n); ``` -------------------------------- ### Including libcdict Fortran Module Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Shows how to include the `libcdict_m` module and the `iso_c_binding` intrinsic module in Fortran source code. This is necessary to use the Fortran API for libcdict. ```fortran use libcdict_m use, intrinsic :: iso_c_binding implicit none ``` -------------------------------- ### Locate Entry in Cdict (C) Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Demonstrates how to check for and retrieve an entry from a cdict using its key. It shows matching keys of any type and specific types, and converting keys/values to strings for output. Requires a `cdict_entry_t` structure and key/keytype arguments. ```c /* * Match a key of any type */ cdict_entry_t * entry = CDict_contains(cdict,key); /* * Match a key of a certain type */ entry = CDict_contains_with_type(cdict,key,keytype); /* * Then, e.g., key and value to strings (so we can output) */ char *keystring, *valuestring; CDict_entry_to_value_string(entry, value_string, NULL); CDict_entry_to_key_string(entry, key_string, NULL); ``` -------------------------------- ### Copy Fortran cdict Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Details the process of copying a Fortran cdict to a new one. This involves creating a new cdict using `CDict_new()` and then using `CDict_copy()` to duplicate the contents. ```fortran type(c_ptr) :: c, c_copy c = CDict_new() call CDict_set(c, 1, 100.91) ! Copy the current state of c into c_copy c_copy = CDict_new() call CDict_copy(c, c_copy) ``` -------------------------------- ### Set Data in Fortran cdict Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Demonstrates how to set key-value pairs in a Fortran cdict. Supports various data types including integers, floats, pointers, and strings. For pointers, variables must be declared as 'target'. Strings require careful handling with null termination. ```fortran type(c_ptr) :: c c = CDict_new() ! Set a key-value pair 1->100.91 in the c cdict call CDict_set(c, 1, 100.91) ``` ```fortran type(c_ptr) :: c integer(c_int), target :: key c = CDict_new() key = 1 ! Set a pointer to an integer as the key in the c cdict call CDict_set(c, c_loc(key), 100.91) ``` ```fortran type(c_ptr) :: c character(kind=c_char, len=10) :: string_key c = CDict_new() ! Set a key-value pair "First key"->100.91 in the c cdict string_key = "First key" call CDict_set(c, trim(string_key)//C_NULL_CHAR, 100.91) ``` -------------------------------- ### Include Libcdict Header in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Includes the main header file for Libcdict to use its functionalities in C programs. This is required to access all Libcdict features. ```C #include ``` -------------------------------- ### Create New CDict in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Initializes a new associative array (cdict) in C. Optionally, a parent cdict can be specified to inherit settings like error handlers and cache usage. ```C CDict_new(c); ``` ```C CDict_new(new_cdict,parent_cdict); ``` -------------------------------- ### Setting Parent/Ancestor CDICT in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Demonstrates how to set the parent and ancestor for a cdict structure. This can be done during initialization using a two-argument CDict_new or by manually assigning the parent and ancestor pointers. ```c CDict_new(new_cdict,parent_cdict); /* or */ new_cdict->parent = parent_cdict; /* ancestor is set automatically by CDict_new */ new_cdict->ancestor = parent_cdict->ancestor; ``` -------------------------------- ### Sort Cdict (C) Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Provides methods to sort a cdict. Includes automatic sorting (alphanumerically, low to high), reverse sorting, and sorting using a custom comparison function compatible with Gnu C's `qsort`. Sorting is useful before looping through dictionary elements. ```c /* * Sort automatically, e.g. low to high, alphanumerically */ CDict_sort(cdict); /* * As CDict_sort in reverse. */ CDict_reverse_sort(cdict); ``` ```c /* * Sort using my_sortfunc */ CDict_sort_by_func(cdict, my_sortfunc); ``` -------------------------------- ### CDict_set: Set Key-Value with Dynamic Metadata in Cdict Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Illustrates setting a key-value pair in a CDict with dynamically allocated string metadata. The CDict_string function allocates memory, and this metadata is automatically freed when the cdict is freed. ```c /* * Set dynamically allocated strings as metadata: these * require a function to free them. * * Here we CDict_string to allocate space for the string in memory * and CDict_free_metadata to free it. * * CDict_string just allocates some memory and sets the string in * it. Any string allocated with CDict_string is automatically freed * when the cdict (teams in this case) is freed with CDict_free(). */ CDict_set(teams, "Tottenham","Hotspur", CDict_string(teams,"League winners only 1950–51 and 1960–61")); ``` -------------------------------- ### Add Pointers to CDict's To-Free List (C) Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Shows how to manually add pointers to a cdict's 'tofree list' using `CDict_add_to_free_list`. Pointers in this list are automatically freed when the cdict itself is freed, providing a form of internal garbage collection. ```c CDict_add_to_free_list(nestcdict, x); ``` -------------------------------- ### Set Numeric Tolerances for Cdict (C) Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Configures internal numeric handling for floating-point numbers within the cdict. Allows setting absolute and relative epsilon values for comparisons, and the number of bytes to use for hashing doubles and floats. Default values are provided, but can be customized for specific projects. ```c CDict_set_numerics(abseps, releps, nbytes_double, nbytes_float); ``` -------------------------------- ### CDict_set: Set Key-Value with Static Metadata in Cdict Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Shows how to set a key-value pair in a CDict with a static string as metadata. This metadata is not output as JSON and does not require explicit freeing. ```c /* * Set a key-value pair, a with static string as metadata. * (Static strings require no freeing.) */ CDict_set(teams, "Pride of London","Arsenal", "League winners 1930–31, 1932–33, 1933–34, 1934–35, 1937–38, 1947–48, 1952–53, 1970–71, 1988–89, 1990–91, 1997–98, 2001–02 and 2003–04"); ``` -------------------------------- ### Manually Setting char* Type in CDict Entry (C) Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Explains a workaround for handling `char *` types within CDict_nest, as the compiler cannot distinguish between `char[]` and `char *`. It involves casting to `void *` or manually setting the `value.type` and `value.format` of the `cdict_entry_t`. ```c /* char * type: special case */ struct cdict_entry_t * const e = CDict_nest("key",(char*)char_pointer,"nest","location"); e->value.type = CDICT_DATA_TYPE_CHAR_POINTER; e->value.format = "%p"; ``` -------------------------------- ### CDict_set: Set Key-Value Pair in Cdict Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Demonstrates setting a simple key-value pair in a CDict. The CDict_set function implicitly guesses the types of the key and value. It is useful for basic data storage within the cdict. ```c /* make new cdict "teams" */ CDict_new(teams); /* just set a key-value pair "Euro 2022 Winners"->"England" in the teams cdict */ CDict_set(teams, "Euro 2022 Winners","England"); ``` -------------------------------- ### Freeing a Fortran cdict Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Illustrates how to free a cdict object in Fortran using the `CDict_free` subroutine. This releases the memory associated with the cdict and its metadata. ```fortran type(c_ptr) :: c c = CDict_new() call CDict_free(c) ``` -------------------------------- ### Convert Fortran cdict to JSON Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Shows how to print the contents of a Fortran cdict in JSON format using `CDict_print_JSON()`. This function accepts sorting constants (`CDICT_JSON_SORT` or `CDICT_JSON_NO_SORT`) to control the output order. ```fortran type(c_ptr) :: c c = CDict_new() call CDict_set(c, 2, 200.91) call CDict_set(c, 1, 100.91) call CDict_print_JSON(c, CDICT_JSON_SORT) call CDict_print_JSON(c, CDICT_JSON_NO_SORT) ``` -------------------------------- ### Dynamic String Allocation for Nested CDict Keys (C) Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Illustrates how to use CDict_asprintf to dynamically allocate memory for string keys used in nested cdicts. Strings allocated this way are automatically freed when the parent cdict is freed. ```c char * x = NULL; int n = 2; CDict_asprintf(nestcdict,x,"key",n); // x is now "key2" CDict_nest(nestcdict, "some data", x); n=3; char * y = NULL; CDict_asprintf(nestcdict,y,"key",n); // y is now "key3" CDict_nest(nestcdict, "some more data", y); ``` -------------------------------- ### CDict_set_with_types_and_metadata: Set Explicit Types with Metadata in Cdict Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Shows how to set a key and value with explicitly defined C types, along with static metadata and no associated free function. This provides granular control over data types and metadata handling. ```c /* * Set key = 350.0 (double), value = 33 (int), and a static metadata * string "..." with no metadata free function (NULL). */ CDict_set_with_types_and_metadata(cdict, 350.0,CDICT_DATA_TYPE_DOUBLE, 33,CDICT_DATA_TYPE_INT, "this is a metadata string", NULL); ``` -------------------------------- ### Copying a CDICT in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Illustrates the process of copying a cdict object using the CDict_copy function. This function ensures that the content of the original cdict, including nested structures like strings and arrays, is duplicated in the new cdict. ```c CDict_new(cdict); CDict_new(cdict_copy); CDict_copy(cdict,cdict_copy); ``` -------------------------------- ### Loop over cdict keys in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Iterates over the keys in a cdict, allowing access to each entry's key and value. Supports sorted or unsorted iteration. Useful for processing all entries in a cdict. ```c CDict_sorted_loop(cdict,entry) { char *keystring, *valuestring; int ret = CDict_entry_to_key_and_value_strings(entry,keystring,valuestring); if(ret < 0) { printf("error %d setting key/value strings\n",ret); exit(0); } else { printf("key (type %s) = %s : value (type %s) = %s\n", CDict_key_descriptor(entry->keytype), keystring, CDict_value_descriptor(entry->valuetype), valuestring); } CDict_Safe_free(keystring); CDict_Safe_free(valuestring); } ``` ```c char *keystring, *valuestring; CDict_entry_to_value_string(entry, value_string, NULL); CDict_entry_to_key_string(entry, key_string, NULL); ``` -------------------------------- ### Set Custom Format in CDict in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Sets a custom format for keys and values in a CDict. Allows precise control over how data is formatted when output. Useful for ensuring consistent data presentation. ```c double r1 = 10.0; int i = 1234; CDict_set_with_types_and_formats(cdict, r1, CDICT_DATA_TYPE_DOUBLE, "%.15e", i, CDICT_DATA_TYPE_INT, "%d"); ``` -------------------------------- ### Set Metadata with Types in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Demonstrates how to set metadata using CDict_set_with_types_and_metadata. This involves defining a metadata structure using malloc and a function to free the allocated memory to prevent memory leaks. The metadata is not emitted as JSON. ```c CDict_set_with_types_and_metadata(cdict, "2.0",CDICT_DATA_TYPE_STRING, 2.0,CDICT_DATA_TYPE_DOUBLE); "metadata label string", NULL); ``` ```c /* * Define a struct to hold our metadata, * in this case a label (char *) and a * type number (int). */ struct custom_metadata_struct_t { char * label; int type; }; /* * Allocate memory for the struct */ struct custom_metadata_struct_t * my_metadata = malloc(sizeof(struct custom_metadata_struct_t)); /* * Use asprintf to put a string in the label: * note that this automatically allocates the memory * for the label so we must free it later */ if(asprintf(&my_metadata->label, "my metadata label")<0) { fprintf(stderr,"Could not allocate metadata label\n"); exit(0); } /* * Set the metadata type : this is useful when * giving your data custom typing. */ my_metadata->type = 666; /* * Here we use the GCC extension "auto" to put the function * here... you may need to move this function outside the * scope of the current subroutine. */ auto void free_my_metadata(struct cdict_entry_t * entry); void free_my_metadata(struct cdict_entry_t * entry) { struct custom_metadata_struct_t * metadata = entry->metadata; printf("free metadata at %p with label \"%s\" and type \"%d\"\n", entry, metadata->label, metadata->type ); if(metadata != NULL) { CDict_Safe_free(metadata->label); CDict_Safe_free(metadata); } } /* * Now set the metadata */ CDict_set_with_types_and_metadata(cdict, 400.0,CDICT_DATA_TYPE_DOUBLE, 123.0,CDICT_DATA_TYPE_DOUBLE, my_metadata, free_my_metadata); ``` -------------------------------- ### CDict_set: Set Key-Value with Custom Metadata Free Function in Cdict Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Demonstrates using a four-argument version of CDict_set to provide a custom function for freeing dynamically allocated metadata. This allows for specific memory management strategies for metadata. ```c void free_metadata_function(struct cdict_t * const cdict, struct cdict_entry_t * entry) { /* free data in here */ } CDict_set(teams, "Tottenham","Hotspur", CDict_string(teams,"League winners only 1950–51 and 1960–61"), free_metadata_function); ``` -------------------------------- ### Load JSON into CDict in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Loads JSON data from a buffer or file into a CDict. Note the limitation with C arrays requiring constant types. Useful for deserializing JSON data into a CDict structure. ```c CDict_JSON_buffer_to_CDict(buffer,cdict); ``` ```c CDict_JSON_file_to_CDict(cdict,filename); ``` -------------------------------- ### Converting Dictionary Keys to Floats in Python Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Provides a Python function `keys_to_floats` that recursively converts dictionary keys to floating-point numbers if possible. This is useful when JSON data is loaded and numeric keys are expected. ```python import collections.abc import json def keys_to_floats(input_dict: dict) -> dict: """ Function to convert all the keys of the dictionary to floats if they can be converted. Args: input_dict: dict of which we want to turn all the keys to float types if possible Returns: new_dict: dict of which the keys have been turned to float types where possible """ # this adopts the type correctly new_dict = type(input_dict)() for k, v in input_dict.items(): # convert key to a float, if we can # otherwise leave as is try: newkey = float(k) except ValueError: newkey = k # act on value(s) if isinstance(v, list): # list data new_dict[newkey] = [ keys_to_floats(item) if isinstance(item, collections.abc.Mapping) else item for item in v ] elif isinstance(v, collections.abc.Mapping): # dict, ordereddict, etc. data new_dict[newkey] = keys_to_floats(v) else: # assume all other data are scalars new_dict[newkey] = v return new_dict data = json.load(filename) print('keys loaded in:',data.keys()) data = keys_to_floats(data) print('keys converted to floats:',data.keys()) ``` -------------------------------- ### CDict Nested Structure with Statically Allocated Keys (C) Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Presents an alternative CDict nesting strategy that utilizes only statically allocated keys. This approach is generally more memory-efficient for storing string keys within the cdict structure. ```c CDict_nest(nestcdict, "some data", "key",2); CDict_nest(nestcdict, "some more data", "key",3); ``` -------------------------------- ### Output CDict to JSON in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Outputs the contents of a CDict to a JSON buffer or stdout. Options include sorting and formatting choices like indentation and newlines. Useful for debugging or data serialization. ```c CDict_print_JSON(cdict,CDICT_JSON_NO_SORT); ``` ```c CDict_print_JSON(cdict,CDICT_JSON_SORT,stream); ``` ```c char * json_buffer = NULL; size_t json_size = 0; CDict_to_JSON(cdict, json_buffer, json_size, ",", " : ", CDICT_JSON_INDENT, CDICT_JSON_NEWLINES, CDICT_JSON_SORT); printf("JSON buffer at %p, size %zu\n",json_buffer,json_size); printf("%s\n",json_buffer); CDict_Safe_free(json_buffer); ``` ```c CDict_to_JSON(cdict, json_buffer, json_size, ",", ":", CDICT_JSON_NO_INDENT, CDICT_JSON_NO_NEWLINES, CDICT_JSON_NO_SORT); ``` -------------------------------- ### Nest Data in Fortran cdict Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Explains how to set nested data within a Fortran cdict. The `CDict_nest` function allows specifying a parent key and an index for nesting. Currently, the maximum nesting depth is 2. ```fortran type(c_ptr) :: c integer(c_int), target :: key c = CDict_new() ! Set a key-value pair 1->100.91 in the cdict at location 999 call CDict_nest(c, 1, 100.91, 999) ! Set a key-value pair 2->200.91 in the cdict at location 999->2 call CDict_nest(c, 2, 200.91, 999, 2) ``` -------------------------------- ### Set Nested Variables in CDict using CDict_nest (C) Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Demonstrates using CDict_nest to insert various data types (numbers, strings, booleans) into nested cdict structures. Handles appending to existing entries by type (addition, concatenation, AND, pointer increment). Prevents nesting cdicts within cdicts directly. ```c /* * You can use the CDict_nest function to put items * into a nested cdict structure. */ CDict_new(nestcdict); CDict_nest( nestcdict, /* cdict */ 1,0.1234, /* key and value */ "hello","world" /* nest location "hello" -> "world" -> ... */ ); CDict_nest(nestcdict, 2.0,"nested data", "hello" /* nest location "hello" -> ... */ ); CDict_nest(nestcdict, TRUE,"Bergkamp Ajax", /* data to nest */ "Denis",10 /* nest location "Denis" -> 10 -> ... */); CDict_nest(nestcdict, 1,"Bergkamp Arsenal", /* data to nest */ "Denis","10" /* nest location "Denis" -> "10" -> ... */); CDict_print_JSON(nestcdict,CDICT_JSON_SORT); printf("\n"); CDict_free(nestcdict); ``` -------------------------------- ### Access nested cdict entry in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Retrieves a nested cdict entry by specifying its location path. Returns NULL if the entry does not exist. Useful for accessing specific data within nested structures. ```c struct cdict_entry_t * entry = CDict_nest_get_entry(nestcdict, "nested", "location", ...); ``` -------------------------------- ### Iterate over cdict using iterator in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Uses an iterator to traverse a cdict, including nested structures. Useful for processing large cdicts without loading everything into memory. The iterator must be freed after use. ```c struct CDict_iterator_t * iterator = NULL; Boolean sort = FALSE; /* perhaps sort output? */ while(TRUE) { struct cdict_entry_t * const top_entry = CDict_iter(cdict, &iterator, sort); for(size_t i=0; istack->nstack; i++) { struct cdict_entry_t * this_entry = iterator->stack->items[i] ? iterator->stack->items[i]->entry : NULL; /* * do stuff with this_entry * ... */ } if(!top_entry) break; /* no more data */ } CDict_iter(NULL,&iterator,FALSE); /* free iterator */ ``` -------------------------------- ### CDict_set_with_types: Set Array with Explicit Type in Cdict Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Demonstrates setting an array of doubles into a CDict using CDict_set_with_types, explicitly specifying the key as a string and the value as a double array. This function requires manual type specification. ```c /* * Set array of doubles "xarray" in the cdict. */ double xarray[3] = {100.0,200.0,300.0}; CDict_set_with_types(cdict, "array X of doubles",CDICT_DATA_TYPE_STRING, xarray,CDICT_DATA_TYPE_DOUBLE_ARRAY); ``` -------------------------------- ### Append to CDict Entry in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Appends a value to a CDict entry, with automatic type handling. Supports numeric addition and string concatenation. Useful for dynamically updating CDict values. ```c CDict_append(cdict,key,value); ``` -------------------------------- ### Custom Error Handling in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Defines a custom error handler function for libcdict. This function receives data, error number, and format arguments to process errors. The return value determines whether the error string is shown and if the program exits. ```c int cdict_error_handler_function(void * p, const int error_number, char * format, va_list args) { struct my_data_t * s = (struct my_data_t *)p; /* ... do something with s ... */ /* maybe show the default error string */ vprintf(format,args); /* * then exit(...) or longjmp(...) : * if you return, cdict will exit. */ exit(error_number); } struct my_data_t my_data_structure; c_dict->error_handler = cdict_error_handler_function; c_dict->error_data = &my_data_structure; ``` -------------------------------- ### Free CDict Memory in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Releases memory associated with a cdict. Options include freeing just the struct, the struct and its contents, or using a custom function for freeing entries. ```C CDict_free(c); ``` ```C CDict_free_and_free_contents(c); ``` ```C CDict_free_with_function(c,my_custom_function); ``` -------------------------------- ### Delete CDict Entry in C Source: https://gitlab.com/binary_c/binary_c/-/blob/master/src/libcdict/README.md Deletes an entry from a CDict, with options to also delete the entry's contents or just the reference. Useful for memory management and data cleanup. ```c CDict_del_and_contents(cdict,entry,TRUE); ``` ```c CDict_del_and_contents(cdict,entry,TRUE); ``` ```c CDict_del(cdict,entry); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.