Skip to content

Instantly share code, notes, and snippets.

@mlabbe
Created April 24, 2015 18:47
Show Gist options
  • Select an option

  • Save mlabbe/5586f0f73b1029578547 to your computer and use it in GitHub Desktop.

Select an option

Save mlabbe/5586f0f73b1029578547 to your computer and use it in GitHub Desktop.
libplist example
7 dictionary keys:
Nice is type number
OnDemand is type bool
ProgramArguments is type dict
StandardErrorPath is type string
StandardOutPath is type string
StartInterval is type number
label is type string
#include <stdio.h>
#include <stdlib.h>
#include <prop/proplib.h>
const char IN_PATH[]="./sample1.plist";
static void PrintType(prop_object_t object)
{
switch( prop_object_type(object) )
{
case PROP_TYPE_BOOL:
printf("bool");
break;
case PROP_TYPE_NUMBER:
printf("number");
break;
case PROP_TYPE_STRING:
printf("string");
break;
case PROP_TYPE_DATA:
printf("data");
break;
case PROP_TYPE_ARRAY:
printf("dict");
break;
case PROP_TYPE_DICT_KEYSYM:
printf("keysym");
break;
default:
printf("unknown");
}
}
int main(void)
{
// parse buffer with dict root
prop_dictionary_t dict = prop_dictionary_internalize_from_file(IN_PATH);
// Get keys for iteration
prop_array_t keys = prop_dictionary_all_keys(dict);
printf("%i dictionary keys:", prop_array_count(keys) );
// Iterate over keys
prop_object_iterator_t iter = prop_array_iterator(keys);
prop_object_t current_key;
while( (current_key = prop_object_iterator_next(iter)) != NULL )
{
if ( prop_object_type(current_key) == PROP_TYPE_DICT_KEYSYM )
{
printf("%s is type ",
prop_dictionary_keysym_cstring_nocopy((prop_dictionary_keysym_t)current_key));
prop_object_t value = prop_dictionary_get_keysym(dict, (prop_dictionary_keysym_t)current_key);
PrintType(value);
puts("");
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>label</key>
<string>com.devdaily.pingwebsites</string>
<key>ProgramArguments</key>
<array>
<string>/Users/al/bin/crontab-test.sh</string>
</array>
<key>OnDemand</key>
<false/>
<key>Nice</key>
<integer>1</integer>
<key>StartInterval</key>
<integer>60</integer>
<key>StandardErrorPath</key>
<string>/tmp/AlTest1.err</string>
<key>StandardOutPath</key>
<string>/tmp/AlTest1.out</string>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment