Browse Source Download (without any required ccan dependencies)

Module:

rbuf

Summary:

buffered I/O input primitive.

Author:

Rusty Russell <rusty@rustcorp.com.au>

Dependencies:

Description:

This code is like stdio, only simpler and more transparent to the user.

Example:

#include <ccan/rbuf/rbuf.h>
#include <ccan/err/err.h>
#include <stdlib.h>
#include <unistd.h>

// Dumb demo program to replace ' ' with '*'.
int main(int argc, char *argv[])
{
        struct rbuf in;
        char *word;

        if (argv[1]) {
                if (!rbuf_open(&in, argv[1], NULL, 0, membuf_realloc))
                        err(1, "Failed opening %s", argv[1]);
        } else
                rbuf_init(&in, STDIN_FILENO, NULL, 0, membuf_realloc);

        while ((word = rbuf_read_str(&in, ' ')) != NULL)
                printf("%s*", word);

        if (errno)
                err(1, "Reading %s", argv[1] ? argv[1] : "<stdin>");

        // Free the buffer, just because we can.
        free(rbuf_cleanup(&in));
        return 0;
}

License:

BSD-MIT