From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id AB070431FC0 for ; Sun, 30 Mar 2014 10:32:00 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 86GDsAuzGU1x for ; Sun, 30 Mar 2014 10:31:55 -0700 (PDT) Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 45339431FBC for ; Sun, 30 Mar 2014 10:31:55 -0700 (PDT) Received: from remotemail by yantan.tethera.net with local (Exim 4.80) (envelope-from ) id 1WUJaI-0004AF-Ru; Sun, 30 Mar 2014 14:31:50 -0300 Received: (nullmailer pid 29602 invoked by uid 1000); Sun, 30 Mar 2014 17:31:43 -0000 From: David Bremner To: notmuch@notmuchmail.org Subject: [Patch v4 2/3] util: add gzreadline Date: Sun, 30 Mar 2014 14:31:39 -0300 Message-Id: <1396200700-29361-3-git-send-email-david@tethera.net> X-Mailer: git-send-email 1.9.0 In-Reply-To: <1396200700-29361-1-git-send-email-david@tethera.net> References: <1396200700-29361-1-git-send-email-david@tethera.net> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 30 Mar 2014 17:32:00 -0000 The idea is to provide a more or less drop in replacement for readline to read from zlib/gzip streams. Take the opportunity to replace malloc with talloc. --- util/Makefile.local | 2 +- util/util.h | 12 +++++++++ util/zlib-extra.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ util/zlib-extra.h | 11 ++++++++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 util/util.h create mode 100644 util/zlib-extra.c create mode 100644 util/zlib-extra.h diff --git a/util/Makefile.local b/util/Makefile.local index 29c0ce6..e2a5b65 100644 --- a/util/Makefile.local +++ b/util/Makefile.local @@ -4,7 +4,7 @@ dir := util extra_cflags += -I$(srcdir)/$(dir) libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \ - $(dir)/string-util.c $(dir)/talloc-extra.c + $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c libutil_modules := $(libutil_c_srcs:.c=.o) diff --git a/util/util.h b/util/util.h new file mode 100644 index 0000000..8663cfc --- /dev/null +++ b/util/util.h @@ -0,0 +1,12 @@ +#ifndef _UTIL_H +#define _UTIL_H + +typedef enum util_status { + UTIL_SUCCESS = 0, + UTIL_ERROR = 1, + UTIL_OUT_OF_MEMORY, + UTIL_EOF, + UTIL_FILE, +} util_status_t; + +#endif diff --git a/util/zlib-extra.c b/util/zlib-extra.c new file mode 100644 index 0000000..cb1eba0 --- /dev/null +++ b/util/zlib-extra.c @@ -0,0 +1,76 @@ +/* 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 */ +util_status_t +gz_getline (void *talloc_ctx, char **bufptr, size_t *bufsiz, ssize_t *bytes_read, + gzFile stream) +{ + size_t len = *bufsiz; + char *buf = *bufptr; + size_t offset = 0; + + if (len == 0 || buf == NULL) { + /* same as getdelim from gnulib */ + len = 120; + buf = talloc_size (talloc_ctx, len); + if (buf == NULL) + return UTIL_OUT_OF_MEMORY; + } + + while (1) { + if (! gzgets (stream, buf + offset, len - offset)) { + int zlib_status = 0; + (void) gzerror (stream, &zlib_status); + switch (zlib_status) { + case Z_OK: + /* follow getline behaviour */ + *bytes_read = -1; + return UTIL_EOF; + break; + case Z_ERRNO: + return UTIL_FILE; + break; + default: + return UTIL_ERROR; + } + } + + offset += strlen (buf + offset); + + if ( buf[offset - 1] == '\n' ) + break; + + len *= 2; + buf = talloc_realloc (talloc_ctx, buf, char, len); + if (buf == NULL) + return UTIL_OUT_OF_MEMORY; + } + + *bufptr = buf; + *bufsiz = len; + *bytes_read = offset; + return UTIL_SUCCESS; +} diff --git a/util/zlib-extra.h b/util/zlib-extra.h new file mode 100644 index 0000000..ed46ac1 --- /dev/null +++ b/util/zlib-extra.h @@ -0,0 +1,11 @@ +#ifndef _ZLIB_EXTRA_H +#define _ZLIB_EXTRA_H + +#include +#include "util.h" +/* Like getline, but read from a gzFile. Allocation is with talloc */ +util_status_t +gz_getline (void *ctx, char **lineptr, size_t *line_size, ssize_t *bytes_read, + gzFile stream); + +#endif -- 1.9.0