Annotation of wikisrc/examples/proplib.mdwn, revision 1.2

1.1       wiki        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: 
1.2     ! wiki        7:     int
        !             8:     main(void)
        !             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:     }
1.1       wiki       35: 
                     36: And this will read it and display the values:
                     37: 
1.2     ! wiki       38:     int
        !            39:     main(void)
        !            40:     {
        !            41:         prop_dictionary_t d;
        !            42:         uint32_t year;
        !            43:         char *name;
        !            44:         bool r;
        !            45: 
        !            46:         d = prop_dictionary_internalize_from_file("test.plist");
        !            47:         if (d == NULL)
        !            48:             goto error;
        !            49: 
        !            50:         r = prop_dictionary_get_cstring(d, "name", &name);
        !            51:         if (!r)
        !            52:             goto error;
        !            53:         r = prop_dictionary_get_uint32(d, "year", &year);
        !            54:         if (!r)
        !            55:             goto error;
        !            56: 
        !            57:         printf("name: %s, year: %u\n", name, year);
        !            58: 
        !            59:         return EXIT_SUCCESS;
        !            60: 
        !            61:         error :
        !            62:             fprintf(stderr, "error\n");
        !            63:             return EXIT_FAILURE;
        !            64:     }

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