File:  [NetBSD Developer Wiki] / wikisrc / examples / proplib.mdwn
Revision 1.1: download - view: text, annotated - select for diffs
Sat Apr 17 17:01:32 2010 UTC (4 years ago) by wiki
Branches: MAIN
CVS tags: HEAD
web commit by ahoka: copypaste from my blog

Proplib is an API/lib to create, handle and store property lists.
It was created by Apple but NetBSD also have a clean room implementation in the system, and it's mostly used for implementing sysctl interfaces. It's a somehow limited implementation to make it faster/smaller (maybe extended in the future as demands grow).
Here's a little example code to show some of the basics:

This will create a dictionary and store it as an xml file on the disk:

int
main()
{
   prop_dictionary_t d;
   bool r;

   d = prop_dictionary_create();

   if (d == NULL)
       goto error;

   r = prop_dictionary_set_cstring(d, "name", "Adam");
   if (!r)
       goto error;
   r = prop_dictionary_set_uint32(d, "year", 1986);
   if (!r)
       goto error;

   r = prop_dictionary_externalize_to_file(d, "test.plist");
   if (!r)
       goto error;

   return EXIT_SUCCESS;

error :
   fprintf(stderr, "error\n");
   return EXIT_FAILURE;
}


And this will read it and display the values:

int
main()
{
   prop_dictionary_t d;
   uint32_t year;
   char *name;
   bool r;

   d = prop_dictionary_internalize_from_file("test.plist");
   if (d == NULL)
       goto error;

   r = prop_dictionary_get_cstring(d, "name", &name);
   if (!r)
       goto error;
   r = prop_dictionary_get_uint32(d, "year", &year);
   if (!r)
       goto error;

   printf("name: %s, year: %u\n", name, year);

   return EXIT_SUCCESS;

error :
   fprintf(stderr, "error\n");
   return EXIT_FAILURE;
}

CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb