]> git.ozlabs.org Git - ccan-lca-2011.git/blobdiff - ccan/cdump/cdump_internal.h
cdump: first cut of translation of Tridge's genstruct junkcode.
[ccan-lca-2011.git] / ccan / cdump / cdump_internal.h
diff --git a/ccan/cdump/cdump_internal.h b/ccan/cdump/cdump_internal.h
new file mode 100644 (file)
index 0000000..7e85f5f
--- /dev/null
@@ -0,0 +1,81 @@
+#ifndef CCAN_CDUMP_INTERNAL
+#define CCAN_CDUMP_INTERNAL
+#include <stdbool.h>
+#include <stdlib.h>
+
+/*
+  automatic marshalling/unmarshalling system for C structures
+*/
+
+/* Always printed even if it's zero (for enums). */
+#define CDUMP_FLAG_ALWAYS 1
+
+struct cdump_enum {
+       const char *name;
+       unsigned value;
+};
+
+struct cdump_string;
+
+typedef bool (*cdump_bundle_fn)(struct cdump_string *, const void *ptr, unsigned indent);
+typedef bool (*cdump_unbundle_fn)(const void *ctx, void *ptr, const char *str);
+
+/* genstruct.pl generates arrays of these */
+struct cdump_desc {
+       /* Name of the member. */
+       const char *name;
+       /* Number of levels of pointers. */
+       size_t ptr_count;
+       /* Size of the underlying type. */
+       size_t size;
+       /* Offset within the struct. */
+       size_t offset;
+       /* Non-zero if it's a fixed-length array. */
+       size_t array_len;
+       /* Set to field name or literal number, from CDUMP_LEN. */
+       const char *dynamic_len;
+       /* CDUMP_FLAG_ALWAYS */
+       unsigned flags;
+       /* Function to dump this type. */
+       cdump_bundle_fn bundle;
+       /* Function to restore this type. */
+       cdump_unbundle_fn unbundle;
+};
+
+/* For writing your own dump types. */
+bool cdump_addstr(struct cdump_string *p, const char *fmt, ...);
+
+bool cdump_bundle_enum(const struct cdump_enum *einfo,
+                      struct cdump_string *p,
+                      const void *ptr,
+                      unsigned indent);
+bool cdump_unbundle_enum(const struct cdump_enum *einfo,
+                        void *ptr, const char *str);
+
+bool cdump_bundle_struct(const struct cdump_desc *pinfo,
+                        struct cdump_string *p,
+                        const void *ptr,
+                        unsigned indent);
+bool cdump_unbundle_struct(const void *ctx,
+                          const struct cdump_desc *info,
+                          void *data, const char *s);
+
+/* for convenience supply some standard dumpers and parsers here */
+bool cdump_bundle_char(struct cdump_string *p, const void *ptr, unsigned ind);
+bool cdump_bundle_int(struct cdump_string *p, const void *ptr, unsigned ind);
+bool cdump_bundle_unsigned(struct cdump_string *p, const void *ptr, unsigned);
+bool cdump_bundle_time_t(struct cdump_string *p, const void *ptr, unsigned ind);
+bool cdump_bundle_double(struct cdump_string *p, const void *ptr, unsigned ind);
+bool cdump_bundle_float(struct cdump_string *p, const void *ptr, unsigned ind);
+
+bool cdump_unbundle_char(const void *ctx, void *ptr, const char *str);
+bool cdump_unbundle_int(const void *ctx, void *ptr, const char *str);
+bool cdump_unbundle_unsigned(const void *ctx, void *ptr, const char *str);
+bool cdump_unbundle_time_t(const void *ctx, void *ptr, const char *str);
+bool cdump_unbundle_double(const void *ctx, void *ptr, const char *str);
+bool cdump_unbundle_float(const void *ctx, void *ptr, const char *str);
+
+#define cdump_bundle_unsigned_char cdump_bundle_char
+#define cdump_unbundle_unsigned_char cdump_unbundle_char
+
+#endif /* CCAN_CDUMP_INTERNAL */