我正在编写一些代码来处理Objective-C ++中的plists。
当我将函数传递给XML plist的路径时,一切都在顺畅运行,输出结果如下:
2014-08-12 17:06:47.932 plist_tests[96368:507] plist was in xml format
当我将函数传递给二进制plist的路径时,我收到以下错误:
2014-08-12 17:02:23.598 plist_tests[95709:507] could not deserialize plist: Error Domain=NSCocoaErrorDomain Code=3840 "The data couldn’t be read because it isn’t in the correct format." (Unexpected character b at line 1) UserInfo=0x7f9f2040cd60 {NSDebugDescription=Unexpected character b at line 1, kCFPropertyListOldStyleParsingError=The data couldn’t be read because it isn’t in the correct format.}
这是代码。请注意,我无法将dictionaryWithContentsOfFile
用于我的用例。
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#import <Foundation/Foundation.h>
class Status {
public:
Status(int c, std::string m) : code_(c), message_(m) {}
public:
int getCode() { return code_; }
std::string getMessage() { return message_; }
bool ok() { return getCode() == 0; }
std::string toString() { return getMessage(); }
private:
int code_;
std::string message_;
};
Status readFile(const std::string& path, std::string& content) {
if (!boost::filesystem::exists(path)) {
return Status(1, "File not found");
}
std::ifstream file_h(path);
if (file_h) {
file_h.seekg (0, file_h.end);
int len = file_h.tellg();
file_h.seekg (0, file_h.beg);
char *buffer = new char [len];
file_h.read(buffer, len);
if (!file_h) {
return Status(1, "Could not entire file");
}
content.assign(buffer, len);
} else {
return Status(1, "Could not open file for reading");
}
return Status(0, "OK");
}
void parsePlist(const std::string& path) {
std::string file_content;
Status readFileStatus = readFile(path, file_content);
if (!readFileStatus.ok()) {
NSLog(@"Couldn't read file");
return;
}
NSData *plist_content = [NSData dataWithBytes:file_content.c_str()
length:file_content.size()];
NSError *error;
NSPropertyListFormat plist_format;
id plist = [NSPropertyListSerialization propertyListWithData:plist_content
options:NSPropertyListImmutable
format:&plist_format
error:&error];
if (plist == nil) {
NSLog(@"could not deserialize plist: %@", error);
} else {
switch (plist_format) {
case NSPropertyListOpenStepFormat:
NSLog(@"plist was in openstep format");
break;
case NSPropertyListXMLFormat_v1_0:
NSLog(@"plist was in xml format");
break;
case NSPropertyListBinaryFormat_v1_0:
NSLog(@"plist was in binary format");
break;
default:
NSLog(@"plist was in unknown format");
break;
}
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cout << "Usage: plist <filename>" << std::endl;
return 1;
}
parsePlist(argv[1]);
return 0;
}
编译:{{1}}
我搔痒的原因是因为如果我在二进制plist上做g++ -lboost_system -lboost_filesystem -lglog -fobjc-arc -fobjc-link-runtime -framework Foundation plist.mm -o plist
,它就可以了。
答案 0 :(得分:1)
将数据转换为NSString
然后转换为NSData
的方式对于XML来说有点不好,对二进制数据来说非常糟糕。而是这样做:
NSData *plist_content = [NSData dataWithBytes: file_content.c_str()
length: file_content.size() ];
您的readFile
功能也存在问题。考虑一下
content = std::string(buffer);
std :: string如何知道缓冲区的长度?它寻找一个NUL字节,但这对二进制数据来说是错误的。您可以用
替换该行content.assign( buffer, len );
或者如果您的路径是绝对路径,您可以摆脱readFile
并说出
NSData *plist_content = [NSData dataWithContentsOfFile:
[NSString stringWithUTF8String: path.c_str() ];