Changeset: 4a2c3a7196e8 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=4a2c3a7196e8
Modified Files:
        clients/mapiclient/mclient.c
        clients/mapiclient/msqldump.c
        common/stream/stdio_stream.c
        common/stream/stream.h
        gdk/gdk_utils.c
Branch: makelibstreamgreatagain
Log Message:

Introduce stdin_rastream() and stdout/err_wastream()

Use them instead of file_{r,w}astream.


diffs (194 lines):

diff --git a/clients/mapiclient/mclient.c b/clients/mapiclient/mclient.c
--- a/clients/mapiclient/mclient.c
+++ b/clients/mapiclient/mclient.c
@@ -1733,7 +1733,7 @@ start_pager(stream **saveFD)
                else {
                        *saveFD = toConsole;
                        /* put | in name to indicate that file should be closed 
with pclose */
-                       if ((toConsole = file_wastream(p, "|pager")) == NULL) {
+                       if ((toConsole = file_wstream(p, false, "|pager")) == 
NULL) {
                                toConsole = *saveFD;
                                *saveFD = NULL;
                                fprintf(stderr, "Starting '%s' failed\n", 
pager);
@@ -3242,8 +3242,8 @@ main(int argc, char **argv)
                exit(2);
        }
 
-       toConsole = stdout_stream = file_wastream(stdout, "stdout");
-       stderr_stream = file_wastream(stderr, "stderr");
+       toConsole = stdout_stream = stdout_wastream();
+       stderr_stream = stderr_wastream();
        if(!stdout_stream || !stderr_stream) {
                if(stdout_stream)
                        close_stream(stdout_stream);
@@ -3674,19 +3674,25 @@ main(int argc, char **argv)
                /* execute from file(s) */
                while (optind < argc) {
                        stream *s;
+                       const char *arg = argv[optind];
 
-                       if (fp == NULL &&
-                           (fp = (strcmp(argv[optind], "-") == 0 ?
-                                  stdin :
-                                  fopen(argv[optind], "r"))) == NULL) {
-                               fprintf(stderr, "%s: cannot open\n", 
argv[optind]);
+                       if (fp != NULL)
+                               s = file_rstream(fp, false, arg);
+                       else if (strcmp(arg, "-") == 0)
+                               s = stdin_rastream();
+                       else
+                               s = open_rastream(arg);
+                       if (s == NULL) {
+                               fprintf(stderr, "%s: cannot open: %s", arg, 
mnstr_peek_error(NULL));
+                               if (fp) {
+                                       fclose(fp);
+                                       fp = NULL;
+                               }
                                c |= 1;
-                       } else if ((s = file_rastream(fp, argv[optind])) == 
NULL) {
-                               fclose(fp);
-                               c |= 1;
-                       } else {
-                               c |= doFile(mid, s, useinserts, interactive, 
save_history);
+                               continue;
                        }
+                       // doFile closes 's'.
+                       c |= doFile(mid, s, useinserts, interactive, 
save_history);
                        fp = NULL;
                        optind++;
                }
@@ -3694,7 +3700,7 @@ main(int argc, char **argv)
                c = doFileBulk(mid, NULL);
 
        if (!has_fileargs && command == NULL) {
-               stream *s = file_rastream(stdin, "<stdin>");
+               stream *s = stdin_rastream();
                if(!s) {
                        mapi_destroy(mid);
                        mnstr_destroy(stdout_stream);
diff --git a/clients/mapiclient/msqldump.c b/clients/mapiclient/msqldump.c
--- a/clients/mapiclient/msqldump.c
+++ b/clients/mapiclient/msqldump.c
@@ -198,7 +198,7 @@ main(int argc, char **argv)
        mapi_trace(mid, trace);
        mapi_cache_limit(mid, -1);
 
-       out = file_wastream(stdout, "stdout");
+       out = stdout_wastream();
        if (out == NULL) {
                fprintf(stderr, "failed to allocate stream: %s\n", 
mnstr_peek_error(NULL));
                exit(2);
diff --git a/common/stream/stdio_stream.c b/common/stream/stdio_stream.c
--- a/common/stream/stdio_stream.c
+++ b/common/stream/stdio_stream.c
@@ -692,7 +692,7 @@ file_stream(const char *name)
 }
 
 stream *
-file_rstream(FILE *restrict fp, const char *restrict name)
+file_rstream(FILE *restrict fp, bool binary, const char *restrict name)
 {
        stream *s;
 
@@ -703,13 +703,13 @@ file_rstream(FILE *restrict fp, const ch
 #endif
        if ((s = file_stream(name)) == NULL)
                return NULL;
-       s->binary = true;
+       s->binary = binary;
        s->stream_data.p = (void *) fp;
        return s;
 }
 
 stream *
-file_wstream(FILE *restrict fp, const char *restrict name)
+file_wstream(FILE *restrict fp, bool binary, const char *restrict name)
 {
        stream *s;
 
@@ -721,11 +721,39 @@ file_wstream(FILE *restrict fp, const ch
        if ((s = file_stream(name)) == NULL)
                return NULL;
        s->readonly = false;
-       s->binary = true;
+       s->binary = binary;
        s->stream_data.p = (void *) fp;
        return s;
 }
 
+
+stream *
+stdin_rastream(void)
+{
+#ifdef _MSC_VER
+#error "still have to implement windows support"
+#endif
+       return file_rstream(stdin, false, "<stdin>");
+}
+
+stream *
+stdout_wastream(void)
+{
+#ifdef _MSC_VER
+#error "still have to implement windows support"
+#endif
+       return file_wstream(stdout, false, "<stdout>");
+}
+
+stream *
+stderr_wastream(void)
+{
+#ifdef _MSC_VER
+#error "still have to implement windows support"
+#endif
+       return file_wstream(stderr, false, "<stderr>");
+}
+
 stream *
 file_rastream(FILE *restrict fp, const char *restrict name)
 {
@@ -847,6 +875,8 @@ getFile(stream *s)
 {
        for (; s != NULL; s = s->inner) {
 #ifdef _MSC_VER
+#error "oops not implemented yet"
+       // console is a separate stream type now?
                if (s->read == console_read)
                        return stdin;
                if (s->write == console_write)
diff --git a/common/stream/stream.h b/common/stream/stream.h
--- a/common/stream/stream.h
+++ b/common/stream/stream.h
@@ -165,10 +165,11 @@ stream_export void close_stream(stream *
 
 stream_export stream *open_urlstream(const char *url); // mclient.c, future 
copy from remote
 
-stream_export stream *file_rstream(FILE *restrict fp, const char *restrict 
name); // unused
-stream_export stream *file_wstream(FILE *restrict fp, const char *restrict 
name); // unused
-stream_export stream *file_rastream(FILE *restrict fp, const char *restrict 
name); // mclient.c, gdk_utils.c/THRinit
-stream_export stream *file_wastream(FILE *restrict fp, const char *restrict 
name); // mclient.c, msqldump.c, gdk_utils/THRinit
+stream_export stream *file_rstream(FILE *restrict fp, bool binary, const char 
*restrict name); // unused
+stream_export stream *file_wstream(FILE *restrict fp, bool binary, const char 
*restrict name); // unused
+stream_export stream *stdin_rastream(void);
+stream_export stream *stdout_wastream(void);
+stream_export stream *stderr_wastream(void);
 
 stream_export stream *xz_stream(stream *inner, int preset);
 stream_export stream *gz_stream(stream *inner, int preset);
diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -1560,11 +1560,11 @@ THRinit(void)
        Thread s;
        static bool first = true;
 
-       if ((THRdata[0] = (void *) file_wastream(stdout, "stdout")) == NULL) {
+       if ((THRdata[0] = (void *) stdout_wastream()) == NULL) {
                TRC_CRITICAL(GDK, "malloc for stdout failed\n");
                return -1;
        }
-       if ((THRdata[1] = (void *) file_rastream(stdin, "stdin")) == NULL) {
+       if ((THRdata[1] = (void *) stdin_rastream()) == NULL) {
                TRC_CRITICAL(GDK, "malloc for stdin failed\n");
                mnstr_destroy(THRdata[0]);
                THRdata[0] = NULL;
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to