General purpose library for name/value pairs.
Pawel Jakub Dawidek
pjd at FreeBSD.org
Fri Jul 26 07:33:25 UTC 2013
On Fri, Jul 26, 2013 at 12:26:15AM -0700, Jordan Hubbard wrote:
>
> On Jul 25, 2013, at 1:28 PM, Pawel Jakub Dawidek <pjd at freebsd.org> wrote:
>
> > Returning to this thread after a short break. I removed all
> > {,u}int{8,16.32.64} types and implemented only 'number' type which is
> > uint64_t. Looks much nicer now.
>
> Indeed!
>
> > Not sure if you looked at the API, but with nvlist you can lookup
> > element by name:
> >
> > const char *nvlist_get_string(const nvlist_t *nvl, const char *name);
>
> Sorry, I must have misunderstood that function since I thought it would just get you a named string - you can also do something like this?
>
> { { "name", "Pawel Jakub Dawidek" }, { "age", 37 }, { "favorite-float-num", 1.E027 }, { "likes", { "cats", "ZFS", "fashion models" } }, { "company", "wheel systems" } }
>
> And then look up "name" and get a string, "age" and get a number, "favorite-float-num" for a float, "likes" for an array of strings, etc?
Exactly. The code would look like this:
static const char * const *likes = {
"cats",
"ZFS",
"fashion models"
};
nvlist_t *nvl;
nvl = nvlist_create(0);
nvlist_add_string(nvl, "name", "Pawel Jakub Dawidek");
nvlist_add_number(nvl, "age", 37); /* XXX: Not my age, yet:) */
/* No floats. */
nvlist_add_array_string(nvl, "likes", likes, 3);
nvlist_add_string(nvl, "company", "Wheel Systems");
if (nvlist_error(nvl) != 0)
errx(1, "Unable to create nvlist.");
Then:
printf("Name: %s\nAge: %d\nCompany %s\n",
nvlist_get_string(nvl, "name"),
(int)nvlist_get_number(nvl, "age"),
nvlist_get_string(nvl, "company"));
--
Pawel Jakub Dawidek http://www.wheelsystems.com
FreeBSD committer http://www.FreeBSD.org
Am I Evil? Yes, I Am! http://mobter.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-arch/attachments/20130726/593516e2/attachment.sig>
More information about the freebsd-arch
mailing list