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 (12 years, 2 months ago) by wiki
Branches: MAIN
CVS tags: HEAD
web commit by ahoka: copypaste from my blog

    1: Proplib is an API/lib to create, handle and store property lists.
    2: 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).
    3: Here's a little example code to show some of the basics:
    4: 
    5: This will create a dictionary and store it as an xml file on the disk:
    6: 
    7: int
    8: main()
    9: {
   10:    prop_dictionary_t d;
   11:    bool r;
   12: 
   13:    d = prop_dictionary_create();
   14: 
   15:    if (d == NULL)
   16:        goto error;
   17: 
   18:    r = prop_dictionary_set_cstring(d, "name", "Adam");
   19:    if (!r)
   20:        goto error;
   21:    r = prop_dictionary_set_uint32(d, "year", 1986);
   22:    if (!r)
   23:        goto error;
   24: 
   25:    r = prop_dictionary_externalize_to_file(d, "test.plist");
   26:    if (!r)
   27:        goto error;
   28: 
   29:    return EXIT_SUCCESS;
   30: 
   31: error :
   32:    fprintf(stderr, "error\n");
   33:    return EXIT_FAILURE;
   34: }
   35: 
   36: 
   37: And this will read it and display the values:
   38: 
   39: int
   40: main()
   41: {
   42:    prop_dictionary_t d;
   43:    uint32_t year;
   44:    char *name;
   45:    bool r;
   46: 
   47:    d = prop_dictionary_internalize_from_file("test.plist");
   48:    if (d == NULL)
   49:        goto error;
   50: 
   51:    r = prop_dictionary_get_cstring(d, "name", &name);
   52:    if (!r)
   53:        goto error;
   54:    r = prop_dictionary_get_uint32(d, "year", &year);
   55:    if (!r)
   56:        goto error;
   57: 
   58:    printf("name: %s, year: %u\n", name, year);
   59: 
   60:    return EXIT_SUCCESS;
   61: 
   62: error :
   63:    fprintf(stderr, "error\n");
   64:    return EXIT_FAILURE;
   65: }

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