CCAN Archive mirror
 help / color / mirror / Atom feed
From: Rusty Russell <rusty@rustcorp.com.au>
To: Richard Weinberger <richard.weinberger@gmail.com>, ccan@lists.ozlabs.org
Cc: ndevilla@free.fr, Tim Post <tinkertim@gmail.com>
Subject: Re: cinitparser: section numbering issue
Date: Wed, 27 Jan 2016 15:14:27 +1030	[thread overview]
Message-ID: <87oac7tyvo.fsf@rustcorp.com.au> (raw)
In-Reply-To: <CAFLxGvwkTjasU16thHSyicTqDSCD=5nBoxYF0e-btBEpTTOjWQ@mail.gmail.com>

Richard Weinberger <richard.weinberger@gmail.com> writes:
> Hi!
>
> ciniparser seems to be confused with its own API.

Hmm, ciniparser should have been removed ages ago.  Tim thought it was
abandoned when he added it to CCAN, but it is an active external project
which you can find here:

        https://github.com/ndevilla/iniparser

Unless there are strong objections, I will remove it from the repo.

Thanks,
Rusty.

> From the docs:
> /**
>  * @brief    Get name for section n in a dictionary.
>  * @param    d   Dictionary to examine
>  * @param    n   Section number (from 0 to nsec-1).
>  * @return   Pointer to char string, NULL on error
>  *
>  * This function locates the n-th section in a dictionary and returns
>  * its name as a pointer to a string statically allocated inside the
>  * dictionary. Do not free or modify the returned string!
>  */
> char *ciniparser_getsecname(dictionary *d, int n);
>
> So, the section numbers are 0 to ciniparser_getnsec() - 1.
>
> But in the test directory run.c does:
>         ok(stmp2 = ciniparser_getsecname(ini, 1),
>                 "ciniparser_getsecname(): first dict entry is %s", stmp2);
>
> First entry should be 0, not 1.
>
> ciniparser_dump_ini() follows the docs and fails.
> ---cut---
> rw@sandpuppy:~/ccan/ciniparser> cat test.ini
> [one]
> foo = bar
> [two]
> foo = bar
> [three]
> foo = bar
> rw@sandpuppy:~/ccan/ciniparser> cat main.c
> #include <stdio.h>
> #include <stdbool.h>
> #include "ciniparser.h"
>
> #define CONFIG_FILE "test.ini"
>
> int main(int argc, char *argv[])
> {
>         dictionary *d;
>         d = ciniparser_load(CONFIG_FILE);
>         if (d == NULL)
>                 return 1;
>
>         ciniparser_dump_ini(d, stdout);
>
>         ciniparser_freedict(d);
>
>         return 0;
> }
> rw@sandpuppy:~/ccan/ciniparser> ./main
>
> [one]
> foo                            = bar
>
> [one]
> foo                            = bar
>
> [two]
> foo                            = bar
>
> ---cut---
>
> 3rd section is missing but 1st is here twice.
>
> I'd suggest the following fix:
> diff --git a/ccan/ciniparser/ciniparser.c b/ccan/ciniparser/ciniparser.c
> index 2b60e40..a6082da 100644
> --- a/ccan/ciniparser/ciniparser.c
> +++ b/ccan/ciniparser/ciniparser.c
> @@ -270,7 +270,7 @@ void ciniparser_dump_ini(dictionary *d, FILE *f)
>                 return;
>         }
>
> -       for (i = 0; i < nsec; i++) {
> +       for (i = 1; i <= nsec; i++) {
>                 secname = ciniparser_getsecname(d, i);
>                 seclen  = (int)strlen(secname);
>                 fprintf(f, "\n[%s]\n", secname);
> diff --git a/ccan/ciniparser/ciniparser.h b/ccan/ciniparser/ciniparser.h
> index f61b357..e676c23 100644
> --- a/ccan/ciniparser/ciniparser.h
> +++ b/ccan/ciniparser/ciniparser.h
> @@ -65,7 +65,7 @@ int ciniparser_getnsec(dictionary *d);
>  /**
>   * @brief    Get name for section n in a dictionary.
>   * @param    d   Dictionary to examine
> - * @param    n   Section number (from 0 to nsec-1).
> + * @param    n   Section number (from 1 to nsec).
>   * @return   Pointer to char string, NULL on error
>   *
>   * This function locates the n-th section in a dictionary and returns
>
> ---
>
> Side note, ciniparser_getsecname() returns the first section
> if asked for both 0 and 1. 0 should be undefined behavior IMHO.
>
> -- 
> Thanks,
> //richard
> _______________________________________________
> ccan mailing list
> ccan@lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/ccan
_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan

      reply	other threads:[~2016-01-27  5:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-23 23:05 cinitparser: section numbering issue Richard Weinberger
2016-01-27  4:44 ` Rusty Russell [this message]

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

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

  git send-email \
    --in-reply-to=87oac7tyvo.fsf@rustcorp.com.au \
    --to=rusty@rustcorp.com.au \
    --cc=ccan@lists.ozlabs.org \
    --cc=ndevilla@free.fr \
    --cc=richard.weinberger@gmail.com \
    --cc=tinkertim@gmail.com \
    /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.
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).