From: mquinson Date: Thu, 8 Mar 2007 22:51:28 +0000 (+0000) Subject: remplacement implementation of the GNU only getline X-Git-Tag: v3.3~2141 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/83e7d41831db5d0af8dc1430c4fd1bd1e1abbbdc remplacement implementation of the GNU only getline git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@3218 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/src/Makefile.am b/src/Makefile.am index 17073f46c3..9124ac37cc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ -AM_CFLAGS= -g +AM_CFLAGS+= -g #AM_CFLAGS= -DNDEBUG # -DNLOG cuts absolutely all logs at compilation time. @@ -111,7 +111,7 @@ lib_LTLIBRARIES= libsimgrid.la libgras.la COMMON_SRC=\ \ - xbt/snprintf.c \ + xbt/snprintf.c xbt/getline.c \ xbt/ex.c xbt/xbt_thread.c \ \ xbt_modinter.h gras_modinter.h \ diff --git a/src/xbt/getline.c b/src/xbt/getline.c new file mode 100644 index 0000000000..dfda65059b --- /dev/null +++ b/src/xbt/getline.c @@ -0,0 +1,59 @@ +/* $Id$ */ + +/* getline -- reimplementation of the GNU's getline() for other platforms */ + +/* Copyright (C) 2007 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +/** @brief Reads an entire line. + * + * Reads a line from the file specified by the file pointer passed + * as parameter. This function is intead to replace the non-portable + * GNU getline() function. + * + * @param buf address to a string buffer. If null, it will be allocated to receive the read data. If not and if too short, it will be reallocated. Must be freed by caller. + * @param n size of the buffer. Updated accordingly by the function. + * @param stream File pointer to the file to read from. + * @return The amount of chars read (or -1 on failure, ie on end of file condition) + */ +#include "xbt/misc.h" +#include "xbt/sysdep.h" /* headers of this function */ +#include "portable.h" + +ssize_t getline(char **buf, size_t *n, FILE *stream) { + + int i, ch; + + if (!*buf) { + *buf = xbt_malloc(512); + *n = 512; + } + + if (feof(stream)) + return (ssize_t)-1; + + for (i=0; (ch = fgetc(stream)) != EOF; i++) { + + if (i >= (*n) + 1) + *buf = xbt_realloc(*buf, *n += 512); + + (*buf)[i] = ch; + + if ((*buf)[i] == '\n') { + i++; + (*buf)[i] = '\0'; + break; + } + } + + if (i == *n) + *buf = xbt_realloc(*buf, *n += 1); + + (*buf)[i] = '\0'; + + return (ssize_t)i; +} + +