-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
202 lines (163 loc) · 5.43 KB
/
Copy pathexample.c
File metadata and controls
202 lines (163 loc) · 5.43 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
#include <stdio.h>
#include <stdlib.h>
#define JSON_IMPLEMENTATION
#include "./json.h"
char *read_file(const char *filename);
int main(int argc, char **argv) {
// Read JSON from file
char *mission_json = read_file("example.json");
if (!mission_json) {
return 1;
}
printf("=== ARTEMIS III MISSION DATA PARSER ===\n\n");
// Example 1: Extract mission overview (6 fields in one call)
printf("1. MISSION OVERVIEW\n");
printf("-------------------\n");
const char *mission_keys[] = {
"mission.name",
"mission.launch_date",
"mission.status",
"mission.duration_days",
"mission.budget_millions",
"mission.is_crewed"
};
char **mission_data = json_gets(mission_json, mission_keys, 6);
if (mission_data) {
printf("Mission: %s\n", mission_data[0]);
printf("Launch Date: %s\n", mission_data[1]);
printf("Status: %s\n", mission_data[2]);
printf("Duration: %s days | Budget: $%s million | Crewed: %s\n",
mission_data[3], mission_data[4], mission_data[5]);
for (int i = 0; i < 6; i++) free(mission_data[i]);
free(mission_data);
}
// Example 2: Array access - fetch all crew members
printf("\n2. CREW ROSTER (array access)\n");
printf("------------------------------\n");
const char *crew_keys[] = {
"crew.0.name", "crew.0.role",
"crew.1.name", "crew.1.role",
"crew.2.name", "crew.2.role",
"crew.3.name", "crew.3.role"
};
char **crew = json_gets(mission_json, crew_keys, 8);
if (crew) {
for (int i = 0; i < 4; i++) {
printf(" %s - %s\n", crew[i*2], crew[i*2+1]);
}
for (int i = 0; i < 8; i++) free(crew[i]);
free(crew);
}
// Example 3: Deep nesting - spacecraft systems
printf("\n3. SPACECRAFT SYSTEMS (deep nesting)\n");
printf("-------------------------------------\n");
const char *spacecraft_keys[] = {
"spacecraft.name",
"spacecraft.manufacturer",
"spacecraft.systems.propulsion",
"spacecraft.systems.life_support",
"spacecraft.systems.power"
};
char **spacecraft = json_gets(mission_json, spacecraft_keys, 5);
if (spacecraft) {
printf("%s (by %s)\n", spacecraft[0], spacecraft[1]);
printf(" Propulsion: %s\n", spacecraft[2]);
printf(" Life Support: %s\n", spacecraft[3]);
printf(" Power: %s\n", spacecraft[4]);
for (int i = 0; i < 5; i++) free(spacecraft[i]);
free(spacecraft);
}
// Example 4: Coordinates and numeric values
printf("\n4. LANDING COORDINATES\n");
printf("----------------------\n");
const char *location_keys[] = {
"landing_site.name",
"landing_site.coordinates.latitude",
"landing_site.coordinates.longitude",
"landing_site.elevation_meters"
};
char **location = json_gets(mission_json, location_keys, 4);
if (location) {
printf("Site: %s\n", location[0]);
printf("Coordinates: %s°, %s° | Elevation: %s m\n",
location[1], location[2], location[3]);
for (int i = 0; i < 4; i++) free(location[i]);
free(location);
}
// Example 5: String array values
printf("\n5. MISSION OBJECTIVES\n");
printf("---------------------\n");
const char *obj_keys[] = {
"objectives.0", "objectives.1", "objectives.2", "objectives.3"
};
char **objectives = json_gets(mission_json, obj_keys, 4);
if (objectives) {
for (int i = 0; i < 4; i++) {
printf(" %d. %s\n", i+1, objectives[i]);
}
for (int i = 0; i < 4; i++) free(objectives[i]);
free(objectives);
}
// Example 6: Mixed data from different sections
printf("\n6. MISSION SUMMARY (mixed paths)\n");
printf("---------------------------------\n");
const char *summary_keys[] = {
"mission.name",
"crew.0.name",
"spacecraft.name",
"landing_site.name"
};
char **summary = json_gets(mission_json, summary_keys, 4);
if (summary) {
printf("%s: Commander %s will pilot %s to %s\n",
summary[0], summary[1], summary[2], summary[3]);
for (int i = 0; i < 4; i++) free(summary[i]);
free(summary);
}
// Example 7: Single value with json_get()
printf("\n7. SINGLE VALUE (json_get)\n");
printf("--------------------------\n");
char *commander = json_get(mission_json, "crew.0.specialization");
printf("Commander specialization: %s\n", commander ? commander : "N/A");
free(commander);
// Example 8: Null/missing values
printf("\n8. ERROR HANDLING\n");
printf("-----------------\n");
const char *test_keys[] = {
"mission.name",
"invalid.key",
"crew.99.name"
};
char **test = json_gets(mission_json, test_keys, 3);
if (test) {
printf("Valid: %s\n", test[0] ? test[0] : "NULL");
printf("Invalid key: %s\n", test[1] ? test[1] : "NULL");
printf("Out of bounds: %s\n", test[2] ? test[2] : "NULL");
for (int i = 0; i < 3; i++) free(test[i]);
free(test);
}
printf("\n=== END OF DEMO ===\n");
free(mission_json);
return 0;
}
// Helper function to read entire file into a string
char *read_file(const char *filename) {
FILE *file = fopen(filename, "r");
if (!file) {
fprintf(stderr, "Error: Could not open file '%s'\n", filename);
return NULL;
}
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
char *buffer = (char *)malloc(file_size + 1);
if (!buffer) {
fprintf(stderr, "Error: Memory allocation failed\n");
fclose(file);
return NULL;
}
size_t bytes_read = fread(buffer, 1, file_size, file);
buffer[bytes_read] = '\0';
fclose(file);
return buffer;
}