C 解析二进制PLIST

C++ Parse Binary plist

本文关键字:PLIST 二进制      更新时间:2023-10-16

我正在编写C 的程序,该程序需要解析二进制文件。XML解析不是问题,所以我认为我可以将二进制文件转换为XML,然后解析。有没有办法在C 本地进行此操作?我知道Apple的plutil具有此功能,但是从程序中执行它似乎是不好的练习。

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

假设您想在Apple平台上执行此操作,则可以使用CfPropopertylistCreateFromStream,CfPropopertylistcreatewithdata或Cfpropertylistylistylistylistylistylylistylewithewithstream,它们是CoreCoruoldation框架的一部分:

所有这些功能都有以下参数:

格式:指定属性列表格式的常数。有关可能的值,请参见属性列表格式。

cfpropopertylistyristefromstream也具有以下参数:

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

cfproperty常数定义定义了以下内容:

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

这倾向于表明上述方法实际上可以读取二进制文件。
二进制PLIST实施细节也已由Apple开源。

Apple具有进一步的示例代码,其瘦的是:

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