BPicture: Fix archive constructor.
[haiku.git] / src / kits / mail / NodeMessage.cpp
blob06cad88c157e5be3a22207782af5e9cc4939c9d6
1 #include "NodeMessage.h"
2 #include <StorageKit.h>
3 #include <fs_attr.h>
4 #include <stdlib.h>
6 /*
7 These functions gives a nice BMessage interface to node attributes,
8 by letting you transfer attributes to and from BMessages. It makes
9 it so you can use all the convenient Find...() and Add...() functions
10 provided by BMessage for attributes too. You use it as follows:
12 BMessage m;
13 BNode n(path);
14 if (reading) { n>>m; printf("woohoo=%s\n",m.FindString("woohoo")) }
15 else { m.AddString("woohoo","it's howdy doody time"); n<<m; }
17 If there is more than one data item with a given name, the first
18 item is the one writen to the node.
20 _EXPORT BNode& operator<<(BNode& n, const BMessage& m)
22 #if defined(HAIKU_TARGET_PLATFORM_DANO)
23 const
24 #endif
25 char *name;
26 type_code type;
27 ssize_t bytes;
28 const void *data;
30 for (int32 i = 0;
31 m.GetInfo(B_ANY_TYPE, i, &name, &type) == 0;
32 i++) {
33 m.FindData (name,type,0,&data,&bytes);
34 n.WriteAttr(name,type,0, data, bytes);
37 return n;
40 _EXPORT BNode& operator>>(BNode& n, BMessage& m)
42 char name[B_ATTR_NAME_LENGTH];
43 attr_info info;
44 char *buf = NULL;
46 n.RewindAttrs();
47 while (n.GetNextAttrName(name) == B_OK) {
48 if (n.GetAttrInfo(name,&info) != B_OK)
49 continue;
51 // resize the buffer
52 if (char *newBuffer = (char*)realloc(buf, info.size))
53 buf = newBuffer;
54 else
55 continue;
57 info.size=n.ReadAttr(name,info.type,0,buf,info.size);
58 if (info.size >= 0)
59 m.AddData(name,info.type,buf,info.size);
61 n.RewindAttrs();
63 free(buf);
65 return n;