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