unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Tomi Ollila <tomi.ollila@iki.fi>
To: David Bremner <david@tethera.net>, notmuch@notmuchmail.org
Subject: Re: [Patch v2 2/3] util: add gzreadline
Date: Sun, 30 Mar 2014 11:30:21 +0300	[thread overview]
Message-ID: <m2zjk8um02.fsf@guru.guru-group.fi> (raw)
In-Reply-To: <1396116992-29640-3-git-send-email-david@tethera.net>

On Sat, Mar 29 2014, David Bremner <david@tethera.net> 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.
> ---

This series looks pretty good to me. I thought I found a place for real
improvement but I was wrong. Therefore I look in this for more detail ;D

>  util/Makefile.local |  2 +-
>  util/zlib-extra.c   | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++
>  util/zlib-extra.h   | 10 +++++++++
>  3 files changed, 70 insertions(+), 1 deletion(-)
>  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/zlib-extra.c b/util/zlib-extra.c
> new file mode 100644
> index 0000000..cda369f
> --- /dev/null
> +++ b/util/zlib-extra.c
> @@ -0,0 +1,59 @@
> +/* 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 <david@tethera.net>
> + */
> +
> +#include "zlib-extra.h"
> +#include <talloc.h>
> +#include <stdio.h>
> +#include <string.h>
> +
> +/* 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) {

The naming space of this function looks like the gz*functions in general;
But this adds a special talloc usage. getline() > gzgetline() conversion
should be consistent w/ other gz*() functions. I'd suggest to make slight
change to the function name to sanction the talloc usage. Ah, the fine
art & pain in naming considerations...

> +
> +    size_t len=*n;

spaces around '='

> +    char *buf = *lineptr;
> +    size_t offset = 0;
> +
> +    if (len == 0 || buf == NULL) {
> +	/* XXX arbitrary choice of initial size */

what does getline() use there.. could the 'XXX' just be dropped.

> +	len = BUFSIZ;
> +	buf = talloc_size (ctx, len);
> +    }
> +
> +    while (1) {
> +
> +	if (!gzgets (stream, buf + offset, len))
> +	    return -1;
> +
> +	/* XXX wasted effort re-counting length of whole line */
> +	offset = strlen (buf);

offset += strlen (buf + offset); would drop the whole line recounting...
just would that ever happen (gzgets() just usually returns the whole line
with '\n').. so many alternatives there...

...maybe just comment the usual case that full lines are returned.


Tomi


> +
> +	if ( buf[offset-1] == '\n' )
> +	    break;
> +
> +	len *= 2;
> +	buf = talloc_realloc (ctx, buf, char, len);
> +
> +    }
> +
> +    *lineptr = buf;
> +    *n = len;
> +    return offset;
> +}
> diff --git a/util/zlib-extra.h b/util/zlib-extra.h
> new file mode 100644
> index 0000000..c18480f
> --- /dev/null
> +++ b/util/zlib-extra.h
> @@ -0,0 +1,10 @@
> +#ifndef _ZLIB_EXTRA_H
> +#define _ZLIB_EXTRA_H
> +
> +#include <zlib.h>
> +
> +/* Like getline, but read from a gzFile. Allocation is with talloc */
> +ssize_t
> +gzgetline (void *ctx, char **lineptr, size_t *n, gzFile stream);
> +
> +#endif
> -- 
> 1.9.0
>
> _______________________________________________
> notmuch mailing list
> notmuch@notmuchmail.org
> http://notmuchmail.org/mailman/listinfo/notmuch

  reply	other threads:[~2014-03-30  8:30 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-25 17:07 [PATCH 0/2] cli: notmuch dump abstractions Jani Nikula
2014-03-25 17:07 ` [PATCH 1/2] cli: abstract database dumping from the dump command Jani Nikula
2014-03-25 17:07 ` [PATCH 2/2] cli: abstract dump file open " Jani Nikula
2014-03-25 17:48   ` [PATCH v2] " Jani Nikula
2014-03-26 22:01 ` [PATCH 0/2] cli: notmuch dump abstractions Mark Walters
2014-03-29  1:20   ` [PATCH] RFC: impliment gzipped output for notmuch dump David Bremner
2014-03-29  7:16     ` Tomi Ollila
2014-03-29  9:25     ` Jani Nikula
2014-03-29 12:29       ` David Bremner
2014-03-29 13:02         ` Jani Nikula
2014-03-29 16:25           ` David Bremner
2014-03-29 13:46     ` [PATCH] dump: support gzipped output David Bremner
2014-03-29 18:16       ` David Bremner
2014-03-29 18:16         ` [Patch v2 1/3] dump: support gzipped output David Bremner
2014-03-29 18:16         ` [Patch v2 2/3] util: add gzreadline David Bremner
2014-03-30  8:30           ` Tomi Ollila [this message]
2014-03-30 11:23             ` [Patch v3] " David Bremner
2014-03-30 12:45               ` Tomi Ollila
2014-03-30 14:37                 ` David Bremner
2014-03-30 16:13                   ` Tomi Ollila
2014-03-30 11:03           ` [Patch v2 2/3] " David Bremner
2014-03-29 18:16         ` [Patch v2 3/3] restore: transparently support gzipped input David Bremner
2014-03-30 22:33 ` [PATCH 0/2] cli: notmuch dump abstractions David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2zjk8um02.fsf@guru.guru-group.fi \
    --to=tomi.ollila@iki.fi \
    --cc=david@tethera.net \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).