Changeset: 4196f3a36772 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=4196f3a36772
Modified Files:
        clients/Tests/exports.stable.out
        common/stream/ChangeLog
        common/stream/stream.c
        common/stream/stream.h
        gdk/gdk_logger.c
        sql/backends/monet5/vaults/bam/bam_wrapper.c
Branch: default
Log Message:

Changed interface of mnstr_fgetpos and mnstr_fsetpos.
We now use fgetpos/fsetpos underneath since it is more portable.


diffs (truncated from 335 to 300 lines):

diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out
--- a/clients/Tests/exports.stable.out
+++ b/clients/Tests/exports.stable.out
@@ -2634,9 +2634,9 @@ void mnstr_close(stream *s);
 void mnstr_destroy(stream *s);
 int mnstr_errnr(stream *s);
 char *mnstr_error(stream *s);
-int mnstr_fgetpos(stream *s, lng *p);
+int mnstr_fgetpos(stream *s, fpos_t *p);
 int mnstr_flush(stream *s);
-int mnstr_fsetpos(stream *s, lng p);
+int mnstr_fsetpos(stream *s, fpos_t *p);
 int mnstr_fsync(stream *s);
 buffer *mnstr_get_buffer(stream *s);
 int mnstr_init(void);
diff --git a/common/stream/ChangeLog b/common/stream/ChangeLog
--- a/common/stream/ChangeLog
+++ b/common/stream/ChangeLog
@@ -1,6 +1,11 @@
 # ChangeLog file for stream
 # This file is updated with Maddlog
 
+* Tue Nov 21 2017 Sjoerd Mullender <sjo...@acm.org>
+- The interface of mnstr_fgetpos and mnstr_fsetpos was changed to look
+  more like the standard C functions fsetpos and fgetpos: they both have
+  a second argument "pointer to fpos_t".
+
 * Tue Aug  8 2017 Sjoerd Mullender <sjo...@acm.org>
 - Removed function wbstream.
 
diff --git a/common/stream/stream.c b/common/stream/stream.c
--- a/common/stream/stream.c
+++ b/common/stream/stream.c
@@ -206,8 +206,8 @@ struct stream {
        void (*destroy)(stream *s);
        int (*flush)(stream *s);
        int (*fsync)(stream *s);
-       int (*fgetpos)(stream *s, lng *p);
-       int (*fsetpos)(stream *s, lng p);
+       int (*fgetpos)(stream *s, fpos_t *p);
+       int (*fsetpos)(stream *s, fpos_t *p);
        void (*update_timeout)(stream *s);
        int (*isalive)(stream *s);
 };
@@ -531,7 +531,7 @@ mnstr_fsync(stream *s)
 }
 
 int
-mnstr_fgetpos(stream *s, lng *p)
+mnstr_fgetpos(stream *s, fpos_t *p)
 {
        if (s == NULL || p == NULL)
                return -1;
@@ -546,7 +546,7 @@ mnstr_fgetpos(stream *s, lng *p)
 }
 
 int
-mnstr_fsetpos(stream *s, lng p)
+mnstr_fsetpos(stream *s, fpos_t *p)
 {
        if (s == NULL)
                return -1;
@@ -842,55 +842,23 @@ file_fsync(stream *s)
 }
 
 static int
-file_fgetpos(stream *s, lng *p)
+file_fgetpos(stream *s, fpos_t *p)
 {
        FILE *fp = (FILE *) s->stream_data.p;
 
        if (fp == NULL || p == NULL)
                return -1;
-#ifdef WIN32
-       *p = (lng) _ftelli64(fp);       /* returns __int64 */
-#else
-#ifdef HAVE_FSEEKO
-       *p = (lng) ftello(fp);  /* returns off_t */
-#else
-       *p = (lng) ftell(fp);   /* returns long */
-#endif
-#endif
-       return *p < 0 ? -1 : 0;
+       return fgetpos(fp, p) ? -1 : 0;
 }
 
 static int
