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