|
22 | 22 | #include <string.h> |
23 | 23 | #include <limits.h> |
24 | 24 | #include <ctype.h> |
| 25 | +#include <math.h> |
25 | 26 |
|
26 | 27 | #include "cjlib.h" |
27 | 28 | #include "cjlib_error.h" |
28 | 29 | #include "cjlib_dictionary.h" |
29 | 30 | #include "cjlib_stack.h" |
| 31 | +#include "include/cjlib_dictionary.h" |
| 32 | +#include "include/cjlib_queue.h" |
30 | 33 |
|
31 | 34 | #define DOUBLE_QUOTES (0x22) // ASCII representative of " |
32 | 35 | #define CURLY_BRACKETS_OPEN (0x7B) // ASCII representative of { |
@@ -582,12 +585,6 @@ int cjlib_json_read(struct cjlib_json *restrict dst) |
582 | 585 | .i_data.object = dst->c_dict |
583 | 586 | }; |
584 | 587 |
|
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 | | - |
591 | 588 | if (NULL == curr_incomplete_data.i_name) goto read_err; |
592 | 589 |
|
593 | 590 |
|
@@ -720,28 +717,93 @@ int cjlib_json_read(struct cjlib_json *restrict dst) |
720 | 717 | return -1; |
721 | 718 | } |
722 | 719 |
|
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) |
724 | 730 | { |
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 | + } |
726 | 773 |
|
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 | +} |
731 | 776 |
|
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. |
734 | 784 |
|
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); |
739 | 788 |
|
| 789 | + // Returns Queue<cjlib_dict_node *> |
| 790 | + if (-1 == cjlib_dict_postorder(&object_data_q, src)) return NULL; |
740 | 791 |
|
741 | 792 | 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 | + |
745 | 807 | return NULL; |
746 | 808 | } |
747 | 809 |
|
|
0 commit comments