Skip to content

Commit 80877f6

Browse files
committed
Start making the stringtify function
1 parent 8bbd6c5 commit 80877f6

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

include/cjlib.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,9 @@ extern int cjlib_json_read(struct cjlib_json *restrict dst);
348348
* @return on success, a pointer at the start of a string that represent the string version
349349
* of the given json file. Otherwise, null.
350350
*/
351-
extern const char *cjlib_json_object_stringtify(const cjlib_json_object *restrict src);
351+
extern const char *cjlib_json_object_stringtify(const cjlib_json_object *src);
352352

353-
static inline const char *cjlib_json_stringtify(struct cjlib_json *restrict src)
353+
static inline const char *cjlib_json_stringtify(struct cjlib_json *src)
354354
{
355355
return cjlib_json_object_stringtify(src->c_dict);
356356
}

src/cjlib.c

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
#define ROOT_PROPERTY_NAME ("") // A name used to represent the beginning of the JSON. !NO OTHER PROPERTY MUST OBTAIN THIS NAME EXCEPT ROOT~
4646

47+
#define OBJECT_STR_STATE_INIT_LEN 1000 // The initial length of the state string (see strintify function)
4748

4849

4950
// Indicated whether the currently incomplete attribute is an object.
@@ -65,6 +66,20 @@ struct incomplete_property
6566
} i_data;
6667
};
6768

69+
/**
70+
* Used to describe an incomplete object or array in the process
71+
* of stringtifing the object/array stored in the RAM
72+
*/
73+
struct incomplete_property_str
74+
{
75+
char *i_state; // How much of the object/array is complete.
76+
union
77+
{
78+
cjlib_json_object *object;
79+
cjlib_json_array *array;
80+
} i_data;
81+
};
82+
6883
static void incomplete_property_init(struct incomplete_property *src)
6984
{
7085
(void) memset(src, 0x0, sizeof(struct incomplete_property));
@@ -561,11 +576,20 @@ int cjlib_json_read(struct cjlib_json *restrict dst)
561576
incomplete_property_init(&tmp_data);
562577
incomplete_property_init(&curr_incomplete_data);
563578

564-
curr_incomplete_data.i_name = strdup(ROOT_PROPERTY_NAME); // cause is the root object.
579+
curr_incomplete_data = (struct incomplete_property) {
580+
.i_name = strdup(ROOT_PROPERTY_NAME), // cause is the root object
581+
.i_type = CJLIB_OBJECT,
582+
.i_data.object = dst->c_dict
583+
};
584+
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+
565591
if (NULL == curr_incomplete_data.i_name) goto read_err;
566592

567-
curr_incomplete_data.i_data.object = dst->c_dict;
568-
curr_incomplete_data.i_type = CJLIB_OBJECT;
569593

570594
// Push the first incomplete object into the stack.
571595
if (-1 == cjlib_stack_push((void *) &curr_incomplete_data, sizeof(struct incomplete_property),
@@ -696,14 +720,25 @@ int cjlib_json_read(struct cjlib_json *restrict dst)
696720
return -1;
697721
}
698722

699-
const char *cjlib_json_object_stringtify(const cjlib_json_object *restrict src)
723+
const char *cjlib_json_object_stringtify(const cjlib_json_object *src)
700724
{
701-
struct cjlib_queue object_data_q;
725+
struct cjlib_queue object_data_q;
726+
struct cjlib_stack incomplete_data_stc;
727+
struct incomplete_property_str curr_incomp_p;
728+
702729
cjlib_queue_init(&object_data_q);
730+
cjlib_stack_init(&incomplete_data_stc);
703731

704-
if (-1 == cjlib_dict_postorder(&object_data_q, (cjlib_json_object *) src)) return NULL;
732+
curr_incomp_p = (struct incomplete_property_str) {
733+
.i_state = (char *) malloc(OBJECT_STR_STATE_INIT_LEN),
734+
.i_data.object =
735+
};
736+
737+
738+
do {
739+
740+
} while (!cjlib_queue_is_empty(&object_data_q))
705741

706-
// TODO - implement the algorithm.
707742

708743
return NULL;
709744
}

src/cjlib_dictionary.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ enum rotation_after_left_deletion_type
9696
L_MINUS_1 = -0x1
9797
};
9898

99+
/**
100+
* Set every node of a AVL tree into a QEUEUE
101+
*/
99102
int cjlib_dict_postorder(struct cjlib_queue *restrict dst, const struct avl_bs_tree_node *src)
100103
{
101104
struct cjlib_queue post_order_node_q; // The queue used for the post order traversal.

0 commit comments

Comments
 (0)