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

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

问题描述

我在c ++中编写一个程序,需要解析二进制plists。 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 CFPropertyListCreateWithData CFPropertyListCreateWithStream ,它们是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实现细节也已由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 ++ Parse二进制plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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