Skip to content

Commit e01216a

Browse files
committed
Reresent the entries stored in memory in JSON like format (stringtify process)
1 parent 2a96c9b commit e01216a

File tree

4 files changed

+105
-28
lines changed

4 files changed

+105
-28
lines changed

src/cjlib.c

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222
#include <string.h>
2323
#include <limits.h>
2424
#include <ctype.h>
25+
#include <math.h>
2526

2627
#include "cjlib.h"
2728
#include "cjlib_error.h"
2829
#include "cjlib_dictionary.h"
2930
#include "cjlib_stack.h"
31+
#include "include/cjlib_dictionary.h"
32+
#include "include/cjlib_queue.h"
3033

3134
#define DOUBLE_QUOTES (0x22) // ASCII representative of "
3235
#define CURLY_BRACKETS_OPEN (0x7B) // ASCII representative of {
@@ -582,12 +585,6 @@ int cjlib_json_read(struct cjlib_json *restrict dst)
582585
.i_data.object = dst->c_dict
583586
};
584587

585-
// curr_incomplete_data.i_name = strdup(ROOT_PROPERTY_NAME);
586-
587-
// curr_incomplete_data.i_data.object = dst->c_dict;
588-
// curr_incomplete_data.i_type = CJLIB_OBJECT;
589-
590-
591588
if (NULL == curr_incomplete_data.i_name) goto read_err;
592589

593590

@@ -720,28 +717,93 @@ int cjlib_json_read(struct cjlib_json *restrict dst)
720717
return -1;
721718
}
722719

723-
const char *cjlib_json_object_stringtify(const cjlib_json_object *src)
720+
/**
721+
* Take the data of the current JSON field and its respective key as an argument
722+
* and produce a string that is in format KEY : DATA,
723+
*
724+
* @param src The data of the field.
725+
* @param key The respective key of the field.
726+
* @return A string representing an entry of the JSON file (Format KEY : DATA,) -> (KEY COLON DATA COMMA) in success,
727+
* otherwise NULL is returned.
728+
*/
729+
static char *key_value_paired_stringtify(const struct cjlib_json_data *restrict src, const char *restrict key)
724730
{
725-
(void) src;
731+
const size_t comma_len = 1;
732+
const size_t colon_len = 1;
733+
const size_t boolean_true_len = 4;
734+
const size_t boolean_false_len = 5;
735+
const size_t null_len = 4;
736+
size_t digit_num = 0;
737+
738+
char *result;
739+
switch (src->c_datatype) {
740+
case CJLIB_STRING:
741+
// LEN(KEY) + LEN(:) + LEN(VALUE) + LEN(,) + LEN("\0")
742+
result = (char *) malloc(strlen(key) + strlen(src->c_value.c_str) + comma_len + colon_len + 1);
743+
if (NULL == result) return NULL;
744+
sprintf(result, "%s:%s,", key, src->c_value.c_str);
745+
break;
746+
case CJLIB_NUMBER:
747+
digit_num = snprintf(NULL, 0, "%f", src->c_value.c_num);
748+
result = (char *) malloc(strlen(key) + digit_num + comma_len + colon_len + 1);
749+
if (NULL == result) return NULL;
750+
sprintf(result, "%s:%f,", key, src->c_value.c_num);
751+
break;
752+
case CJLIB_BOOLEAN:
753+
if (src->c_value.c_boolean) {
754+
result = (char *) malloc(strlen(key) + boolean_true_len + comma_len + colon_len + 1);
755+
if (NULL == result) return NULL;
756+
sprintf(result, "%s:%s,", key, "true");
757+
} else {
758+
result = (char *) malloc(strlen(key) + boolean_false_len + comma_len + colon_len + 1);
759+
if (NULL == result) return NULL;
760+
sprintf(result, "%s:%s,", key, "false");
761+
}
762+
break;
763+
case CJLIB_NULL:
764+
result = (char *) malloc(strlen(key) + null_len + comma_len + colon_len + 1);
765+
if (NULL == result) return NULL;
766+
sprintf(result, "%s:%s,", key, "null");
767+
break;
768+
case CJLIB_ARRAY:
769+
break;
770+
default:
771+
break;
772+
}
726773

727-
/*
728-
struct cjlib_queue object_data_q;
729-
struct cjlib_stack incomplete_data_stc;
730-
struct incomplete_property_str curr_incomp_p;
774+
return result;
775+
}
731776

732-
cjlib_queue_init(&object_data_q);
733-
cjlib_stack_init(&incomplete_data_stc);
777+
const char *cjlib_json_object_stringtify(const cjlib_json_object *src)
778+
{
779+
struct cjlib_queue object_data_q; // The queue where all nodes are stored.
780+
struct cjlib_queue pending_objects_q; // The queue where the object to be expanded are stored.
781+
782+
cjlib_dict_node_t *field; // The current key:paired field from the JSON in memory. (contains both the key and data)
783+
struct cjlib_json_data *field_data; // The data of the current field.
734784

735-
curr_incomp_p = (struct incomplete_property_str) {
736-
.i_state = (char *) malloc(OBJECT_STR_STATE_INIT_LEN),
737-
//.i_data.object =
738-
};
785+
// Initialize the queue.
786+
cjlib_queue_init(&object_data_q);
787+
cjlib_queue_init(&pending_objects_q);
739788

789+
// Returns Queue<cjlib_dict_node *>
790+
if (-1 == cjlib_dict_postorder(&object_data_q, src)) return NULL;
740791

741792
do {
742-
743-
} while (!cjlib_queue_is_empty(&object_data_q));
744-
*/
793+
cjlib_queue_deqeue((void *) &field, sizeof(cjlib_dict_node_t *), &object_data_q);
794+
795+
// obtain the data
796+
field_data = (struct cjlib_json_data *) CJLIB_DICT_NODE_DATA(field);
797+
798+
if (field_data->c_datatype == CJLIB_STRING) {
799+
char *result = key_value_paired_stringtify(field_data, CJLIB_DICT_NODE_KEY(field));
800+
(void) printf("%s\n", result); // TODO - REMOVE THIS
801+
802+
free(result);
803+
}
804+
} while(!cjlib_queue_is_empty(&object_data_q));
805+
806+
745807
return NULL;
746808
}
747809

