1 // Mac OS X: llvm-gcc -F<path-to-CFLite-framework> -framework CoreFoundation Examples/plconvert.c -o plconvert
3 // Linux: clang -I/usr/local/include -L/usr/local/lib -lCoreFoundation plconvert.c -o plconvert
6 This example shows usage of CFString, CFData, and other CFPropertyList types. It takes two arguments:
7 1. A property list file to read, in either binary or XML property list format.
8 2. A file name to write a converted property list file to.
9 If the first input is binary, the output is XML and vice-versa.
12 #include <sys/types.h>
19 #include <CoreFoundation/CoreFoundation.h>
21 static void logIt(CFStringRef format
, ...) {
23 va_start(args
, format
);
24 CFStringRef str
= CFStringCreateWithFormatAndArguments(kCFAllocatorSystemDefault
, NULL
, format
, args
);
27 CFIndex blen
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(str
), kCFStringEncodingUTF8
);
28 char *buf
= str
? (char *)malloc(blen
+ 1) : 0;
30 Boolean converted
= CFStringGetCString(str
, buf
, blen
, kCFStringEncodingUTF8
);
38 if (str
) CFRelease(str
); va_end(args
);
41 static CFMutableDataRef
createDataFromFile(const char *fname
) {
42 int fd
= open(fname
, O_RDONLY
);
43 CFMutableDataRef res
= CFDataCreateMutable(kCFAllocatorSystemDefault
, 0);
47 while ((amountRead
= read(fd
, buf
, 4096)) > 0) {
48 CFDataAppendBytes(res
, (const UInt8
*)buf
, amountRead
);
55 static bool writeDataToFile(CFDataRef data
, const char *fname
) {
56 int fd
= open(fname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
58 printf("There was an error creating the file: %d", errno
);
61 int dataLen
= CFDataGetLength(data
);
62 int w
= write(fd
, CFDataGetBytePtr(data
), dataLen
);
65 if (w
!= dataLen
) return false;
69 int main(int argc
, char **argv
) {
72 printf("Usage: plconvert <in file> <out file>\nIf the in file is an XML property list, convert to binary property list in out file. If the in file is a binary property list, convert to XML property list in out file.\n");
74 CFMutableDataRef plistData
= createDataFromFile(argv
[1]);
76 printf("Unable to create data from file name: %s", argv
[1]);
78 CFPropertyListFormat fmt
;
80 CFPropertyListRef plist
= CFPropertyListCreateWithData(kCFAllocatorSystemDefault
, (CFDataRef
)plistData
, 0, &fmt
, &err
);
82 logIt(CFSTR("Unable to create property list from data: %@"), err
);
84 logIt(CFSTR("Property list contents:\n%@"), plist
);
85 if (fmt
== kCFPropertyListBinaryFormat_v1_0
) {
86 logIt(CFSTR("Converting to XML property list at: %s"), argv
[2]);
87 fmt
= kCFPropertyListXMLFormat_v1_0
;
88 } else if (fmt
== kCFPropertyListXMLFormat_v1_0
) {
89 logIt(CFSTR("Converting to binary property list at: %s"), argv
[2]);
90 fmt
= kCFPropertyListBinaryFormat_v1_0
;
92 logIt(CFSTR("Unknown property list format! Not converting output format."));
95 CFDataRef outputData
= CFPropertyListCreateData(kCFAllocatorSystemDefault
, plist
, fmt
, 0, &err
);
97 logIt(CFSTR("Unable to write property list to data: %@"), err
);
99 bool success
= writeDataToFile(outputData
, argv
[2]);
101 logIt(CFSTR("Unable to write data to file"));
103 CFRelease(outputData
);
107 CFRelease(plistData
);