Example #1
0
static void string_stream_tests(void)
{
  ph_string_t *str;
  ph_stream_t *stm;
  char buf[5];
  uint64_t r;

  str = ph_string_make_empty(mt_misc, 16);
  stm = ph_stm_string_open(str);
  ok(stm, "made a stream");

  ph_stm_printf(stm, "hello world");
  ok(ph_string_equal_cstr(str, "hello world"), "see printf");

  ok(ph_stm_seek(stm, 0, SEEK_SET, NULL), "rewound");
  ok(ph_stm_read(stm, buf, sizeof(buf), &r), "read data");
  is(r, sizeof(buf));
  is(memcmp(buf, "hello", 5), 0);

  ph_stm_printf(stm, " kitty and append!");
  ok(ph_string_equal_cstr(str, "hello kitty and append!"), "see printf");

  ok(ph_stm_seek(stm, 6, SEEK_SET, NULL), "rewound");
  ok(ph_stm_read(stm, buf, sizeof(buf), &r), "read data");
  is(r, sizeof(buf));
  is(memcmp(buf, "kitty", 5), 0);

  ok(ph_stm_seek(stm, -5, SEEK_END, NULL), "rewound");
  ok(ph_stm_read(stm, buf, sizeof(buf), &r), "read data");
  is(r, sizeof(buf));
  is(memcmp(buf, "pend!", 5), 0);

  ph_stm_close(stm);
  ph_string_delref(str);
}
Example #2
0
int main(int argc, char **argv)
{
  ph_stream_t *stm;
  char namebuf[128];
  int fd;
  char buf[BUFSIZ];
  uint64_t amount;
  int len;

  ph_unused_parameter(argc);
  ph_unused_parameter(argv);

  ph_library_init();
  plan_tests(18);

  strcpy(namebuf, "/tmp/phenomXXXXXX");
  fd = ph_mkostemp(namebuf, 0);
  diag("opened %s -> %d", namebuf, fd);
  unlink(namebuf);

  stm = ph_stm_fd_open(fd, 0, PH_STM_BUFSIZE);
  ph_stm_write(stm, "lemon\n", 6, &amount);
  is(amount, 6);

  // Shouldn't see it yet
  is(0, pread(fd, buf, sizeof(buf), 0));

  // Should see it now
  ph_stm_flush(stm);
  is(6, pread(fd, buf, sizeof(buf), 0));

  ok(!memcmp("lemon\n", buf, 6), "right content");
  ok(ph_stm_seek(stm, 0, SEEK_SET, NULL), "seeked");
  memset(buf, 0, sizeof(buf));

  ok(ph_stm_read(stm, buf, 3, &amount), "read ok");
  ok(amount == 3, "amount is %" PRIu64, amount);
  ok(!memcmp("lem", buf, 3), "got prefix");

  ok(ph_stm_read(stm, buf, 3, &amount), "read ok");
  ok(amount == 3, "amount is %" PRIu64, amount);
  ok(!memcmp("on\n", buf, 3), "got remainder");

  ok(ph_stm_seek(stm, 0, SEEK_SET, NULL), "seeked");
  len = ph_stm_printf(stm, "testing %d %s!", 1, "two");
  ok(14 == len, "printed len %d", len);
  ok(ph_stm_seek(stm, 0, SEEK_SET, NULL), "seeked");
  memset(buf, 0, sizeof(buf));
  ok(ph_stm_read(stm, buf, 14, &amount), "read ok");
  ok(amount == 14, "len was %" PRIu64, amount);
  ok(!memcmp("testing 1 two!", buf, 14), "got formatted");

  ok(ph_stm_close(stm), "closed");

  return exit_status();
}
Example #3
0
static int stream_getc(stream_t *stream)
{
  uint8_t buf;
  uint64_t nread = 0;

  if (!ph_stm_read(stream->stm, &buf, 1, &nread) || nread == 0) {
    stream->state = STREAM_STATE_EOF;
    return STREAM_STATE_EOF;
  }
  return (int)buf;
}
Example #4
0
bool ph_stm_copy(ph_stream_t *src, ph_stream_t *dest,
    uint64_t num_bytes, uint64_t *nreadp, uint64_t *nwrotep)
{
  uint64_t nread = 0, nwrote = 0;
  uint64_t remain = num_bytes;
  char lbuf[PH_STM_BUFSIZE];
  int err = 0;

  while (remain > 0 && err == 0) {
    uint64_t r, want = MIN(remain, sizeof(lbuf));

    if (!ph_stm_read(src, lbuf, want, &r)) {
      if (ph_stm_errno(src) == EINTR) {
        continue;
      }
      err = ph_stm_errno(src);
      break;
    }

    if (r == 0) {
      break;
    }

    nread += r;
    remain -= r;

    char *buf = lbuf;
    while (r > 0) {
      uint64_t w;
      if (!ph_stm_write(dest, buf, r, &w)) {
        if (ph_stm_errno(dest) == EINTR) {
          continue;
        }
        err = ph_stm_errno(dest);
        break;
      }

      nwrote += w;
      buf += w;
      r -= w;
    }
  }

  if (nreadp) {
    *nreadp = nread;
  }
  if (nwrotep) {
    *nwrotep = nwrote;
  }

  return err == 0;
}
Example #5
0
static int bio_stm_read(BIO *h, char *buf, int size)
{
  uint64_t nread;
  ph_stream_t *stm = h->ptr;

  if (buf == NULL || size == 0 || stm == NULL) {
    return 0;
  }

  BIO_clear_retry_flags(h);
  if (ph_stm_read(stm, buf, size, &nread)) {
    return (int)nread;
  }

  if (should_retry(stm)) {
    BIO_set_retry_read(h);
  }

  return -1;
}