| 1 |
/* |
| 2 |
* Copyright (c) 2012 Elizabeth J. Myers. All rights reserved. |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 9 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 10 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 11 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 12 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 13 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 14 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 15 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 16 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 17 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 18 |
* POSSIBILITY OF SUCH DAMAGE. |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifndef MOWGLI_SRC_LIBMOWGLI_LINEBUF_LINEBUF_H_INCLUDE_GUARD |
| 22 |
#define MOWGLI_SRC_LIBMOWGLI_LINEBUF_LINEBUF_H_INCLUDE_GUARD 1 |
| 23 |
|
| 24 |
#include "eventloop/eventloop.h" |
| 25 |
#include "platform/attributes.h" |
| 26 |
#include "vio/vio.h" |
| 27 |
|
| 28 |
typedef struct _mowgli_linebuf_buf mowgli_linebuf_buf_t; |
| 29 |
|
| 30 |
typedef void mowgli_linebuf_readline_cb_t (mowgli_linebuf_t *, char *, size_t, void *); |
| 31 |
typedef void mowgli_linebuf_shutdown_cb_t (mowgli_linebuf_t *, void *); |
| 32 |
|
| 33 |
extern mowgli_linebuf_t *mowgli_linebuf_create(mowgli_linebuf_readline_cb_t *cb, void *userdata); |
| 34 |
|
| 35 |
/* XXX these are unfortunately named and will change */ |
| 36 |
extern void mowgli_linebuf_attach_to_eventloop(mowgli_linebuf_t *linebuf, mowgli_eventloop_t *eventloop); |
| 37 |
extern void mowgli_linebuf_detach_from_eventloop(mowgli_linebuf_t *linebuf); |
| 38 |
extern void mowgli_linebuf_destroy(mowgli_linebuf_t *linebuf); |
| 39 |
|
| 40 |
extern void mowgli_linebuf_setbuflen(mowgli_linebuf_buf_t *buffer, size_t buflen); |
| 41 |
extern void mowgli_linebuf_delim(mowgli_linebuf_t *linebuf, const char *delim, const char *endl); |
| 42 |
extern void mowgli_linebuf_write(mowgli_linebuf_t *linebuf, const char *data, int len); |
| 43 |
|
| 44 |
extern void mowgli_linebuf_writef(mowgli_linebuf_t *linebuf, const char *format, ...) |
| 45 |
MOWGLI_FATTR_PRINTF(2, 3); |
| 46 |
|
| 47 |
extern void mowgli_linebuf_shut_down(mowgli_linebuf_t *linebuf); |
| 48 |
|
| 49 |
struct _mowgli_linebuf_buf |
| 50 |
{ |
| 51 |
char *buffer; |
| 52 |
size_t buflen; |
| 53 |
size_t maxbuflen; |
| 54 |
}; |
| 55 |
|
| 56 |
/* Errors */ |
| 57 |
#define MOWGLI_LINEBUF_ERR_NONE 0x0000 |
| 58 |
#define MOWGLI_LINEBUF_ERR_READBUF_FULL 0x0001 |
| 59 |
#define MOWGLI_LINEBUF_ERR_WRITEBUF_FULL 0x0002 |
| 60 |
|
| 61 |
/* Informative */ |
| 62 |
#define MOWGLI_LINEBUF_LINE_HASNULLCHAR 0x0004 |
| 63 |
|
| 64 |
/* State */ |
| 65 |
#define MOWGLI_LINEBUF_SHUTTING_DOWN 0x0100 |
| 66 |
|
| 67 |
struct _mowgli_linebuf |
| 68 |
{ |
| 69 |
mowgli_linebuf_readline_cb_t *readline_cb; |
| 70 |
mowgli_linebuf_shutdown_cb_t *shutdown_cb; |
| 71 |
|
| 72 |
mowgli_vio_t *vio; |
| 73 |
|
| 74 |
const char *delim; |
| 75 |
const char *endl; |
| 76 |
size_t endl_len; |
| 77 |
|
| 78 |
int flags; |
| 79 |
|
| 80 |
mowgli_linebuf_buf_t readbuf; |
| 81 |
mowgli_linebuf_buf_t writebuf; |
| 82 |
|
| 83 |
mowgli_eventloop_t *eventloop; |
| 84 |
|
| 85 |
bool return_normal_strings; |
| 86 |
|
| 87 |
void *userdata; |
| 88 |
}; |
| 89 |
|
| 90 |
static inline mowgli_vio_t * |
| 91 |
mowgli_linebuf_get_vio(mowgli_linebuf_t *linebuf) |
| 92 |
{ |
| 93 |
return_val_if_fail(linebuf != NULL, NULL); |
| 94 |
return linebuf->vio; |
| 95 |
} |
| 96 |
|
| 97 |
#endif /* MOWGLI_SRC_LIBMOWGLI_LINEBUF_LINEBUF_H_INCLUDE_GUARD */ |