]> git.saurik.com Git - apple/cf.git/blob - Examples/plconvert.c
CF-635.tar.gz
[apple/cf.git] / Examples / plconvert.c
1 // Mac OS X: llvm-gcc -F<path-to-CFLite-framework> -framework CoreFoundation Examples/plconvert.c -o plconvert
2
3 // Linux: clang -I/usr/local/include -L/usr/local/lib -lCoreFoundation plconvert.c -o plconvert
4
5 /*
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.
10 */
11
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <unistd.h>
18
19 #include <CoreFoundation/CoreFoundation.h>
20
21 static void logIt(CFStringRef format, ...) {
22 va_list args;
23 va_start(args, format);
24 CFStringRef str = CFStringCreateWithFormatAndArguments(kCFAllocatorSystemDefault, NULL, format, args);
25 if (!str) return;
26
27 CFIndex blen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8);
28 char *buf = str ? (char *)malloc(blen + 1) : 0;
29 if (buf) {
30 Boolean converted = CFStringGetCString(str, buf, blen, kCFStringEncodingUTF8);
31 if (converted) {
32 // null-terminate
33 buf[blen] = 0;
34 printf("%s\n", buf);
35 }
36 }
37 if (buf) free(buf);
38 if (str) CFRelease(str); va_end(args);
39 }
40
41 static CFMutableDataRef createDataFromFile(const char *fname) {
42 int fd = open(fname, O_RDONLY);
43 CFMutableDataRef res = CFDataCreateMutable(kCFAllocatorSystemDefault, 0);
44 char buf[4096];
45
46 ssize_t amountRead;
47 while ((amountRead = read(fd, buf, 4096)) > 0) {
48 CFDataAppendBytes(res, (const UInt8 *)buf, amountRead);
49 }
50
51 close(fd);
52 return res;
53 }
54
55 static bool writeDataToFile(CFDataRef data, const char *fname) {
56 int fd = open(fname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
57 if (fd < 0) {
58 printf("There was an error creating the file: %d", errno);
59 return false;
60 }
61 int dataLen = CFDataGetLength(data);
62 int w = write(fd, CFDataGetBytePtr(data), dataLen);
63 fsync(fd);
64 close(fd);
65 if (w != dataLen) return false;
66 return true;
67 }
68
69 int main(int argc, char **argv) {
70
71 if (argc != 3) {
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");
73 } else {
74 CFMutableDataRef plistData = createDataFromFile(argv[1]);
75 if (!plistData) {
76 printf("Unable to create data from file name: %s", argv[1]);
77 } else {
78 CFPropertyListFormat fmt;
79 CFErrorRef err;
80 CFPropertyListRef plist = CFPropertyListCreateWithData(kCFAllocatorSystemDefault, (CFDataRef)plistData, 0, &fmt, &err);
81 if (!plist) {
82 logIt(CFSTR("Unable to create property list from data: %@"), err);
83 } else {
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;
91 } else {
92 logIt(CFSTR("Unknown property list format! Not converting output format."));
93 }
94
95 CFDataRef outputData = CFPropertyListCreateData(kCFAllocatorSystemDefault, plist, fmt, 0, &err);
96 if (!outputData) {
97 logIt(CFSTR("Unable to write property list to data: %@"), err);
98 } else {
99 bool success = writeDataToFile(outputData, argv[2]);
100 if (!success) {
101 logIt(CFSTR("Unable to write data to file"));
102 }
103 CFRelease(outputData);
104 }
105 CFRelease(plist);
106 }
107 CFRelease(plistData);
108 }
109 }
110 }