tech-kern archive

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

Re: RFC: ppath(3): property list paths library



Am 02.11.10 02:00, schrieb David Young:
> I'm working on a library called ppath(3) for making property lists more
> convenient to use in the kernel.  With ppath(3), you refer to a property
> to read/write/delete in a property list by the path from the list's
> outermost container.  Comments welcome.
> 
> The latest source is at
> <ftp://elmendorf.ojctech.com/users/netbsd-e238041d/ppath.tgz>.
> 
> Here is an example of using ppath(3):
> 
> /* Read and write from a personnel property list a user's "favorite color" 
> const char **s;
> int rc;
> ppath_t *p;
> prop_dictionary_t d;
> 
> /* Create the property list. */
> d = prop_dictionary_internalize("<dict> "
> "       <key>David Young</key>"
> "       <dict>"
> "               <key>favorite color</key>"
> "               <string>green</string>"
> "       </dict>"
> "</dict>");
> 
> assert(d != NULL);
> 
> /* Set up the path. */
> p = ppath_create();
> ppath_push_key(p, "David Young");
> ppath_push_key(p, "favorite color");

I like the idea.  Would it be possible to simplify the API, e.g. use
some kind of path expression in ppath_get_string() directly, without
having to setup things with ppat_create() first?

switch (ppath_get_string(d, "David Young/favorite color", &s)) {
   ...

or,  provide a wrapper function for the cases where ppath_t *p is used
only once.

> assert(p != NULL);
> 
> /* Get the string at the path. */
> switch (ppath_get_string(d, p, &s)) {
> case ENOENT:
>         errx(EXIT_FAILURE, "favorite color not found");
>         break;
> case EFTYPE:
>         errx(EXIT_FAILURE, "favorite color is not a string");
>         break;
> case 0:
>         printf("old favorite color: %s\n", s);
>         break;
> default:
>         errx(EXIT_FAILURE, "unknown error");
>         break;
> }
> 
> /* Replace with a new value. */
> switch (ppath_set_string(d, p, "brown")) {
> case ENOENT:
>         errx(EXIT_FAILURE, "favorite color not found");
>         break;
> case EFTYPE:
>         errx(EXIT_FAILURE, "favorite color is not a string");
>         break;
> case 0:
>         printf("set a new favorite color\n");
>         break;
> default:
>         errx(EXIT_FAILURE, "unknown error");
>         break;
> }
> 
> Dave
> 

p would have to be freed after use, right?




Home | Main Index | Thread Index | Old Index