【问题标题】:C++ Parse Binary plistC++ 解析二进制 plist
【发布时间】:2014-02-20 04:41:34
【问题描述】:

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

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

【问题讨论】:

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


【解决方案1】:

假设您想在苹果平台上执行此操作,您可以使用 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 开源。

苹果还有一些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);

【讨论】:

    猜你喜欢
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多