/* Find and print one item using default serialization */ int col_print_item(struct collection_item *handle, const char *name) { struct col_serial_data buf_data; int error = EOK; TRACE_FLOW_STRING("col_print_item", "Entry"); printf("PRINT ITEM:\n"); buf_data.buffer = NULL; buf_data.length = 0; buf_data.size = 0; buf_data.nest_level = 0; error = col_get_item_and_do(handle, name, COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, col_serialize, &buf_data); if(error) printf("Error searching collection %d\n", error); else { if (buf_data.buffer != NULL) { if (buf_data.length > 0) buf_data.length--; buf_data.buffer[buf_data.length] = '\0', printf("%s\n", buf_data.buffer); free(buf_data.buffer); } else { printf("Name %s is not found in the collection %s.\n", name, handle->property); } } TRACE_FLOW_NUMBER("col_print_item returning", error); return error; }
/* Create a buffer containg JSON serialization */ int col_json_collection(struct collection_item *handle, char **storage) { struct col_serial_data buf_data; int error = EOK; TRACE_FLOW_STRING("col_json_collection", "Entry"); if (storage == NULL) { TRACE_ERROR_STRING("Error.", "Storage data is not passed in!"); return EINVAL; } *storage = NULL; buf_data.buffer = NULL; buf_data.length = 0; buf_data.size = 0; buf_data.nest_level = 0; /* Traverse collection */ error = col_traverse_collection(handle, COL_TRAVERSE_DEFAULT | COL_TRAVERSE_END , col_json, (void *)(&buf_data)); if (error) TRACE_ERROR_NUMBER("Error traversing collection ", error); else *storage = buf_data.buffer; TRACE_FLOW_NUMBER("col_json_collection returning", error); return error; }
/* Print the collection using default serialization */ int col_print_collection(struct collection_item *handle) { struct col_serial_data buf_data; int error = EOK; TRACE_FLOW_STRING("col_print_collection", "Entry"); printf("COLLECTION:\n"); buf_data.buffer = NULL; buf_data.length = 0; buf_data.size = 0; buf_data.nest_level = 0; /* Traverse collection */ error = col_traverse_collection(handle, COL_TRAVERSE_DEFAULT | COL_TRAVERSE_END , col_serialize, (void *)(&buf_data)); if (error) printf("Error traversing collection %d\n", error); else printf("%s\n", buf_data.buffer); free(buf_data.buffer); TRACE_FLOW_NUMBER("col_print_collection returning", error); return error; }
static unsigned long get_checked_value(struct collection_item *metadata, const char *key, int *err) { int error = EOK; struct collection_item *item = NULL; unsigned long value; TRACE_FLOW_STRING("get_checked_value", "Entry"); TRACE_INFO_STRING("Key", key); error = get_config_item(INI_META_SEC_ACCESS, key, metadata, &item); if (error) { TRACE_ERROR_NUMBER("Internal collection error.", error); *err = error; return 0; } /* Entry is supposed to be there so it is an error * is the item is not found. */ if (item == NULL) { TRACE_ERROR_NUMBER("Expected item is not found.", ENOENT); *err = ENOENT; return 0; } value = get_ulong_config_value(item, 1, -1, &error); if ((error) || (value == -1)) { TRACE_ERROR_NUMBER("Conversion failed", EINVAL); *err = EINVAL; return 0; } *err = 0; TRACE_FLOW_NUMBER("get_checked_value Returning", value); return value; }
int stack_test(void) { struct collection_item *stack = NULL; char binary_dump[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; struct collection_item *item1 = NULL; struct collection_item *item2 = NULL; int error = EOK; TRACE_FLOW_STRING("stack_test", "Entry."); COLOUT(printf("\n\nSTACK TEST!!!.\n\n\n")); if ((error = col_create_stack(&stack)) || (error = col_push_str_property(stack, "item1", "value 1", 0)) || (error = col_push_int_property(stack, "item2", -1)) || (error = col_push_unsigned_property(stack, "item3", 1)) || (error = col_push_long_property(stack, "item4", 100)) || (error = col_push_ulong_property(stack, "item5", 1000)) || (error = col_push_double_property(stack, "item6", 1.1)) || (error = col_push_bool_property(stack, "item7", 1)) || (error = col_push_binary_property(stack, "item8", binary_dump, sizeof(binary_dump)))) { printf("Failed to push property. Error %d\n", error); col_destroy_collection(stack); return error; } COLOUT(col_debug_collection(stack, COL_TRAVERSE_DEFAULT)); COLOUT(printf("Swapping last two items by popping and pushing them back.\n")); if ((error = col_pop_item(stack, &item1)) || (error = col_pop_item(stack, &item2))) { printf("Failed to pop items. Error %d\n", error); col_destroy_collection(stack); return error; } COLOUT(printf("\nPopped two last items.\n")); COLOUT(col_debug_collection(stack, COL_TRAVERSE_DEFAULT)); COLOUT(printf("\nLast item.\n")); COLOUT(col_debug_item(item1)); COLOUT(printf("\nPrevious item.\n")); COLOUT(col_debug_item(item2)); if ((error = col_push_item(stack, item1)) || (error = col_push_item(stack, item2))) { printf("Failed to pop or push items. Error %d\n", error); col_destroy_collection(stack); return error; } COLOUT(printf("\n\nPushed two items again in reverse order.\n\n")); COLOUT(col_debug_collection(stack, COL_TRAVERSE_DEFAULT)); col_destroy_collection(stack); TRACE_FLOW_NUMBER("stack_test. Returning", error); COLOUT(printf("\n\nEND OF STACK TEST!!!.\n\n")); return error; }