libjapi
0.3.2
Universal JSON API Library
|
Read line from file descriptor. More...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "creadline.h"
Macros | |
#define | __MAX_LINEBUF_SIZE__ 64*1024*1024 |
Functions | |
int | creadline_r (int fd, void **dst, creadline_buf_t *buffer) |
Read a single line from a file descriptor (reentrant version). More... | |
int | creadline (int fd, void **dst) |
Read a single line from a file descriptor. More... | |
Read line from file descriptor.
This readline implementation reads a single line from a file descriptor (e.g. a socket). Two versions are provided.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define __MAX_LINEBUF_SIZE__ 64*1024*1024 |
Internal definition of the maximum allowed line size. Can be overwritten by defining CREADLINE_MAX_LINE_SIZE.
Referenced by creadline_r().
int creadline | ( | int | fd, |
void ** | dst | ||
) |
Read a single line from a file descriptor.
creadline behaves like creadline_r except that no buffer needs to be provided. creadline is not reentrant and reading from a different file descriptor will delete remaining bytes from a previous read. If you need to read lines from different file descriptors use creadline_r instead.
fd | File descriptor |
dst | Pointer to a pointer to the read line |
References creadline_r(), and creadline_buf_t::nbytes.
int creadline_r | ( | int | fd, |
void ** | dst, | ||
creadline_buf_t * | buffer | ||
) |
Read a single line from a file descriptor (reentrant version).
creadline_r reads a single line from a file descriptor (e.g. a socket). Read characters are stored in internally allocated memory. If a newline character is found remaining bytes are moved to the provided buffer and the dst pointer is modified to point to the read line. The length of the \0 terminated line is returned (excluding the '\0'). The caller is responsible for free'ing the memory dst is pointing to!
Subsequent calls to creadline_r reuse the remaining bytes (if any) and continue reading from the file descriptor.
If EOF is read creadline_r returns 0 and sets the dst pointer to NULL if no remaining bytes are left in the buffer. Otherweise -1 is returned.
If a zero length line is read ("\n") creadline_r returns 0 and dst points to a terminated string "\0".
fd | File descriptor |
dst | Pointer to a pointer to the read line |
buffer | creadline buffer for storing remaining characters |
References __MAX_LINEBUF_SIZE__, creadline_buf_t::buf, CREADLINE_BLOCK_SIZE, and creadline_buf_t::nbytes.
Referenced by creadline(), and japi_start_server().