Changeset: 94e45907c5b9 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/94e45907c5b9
Modified Files:
        clients/examples/C/CMakeLists.txt
        clients/examples/C/murltest.c
        clients/examples/C/murltest.h
        clients/examples/C/testsfile.c
        common/stream/stream.c
Branch: monetdburl
Log Message:

Convert murltest to libstream

Windows complains about fopen and strerror


diffs (174 lines):

diff --git a/clients/examples/C/CMakeLists.txt 
b/clients/examples/C/CMakeLists.txt
--- a/clients/examples/C/CMakeLists.txt
+++ b/clients/examples/C/CMakeLists.txt
@@ -90,6 +90,7 @@ target_link_libraries(murltest
   PRIVATE
   monetdb_config_header
   mapi
+  stream
 )
 
 install(TARGETS
diff --git a/clients/examples/C/murltest.c b/clients/examples/C/murltest.c
--- a/clients/examples/C/murltest.c
+++ b/clients/examples/C/murltest.c
@@ -3,7 +3,6 @@
 #include "murltest.h"
 
 #include <errno.h>
-#include <stdio.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
@@ -13,23 +12,20 @@ char *USAGE = "Usage: murltest TESTFILES
 static bool
 run_file(const char *filename, int verbose)
 {
-       FILE *to_close, *f;
+       stream *s;
        if (strcmp(filename, "-") == 0) {
-               f = stdin;
-               to_close = NULL;
+               s = stdin_rastream();
        } else {
-               f = fopen(filename, "r");
-               if (!f) {
-                       fprintf(stderr, "Could not open %s: %s\n", filename, 
strerror(errno));
+               s = open_rastream(filename);
+               if (!s || mnstr_errnr(s) != MNSTR_NO__ERROR) {
+                       fprintf(stderr, "Could not open %s: %s\n", filename, 
mnstr_peek_error(s));
                        return false;
                }
-               to_close = f;
        }
 
-       bool ok = run_tests(filename, f, verbose);
+       bool ok = run_tests(s, verbose);
 
-       if (to_close)
-               fclose(to_close);
+       mnstr_close(s);
        return ok;
 }
 
@@ -48,6 +44,11 @@ main(int argc, char **argv)
 {
        int verbose = 0;
 
+       if (mnstr_init() != 0) {
+               fprintf(stderr, "could not initialize libstream\n");
+               return 1;
+       }
+
        char **files = calloc(argc + 1, sizeof(char*));
        if (!files)
                return 3;
diff --git a/clients/examples/C/murltest.h b/clients/examples/C/murltest.h
--- a/clients/examples/C/murltest.h
+++ b/clients/examples/C/murltest.h
@@ -1,4 +1,5 @@
-#include <stdbool.h>
-#include <stdio.h>
+#include "monetdb_config.h"
 
-bool run_tests(const char *filename, FILE *f, int verbose);
+#include "stream.h"
+
+bool run_tests(stream *s, int verbose);
diff --git a/clients/examples/C/testsfile.c b/clients/examples/C/testsfile.c
--- a/clients/examples/C/testsfile.c
+++ b/clients/examples/C/testsfile.c
@@ -4,6 +4,7 @@
 
 #include "murltest.h"
 #include "msettings.h"
+#include "stream.h"
 
 #include <assert.h>
 #include <ctype.h>
@@ -363,37 +364,40 @@ handle_line(int lineno, const char *loca
 }
 
 static bool
-run_tests_inner(const char *filename, FILE *f, int verbose)
+run_tests_inner(stream *s, int verbose)
 {
        int orig_nstarted = nstarted;
+       const char *filename = mnstr_name(s);
        char *location = malloc(strlen(filename) + 100);
        strcpy(location, filename);
        char *location_lineno = &location[strlen(filename)];
        *location_lineno++ = ':';
        *location_lineno = '\0';
+       char line_buffer[1024];
 
        errno = 0;
 
        int lineno = 0;
-       char *line_buffer = NULL;
-       size_t line_buffer_size = 0;
 
        while (true) {
                lineno++;
                sprintf(location_lineno, "%d", lineno);
-               ssize_t nread = getline(&line_buffer, &line_buffer_size, f);
+               ssize_t nread = mnstr_readline(s, line_buffer, 
sizeof(line_buffer));
+               if (nread == 0)
+                       break;
                if (nread < 0) {
                        if (errno) {
                                fprintf(stderr, "%s: %s\n", location, 
strerror(errno));
-                               free(line_buffer);
                                free(location);
                                return false;
                        } else {
                                break;
                        }
+               } else if (nread >= (ssize_t)sizeof(line_buffer) - 2) {
+                       fprintf(stderr, "%s: line too long\n", location);
+
                }
                if (!handle_line(lineno, location, line_buffer, verbose)) {
-                       free(line_buffer);
                        free(location);
                        return false;
                }
@@ -401,7 +405,6 @@ run_tests_inner(const char *filename, FI
 
        if (mp) {
                fprintf(stderr, "%s:%d: unterminated code block starts here\n", 
filename, start_line);
-               free(line_buffer);
                free(location);
                return false;
        }
@@ -410,16 +413,15 @@ run_tests_inner(const char *filename, FI
                fprintf(stderr, "ran %d succesful tests from %s\n", nstarted - 
orig_nstarted, filename);
        }
 
-       free(line_buffer);
        free(location);
        return true;
 }
 
 bool
-run_tests(const char *filename, FILE *f, int verbose)
+run_tests(stream *s, int verbose)
 {
        assert(mp == NULL);
-       bool ok = run_tests_inner(filename, f, verbose);
+       bool ok = run_tests_inner(s, verbose);
        if (mp) {
                msettings_destroy(mp);
                mp = NULL;
diff --git a/common/stream/stream.c b/common/stream/stream.c
--- a/common/stream/stream.c
+++ b/common/stream/stream.c
@@ -107,7 +107,7 @@ get_tl_error_buf(void)
                *p = (struct tl_error_buf) { .msg = {0} };
                pthread_setspecific(tl_error_key, p);
                struct tl_error_buf *second_attempt = 
pthread_getspecific(tl_error_key);
-               assert(p == second_attempt /* maybe mnstr_init has not been 
called? */);
+               assert(p == second_attempt && "maybe mnstr_init has not been 
called?");
                (void) second_attempt; // suppress warning if asserts disabled
        }
        return p;
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to