LCOV - code coverage report
Current view: top level - common/stream - callback.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 38 0.0 %
Date: 2021-10-27 03:06:47 Functions: 0 5 0.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : #include "monetdb_config.h"
      10             : #include "stream.h"
      11             : #include "stream_internal.h"
      12             : 
      13             : 
      14             : 
      15             : /* ------------------------------------------------------------------ */
      16             : /* callback stream
      17             :  *
      18             :  * read-only stream which calls a user-provided callback function in
      19             :  * order to get more data to be returned to the reader */
      20             : 
      21             : struct cbstream {
      22             :         void *private;
      23             :         void (*destroy)(void *);
      24             :         void (*close)(void *);
      25             :         ssize_t (*read)(void *, void *, size_t, size_t);
      26             :         ssize_t (*write)(void *, const void *, size_t, size_t);
      27             : };
      28             : 
      29             : static void
      30           0 : cb_destroy(stream *s)
      31             : {
      32           0 :         struct cbstream *cb = s->stream_data.p;
      33             : 
      34           0 :         if (cb->destroy)
      35           0 :                 cb->destroy(cb->private);
      36           0 :         free(cb);
      37           0 :         s->stream_data.p = NULL;
      38           0 :         destroy_stream(s);
      39           0 : }
      40             : 
      41             : static void
      42           0 : cb_close(stream *s)
      43             : {
      44           0 :         struct cbstream *cb = s->stream_data.p;
      45             : 
      46           0 :         if (cb->close)
      47           0 :                 cb->close(cb->private);
      48           0 : }
      49             : 
      50             : static ssize_t
      51           0 : cb_read(stream *restrict s, void *restrict buf, size_t elmsize, size_t cnt)
      52             : {
      53           0 :         struct cbstream *cb = s->stream_data.p;
      54             : 
      55           0 :         ssize_t ret = cb->read(cb->private, buf, elmsize, cnt);
      56           0 :         s->eof |= ret == 0;
      57           0 :         return ret;
      58             : }
      59             : 
      60             : static ssize_t
      61           0 : cb_write(stream *restrict s, const void *restrict buf, size_t elmsize, size_t cnt)
      62             : {
      63           0 :         struct cbstream *cb = s->stream_data.p;
      64             : 
      65           0 :         return cb->write(cb->private, buf, elmsize, cnt);
      66             : }
      67             : 
      68             : stream *
      69           0 : callback_stream(void *restrict private,
      70             :                 ssize_t (*read)(void *restrict private, void *restrict buf, size_t elmsize, size_t cnt),
      71             :                 ssize_t (*write)(void *restrict private, const void *restrict buf, size_t elmsize, size_t cnt),
      72             :                 void (*close)(void *private),
      73             :                 void (*destroy)(void *private),
      74             :                 const char *restrict name)
      75             : {
      76             :         stream *s;
      77             :         struct cbstream *cb;
      78             : 
      79           0 :         if ((write && read) || (!write && !read))
      80             :                 return NULL;
      81             : 
      82           0 :         s = create_stream(name);
      83           0 :         if (s == NULL)
      84             :                 return NULL;
      85           0 :         cb = malloc(sizeof(struct cbstream));
      86           0 :         if (cb == NULL) {
      87           0 :                 destroy_stream(s);
      88           0 :                 mnstr_set_open_error(name, errno, NULL);
      89           0 :                 return NULL;
      90             :         }
      91           0 :         *cb = (struct cbstream) {
      92             :                 .private = private,
      93             :                 .destroy = destroy,
      94             :                 .read = read,
      95             :                 .write = write,
      96             :                 .close = close,
      97             :         };
      98           0 :         s->readonly = (read != NULL);
      99           0 :         s->stream_data.p = cb;
     100           0 :         s->read = read ? cb_read : NULL;
     101           0 :         s->write = write ? cb_write : NULL;
     102           0 :         s->destroy = cb_destroy;
     103           0 :         s->close = cb_close;
     104           0 :         return s;
     105             : }

Generated by: LCOV version 1.14