summaryrefslogtreecommitdiff
path: root/tools/rdb_json.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/rdb_json.c')
-rw-r--r--tools/rdb_json.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/tools/rdb_json.c b/tools/rdb_json.c
new file mode 100644
index 0000000..40c9d36
--- /dev/null
+++ b/tools/rdb_json.c
@@ -0,0 +1,123 @@
+/*
+ * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * on the rights to use, copy, modify, merge, publish, distribute, sub
+ * license, and/or sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ * Jerome Glisse
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <errno.h>
+#include "jansson.h"
+#include "rdb_json.h"
+
+json_t *rdb_json_block_find(struct rdb_json *rdb, const char *name)
+{
+ unsigned i;
+ json_t *block;
+ json_t *bname;
+
+ for (i = 0; i < json_array_size(rdb->block); i++) {
+ block = json_array_get(rdb->block, i);
+ bname = json_object_get(block, "name");
+ if (!strcmp(name, json_string_value(bname)))
+ return block;
+ }
+ return NULL;
+}
+
+int rdb_json_block_add(struct rdb_json *rdb, const char *name)
+{
+ unsigned i;
+ json_t *block;
+ json_t *bname;
+
+ block = json_object();
+ bname = json_string(name);
+ json_object_set_new(block, "name", bname);
+ return json_array_append_new(rdb->block, block);
+}
+
+json_t *rdb_json_block_reg_find(json_t *block, unsigned offset)
+{
+ char tmp[16];
+
+ sprintf(tmp, "0x%08X", offset);
+ return json_object_get(block, tmp);
+}
+
+int rdb_json_block_reg_add(json_t *block, unsigned offset, json_t *reg)
+{
+ char tmp[16];
+
+ sprintf(tmp, "0x%08X", offset);
+ return json_object_set_new(block, tmp, reg);
+}
+
+struct rdb_json *rdb_json_load(const char *file)
+{
+ struct rdb_json *rdb = calloc(1, sizeof(struct rdb_json));
+ json_error_t error;
+
+ if (rdb == NULL)
+ return NULL;
+ rdb->root = json_load_file(file, &error);
+ if (rdb->root == NULL) {
+ fprintf(stderr, "%s failed to load %s (%d:%s)\n", __func__, file, error.line, error.text);
+ free(rdb);
+ return NULL;
+ }
+ rdb->block = json_object_get(rdb->root, "block");
+ return rdb;
+}
+
+json_t *rdb_json_block_reg_find_name(json_t *block, const char *name)
+{
+ void *iter;
+ json_t *reg, *tmp;
+
+ iter = json_object_iter(block);
+ while (iter) {
+ reg = json_object_iter_value(iter);
+ if (json_is_object(reg)) {
+ tmp = json_object_get(reg, "name");
+ if (!strcmp(name, json_string_value(tmp)))
+ return reg;
+ }
+ iter = json_object_iter_next(block, iter);
+ }
+ return NULL;
+}
+
+char *strdup(const char *str)
+{
+ char *nstr;
+
+ if (str == NULL)
+ return NULL;
+ nstr = malloc(strlen(str) + 1);
+ if (nstr == NULL)
+ return NULL;
+ strcpy(nstr, str);
+ return nstr;
+}