From df51acc081eecd80db6d83220f337d6ebce836ce Mon Sep 17 00:00:00 2001 From: Tomi Ollila Date: Sun, 6 Apr 2014 13:21:29 +0300 Subject: [PATCH] Re: [Patch v7 2/6] util: add gz_readline --- 8c/77583bb6d24a58aecad5c6d35e264471945c93 | 284 ++++++++++++++++++++++ 1 file changed, 284 insertions(+) create mode 100644 8c/77583bb6d24a58aecad5c6d35e264471945c93 diff --git a/8c/77583bb6d24a58aecad5c6d35e264471945c93 b/8c/77583bb6d24a58aecad5c6d35e264471945c93 new file mode 100644 index 000000000..598c606b0 --- /dev/null +++ b/8c/77583bb6d24a58aecad5c6d35e264471945c93 @@ -0,0 +1,284 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 7DD3E431FBC + for ; Sun, 6 Apr 2014 03:21:45 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: 1.775 +X-Spam-Level: * +X-Spam-Status: No, score=1.775 tagged_above=-999 required=5 + tests=[URIBL_BLACK=1.775] 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 7mVhW2YiBtCZ for ; + Sun, 6 Apr 2014 03:21:36 -0700 (PDT) +Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34]) + by olra.theworths.org (Postfix) with ESMTP id 77B8A431FB6 + for ; Sun, 6 Apr 2014 03:21:36 -0700 (PDT) +Received: from guru.guru-group.fi (localhost [IPv6:::1]) + by guru.guru-group.fi (Postfix) with ESMTP id E0EB9100086; + Sun, 6 Apr 2014 13:21:29 +0300 (EEST) +From: Tomi Ollila +To: David Bremner , notmuch@notmuchmail.org +Subject: Re: [Patch v7 2/6] util: add gz_readline +In-Reply-To: <1396712636-10640-3-git-send-email-david@tethera.net> +References: <1396712636-10640-1-git-send-email-david@tethera.net> + <1396712636-10640-3-git-send-email-david@tethera.net> +User-Agent: Notmuch/0.17+174~gef82849 (http://notmuchmail.org) Emacs/24.3.1 + (x86_64-unknown-linux-gnu) +X-Face: HhBM'cA~ +MIME-Version: 1.0 +Content-Type: text/plain +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, 06 Apr 2014 10:21:45 -0000 + +On Sat, Apr 05 2014, David Bremner wrote: + +> 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 | 3 +- +> util/util.c | 24 +++++++++++++++ +> util/util.h | 29 ++++++++++++++++++ +> util/zlib-extra.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++ +> util/zlib-extra.h | 25 ++++++++++++++++ +> 5 files changed, 165 insertions(+), 1 deletion(-) +> create mode 100644 util/util.c +> 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..905f237 100644 +> --- a/util/Makefile.local +> +++ b/util/Makefile.local +> @@ -4,7 +4,8 @@ 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 \ +> + $(dir)/util.c +> +> libutil_modules := $(libutil_c_srcs:.c=.o) +> +> diff --git a/util/util.c b/util/util.c +> new file mode 100644 +> index 0000000..3bd305d +> --- /dev/null +> +++ b/util/util.c +> @@ -0,0 +1,24 @@ +> +#include "util.h" +> +#include "error_util.h" +> +#include +> +#include +> + +> +const char * +> +util_error_string (util_status_t errnum) +> +{ +> + switch (errnum) { +> + case UTIL_SUCCESS: +> + return "none"; + +The series gets OK from me. This "none" could be amended to "success" to be +'consistent' with libc strerror(0) -- which returns 'Success'. + + +Tomi + + +> + case UTIL_OUT_OF_MEMORY: +> + return "out of memory"; +> + case UTIL_EOF: +> + return "end of file"; +> + case UTIL_ERRNO: +> + return strerror (errno); +> + case UTIL_GZERROR: +> + /* we lack context to be more informative here */ +> + return "zlib error"; +> + default: +> + INTERNAL_ERROR("unexpected error status %d", errnum); +> + } +> +} +> diff --git a/util/util.h b/util/util.h +> new file mode 100644 +> index 0000000..d12fadb +> --- /dev/null +> +++ b/util/util.h +> @@ -0,0 +1,29 @@ +> +#ifndef _UTIL_H +> +#define _UTIL_H +> + +> +typedef enum util_status { +> + /** +> + * No error occurred. +> + */ +> + UTIL_SUCCESS = 0, +> + /** +> + * Out of memory. +> + */ +> + UTIL_OUT_OF_MEMORY, +> + /** +> + * End of stream reached while attempting to read. +> + */ +> + UTIL_EOF, +> + /** +> + * Low level error occured, consult errno. +> + */ +> + UTIL_ERRNO, +> + /** +> + * Zlib error occured, call gzerror for details. +> + */ +> + UTIL_GZERROR +> +} util_status_t; +> + +> +const char * +> +util_error_string (util_status_t status); +> +#endif +> diff --git a/util/zlib-extra.c b/util/zlib-extra.c +> new file mode 100644 +> index 0000000..cb34845 +> --- /dev/null +> +++ b/util/zlib-extra.c +> @@ -0,0 +1,85 @@ +> +/* 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, ssize_t *bytes_read, gzFile stream) +> +{ +> + char *buf = *bufptr; +> + unsigned int len; +> + size_t offset = 0; +> + +> + if (buf) { +> + len = talloc_array_length (buf); +> + } else { +> + /* same as getdelim from gnulib */ +> + len = 120; +> + buf = talloc_array (talloc_ctx, char, len); +> + if (buf == NULL) +> + return UTIL_OUT_OF_MEMORY; +> + } +> + +> + while (1) { +> + if (! gzgets (stream, buf + offset, len - offset)) { +> + /* Null indicates EOF or error */ +> + int zlib_status = 0; +> + (void) gzerror (stream, &zlib_status); +> + switch (zlib_status) { +> + case Z_OK: +> + /* no data read before EOF */ +> + if (offset == 0) +> + return UTIL_EOF; +> + else +> + goto SUCCESS; +> + case Z_ERRNO: +> + return UTIL_ERRNO; +> + default: +> + return UTIL_GZERROR; +> + } +> + } +> + +> + offset += strlen (buf + offset); +> + +> + if (buf[offset - 1] == '\n') +> + goto SUCCESS; +> + +> + len *= 2; +> + buf = talloc_realloc (talloc_ctx, buf, char, len); +> + if (buf == NULL) +> + return UTIL_OUT_OF_MEMORY; +> + } +> + SUCCESS: +> + *bufptr = buf; +> + *bytes_read = offset; +> + return UTIL_SUCCESS; +> +} +> + +> +const char *gz_error_string (util_status_t status, gzFile file) +> +{ +> + if (status == UTIL_GZERROR) +> + return gzerror (file, NULL); +> + else +> + return util_error_string (status); +> +} +> diff --git a/util/zlib-extra.h b/util/zlib-extra.h +> new file mode 100644 +> index 0000000..dbaa0b9 +> --- /dev/null +> +++ b/util/zlib-extra.h +> @@ -0,0 +1,25 @@ +> +#ifndef _ZLIB_EXTRA_H +> +#define _ZLIB_EXTRA_H +> + +> +#include "util.h" +> +#include +> + +> +/* Like getline, but read from a gzFile. Allocation is with talloc. +> + * Returns: +> + * +> + * UTIL_SUCCESS, UTIL_OUT_OF_MEMORY, UTIL_ERRNO, UTIL_GZERROR +> + * Consult util.h for description +> + * +> + * UTIL_EOF End of file encountered before +> + * any characters read +> + */ +> +util_status_t +> +gz_getline (void *ctx, char **lineptr, ssize_t *bytes_read, gzFile stream); +> + +> +/* return a suitable error string based on the return status +> + * from gz_readline +> + */ +> + +> +const char * +> +gz_error_string (util_status_t status, gzFile stream); +> +#endif +> -- +> 1.9.0 +> +> _______________________________________________ +> notmuch mailing list +> notmuch@notmuchmail.org +> http://notmuchmail.org/mailman/listinfo/notmuch -- 2.26.2