chiark / gitweb /
@@@ more mess
[mLib] / test / tvec-types.c
index 481363cdb53ddcb46c7fcecb36480eba0cc5d7be..27381b5865ffb158c86586b93dcf32bc29509269 100644 (file)
@@ -1454,6 +1454,25 @@ static int eq_uint(const union tvec_regval *rv0,
                   const struct tvec_regdef *rd)
   { return (rv0->u == rv1->u); }
 
+/* --- @copy_int@, @copy_uint@ --- *
+ *
+ * Arguments:  @union tvec_regval *rvd@ = destination register value
+ *             @const union tvec_regval *rvs@ = source register value
+ *             @const struct tvec_regdef *rd@ = register definition
+ *
+ * Returns:    ---
+ *
+ * Use:                Copy a register value.
+ */
+
+static void copy_int(union tvec_regval *rvd, const union tvec_regval *rvs,
+                    const struct tvec_regdef *rd)
+  { rvd->i = rvs->i; }
+
+static void copy_uint(union tvec_regval *rvd, const union tvec_regval *rvs,
+                     const struct tvec_regdef *rd)
+  { rvd->u = rvs->u; }
+
 /* --- @tobuf_int@, @tobuf_uint@ --- *
  *
  * Arguments:  @buf *b@ = buffer
@@ -1610,12 +1629,12 @@ static void dump_uint(const union tvec_regval *rv,
 
 /* Integer type definitions. */
 const struct tvec_regty tvty_int = {
-  init_int, trivial_release, eq_int,
+  init_int, trivial_release, eq_int, copy_int,
   tobuf_int, frombuf_int,
   parse_int, dump_int
 };
 const struct tvec_regty tvty_uint = {
-  init_uint, trivial_release, eq_uint,
+  init_uint, trivial_release, eq_uint, copy_uint,
   tobuf_uint, frombuf_uint,
   parse_uint, dump_uint
 };
