[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.1.9 Stream

 
#include <mailutils/stream.h>

These generic flags are interpreted as appropriate to the specific streams.

MU_STREAM_READ
The stream is open read only.
MU_STREAM_WRITE
The stream is open write only.
MU_STREAM_RDWR
The stream is open read and write.
MU_STREAM_APPEND
The stream is open in append mode for writing.
MU_STREAM_CREAT
The stream open will create the underlying resource (such as a file) if it doesn't exist already.
MU_STREAM_NONBLOCK
The stream is set non blocking.
MU_STREAM_NO_CHECK
Stream is destroyed without checking for the owner.
MU_STREAM_SEEKABLE

MU_STREAM_NO_CLOSE
Stream doesn't close it's underlying resource when it is closed or destroyed.
MU_STREAM_ALLOW_LINKS

Function: int file_stream_create (stream_t *stream, const char *filename, int flags)

Function: int tcp_stream_create (stream_t *stream, const char *host, int port, int flags)

Function: int mapfile_stream_create (stream_t *stream, const char *filename, int flags)

Function: int memory_stream_create (stream_t *stream, const char *filename, int flags)

Function: int encoder_stream_create (stream_t *stream, stream_t iostream, const char *encoding)

Function: int decoder_stream_create (stream_t *stream, stream_t iostream, const char *encoding)

Function: int stdio_stream_create (stream_t *stream, FILE *stdio, int flags)
If MU_STREAM_NO_CLOSE is specified, fclose() will not be called on stdio when the stream is closed.

Function: int prog_stream_create (stream_t *stream, const char *progname, int flags)

Function: int filter_prog_stream_create (stream_t *stream, const char *progname, stream_t input)

Function: void stream_destroy (stream_t *stream, void *owner)

Function: int stream_open (stream_t stream)

Function: int stream_close (stream_t stream)

Function: int stream_is_seekable (stream_t stream)

Function: int stream_get_fd (stream_t stream, int *fd)

Function: int stream_get_fd2 (stream_t stream, int *fd1, int *fd2)

Function: int stream_read (stream_t stream, char *buffer, size_t buflen, off_t offset, size_t *writen)

Function: int stream_readline (stream_t stream, char *buffer, size_t buflen, off_t offset, size_t *writen)

Function: int stream_size (stream_t stream, off_t *size)

Function: n int stream_truncate (stream_t stream, off_t size)

Function: int stream_write (stream_t stream, const char *buffer, size_t buflen, off_t offset, size_t *writen)

Function: int stream_setbufsiz (stream_t stream, size_t size)

Function: int stream_flush (stream_t stream)

Function: int stream_create (stream_t *stream, int flags, void *owner)
Used to implement a new kind of stream.

Function: void* stream_get_owner (stream_t stream)

Function: void stream_set_owner (stream_t stream, void *owner)

Function: int stream_get_flags (stream_t stream, int *flags)

Function: int stream_set_flags (stream_t stream, int flags)

Function: int stream_get_property (stream_t stream, property_t *)

Function: int stream_set_property (stream_t stream, property_t, void *)

Function: int stream_get_state (stream_t stream, int *state)
MU_STREAM_STATE_OPEN
Last action was stream_open.
MU_STREAM_STATE_READ
Last action was stream_read or stream_readline.
MU_STREAM_STATE_WRITE
Last action was stream_write.
MU_STREAM_STATE_CLOSE
Last action was stream_close.

Function: int stream_set_destroy (stream_t stream, void (*_destroy) (stream_t), void *owner)

Function: int stream_set_open (stream_t stream, int (*_open) (stream_t), void *owner)

Function: int stream_set_close (stream_t stream, int (*_close) (stream_t), void *owner)

Function: int stream_set_fd (stream_t stream, int (*_get_fd) (stream_t, int *, int *), void *owner)

Function: int stream_set_read (stream_t stream, int (*_read) (stream_t, char *, size_t, off_t, size_t *), void *owner)

Function: int stream_set_readline (stream_t stream, int (*_readline) (stream_t, char *, size_t, off_t, size_t *), void *owner)

Function: int stream_set_size (stream_t stream, int (*_size) (stream_t, off_t *), void *owner)

Function: int stream_set_truncate (stream_t stream, int (*_truncate) (stream_t, off_t), void *owner)

Function: int stream_set_write (stream_t stream, int (*_write) (stream_t, const char *, size_t, off_t, size_t *), void *owner)

Function: int stream_set_flush (stream_t stream, int (*_flush) (stream_t), void *owner)

Function: int stream_set_strerror (stream_t stream, int (*_fp) (stream_t, char **), void *owner)

Function: int stream_sequential_readline (stream_ts stream, char *buf, size_t size, size_t *nbytes)

Function: int stream_sequential_write (stream_t stream, char *buf, size_t size)

Function: int stream_seek (stream_t stream, off_t off, int whence)

Function: int stream_strerror (stream_t stream, char **p)

An example using tcp_stream_create() to make a simple web client:

 
/* This is an example program to illustrate the use of stream functions.
   It connects to a remote HTTP server and prints the contents of its
   index page */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include <mailutils/mailutils.h>

const char *wbuf = "GET / HTTP/1.0\r\n\r\n";
char rbuf[1024];

int
main (void)
{
  int ret, off = 0;
  stream_t stream;
  size_t nb;
  
  ret = tcp_stream_create (&stream, "www.gnu.org", 80, MU_STREAM_NONBLOCK);
  if (ret != 0)
    {
      mu_error ("tcp_stream_create: %s", mu_strerror (ret));
      exit (EXIT_FAILURE);
    }

connect_again:
  ret = stream_open (stream);
  if (ret != 0)
    {
      if (ret == EAGAIN)
        {
	  int wflags = MU_STREAM_READY_WR;
	  stream_wait (stream, &wflags, NULL);
          goto connect_again;
        }
      mu_error ("stream_open: %s", mu_strerror (ret));
      exit (EXIT_FAILURE);
    }

write_again:
  ret = stream_write (stream, wbuf + off, strlen (wbuf), 0, &nb);
  if (ret != 0)
    {
      if (ret == EAGAIN)
        {
	  int wflags = MU_STREAM_READY_WR;
	  stream_wait (stream, &wflags, NULL);
          off += nb;
          goto write_again;
        }
      mu_error ("stream_write: %s", mu_strerror (ret));
      exit (EXIT_FAILURE);
    }

  if (nb != strlen (wbuf))
    {
      mu_error ("stream_write: %s", "nb != wbuf length");
      exit (EXIT_FAILURE);
    }

  do
    {
      ret = stream_read (stream, rbuf, sizeof (rbuf), 0, &nb);
      if (ret != 0)
        {
          if (ret == EAGAIN)
            {
	      int wflags = MU_STREAM_READY_RD;
	      stream_wait (stream, &wflags, NULL);
            }
          else
            {
              mu_error ("stream_read: %s", mu_strerror (ret));
              exit (EXIT_FAILURE);
            }
        }
      write (1, rbuf, nb);
    }
  while (nb || ret == EAGAIN);

  ret = stream_close (stream);
  if (ret != 0)
    {
      mu_error ("stream_close: %s", mu_strerror (ret));
      exit (EXIT_FAILURE);
    }

  stream_destroy (&stream, NULL);
  exit (EXIT_SUCCESS);
}


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Sergey Poznyakoff on December, 23 2004 using texi2html