src/cjlib_dictionary.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ int cjlib_dict_postorder(struct cjlib_queue *restrict dst, const struct avl_bs_t
110110

111111
do {
112112
if (NULL != root) {
113-
if (-1 == cjlib_queue_enqeue((void *) root, sizeof(struct cjlib_json_data), &post_order_data_q)) return -1;
113+
if (-1 == cjlib_queue_enqeue((void *) &root, sizeof(struct cjlib_json_data *), &post_order_data_q)) return -1;
114114
}
115115

116116
if (NULL != root && NULL != root->avl_left) {

src/include/cjlib_dictionary.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828

2929
struct cjlib_json_data;
3030

31+
// Requires a pointer and accesses the key of a node.
32+
#define CJLIB_DICT_NODE_KEY(NODE_PTR) NODE_PTR->avl_key
33+
34+
// Requires a pointer and accesses the data of a node.
35+
#define CJLIB_DICT_NODE_DATA(NODE_PTR) NODE_PTR->avl_data
36+
3137
/**
3238
* AVL Binary search tree node.
3339
*/
@@ -39,7 +45,13 @@ struct avl_bs_tree_node
3945
struct avl_bs_tree_node *avl_right; // The right child of the node.
4046
};
4147

42-
typedef struct avl_bs_tree_node cjlib_dict_t;
48+
/**
49+
* The following two types are the same. The only reason that there
50+
* are two distinct names for the same structure is to enchace the
51+
* readability of the codebose.
52+
*/
53+
typedef struct avl_bs_tree_node cjlib_dict_t; // Used to represent the whole dictionary.
54+
typedef struct avl_bs_tree_node cjlib_dict_node_t; // Used to represent a node of the dictionary (consisting of a key:value pair).
4355

4456
/**
4557
* initialize the dictionary.
@@ -60,10 +72,11 @@ static inline cjlib_dict_t *cjlib_make_dict(void)
6072
}
6173

6274
/**
63-
* Travel through the whole tree using the POST-ORDER method. For each node execute the
64-
* action callback.
75+
* Travel through the whole tree using the POST-ORDER method. This function builds a
76+
* queue that consists of all the available nodes in the dictionary of interest.
6577
*
66-
* @param src A pointer to the dictionary.
78+
* @param dst A pointer pointing to the memory where the built queue is stored.
79+
* @param src A pointer to the dictionary (or object) of interest.
6780
* @return -1 in case an error occur, otherwise -1.
6881
*/
6982
extern int cjlib_dict_postorder(struct cjlib_queue *restrict dst, const cjlib_dict_t *src);

tests/src/main.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int main(void)
6767
6868
(void) printf("%s\n", dst.c_value.c_str);*/
6969

70-
70+
/*
7171
if (-1 == cjlib_json_get(&dst, &json_file, "configurations")) {
7272
(void) printf("Error\n");
7373
exit(-1);
@@ -84,8 +84,10 @@ int main(void)
8484
}
8585
8686
(void) printf("%s\n", dst.c_value.c_str);
87+
*/
88+
8789

88-
//cjlib_json_stringtify(&json_file);
90+
cjlib_json_stringtify(&json_file);
8991

9092
// // Close the json file.
9193
cjlib_json_close(&json_file);

0 commit comments

Comments
 (0)