/* zlib-extra.c - Extra or enhanced routines for compressed I/O. * * Copyright (c) 2014 David Bremner * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/ . * * Author: David Bremner */ #include "zlib-extra.h" #include #include #include /* mimic POSIX/glibc getline, but on a zlib gzFile stream, and using talloc */ ssize_t gzgetline (void *ctx, char **lineptr, size_t *n, gzFile stream) { size_t len = *n; char *buf = *lineptr; size_t offset = 0; if (len == 0 || buf == NULL) { /* same as getdelim from gnulib */ len = 120; buf = talloc_size (ctx, len); if (buf == NULL) return -1; } while (1) { if (!gzgets (stream, buf + offset, len - offset)) return -1; offset += strlen (buf+offset); if ( buf[offset-1] == '\n' ) break; len *= 2; buf = talloc_realloc (ctx, buf, char, len); if (buf == NULL) return -1; } *lineptr = buf; *n = len; return offset; }