C ++ Parse Binary plist

时间:2014-01-28 17:54:32

标签: c++ macos xml-parsing plist osx-mavericks

我正在用c ++编写一个需要解析二进制plist的程序。 XML解析不是问题,所以我想我可以将二进制plist转换为XML然后解析它。有没有办法在c ++中本地执行此操作?我知道苹果的plutil具有这种能力,但是从程序中执行它似乎是不好的做法。

我正在运行最新版本的OS X(10.9)

1 个答案:

答案 0 :(得分:5)

假设您想在苹果平台上执行此操作您可以使用CFPropertyListCreateFromStreamCFPropertyListCreateWithDataCFPropertyListCreateWithStream,它们是CoreFoundation框架的一部分:
所有这些函数都有以下参数:

  

format:一个常量,指定属性列表的格式。有关可能的值,请参阅属性列表格式。

CFPropertyListCreateFromStream还有以下参数:

  

stream:数据包含内容的流。必须打开并配置流 - 此函数只是从流中读取字节。流可以包含任何支持的属性列表类型(请参阅属性列表格式)。

The CFProperty constants definition定义了以下内容:

enum CFPropertyListFormat {
    kCFPropertyListOpenStepFormat = 1,
    kCFPropertyListXMLFormat_v1_0 = 100,
    kCFPropertyListBinaryFormat_v1_0 = 200
};
typedef enum CFPropertyListFormat CFPropertyListFormat;

这往往表明上述方法实际上可以读取二元plist 二进制plist实现细节也由Apple here开源。

Apple还有一些sample code,其中一些是:

CFDataRef resourceData;
SInt32 errorCode;
Boolean status = CFURLCreateDataAndPropertiesFromResource(
           kCFAllocatorDefault, fileURL, &resourceData,
           NULL, NULL, &errorCode);

if (!status) {
    // Handle the error
}
// Reconstitute the dictionary using the XML data
CFErrorRef myError;
CFPropertyListRef propertyList = CFPropertyListCreateWithData(
                      kCFAllocatorDefault, resourceData, kCFPropertyListImmutable, NULL, &myError);

// Handle any errors
CFRelease(resourceData);
CFRelease(myError);
相关问题