Hi Peff,

On Sun, 31 Jan 2016, Jeff King wrote:

> > It's a shame that we can't just factor out this common
> > code, but I don't think it's quite long enough to merit
> > the boilerplate. The interesting part of each function
> > happens inside the loop. If C had lambdas, we could do
> > something like:
> > 
> >   foreach_path_from(stdin, nul_term_line) {
> >         /* now do something interesting with "buf"
> >            and some other local variables */
> >   }

Technically, we do not have to do lambdas for that paradigm, we could
introduce a new data type and a reader, i.e. something like this:

struct path_reader {
        FILE *in;
        int nul_term_line;
        struct strbuf path;
};
#define PATH_READER_INIT { NULL, STRBUF_INIT };

int read_next_path(struct path_reader *reader, FILE *in, int nul_term_line)
{
        if (!reader->in) {
                ... [initialize] ...
        }

        ... [read and possibly unquote path] ...
}

void cleanup_path_reader(struct path_reader *reader)
{
        if (reader->in) {
                fclose(reader->in);
                reader->in = NULL;
        }

        strbuf_release(&reader->buf);
}

And then the repeated code could be replaced by something like this:

        struct path_reader path_reader = PATH_READER_INIT;

        while (read_next_path(&reader, stdin, 1)) {
                ... [work with reader->path.buf] ...
        }

        cleanup_path_reader();

Probably this is actually not limited to path names, so the functions
should be renamed...

(totally untested, of course...)

Ciao,
Dscho
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to