Skip to content

Commit 2183d7a

Browse files
committed
Almost done with stritifying process, one bug remains
1 parent b860495 commit 2183d7a

File tree

2 files changed

+86
-16
lines changed

2 files changed

+86
-16
lines changed

src/cjlib.c

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ static inline char *incomplete_property_str_expand_state
110110
if (NULL == new_state) return NULL;
111111

112112
if (NULL == key) {
113-
sprintf(new_state, "%s%s", state, data);
113+
(void) sprintf(new_state, "%s%s", state, data);
114114
} else {
115-
sprintf(new_state, "%s%s:%s", state, key, data);
115+
(void) sprintf(new_state, "%s%s:%s", state, key, data);
116116
}
117117

118118
return new_state;
@@ -746,6 +746,32 @@ int cjlib_json_read(struct cjlib_json *restrict dst)
746746
return -1;
747747
}
748748

749+
/**
750+
* Enclose a state which represent a complete JSON entry with its
751+
* respective opening and closing symbol. For example is the
752+
* completed state were an object, then the opening symbol is { (curly brackets open)
753+
* and the closing symbol is } (curly bracktes close)
754+
*
755+
* @param entry_state The state of the complete entry.
756+
* @param opening_symbol The opening symbol of the entry.
757+
* @param closing_symbol The closing symbol of the entry.
758+
* @returns A string representing the opening_symbol entry closing_symbol on success.
759+
* Otherwise NULL.
760+
*/
761+
static inline char *wrap_complete_entry(const char *entry_state, char opening_symbol,
762+
char closing_symbol)
763+
{
764+
size_t additional_size = 3; // The size of {} or [].
765+
size_t wrapped_size = strlen(entry_state) + additional_size;
766+
767+
char *wrapped_state = (char *) malloc(wrapped_size + 1);
768+
if (NULL == wrapped_state) return NULL;
769+
770+
(void) sprintf(wrapped_state, "%c%s%c,", opening_symbol, entry_state, closing_symbol);
771+
772+
return wrapped_state;
773+
}
774+
749775
/**
750776
* Take the data of the current JSON field and its respective key as an argument
751777
* and produce a string that is in format KEY : DATA,
@@ -762,37 +788,38 @@ static char *simple_key_value_paired_stringtify
762788
const size_t boolean_true_len = 4;
763789
const size_t boolean_false_len = 5;
764790
const size_t null_len = 4;
791+
const size_t double_quotes_len = 2;
765792
size_t digit_num = 0;
766793

767794
char *result = NULL;
768795
switch (src->c_datatype) {
769796
case CJLIB_STRING:
770797
// LEN(KEY) + LEN(:) + LEN(VALUE) + LEN(,) + LEN("\0")
771-
result = (char *) malloc(strlen(src->c_value.c_str) + comma_len + colon_len + 1);
798+
result = (char *) malloc(strlen(src->c_value.c_str) + comma_len + colon_len + double_quotes_len + 1);
772799
if (NULL == result) return NULL;
773-
sprintf(result, "%s,", src->c_value.c_str);
800+
(void) sprintf(result, "\"%s\",", src->c_value.c_str);
774801
break;
775802
case CJLIB_NUMBER:
776803
digit_num = snprintf(NULL, 0, "%f", src->c_value.c_num);
777804
result = (char *) malloc(digit_num + comma_len + colon_len + 1);
778805
if (NULL == result) return NULL;
779-
sprintf(result, "%f,", src->c_value.c_num);
806+
(void) sprintf(result, "%f,", src->c_value.c_num);
780807
break;
781808
case CJLIB_BOOLEAN:
782809
if (src->c_value.c_boolean) {
783810
result = (char *) malloc(boolean_true_len + comma_len + colon_len + 1);
784811
if (NULL == result) return NULL;
785-
sprintf(result, "%s,", "true");
812+
(void) sprintf(result, "%s,", "true");
786813
} else {
787814
result = (char *) malloc(boolean_false_len + comma_len + colon_len + 1);
788815
if (NULL == result) return NULL;
789-
sprintf(result, "%s,", "false");
816+
(void) sprintf(result, "%s,", "false");
790817
}
791818
break;
792819
case CJLIB_NULL:
793820
result = (char *) malloc(null_len + comma_len + colon_len + 1);
794821
if (NULL == result) return NULL;
795-
sprintf(result, "%s,", "null");
822+
(void) sprintf(result, "%s,", "null");
796823
break;
797824
default:
798825
break;
@@ -814,9 +841,18 @@ static inline int switch_from_incomplete_obj_str_data
814841
(struct incomplete_property_str *restrict dst, enum cjlib_json_datatypes type,
815842
cjlib_json_object *entry)
816843
{
844+
char opening_symbol_str = DOUBLE_QUOTES;
845+
char closing_symbol_str = DOUBLE_QUOTES;
846+
size_t key_wrapped_size = strlen(CJLIB_DICT_NODE_KEY(entry)) + 2;
847+
char *key_wrapped = (char *) malloc(key_wrapped_size + 1);
848+
if (NULL == key_wrapped) return -1;
849+
850+
(void) sprintf(key_wrapped, "%c%s%c", opening_symbol_str, CJLIB_DICT_NODE_KEY(entry),
851+
closing_symbol_str);
852+
817853
struct cjlib_json_data *examine_entry_data = CJLIB_DICT_NODE_DATA(entry);
818854
*dst = (struct incomplete_property_str) {
819-
.i_key = strdup(CJLIB_DICT_NODE_KEY(entry)),
855+
.i_key = key_wrapped,
820856
.i_type = type,
821857
.i_state = strdup(""),
822858
.i_pending_data_q = (struct cjlib_queue *) malloc(sizeof(struct cjlib_queue))
@@ -836,9 +872,18 @@ static inline int switch_from_incomplete_arr_str_data
836872
(struct incomplete_property_str *restrict dst, enum cjlib_json_datatypes type,
837873
struct cjlib_json_data *entry_data, const char *key)
838874
{
875+
char opening_symbol_str = DOUBLE_QUOTES;
876+
char closing_symbol_str = DOUBLE_QUOTES;
877+
size_t key_wrapped_size = strlen(key) + 2;
878+
char *key_wrapped = (char *) malloc(key_wrapped_size + 1);
879+
if (NULL == key_wrapped) return -1;
880+
881+
(void) sprintf(key_wrapped, "%c%s%c", opening_symbol_str, key,
882+
closing_symbol_str);
883+
839884
struct cjlib_json_data *examine_entry_data = entry_data;
840885
*dst = (struct incomplete_property_str) {
841-
.i_key = strdup(key),
886+
.i_key = key_wrapped,
842887
.i_type = type,
843888
.i_state = strdup(""),
844889
.i_pending_data_q = (struct cjlib_queue *) malloc(sizeof(struct cjlib_queue))
@@ -865,7 +910,7 @@ struct cjlib_json_data *entry_data, const char *key)
865910
* @param src A pointer to the list from which the paointers will be extracted.
866911
* @return 0 on success, otherwise -1.
867912
*/
868-
static int convert_list_to_queue(struct cjlib_queue *restrict dst, const struct cjlib_list *restrict src)
913+
static inline int convert_list_to_queue(struct cjlib_queue *restrict dst, const struct cjlib_list *restrict src)
869914
{
870915
struct cjlib_json_data *item;
871916

@@ -879,8 +924,13 @@ static int convert_list_to_queue(struct cjlib_queue *restrict dst, const struct
879924
return 0;
880925
}
881926

927+
882928
const char *cjlib_json_object_stringtify(const cjlib_json_object *src)
883929
{
930+
/**
931+
* TODO - 1. Why only a few kies are wrapped with double quotes, while the majority isn't
932+
* 2. Why some fields don't appear in the stringtifying version? (look type field in the JSON).
933+
*/
884934
struct incomplete_property_str curr_incomp; // The currently expanding data.
885935
struct cjlib_stack incomplete_st; // The stack of incomplete data.
886936

@@ -890,6 +940,14 @@ const char *cjlib_json_object_stringtify(const cjlib_json_object *src)
890940
char *tmp_state = NULL; // Used to control the memory (see default case in the switch below)
891941
char *tmp_key = NULL; // Used to temporary store the key of the complete JSON object/array.
892942
char *value_str = NULL; // Used to temporary store the strintify data (see default case in the switch below)
943+
944+
char opening_symbol_obj = CURLY_BRACKETS_OPEN;
945+
char opening_symbol_arr = SQUARE_BRACKETS_OPEN;
946+
char closing_symbol_obj = CURLY_BRACKETS_CLOSE;
947+
char closing_symbol_arr = SQUARE_BRACKETS_CLOSE;
948+
949+
char opening_symbol;
950+
char closing_symbol;
893951

894952
// Initiazize the incomplete data.
895953
incomplete_property_str_init(&curr_incomp);
@@ -948,7 +1006,7 @@ const char *cjlib_json_object_stringtify(const cjlib_json_object *src)
9481006
case CJLIB_ARRAY:
9491007
cjlib_stack_push(&curr_incomp, sizeof(struct incomplete_property_str), &incomplete_st);
9501008

951-
if (-1 == switch_from_incomplete_arr_str_data(&curr_incomp, CJLIB_ARRAY, examine_entry_data, curr_incomp.i_key)) return NULL;
1009+
if (-1 == switch_from_incomplete_arr_str_data(&curr_incomp, CJLIB_ARRAY, examine_entry_data, CJLIB_DICT_NODE_KEY(examine_entry))) return NULL;
9521010

9531011
convert_list_to_queue(curr_incomp.i_pending_data_q, curr_incomp.i_data.array);
9541012
break;
@@ -971,7 +1029,19 @@ const char *cjlib_json_object_stringtify(const cjlib_json_object *src)
9711029
}
9721030
}
9731031

