Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 26 Jul 2013 09:34:08 +0200
From:      Pawel Jakub Dawidek <pjd@FreeBSD.org>
To:        Jordan Hubbard <jordan.hubbard@gmail.com>
Cc:        arch@FreeBSD.org
Subject:   Re: General purpose library for name/value pairs.
Message-ID:  <20130726073408.GA1383@garage.freebsd.pl>
In-Reply-To: <818200C6-2AF2-465D-873B-CA610A9C763A@gmail.com>
References:  <20130704215329.GG1402@garage.freebsd.pl> <4818.1373008073@critter.freebsd.dk> <20130705195255.GB25842@garage.freebsd.pl> <60317.1373055040@critter.freebsd.dk> <20130708150308.GE1383@garage.freebsd.pl> <717D098F-D07E-45B0-B9F0-8D8BCEF06923@mail.turbofuzz.com> <20130708213351.GB1405@garage.freebsd.pl> <D2E98A8F-F765-4A56-96CD-4410944A2910@turbofuzz.com> <20130725202832.GD1400@garage.freebsd.pl> <818200C6-2AF2-465D-873B-CA610A9C763A@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--liOOAslEiF7prFVr
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Jul 26, 2013 at 12:26:15AM -0700, Jordan Hubbard wrote:
>=20
> On Jul 25, 2013, at 1:28 PM, Pawel Jakub Dawidek <pjd@freebsd.org> wrote:
>=20
> > 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.
>=20
> Indeed!
>=20
> > Not sure if you looked at the API, but with nvlist you can lookup
> > element by name:
> >=20
> > 	const char *nvlist_get_string(const nvlist_t *nvl, const char *name);
>=20
> Sorry, I must have misunderstood that function since I thought it would j=
ust get you a named string - you can also do something like this?
>=20
> 	{ { "name", "Pawel Jakub Dawidek" }, { "age", 37 }, { "favorite-float-nu=
m", 1.E027 }, { "likes", { "cats", "ZFS", "fashion models" } }, { "company"=
, "wheel systems" } }
>=20
> And then look up "name" and get a string, "age" and get a number, "favori=
te-float-num" for a float, "likes" for an array of strings, etc?

Exactly. The code would look like this:

	static const char * const *likes =3D {
		"cats",
		"ZFS",
		"fashion models"
	};
	nvlist_t *nvl;

	nvl =3D 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) !=3D 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"));

--=20
Pawel Jakub Dawidek                       http://www.wheelsystems.com
FreeBSD committer                         http://www.FreeBSD.org
Am I Evil? Yes, I Am!                     http://mobter.com

--liOOAslEiF7prFVr
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.20 (FreeBSD)

iEYEARECAAYFAlHyJnAACgkQForvXbEpPzTIMgCg5DbrGXJTWqIyjaZrJWrnGOvT
VkEAoK+HWaWyl8E2OXHyxVwop8ciSXvq
=j0XC
-----END PGP SIGNATURE-----

--liOOAslEiF7prFVr--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20130726073408.GA1383>