@@ -1755,7 +1774,7 @@ static void dump_size(const union tvec_regval *rv,
 
 /* Size type definitions. */
 const struct tvec_regty tvty_size = {
-  init_uint, trivial_release, eq_uint,
+  init_uint, trivial_release, eq_uint, copy_uint,
   tobuf_uint, frombuf_uint,
   parse_size, dump_size
 };
@@ -1823,6 +1842,21 @@ static int eq_float(const union tvec_regval *rv0,
                    const struct tvec_regdef *rd)
   { return (eqish_floating_p(rv1->f, rv0->f, rd->arg.p)); }
 
+/* --- @copy_float@ --- *
+ *
+ * Arguments:  @union tvec_regval *rvd@ = destination register value
+ *             @const union tvec_regval *rvs@ = source register value
+ *             @const struct tvec_regdef *rd@ = register definition
+ *
+ * Returns:    ---
+ *
+ * Use:                Copy a register value.
+ */
+
+static void copy_float(union tvec_regval *rvd, const union tvec_regval *rvs,
+                      const struct tvec_regdef *rd)
+  { rvd->f = rvs->f; }
+
 /* --- @tobuf_float@ --- *
  *
  * Arguments:  @buf *b@ = buffer
@@ -1926,7 +1960,7 @@ static void dump_float(const union tvec_regval *rv,
 
 /* Floating-point type definition. */
 const struct tvec_regty tvty_float = {
-  init_float, trivial_release, eq_float,
+  init_float, trivial_release, eq_float, copy_float,
   tobuf_float, frombuf_float,
   parse_float, dump_float
 };
@@ -2204,7 +2238,7 @@ static void dump_duration(const union tvec_regval *rv,
 
 /* Duration type definition. */
 const struct tvec_regty tvty_duration = {
-  init_float, trivial_release, eq_float,
+  init_float, trivial_release, eq_float, copy_float,
   tobuf_float, frombuf_float,
   parse_duration, dump_duration
 };
@@ -2319,6 +2353,25 @@ static int eq_penum(const union tvec_regval *rv0,
                    const struct tvec_regdef *rd)
   { return (rv0->p == rv1->p); }
 
+/* --- @copy_tenum@ --- *
+ *
+ * Arguments:  @union tvec_regval *rvd@ = destination register value
+ *             @const union tvec_regval *rvs@ = source register value
+ *             @const struct tvec_regdef *rd@ = register definition
+ *
+ * Returns:    ---
+ *
+ * Use:                Copy a register value.
+ */
+
+#define copy_ienum copy_int
+#define copy_uenum copy_uint
+#define copy_fenum copy_float
+
+static void copy_penum(union tvec_regval *rvd, const union tvec_regval *rvs,
+                      const struct tvec_regdef *rd)
+  { rvd->p = rvs->p; }
+
 /* --- @tobuf_tenum@ --- *
  *
  * Arguments:  @buf *b@ = buffer
@@ -2553,7 +2606,7 @@ TVEC_MISCSLOTS(DEFDUMP_ENUM)
 /* Enumeration type definitions. */
 #define DEFTY_ENUM(tag, ty, slot)                                      \
   const struct tvec_regty tvty_##slot##enum = {                                \
-    init_##slot##enum, trivial_release, eq_##slot##enum,               \
+    init_##slot##enum, trivial_release, eq_##slot##enum, copy_##slot##enum, \
     tobuf_##slot##enum, frombuf_##slot##enum,                          \
     parse_##slot##enum, dump_##slot##enum                              \
   };
@@ -2769,7 +2822,7 @@ static void dump_flags(const union tvec_regval *rv,
 
 /* Flags type definition. */
 const struct tvec_regty tvty_flags = {
-  init_uint, trivial_release, eq_uint,
+  init_uint, trivial_release, eq_uint, copy_uint,
   tobuf_uint, frombuf_uint,
   parse_flags, dump_flags
 };
@@ -3089,7 +3142,7 @@ static void dump_char(const union tvec_regval *rv,
 
 /* Character type definition. */
 const struct tvec_regty tvty_char = {
-  init_int, trivial_release, eq_int,
+  init_int, trivial_release, eq_int, copy_int,
   tobuf_char, frombuf_char,
   parse_char, dump_char
 };
@@ -3189,6 +3242,43 @@ static int eq_bytes(const union tvec_regval *rv0,
           MEMCMP(rv0->bytes.p, ==, rv1->bytes.p, rv1->bytes.sz)));
 }
 
+/* --- @copy_text@, @copy_bytes@ --- *
+ *
+ * Arguments:  @union tvec_regval *rvd@ = destination register value
+ *             @const union tvec_regval *rvs@ = source register value
+ *             @const struct tvec_regdef *rd@ = register definition
+ *
+ * Returns:    ---
+ *
+ * Use:                Copy a register value.
+ */
+
+static void copy_text(union tvec_regval *rvd, const union tvec_regval *rvs,
+                     const struct tvec_regdef *rd)
+{
+  size_t sz = rvs->text.sz;
+
+  if (!sz)
+    rvd->text.sz = 0;
+  else {
+    tvec_alloctext(rvd, sz);
+    memcpy(rvd->text.p, rvs->text.p, sz); rvd->text.p[sz] = 0;
+  }
+}
+
+static void copy_bytes(union tvec_regval *rvd, const union tvec_regval *rvs,
+                      const struct tvec_regdef *rd)
+{
+  size_t sz = rvs->bytes.sz;
+
+  if (!sz)
+    rvd->bytes.sz = 0;
+  else {
+    tvec_alloctext(rvd, sz);
+    memcpy(rvd->bytes.p, rvs->bytes.p, sz);
+  }
+}
+
 /* --- @tobuf_text@, @tobuf_bytes@ --- *
  *
  * Arguments:  @buf *b@ = buffer
@@ -3478,12 +3568,12 @@ static void dump_bytes(const union tvec_regval *rv,
 
 /* Text and byte string type definitions. */
 const struct tvec_regty tvty_text = {
-  init_text, release_text, eq_text,
+  init_text, release_text, eq_text, copy_text,
   tobuf_text, frombuf_text,
   parse_text, dump_text
 };
 const struct tvec_regty tvty_bytes = {
-  init_bytes, release_bytes, eq_bytes,
+  init_bytes, release_bytes, eq_bytes, copy_bytes,
   tobuf_bytes, frombuf_bytes,
   parse_bytes, dump_bytes
 };
@@ -3605,7 +3695,7 @@ void tvec_alloctext(union tvec_regval *rv, size_t sz)
 {
   if (rv->text.sz <= sz)
     { free(rv->text.p); rv->text.p = x_alloc(&arena_stdlib, sz + 1); }
-  memset(rv->text.p, '?', sz); rv->text.sz = sz;
+  memset(rv->text.p, '?', sz); rv->text.p[sz] = 0; rv->text.sz = sz;
 }
 
 void tvec_allocbytes(union tvec_regval *rv, size_t sz)
@@ -3672,6 +3762,27 @@ static int eq_buffer(const union tvec_regval *rv0,
          rv0->buf.m == rv1->buf.m);
 }
 
+/* --- @copy_buffer@ --- *
+ *
+ * Arguments:  @union tvec_regval *rvd@ = destination register value
+ *             @const union tvec_regval *rvs@ = source register value
+ *             @const struct tvec_regdef *rd@ = register definition
+ *
+ * Returns:    ---
+ *
+ * Use:                Copy a register value.
+ */
+
+static void copy_buffer(union tvec_regval *rvd, const union tvec_regval *rvs,
+                       const struct tvec_regdef *rd)
+{
+  if (rvd->buf.p) { free(rvd->buf.p); rvd->buf.p = 0; }
+  rvd->buf.sz = rvs->buf.sz;
+  rvd->buf.a = rvs->buf.a;
+  rvd->buf.m = rvs->buf.m;
+  rvd->buf.off = 0;
+}
+
 /* --- @tobuf_buffer@ --- *
  *
  * Arguments:  @buf *b@ = buffer
@@ -3831,7 +3942,7 @@ static void dump_buffer(const union tvec_regval *rv,
 
 /* Buffer type definition. */
 const struct tvec_regty tvty_buffer = {
-  init_buffer, release_buffer, eq_buffer,
+  init_buffer, release_buffer, eq_buffer, copy_buffer,
   tobuf_buffer, frombuf_buffer,
   parse_buffer, dump_buffer
 };