974-
value_str = curr_incomp.i_state; // Save the pointer that points to the state of the complete object/array.
1032+
opening_symbol = (CJLIB_OBJECT == curr_incomp.i_type)? opening_symbol_obj : opening_symbol_arr;
1033+
closing_symbol = (CJLIB_OBJECT == curr_incomp.i_type)? closing_symbol_obj : closing_symbol_arr;
1034+
1035+
if (CJLIB_OBJECT == curr_incomp.i_type || CJLIB_ARRAY == curr_incomp.i_type) {
1036+
tmp_state = curr_incomp.i_state;
1037+
curr_incomp.i_state = wrap_complete_entry(curr_incomp.i_state, opening_symbol, closing_symbol);
1038+
1039+
free(tmp_state);
1040+
if (NULL == curr_incomp.i_state) return NULL;
1041+
}
1042+
1043+
value_str = curr_incomp.i_state;
1044+
9751045
// TODO - wrap the array/object with the correct {} or [].
9761046
tmp_key = curr_incomp.i_key;
9771047
// Free the pending queue (there are no more incomplete data for the JSON object/array that were examined).
@@ -981,6 +1051,7 @@ const char *cjlib_json_object_stringtify(const cjlib_json_object *src)
9811051

9821052
free(curr_incomp.i_key);
9831053

1054+
(void) printf("%s\n", curr_incomp.i_state);
9841055
// Return the now completed JSON.
9851056
return curr_incomp.i_state;
9861057
}

tests/src/main.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,12 @@ int main(void)
7878
exit(-1);
7979
}
8080
81-
if (-1 == cjlib_json_object_get(&dst, dst.c_value.c_obj, "program")) {
81+
if (-1 == cjlib_json_object_get(&dst, dst.c_value.c_obj, "cwd")) {
8282
(void) printf("Error\n");
8383
exit(-1);
8484
}
8585
86-
(void) printf("%s\n", dst.c_value.c_str);
87-
*/
86+
(void) printf("%s\n", dst.c_value.c_str);*/
8887

8988

9089
cjlib_json_stringtify(&json_file);

0 commit comments

Comments
 (0)