confuse-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Confuse-devel] cfg_getsec() assertions [Was: Re: libconfuse help (


From: Martin Hedenfalk
Subject: Re: [Confuse-devel] cfg_getsec() assertions [Was: Re: libconfuse help (several questions)]
Date: Tue, 29 Apr 2003 14:24:39 +0200

On Mon, 28 Apr 2003 23:53:11 -0300
"Ademar de Souza Reis Jr." <address@hidden> wrote:

> On Mon, Apr 28, 2003 at 09:47:19AM +0200, Martin Hedenfalk wrote:
> >
> >  [...]
> >
> > As it is now, the cfg_getXXX functions returns zero if the option is
> > not declared (with the CFG_XXX macros) and an assertion is raised if
> > you try to access a nonexistent index. Since sections can't have
> > default values, an assertion is raised even on index 0 (zero).
> > 
> 
> I think it would make more sense to invert the logic here: What about
> raising an exception when there's a coding mistake (a section name not
> declared is called) but returning 0 when some section, declared, is
> not found?

I've updated the CVS now, and checked in the changes you suggested. It
makes more sense to abort on undeclared options, and is more in line
with my intentions :-)

> May be just turning the exception into a return would be better (for
> cases where the section name is passed at runtime or something like
> that).

I'm not convinced about always returning 0, even on undeclared options.
In the case of runtime generated option names one have to stick with
checking the existence of the option with cfg_getopt() first.

> > The function cfg_size() should be used to check the number of
> > values. So your code should be changed to something like:
> > 
> > if(cfg_size(cfg, "sectionname"))
> >    sec = cfg_getsec(cfg, "sectionname"))
> > 
> > I'm open for a discussion about what's the Right Way to do this.
> 
> Calling cfg_size() just to see if a section name exists before calling
> a"get" function looks very counterintuitive. Actually, it just makes
> no sense to me (generally speaking, "size 0" != "non existent"
> object).

I guess what I really want is a way to distinguish between "size zero" 
and "non existent". With strings and sections it's fine (returning a
NULL pointer), but with integers or booleans it doesn't work. The
solution is to pass a pointer to the cfg_getXXX function and store the
result there, but that would require a big change in the API. That would
also make it harder to use the library, since you can't write things
like printf("variable == %s\n", cfg_getstr(cfg, "variable"));.

/mhe

--
Martin Hedenfalk <address@hidden>
Systemutvecklare - Esplanad Business Solutions AB




reply via email to

[Prev in Thread] Current Thread [Next in Thread]