-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.c
More file actions
225 lines (206 loc) · 4.59 KB
/
json.c
File metadata and controls
225 lines (206 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include "json.h"
#include "clog.h"
#include <limits.h>
#include <string.h>
bool parse_int(int* a_value, char const** a_pos) {
bool int_n = false;
bool neg_n = false;
//int nm = 0;
if(**a_pos == '-') {
(*a_pos)+=1;
neg_n = true;
}
if(isdigit(**a_pos)) {
int_n = true;
}
int numbb = 0;
//*a_value = numbb; code fails here
while(isdigit(**a_pos)) {
int nm = (**a_pos) - '0';
numbb = nm + (10 * numbb);
(*a_pos) += 1;
}
*a_value = numbb; //code passes here
if(neg_n && int_n) {
*a_value = -1 * (*a_value);
}
return int_n;
}
bool parse_string(char** a_s, char const** a_pos){ //passing 18, 0 compiler errors, 1 fail maybe int invalid ?
int sz = 0; //size
bool booln;
booln = false;
if(**a_pos == '\"') {
booln = true;
(*a_pos)++;
}
while((**a_pos) != '\"'){
log_char(**a_pos);
if((**a_pos) == '\0' || ((**a_pos) == '\n')){
booln = false;
//break; //check that again
}
//might need an else statement
sz++;
(*a_pos)++;
}
if(booln){
char* otp = malloc(sizeof(**a_s) * (sz+1));
(*a_pos) -= sz;
for(int id =0; id<sz; id++){
(otp)[id] = **a_pos;
(*a_pos)++;
}
otp[sz] = '\0';
(*a_pos)++;
*a_s = otp;
}
return booln;
}
bool parse_element(Element* a_element, char const** a_pos){ // same twice
while(isspace(**a_pos)){
(*a_pos)++;
}
if((isdigit(**a_pos))|| (**a_pos == '-') ){
a_element -> type = ELEMENT_INT;
return parse_int(&(a_element -> as_int), a_pos);
}
else if((**a_pos) == '\"'){
bool alp;
alp = parse_string(&(a_element -> as_string), a_pos);
if(alp) {
a_element -> type = ELEMENT_STRING;
return true;
}
else {
a_element -> type = ELEMENT_INT;
return false;
}
}
else if((**a_pos) == '['){
a_element -> type = ELEMENT_LIST;
return parse_list(&(a_element -> as_list), a_pos);
}
return false;
}
void _free_mem(Node* head) { //helper function to free list ( call it in free element)
while(head != NULL) {
Node* temp = head;
head = head -> next;
free_element(temp -> element);
free(temp);
}
}
void free_element(Element element){ // function to deallocate meory
//
if(element.type == ELEMENT_STRING) {
//deallocating memory wrong
//if(ELEMENT_STRING){
free(element.as_string);
//}
}
else if(element.type == ELEMENT_LIST){
Node* head = element.as_list;
_free_mem(head);
/*while(head != NULL) {
Node* new_head = head;
head = head -> next;
free_element(temp -> element); //breaks here : undeclared 'temp'
free(new_head);
}*/
}
}
/*void print_element(Element element){ //
if(element.type == ELEMENT_INT){
printf("%d", element.as_int);
}
else if(element.type == ELEMENT_STRING){
printf("\"%s\"", element.as_string);
}*/
void print_element(Element element) {
//printf("r");
if(element.type == ELEMENT_INT) {
printf("%d", element.as_int);
}
else if(element.type == ELEMENT_STRING) {
printf("\"%s\"", element.as_string);
}
else if(element.type == ELEMENT_LIST) {
printf("[");
//printf("x");
for(Node* ind = element.as_list; ind != NULL; ind = ind-> next) {
//printf("f");
print_element(ind -> element);
if(ind -> next != NULL) {
printf(",");
}
}
printf("]");
}
}
// Passing 8/14 without parse_list // //passing 13/14, check coverage again // check valgrind issue //42/43 with valrgind and partial
bool parse_list(Node** a_head, char const** a_pos){
bool lsd = true;
if((**a_pos) == '['){
(*a_pos)++;
Node* new_h = NULL; //
Node* tail = NULL;
while((**a_pos) != ']'){
if((**a_pos) == ','){
(*a_pos)++;
if((**a_pos) == ','){
lsd = false;
_free_mem(new_h);
break;
}
/* if(new_h == NULL){
new_h = new_n;
tail = new_n;
}*/
}
Node* newnode = malloc(sizeof(*newnode));
newnode -> next = NULL;
bool is_success = parse_element(&(newnode -> element), a_pos);// holds bool
if(new_h == NULL){
tail = newnode;
new_h = newnode;
}
else{
tail -> next = newnode; // ????
tail = newnode;
}
while(isspace(**a_pos)){
(*a_pos)++;
}
if(**a_pos != ',' && **a_pos != ']') {
_free_mem(new_h);
lsd = false;
break;
}
if(!is_success) { //if fails it will free space for new node and head
_free_mem(new_h); // check if we need to free new node
lsd = false;
break;
}
/*while(isspace(**a_pos)){
(*a_pos)++;
}*/
}
*a_head = new_h;
(*a_pos)++;
}
else{
lsd = false;
}
if(!lsd) {
*a_head = NULL;
//free_element(element);
}
return lsd;
}
//add helper function to free list, and call function in free_element