C++ 解析二进制 plist [英] C++ Parse Binary plist

查看:45
本文介绍了C++ 解析二进制 plist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a program in c++ that will need to parse binary plists. XML parsing is not a problem, so I was thinking I could convert the binary plist to XML and then parse that. Is there a way to do this natively in c++? I know that apple's plutil has this capability but executing that from within the program seems like bad practice.

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

I am running the latest version of OS X (10.9)

推荐答案

假设你想在苹果平台上做这个你可以使用 CFPropertyListCreateFromStream, CFPropertyListCreateWithDataCFPropertyListCreateWithStream,它们是 CoreFoundation 框架的一部分:
所有这些函数都有以下参数:

Assuming you want to do this on an apple platform You can use CFPropertyListCreateFromStream, CFPropertyListCreateWithData or CFPropertyListCreateWithStream, which are part of the CoreFoundation framework:
All of these functions have the following argument:

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

format: A constant that specifies the format of the property list. See Property List Formats for possible values.

CFPropertyListCreateFromStream 也有以下参数:

CFPropertyListCreateFromStream also has the following argument:

stream:数据包含内容的流.流必须打开和配置——这个函数只是从流中读取字节.流可能包含任何受支持的属性列表类型(请参阅属性列表格式).

stream: The stream whose data contains the content. The stream must be opened and configured—this function simply reads bytes from the stream. The stream may contain any supported property list type (see Property List Formats).

CFProperty 常量定义 定义如下:

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

这往往表明上面提到的方法实际上可以读取二进制 plist.
二进制 plist 实现细节也已由 Apple 此处开源.

This tends to indicate that the methods mentioned above can actually read binary plists.
Binary plist implementation details have also been open-sourced by Apple here.

Apple 还有一些 示例代码,其简写为:

Apple has some further sample code, the skinny of which is:

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);

这篇关于C++ 解析二进制 plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