commit 31e13818a27bc2c2eefc15379033414b8bee433d
parent 161a2a5a6d2eb679e010a84cf29793e1cb75ef9f
Author: libredev <libredev@ircforever.org>
Date: Tue, 3 Jan 2023 17:14:30 +0530
handle array of sources
Diffstat:
M | main.c | | | 63 | ++++++++++++++++++++++++++++++++++++++++++++------------------- |
1 file changed, 44 insertions(+), 19 deletions(-)
diff --git a/main.c b/main.c
@@ -55,6 +55,7 @@ struct source {
struct icestats icestats;
struct source *sources = NULL;
int sources_length = 0;
+int source_active = FALSE;
char *
read_file(const char* fname)
@@ -265,34 +266,58 @@ print_navigation_bar(int index)
}
void
-json_handle(json_stream *json)
+source_increment(void)
{
- if (json_next(json) != JSON_OBJECT) {
- printf("ERROR: Invalid json key type.\n");
+ sources_length++;
+ sources = realloc(sources, sources_length * sizeof(struct source));
+ if (sources == NULL) {
+ printf("ERROR: relloc: %s\n", strerror(errno));
+ exit(1);
+ }
+ memset(&sources[sources_length-1], 0, sizeof(struct source));
+}
+
+void json_handle_object(json_stream *json, enum json_type jtype);
+
+void
+json_handle_source(json_stream *json, enum json_type jtype)
+{
+ if (jtype == JSON_ARRAY) {
+ while (json_peek(json) != JSON_ARRAY_END
+ && !json_get_error(json)) {
+ source_increment();
+ source_active = TRUE;
+ json_handle_object(json, json_next(json));
+ source_active = FALSE;
+ }
+ json_next(json);
+ } else if (jtype == JSON_OBJECT) {
+ source_increment();
+ source_active = TRUE;
+ json_handle_object(json, JSON_OBJECT);
+ source_active = FALSE;
+ } else {
+ printf("ERROR: json_handle_source: invalid json_type.\n");
exit(EXIT_FAILURE);
}
- static int source_index = 0;
- static int source_active = FALSE;
+}
+void
+json_handle_object(json_stream *json, enum json_type jtype)
+{
+ if (jtype != JSON_OBJECT) {
+ printf("ERROR: json_handle_object: invalid json_type.\n");
+ exit(EXIT_FAILURE);
+ }
while (json_peek(json) != JSON_OBJECT_END && !json_get_error(json)) {
json_next(json);
const char *key = json_get_string(json, NULL);
if (source_active == TRUE) {
- source_set(&sources[source_index], json, key);
+ source_set(&sources[sources_length-1], json, key);
} else if (strcmp(key, "source") == 0) {
- if (source_index == sources_length) {
- sources_length++;
- if ((sources = realloc(sources, sources_length * sizeof(struct source))) == NULL) {
- printf("ERROR: relloc: %s\n", strerror(errno));
- exit(1);
- }
- }
- source_active = TRUE;
- json_handle(json);
- source_index++;
- source_active = FALSE;
+ json_handle_source(json, json_next(json));
} else if (strcmp(key, "icestats") == 0) {
- json_handle(json);
+ json_handle_object(json, json_next(json));
} else {
icestats_set(&icestats, json, key);
}
@@ -332,7 +357,7 @@ main(int argc, char *argv[])
json_open_string(&json, buffer);
json_set_streaming(&json, false);
- json_handle(&json);
+ json_handle_object(&json, json_next(&json));
if (json_get_error(&json)) {
printf("ERROR: %lu: %s\n", json_get_lineno(&json), json_get_error(&json));
exit(1);