xref: /haiku/src/kits/mail/NodeMessage.cpp (revision 0dacda17d52333639ef3874b7e67702b646a5672)
1 #include "NodeMessage.h"
2 #include <StorageKit.h>
3 #include <fs_attr.h>
4 #include <stdlib.h>
5 
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:
11 
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; }
16 
17    If there is more than one data item with a given name, the first
18    item is the one writen to the node.
19 */
20 _EXPORT BNode& operator<<(BNode& n, const BMessage& m)
21 {
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;
29 
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);
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 	char *buf = NULL;
45 
46 	n.RewindAttrs();
47 	while (n.GetNextAttrName(name) == B_OK) {
48 		if (n.GetAttrInfo(name,&info) != B_OK)
49 			continue;
50 
51 		// resize the buffer
52 		if (char *newBuffer = (char*)realloc(buf, info.size))
53 			buf = newBuffer;
54 		else
55 			continue;
56 
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);
60 	}
61 	n.RewindAttrs();
62 
63 	free(buf);
64 
65 	return n;
66 }
67