Changeset: bbfab8eb375f for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/bbfab8eb375f
Modified Files:
        clients/examples/C/bincopydata.c
        clients/examples/C/bincopydata.h
        clients/examples/C/bincopytemporaldata.c
        clients/examples/C/bincopyuuid.c
Branch: Jun2023
Log Message:

Allow to pass arguments to bincopydata generators


diffs (truncated from 378 to 300 lines):

diff --git a/clients/examples/C/bincopydata.c b/clients/examples/C/bincopydata.c
--- a/clients/examples/C/bincopydata.c
+++ b/clients/examples/C/bincopydata.c
@@ -18,8 +18,9 @@
 static char *exe_name = "<to_be_filled_in>";
 
 static void
-gen_tinyints(FILE *f, bool byteswap, long nrecs)
+gen_tinyints(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        for (long i = 0; i < nrecs; i++) {
                uint8_t v = (uint8_t)i;
                (void)byteswap;
@@ -28,8 +29,9 @@ gen_tinyints(FILE *f, bool byteswap, lon
 }
 
 static void
-gen_smallints(FILE *f, bool byteswap, long nrecs)
+gen_smallints(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        for (long i = 0; i < nrecs; i++) {
                uint16_t v = (uint16_t)i;
                if (byteswap) {
@@ -40,8 +42,9 @@ gen_smallints(FILE *f, bool byteswap, lo
 }
 
 static void
-gen_bigints(FILE *f, bool byteswap, long nrecs)
+gen_bigints(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        for (long i = 0; i < nrecs; i++) {
                uint64_t v = (uint64_t)i;
                if (byteswap) {
@@ -53,8 +56,9 @@ gen_bigints(FILE *f, bool byteswap, long
 
 #ifdef HAVE_HGE
 static void
-gen_hugeints(FILE *f, bool byteswap, long nrecs)
+gen_hugeints(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        for (long i = 0; i < nrecs; i++) {
                uhge v = (uhge)i;
                if (byteswap) {
@@ -66,8 +70,9 @@ gen_hugeints(FILE *f, bool byteswap, lon
 #endif
 
 static void
-gen_ints(FILE *f, bool byteswap, long nrecs)
+gen_ints(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        assert((uintmax_t)nrecs <= (uintmax_t) UINT32_MAX);
        uint32_t n = (uint32_t) nrecs;
        for (uint32_t i = 0; i < n; i++) {
@@ -80,8 +85,9 @@ gen_ints(FILE *f, bool byteswap, long nr
 }
 
 static void
-gen_more_ints(FILE *f, bool byteswap, long nrecs)
+gen_more_ints(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        assert((uintmax_t)nrecs <= (uintmax_t) UINT32_MAX);
        uint32_t n = (uint32_t) nrecs;
        for (uint32_t i = 0; i < n; i++) {
@@ -94,8 +100,9 @@ gen_more_ints(FILE *f, bool byteswap, lo
 }
 
 static void
-gen_null_ints(FILE *f, bool byteswap, long nrecs)
+gen_null_ints(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        assert((uintmax_t)nrecs <= (uintmax_t) UINT32_MAX);
        uint32_t n = (uint32_t) nrecs;
        uint32_t nil = 0x80000000;
@@ -109,8 +116,9 @@ gen_null_ints(FILE *f, bool byteswap, lo
 }
 
 static void
-gen_bools(FILE *f, bool byteswap, long nrecs)
+gen_bools(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        for (long i = 0; i < nrecs; i++) {
                char b = i % 2;
                (void)byteswap;
@@ -119,8 +127,9 @@ gen_bools(FILE *f, bool byteswap, long n
 }
 
 static void
-gen_floats(FILE *f, bool byteswap, long nrecs)
+gen_floats(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        // Assume for now that the raw bits are portable enough
 
        for (long i = 0; i < nrecs; i++) {
@@ -133,8 +142,9 @@ gen_floats(FILE *f, bool byteswap, long 
 }
 
 static void
-gen_doubles(FILE *f, bool byteswap, long nrecs)
+gen_doubles(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        // Assume for now that the raw bits are portable enough
 
        for (long i = 0; i < nrecs; i++) {
@@ -147,8 +157,9 @@ gen_doubles(FILE *f, bool byteswap, long
 }
 
 static void
-gen_strings(FILE *f, bool byteswap, long nrecs)
+gen_strings(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        (void)byteswap;
        for (long i = 0; i < nrecs; i++) {
                fprintf(f, "int%ld", i);
@@ -157,8 +168,9 @@ gen_strings(FILE *f, bool byteswap, long
 }
 
 static void
-gen_large_strings(FILE *f, bool byteswap, long nrecs)
+gen_large_strings(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        size_t n = 280000;
        char *buf = malloc(n);
        memset(buf, 'a', n);
@@ -173,8 +185,9 @@ gen_large_strings(FILE *f, bool byteswap
 }
 
 static void
-gen_broken_strings(FILE *f, bool byteswap, long nrecs)
+gen_broken_strings(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        // "bröken"
        char utf8[] =   {0x62, 0x72,   0xc3, 0xb6,   0x6b, 0x65, 0x6e, 0x00};
        char latin1[] = {0x62, 0x72,   0xf6,         0x6b, 0x65, 0x6e, 0x00};
@@ -189,8 +202,9 @@ gen_broken_strings(FILE *f, bool byteswa
 }
 
 static void
-gen_newline_strings(FILE *f, bool byteswap, long nrecs)
+gen_newline_strings(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        (void)byteswap;
        for (long i = 0; i < nrecs; i++) {
                fprintf(f, "RN\r\nR\r%ld", i);
@@ -199,8 +213,9 @@ gen_newline_strings(FILE *f, bool bytesw
 }
 
 static void
-gen_null_strings(FILE *f, bool byteswap, long nrecs)
+gen_null_strings(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        (void)byteswap;
        for (long i = 0; i < nrecs; i++) {
                if (i % 2 == 0)
@@ -212,8 +227,9 @@ gen_null_strings(FILE *f, bool byteswap,
 }
 
 static void
-gen_null_blobs(FILE *f, bool byteswap, long nrecs)
+gen_null_blobs(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        uint8_t *buffer = malloc(nrecs);
        for (long i = 0; i < nrecs; i++) {
                buffer[i] = 0xD0 + 3 - (i % 3);
@@ -241,8 +257,9 @@ gen_null_blobs(FILE *f, bool byteswap, l
 }
 
 static void
-gen_json(FILE *f, bool byteswap, long nrecs)
+gen_json(FILE *f, bool byteswap, long nrecs, char *arg)
 {
+       (void)arg;
        (void)byteswap;
        for (long i = 0; i < nrecs; i++) {
                if (i % 100 == 99) {
@@ -254,9 +271,12 @@ gen_json(FILE *f, bool byteswap, long nr
        }
 }
 
+typedef void (*generator_t)(FILE *f, bool byteswap, long nrecs, char 
*argument);
+
 static struct gen {
        char *name;
-       void (*gen)(FILE *f, bool byteswap, long nrecs);
+       generator_t gen;
+       bool arg_allowed;
 } generators[] = {
        { "ints", gen_ints },
        { "more_ints", gen_more_ints },
@@ -297,14 +317,11 @@ static struct gen {
        { NULL, NULL },
 };
 
-_Noreturn static void croak(int status, const char *msg, ...)
-       __attribute__((__format__(__printf__, 2, 3)));
-
 /* Format the message and write it to stderr. Then exit with the given status.
  * If status is 1, include USAGE in the message.
  * Otherwise, if errno is set, include the error message.
  */
-static void
+void
 croak(int status, const char *ctx, ...)
 {
        va_list ap;
@@ -329,11 +346,40 @@ croak(int status, const char *ctx, ...)
        exit(status);
 }
 
+static generator_t
+pick_generator(const char *full_name, char **arg)
+{
+       char *name = strdup(full_name);
+       *arg = NULL;
+
+       char *sep = strchr(name, ':');
+       if (sep != NULL) {
+               *arg = strdup(sep + 1);
+               *sep = '\0';
+       }
+
+       generator_t gen = NULL;
+       for (struct gen *g = generators; g->name; g++) {
+               if (strcmp(g->name, name) == 0) {
+                       if (*arg && !g->arg_allowed)
+                               croak(2, "Generator '%s' does not take 
arguments", name);
+                       gen = g->gen;
+                       break;
+               }
+       }
+
+       free(name);
+
+       return gen;
+}
+
+
 int
 main(int argc, char *argv[])
 {
        exe_name = argv[0];
-       void (*gen)(FILE*, bool, long);
+       generator_t gen;
+       char *gen_arg = NULL;
        long nrecs;
        FILE *dest;
        bool byteswap = false;
@@ -364,13 +410,7 @@ main(int argc, char *argv[])
        if (args_end - args != 3)
                croak(1, "Unexpected number of arguments");
 
-       gen = NULL;
-       for (struct gen *g = generators; g->name; g++) {
-               if (strcmp(g->name, args[0]) == 0) {
-                       gen = g->gen;
-                       break;
-               }
-       }
+       gen = pick_generator(args[0], &gen_arg);
        if (gen == NULL)
                croak(1, "Unknown TYPE: %s", args[0]);
 
@@ -392,7 +432,7 @@ main(int argc, char *argv[])
        if (dest == NULL)
                croak(2, "Cannot open '%s' for writing", args[2]);
 
-       gen(dest, byteswap, nrecs);
+       gen(dest, byteswap, nrecs, gen_arg);
 
        fclose(dest);
 
diff --git a/clients/examples/C/bincopydata.h b/clients/examples/C/bincopydata.h
--- a/clients/examples/C/bincopydata.h
+++ b/clients/examples/C/bincopydata.h
@@ -12,22 +12,26 @@
 #include "copybinary.h"
 #include "copybinary_support.h"
 
-void gen_timestamps(FILE *f, bool byteswap, long nrecs);
+_Noreturn void croak(int status, const char *msg, ...)
+       __attribute__((__format__(__printf__, 2, 3)));
+
 
-void gen_timestamp_times(FILE *f, bool byteswap, long nrecs);
-void gen_timestamp_dates(FILE *f, bool byteswap, long nrecs);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to