summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2009-08-27 12:33:38 +0000
committerEric S. Raymond <esr@thyrsus.com>2009-08-27 12:33:38 +0000
commitccc3c5d85f1fabc99e3d4acd43fa5b9da8602cdf (patch)
tree8176a0cedcfba11f745c6de2d22ec4876eb06b3e
parent2e4cec8f9ff75c03bc5adf55ef3dbf4441c58d37 (diff)
downloadgpsd-ccc3c5d85f1fabc99e3d4acd43fa5b9da8602cdf.tar.gz
Create a convenience macro for JSON parser template declarations.
-rw-r--r--json.c1
-rw-r--r--json.h20
-rw-r--r--rtcm2_json.c23
3 files changed, 30 insertions, 14 deletions
diff --git a/json.c b/json.c
index 952778af..0e7eb880 100644
--- a/json.c
+++ b/json.c
@@ -428,6 +428,7 @@ int json_read_array(const char *cp, const struct json_array_t *arr, const char *
return substatus;
break;
case integer:
+ case uinteger:
case real:
case boolean:
case character:
diff --git a/json.h b/json.h
index 54aedf4e..b732a39f 100644
--- a/json.h
+++ b/json.h
@@ -76,4 +76,24 @@ const char *json_error_string(int);
#define JSON_ERR_BADENUM 17 /* invalid flag token */
#define JSON_ERR_NOPARSTR 18 /* can't support strings in aparalle. arrays */
+/*
+ * Use the following macros to declare template initializers for structobject
+ * arrays. Writing the equivalents out by hand is error-prone.
+ *
+ * STRUCTOBJECT takes a type t (one of the enumerated scalar field types
+ * above), a structure name s, and a fieldname f in s.
+ *
+ * STRUCTARRAY takes the name of a structure array, a pointer to a an
+ * initializer defining the subobject type, and the address of an integer to
+ * store the length in.
+ */
+#define STRUCTOBJECT(t, s, f) .type = t, .addr.offset = offsetof(s, f)
+#define STRUCTARRAY(a, e, n) .type = array, \
+ .addr.array.element_type = structobject, \
+ .addr.array.arr.objects.subtype = e, \
+ .addr.array.arr.objects.base = (char*)a, \
+ .addr.array.arr.objects.stride = sizeof(a[0]), \
+ .addr.array.count = n, \
+ .addr.array.maxlen = NITEMS(a)
+
/* json.h ends here */
diff --git a/rtcm2_json.c b/rtcm2_json.c
index 7bd7b7cd..912ca1a7 100644
--- a/rtcm2_json.c
+++ b/rtcm2_json.c
@@ -39,25 +39,19 @@ int json_rtcm2_read(const char *buf,
{"length", uinteger, .addr.uinteger = &rtcm2->length}, \
{"station_health", uinteger, .addr.uinteger = &rtcm2->stathlth},
-#define STRUCTOBJECT(s, f) .addr.offset=offsetof(s, f)
-
int status, satcount;
- const struct json_attr_t json_rtcm1_satellite[] = {
- {"ident", uinteger, STRUCTOBJECT(struct rangesat_t, ident)},
- {"udre", uinteger, STRUCTOBJECT(struct rangesat_t, udre)},
- {"issuedata", real, STRUCTOBJECT(struct rangesat_t, issuedata)},
- {"rangerr", real, STRUCTOBJECT(struct rangesat_t, rangerr)},
- {"rangerate", real, STRUCTOBJECT(struct rangesat_t, rangerate)},
+ const struct json_attr_t rtcm1_satellite[] = {
+ {"ident", STRUCTOBJECT(uinteger, struct rangesat_t, ident)},
+ {"udre", STRUCTOBJECT(uinteger, struct rangesat_t, udre)},
+ {"issuedata", STRUCTOBJECT(real, struct rangesat_t, issuedata)},
+ {"rangerr", STRUCTOBJECT(real, struct rangesat_t, rangerr)},
+ {"rangerate", STRUCTOBJECT(real, struct rangesat_t, rangerate)},
{NULL},
};
const struct json_attr_t json_rtcm1[] = {
RTCM2_HEADER
- {"satellites", array, .addr.array.element_type = structobject,
- .addr.array.arr.objects.base = (char*)rtcm2->ranges.sat,
- .addr.array.arr.objects.stride = sizeof(rtcm2->ranges.sat[0]),
- .addr.array.arr.objects.subtype = json_rtcm1_satellite,
- .addr.array.count = &satcount,
- .addr.array.maxlen = NITEMS(rtcm2->ranges.sat)},
+ {"satellites", STRUCTARRAY(rtcm2->ranges.sat,
+ rtcm1_satellite, &satcount)},
{NULL},
};
@@ -120,6 +114,7 @@ int json_rtcm2_read(const char *buf,
{NULL},
};
+#undef STRUCTARRAY
#undef STRUCTOBJECT
#undef RTCM2_HEADER