Skip to content
Snippets Groups Projects
Commit 7055b790 authored by Daniel Salzman's avatar Daniel Salzman Committed by Jan Včelák
Browse files

Refactor knot_getline function

parent 14027a7f
No related branches found
No related tags found
No related merge requests found
......@@ -98,8 +98,8 @@ src/common/errcode.h
src/common/errcode.c
src/common/errors.h
src/common/errors.c
src/common/getline_wrap.h
src/common/getline_wrap.c
src/common/getline.h
src/common/getline.c
src/common/acl.c
src/common/acl.h
src/common/sockaddr.h
......
......@@ -248,8 +248,8 @@ libknots_la_SOURCES = \
common/fdset_kqueue.c \
common/fdset_epoll.h \
common/fdset_epoll.c \
common/getline_wrap.h \
common/getline_wrap.c \
common/getline.h \
common/getline.c \
common/log.c \
common/log.h \
common/hattrie/ahtable.c \
......
......@@ -16,73 +16,43 @@
// FreeBSD POSIX2008 getline
#ifndef _WITH_GETLINE
#define _WITH_GETLINE
#define _WITH_GETLINE
#endif
#include "common/getline_wrap.h"
#include "common/getline.h"
#include "config.h" // HAVE_
#include <stdio.h> // getline or fgetln
#include <stdlib.h> // free
#include <string.h> // memcpy
char* getline_wrap(FILE *stream, size_t *len)
{
char *buf = NULL;
#ifdef HAVE_GETLINE
ssize_t size = getline(&buf, len, stream);
if (size <= 0) {
return NULL;
}
*len = size;
return buf;
#elif HAVE_FGETLN
buf = fgetln(stream, len);
if (buf == NULL) {
return NULL;
}
if (buf[*len - 1] == '\n') {
buf[*len - 1] = '\0';
} else {
char *lbuf = NULL;
if ((lbuf = (char *)malloc(*len + 1)) == NULL) {
free(buf);
return NULL;
}
memcpy(lbuf, buf, *len);
lbuf[*len] = '\0';
free(buf);
buf = lbuf;
}
return buf;
#else
#error Missing getline or fgetln function
#endif
}
ssize_t knot_getline(char **lineptr, size_t *n, FILE *stream)
{
#ifdef HAVE_GETLINE
return getline(lineptr, n, stream);
#elif HAVE_FGETLN
size_t length = 0;
char *buffer = fgetln(stream, *length);
if (data == NULL)
char *buffer = fgetln(stream, &length);
if (buffer == NULL) {
return -1;
}
if (*lineptr)
free(*lineptr);
/* NOTE: Function fgetln doesn't return terminated string!
* Output buffer from the fgetln can't be freed.
*/
*lineptr = buffer;
// If the output buffer is not specified or is small, extend it.
if (*lineptr == NULL || *n <= length) {
char *tmp = realloc(*lineptr, length + 1);
if (tmp == NULL) {
return -1;
}
*lineptr = tmp;
}
memcpy(*lineptr, buffer, length);
(*lineptr)[length] = '\0';
*n = length;
return length;
......
......@@ -14,34 +14,21 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file getline_wrap.h
* \file getline.h
*
* \author Daniel Salzman <daniel.salzman@nic.cz>
* \author Jan Vcelak <jan.vcelak@nic.cz>
*
* \brief getline wrapper.
* \brief Multiplatform getline wrapper.
*
* \addtogroup common_lib
* @{
*/
#ifndef _KNOTD_COMMON_GETLINE_WRAP_H_
#define _KNOTD_COMMON_GETLINE_WRAP_H_
#ifndef _GETLINE_H_
#define _GETLINE_H_
#include <stdio.h> // size_t
/*!
* \brief Reads a line from stream.
*
* This wrapper switches between getline (Linux) and fgetln (BSD).
*
* \note It is necessary to free buffer after use.
*
* \param stream input stream.
* \param len length of output buffer.
*
* \retval pointer to a buffer.
*/
char* getline_wrap(FILE *stream, size_t *len);
#include <stdio.h>
/*!
* \brief Reads a line from a stream.
......@@ -58,5 +45,4 @@ char* getline_wrap(FILE *stream, size_t *len);
*/
ssize_t knot_getline(char **lineptr, size_t *n, FILE *stream);
#endif // _KNOTD_COMMON_GETLINE_WRAP_H_
#endif // _GETLINE_H_
......@@ -25,7 +25,7 @@
#include "binary.h"
#include "common.h"
#include "common/getline_wrap.h"
#include "common/getline.h"
#include "dname.h"
#include "sign/key.h"
#include "sign/sig0.h"
......
......@@ -22,7 +22,7 @@
#include <string.h> // memcmp
#include "common/errcode.h" // KNOT_EOK
#include "common/getline_wrap.h" // getline_wrap
#include "common/getline.h" // knot_getline
#include "utils/common/msg.h" // ERR
int tok_scan(const char* lp, const char **tbl, int *lpm)
......@@ -123,13 +123,14 @@ int tok_process_lines(FILE *fp, lparse_f cb, void *arg)
/* Parse lines. */
char *buf = NULL;
size_t rb = 0;
while ((buf = getline_wrap(fp, &rb)) != NULL && rb > 0) {
size_t buflen = 0;
ssize_t rb = 0;
while ((rb = knot_getline(&buf, &buflen, fp)) != -1) {
ret = cb(buf, rb, arg);
free(buf);
if (ret != KNOT_EOK) break;
}
free(buf);
return ret;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment