使用纯文本文件存储任意元数据 [英] Storing arbitrary metadata with a plain-text file

查看:136
本文介绍了使用纯文本文件存储任意元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个文本编辑器,我需要存储几个信息(通常只有几个字符串;存储不需要特别耐用)与应用程序保存的每个文件(不包括文本文件,因为其他应用程序可能会读取它,并且该信息仅特定于我的应用程序)。



我该如何解决?






更多信息:我有一个NSDocument设置,我想简单地存储一个NSString实例变量作为每个文件元数据。根据下面的答案我想出了这是目前的bug,并导致程序崩溃在启动时:

  #import< sys / xattr.h> 
@interface MyDocument:NSDocument {
NSString * metadatum;
}

@implementation MyDocument

- (BOOL)writeToURL:(NSURL *)url ofType:(NSString *)type error:(NSError **)err
{
BOOL output = [super writeToURL:url ofType:type error:err];
if(!setxattr([[url path] cStringUsingEncoding:NSUTF8StringEncoding],
eu.gampleman.xattrs.style,
[metadatum cStringUsingEncoding:NSUTF8StringEncoding],
sizeof char)* [styleName length],0,0))
{
NSLog(@Write failure);
}
return output;
}

- (BOOL)readFromURL:(NSURL *)url ofType:(NSString *)type error:(NSError **)err {
char * output;
ssize_t bytes = getxattr([[url path] cStringUsingEncoding:NSUTF8StringEncoding],
eu.gampleman.xattrs.style,& output,1024,0,0)
if(bytes> 0){
metadatum = [[NSString alloc] initWithBytes:output length:bytes
encoding:NSUTF8StringEncoding]; //< - crashes here withEXC_BAD_ACCESS
}
return [super readFromURL:url ofType:type error:err];
}

// ...
//相当标准-dataOfType:error:和
// -readFromData:ofType:error:实现






PS:如果你的答案真的很好。),我将授予100rep奖励。

解决方案

使用扩展属性。请参见 setxattr()



以下是调用写入字符串的示例:

  NSData * encodedString = [theString dataUsingEncoding:NSUTF8StringEncoding]; 
int rc = setxattr(/ path / to / your / file,com.yourcompany.yourapp.yourattributename,[encodedString bytes],[encodedString length],0,0);
if(rc)
/ *句柄错误* /;

要读取字符串:

  ssize_t len = getxattr(/ path / to / your / file,com.yourcompany.yourapp.yourattributename,NULL,0,0,0); 
if(len< 0)
/ *句柄错误* /;
NSMutableData * data = [NSMutableData dataWithLength:len];
len = getxattr(/ path / to / your / file,com.yourcompany.yourapp.yourattributename,[data mutableBytes],len,0,0);
NSString * string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

PS:你不必在问题回答之前设置赏金?


I am writing a text-editor and I would need to store a few pieces of information (generally just a few strings; the storage needn't be particularly durable) with each file the app saves (without that being part of the text-file as other apps might read it and the info is only specific to my app).

How would I go about this?


More info: I have a NSDocument set up and I would like to simply store a NSString instance variable as a per file meta-datum. Based on the answers below I've come up with this, which is currently buggy and causes the program to crash on startup:

#import <sys/xattr.h>
@interface MyDocument : NSDocument {
  NSString *metadatum;
}

@implementation MyDocument 

- (BOOL)writeToURL:(NSURL *)url ofType:(NSString *)type error:(NSError **)err
{
  BOOL output = [super writeToURL:url ofType:type error:err];
  if(!setxattr([[url path] cStringUsingEncoding:NSUTF8StringEncoding], 
               "eu.gampleman.xattrs.style", 
               [metadatum cStringUsingEncoding:NSUTF8StringEncoding], 
               sizeof(char) * [styleName length], 0, 0)) 
  {
      NSLog(@"Write failure");
  }
  return output;
}

- (BOOL)readFromURL:(NSURL *)url ofType:(NSString *)type error:(NSError **)err {
  char *output;
  ssize_t bytes = getxattr([[url path] cStringUsingEncoding:NSUTF8StringEncoding],
                           "eu.gampleman.xattrs.style", &output, 1024, 0, 0);
  if (bytes > 0) {
    metadatum = [[NSString alloc] initWithBytes:output length:bytes 
          encoding:NSUTF8StringEncoding]; // <- crashes here with "EXC_BAD_ACCESS"
  }
  return [super readFromURL:url ofType:type error: err];
}

// ...
// fairly standard -dataOfType:error: and 
// -readFromData:ofType:error: implementations


PS: If your answer is really good (with sample code, etc.), I will award a 100rep bounty.

解决方案

Use extended attributes. See setxattr().

Here's a sample call to write a string:

NSData* encodedString = [theString dataUsingEncoding:NSUTF8StringEncoding];
int rc = setxattr("/path/to/your/file", "com.yourcompany.yourapp.yourattributename", [encodedString bytes], [encodedString length], 0, 0);
if (rc)
    /* handle error */;

To read a string:

ssize_t len = getxattr("/path/to/your/file", "com.yourcompany.yourapp.yourattributename", NULL, 0, 0, 0);
if (len < 0)
    /* handle error */;
NSMutableData* data = [NSMutableData dataWithLength:len];
len = getxattr("/path/to/your/file", "com.yourcompany.yourapp.yourattributename", [data mutableBytes], len, 0, 0);
NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

PS: Don't you have to set the bounty on the question before it's answered?

这篇关于使用纯文本文件存储任意元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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