-file_fsetpos(stream *s, lng p)
-{
-       int res = 0;
+file_fsetpos(stream *s, fpos_t *p)
+{
        FILE *fp = (FILE *) s->stream_data.p;
 
-       if (fp == NULL)
+       if (fp == NULL || p == NULL)
                return -1;
-#ifdef WIN32
-       res = _fseeki64(fp, (__int64) p, SEEK_SET);
-#else
-#ifdef HAVE_FSEEKO
-       res = fseeko(fp, (off_t) p, SEEK_SET);
-#else
-       res = fseek(fp, (long) p, SEEK_SET);
-#endif
-#endif
-       return res;
-}
-
-size_t
-getFileSize(stream *s)
-{
-       if (s->read == file_read) {
-               struct stat stb;
-
-               if (fstat(fileno((FILE *) s->stream_data.p), &stb) == 0)
-                       return (size_t) stb.st_size;
-               /* we shouldn't get here... */
-       }
-       return 0;               /* unknown */
+       return fsetpos(fp, p) ? -1 : 0;
 }
 
 static stream *
@@ -898,7 +866,7 @@ open_stream(const char *filename, const 
 {
        stream *s;
        FILE *fp;
-       lng pos;
+       fpos_t pos;
        char buf[UTF8BOMLENGTH + 1];
 
        if ((s = create_stream(filename)) == NULL)
@@ -943,11 +911,11 @@ open_stream(const char *filename, const 
        /* if a text file is opened for reading, and it starts with
         * the UTF-8 encoding of the Unicode Byte Order Mark, skip the
         * mark, and mark the stream as being a UTF-8 stream */
-       if (flags[0] == 'r' && flags[1] != 'b' && file_fgetpos(s, &pos) == 0) {
+       if (flags[0] == 'r' && flags[1] != 'b' && fgetpos(fp, &pos) == 0) {
                if (file_read(s, buf, 1, UTF8BOMLENGTH) == UTF8BOMLENGTH &&
                    strncmp(buf, UTF8BOM, UTF8BOMLENGTH) == 0)
                        s->isutf8 = 1;
-               else if (file_fsetpos(s, pos) < 0) {
+               else if (fsetpos(fp, &pos) != 0) {
                        /* unlikely: we couldn't seek the file back */
                        fclose(fp);
                        destroy(s);
@@ -1858,6 +1826,43 @@ open_wastream(const char *filename)
        return s;
 }
 
+/* some lower-level access functions */
+FILE *
+getFile(stream *s)
+{
+#ifdef _MSC_VER
+       if (s->read == console_read)
+               return stdin;
+       if (s->write == console_write)
+               return stdout;
+#endif
+       if (s->read != file_read)
+               return NULL;
+       return (FILE *) s->stream_data.p;
+}
+
+int
+getFileNo(stream *s)
+{
+       FILE *f;
+
+       f = getFile(s);
+       if (f == NULL)
+               return -1;
+       return fileno(f);
+}
+
+size_t
+getFileSize(stream *s)
+{
+       struct stat stb;
+       int fd = getFileNo(s);
+
+       if (fd >= 0 && fstat(fd, &stb) == 0)
+               return (size_t) stb.st_size;
+       return 0;               /* unknown */
+}
+
 /* ------------------------------------------------------------------ */
 /* streams working on a remote file using cURL */
 
@@ -2711,7 +2716,7 @@ stream *
 file_rastream(FILE *fp, const char *name)
 {
        stream *s;
-       lng pos;
+       fpos_t pos;
        char buf[UTF8BOMLENGTH + 1];
        struct stat stb;
 
@@ -2726,13 +2731,13 @@ file_rastream(FILE *fp, const char *name
        s->stream_data.p = (void *) fp;
        if (fstat(fileno(fp), &stb) == 0 &&
            S_ISREG(stb.st_mode) &&
-           file_fgetpos(s, &pos) == 0) {
+           fgetpos(fp, &pos) == 0) {
                if (file_read(s, buf, 1, UTF8BOMLENGTH) == UTF8BOMLENGTH &&
                    strncmp(buf, UTF8BOM, UTF8BOMLENGTH) == 0) {
                        s->isutf8 = 1;
                        return s;
                }
-               if (file_fsetpos(s, pos) < 0) {
+               if (fsetpos(fp, &pos) != 0) {
                        /* unlikely: we couldn't seek the file back */
                        destroy(s);
                        return NULL;
@@ -5030,34 +5035,6 @@ callback_stream(void *private,
        return s;
 }
 
-/* Front-ends may wish to have more control over the designated file
- * activity. For this they need access to the file descriptor or even
- * duplicate it. (e.g. tablet loader) */
-FILE *
-getFile(stream *s)
-{
-#ifdef _MSC_VER
-       if (s->read == console_read)
-               return stdin;
-       if (s->write == console_write)
-               return stdout;
-#endif
-       if (s->read != file_read)
-               return NULL;
-       return (FILE *) s->stream_data.p;
-}
-
-int
-getFileNo(stream *s)
-{
-       FILE *f;
-
-       f = getFile(s);
-       if (f == NULL)
-               return -1;
-       return fileno(f);
-}
-
 static ssize_t
 stream_blackhole_write(stream *s, const void *buf, size_t elmsize, size_t cnt)
 {
diff --git a/common/stream/stream.h b/common/stream/stream.h
--- a/common/stream/stream.h
+++ b/common/stream/stream.h
@@ -148,8 +148,8 @@ stream_export void mnstr_destroy(stream 
 stream_export char *mnstr_error(stream *s);
 stream_export int mnstr_flush(stream *s);
 stream_export int mnstr_fsync(stream *s);
-stream_export int mnstr_fgetpos(stream *s, lng *p);
-stream_export int mnstr_fsetpos(stream *s, lng p);
+stream_export int mnstr_fgetpos(stream *s, fpos_t *p);
+stream_export int mnstr_fsetpos(stream *s, fpos_t *p);
 stream_export char *mnstr_name(stream *s);
 stream_export int mnstr_errnr(stream *s);
 stream_export void mnstr_clearerr(stream *s);
diff --git a/gdk/gdk_logger.c b/gdk/gdk_logger.c
--- a/gdk/gdk_logger.c
+++ b/gdk/gdk_logger.c
@@ -79,6 +79,16 @@
 #define printf(fmt,...) ((void) 0)
 #endif
 
+#ifdef WIN32
+#define getfilepos _ftelli64
+#else
+#ifdef HAVE_FSEEKO
+#define getfilepos ftello
+#else
+#define getfilepos ftell
+#endif
+#endif
+
 static char *log_commands[] = {
        NULL,
        "LOG_START",
@@ -1019,7 +1029,8 @@ logger_readlog(logger *lg, char *filenam
                        lng fpos;
                        t0 = t1;
                        /* not more than once every 10 seconds */
-                       if (mnstr_fgetpos(lg->log, &fpos) == 0) {
+                       fpos = (lng) getfilepos(getFile(lg->log));
+                       if (fpos >= 0) {
                                printf("# still reading write-ahead log \"%s\" 
(%d%% done)\n", filename, (int) ((fpos * 100 + 50) / sb.st_size));
                                fflush(stdout);
                        }
@@ -2706,7 +2717,8 @@ pre_allocate(logger *lg)
        // FIXME: this causes serious issues on Windows at least with MinGW
 #ifndef WIN32
        lng p;
-       if (mnstr_fgetpos(lg->log, &p) != 0)
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to