2004-09-21 02:31:50 +04:00
|
|
|
#include "NodeMessage.h"
|
|
|
|
#include <StorageKit.h>
|
|
|
|
#include <fs_attr.h>
|
2005-12-01 02:55:02 +03:00
|
|
|
#include <stdlib.h>
|
2005-12-01 02:41:40 +03:00
|
|
|
|
2004-09-21 02:31:50 +04:00
|
|
|
/*
|
|
|
|
These functions gives a nice BMessage interface to node attributes,
|
|
|
|
by letting you transfer attributes to and from BMessages. It makes
|
|
|
|
it so you can use all the convenient Find...() and Add...() functions
|
|
|
|
provided by BMessage for attributes too. You use it as follows:
|
|
|
|
|
|
|
|
BMessage m;
|
|
|
|
BNode n(path);
|
|
|
|
if (reading) { n>>m; printf("woohoo=%s\n",m.FindString("woohoo")) }
|
|
|
|
else { m.AddString("woohoo","it's howdy doody time"); n<<m; }
|
|
|
|
|
|
|
|
If there is more than one data item with a given name, the first
|
|
|
|
item is the one writen to the node.
|
|
|
|
*/
|
|
|
|
_EXPORT BNode& operator<<(BNode& n, const BMessage& m)
|
|
|
|
{
|
2006-01-02 07:37:47 +03:00
|
|
|
#if defined(HAIKU_TARGET_PLATFORM_DANO)
|
2004-09-21 02:31:50 +04:00
|
|
|
const
|
|
|
|
#endif
|
|
|
|
char *name;
|
|
|
|
type_code type;
|
|
|
|
ssize_t bytes;
|
|
|
|
const void *data;
|
|
|
|
|
2008-04-26 16:16:53 +04:00
|
|
|
for (int32 i = 0;
|
2004-09-21 02:31:50 +04:00
|
|
|
m.GetInfo(B_ANY_TYPE, i, &name, &type) == 0;
|
2008-04-26 16:16:53 +04:00
|
|
|
i++) {
|
2004-09-21 02:31:50 +04:00
|
|
|
m.FindData (name,type,0,&data,&bytes);
|
|
|
|
n.WriteAttr(name,type,0, data, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
_EXPORT BNode& operator>>(BNode& n, BMessage& m)
|
|
|
|
{
|
|
|
|
char name[B_ATTR_NAME_LENGTH];
|
|
|
|
attr_info info;
|
2005-12-01 02:55:02 +03:00
|
|
|
char *buf = NULL;
|
2004-09-21 02:31:50 +04:00
|
|
|
|
|
|
|
n.RewindAttrs();
|
2008-04-26 16:16:53 +04:00
|
|
|
while (n.GetNextAttrName(name) == B_OK) {
|
2005-12-01 02:55:02 +03:00
|
|
|
if (n.GetAttrInfo(name,&info) != B_OK)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// resize the buffer
|
|
|
|
if (char *newBuffer = (char*)realloc(buf, info.size))
|
|
|
|
buf = newBuffer;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
info.size=n.ReadAttr(name,info.type,0,buf,info.size);
|
2004-09-21 02:31:50 +04:00
|
|
|
if (info.size >= 0)
|
2005-12-01 02:55:02 +03:00
|
|
|
m.AddData(name,info.type,buf,info.size);
|
2004-09-21 02:31:50 +04:00
|
|
|
}
|
|
|
|
n.RewindAttrs();
|
2005-12-01 02:55:02 +03:00
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
2004-09-21 02:31:50 +04:00
|
|
|
return n;
|
|
|
|
}
|