1#ifndef _MESSAGE_UTILS_H_
2#define _MESSAGE_UTILS_H_
3
4#include <ByteOrder.h>
5#include <DataIO.h>
6#include <Entry.h>
7#include <Message.h>
8#include <SupportDefs.h>
9
10
11namespace BPrivate {	// Only putting these here because Be did
12
13status_t entry_ref_flatten(char* buffer, size_t* size, const entry_ref* ref);
14status_t entry_ref_unflatten(entry_ref* ref, const char* buffer, size_t size);
15status_t entry_ref_swap(char* buffer, size_t size);
16
17uint32 CalculateChecksum(const uint8 *buffer, int32 size);
18
19}	// namespace BPrivate
20
21
22template<class T>
23inline void
24byte_swap(T &/*data*/)
25{
26	// Specialize for data types which actually swap
27}
28
29
30inline void
31write_helper(BDataIO *stream, const void *data, size_t size)
32{
33	status_t error = stream->Write(data, size);
34	if (error < B_OK)
35		throw error;
36}
37
38
39class TReadHelper {
40public:
41							TReadHelper(BDataIO *stream)
42								:	fStream(stream),
43									fError(B_OK),
44									fSwap(false)
45							{
46							}
47
48							TReadHelper(BDataIO *stream, bool swap)
49								:	fStream(stream),
50									fError(B_OK),
51									fSwap(swap)
52							{
53							}
54
55		template<class T>
56		inline void			operator()(T &data)
57							{
58								fError = fStream->Read((void *)&data, sizeof(T));
59								if (fError > B_OK) {
60									if (IsSwapping())
61										byte_swap(data);
62									return;
63								}
64
65								if (fError == 0)
66									fError = B_ERROR;
67								throw fError;
68							}
69
70		template<class T>
71		inline void			operator()(T data, size_t len)
72							{
73								fError = fStream->Read((void *)data, len);
74								if (fError >= B_OK)
75									return;
76
77								throw fError;
78							}
79
80		status_t			Status() const { return fError >= B_OK ? B_OK : fError; };
81
82		void				SetSwap(bool yesNo) { fSwap = yesNo; };
83		bool				IsSwapping() const { return fSwap; };
84
85private:
86		BDataIO				*fStream;
87		status_t			fError;
88		bool				fSwap;
89};
90
91
92class TChecksumHelper {
93public:
94							TChecksumHelper(uchar* buffer)
95								:	fBuffer(buffer),
96									fBufPtr(buffer)
97							{
98							}
99
100		template<class T>
101		inline void			Cache(const T &data)
102							{
103								*((T*)fBufPtr) = data;
104								fBufPtr += sizeof (T);
105							}
106
107		int32    			CheckSum();
108
109private:
110		uchar				*fBuffer;
111		uchar				*fBufPtr;
112};
113
114
115template<>
116inline void
117byte_swap(double &data)
118{
119	data = __swap_double(data);
120}
121
122
123template<>
124inline void
125byte_swap(float &data)
126{
127	data = __swap_float(data);
128}
129
130
131template<>
132inline void
133byte_swap(int64 &data)
134{
135	data = __swap_int64(data);
136}
137
138
139template<>
140inline void
141byte_swap(uint64 &data)
142{
143	data = __swap_int64(data);
144}
145
146
147template<>
148inline void
149byte_swap(int32 &data)
150{
151	data = __swap_int32(data);
152}
153
154
155template<>
156inline void
157byte_swap(uint32 &data)
158{
159	data = __swap_int32(data);
160}
161
162
163template<>
164inline void
165byte_swap(int16 &data)
166{
167	data = __swap_int16(data);
168}
169
170
171template<>
172inline void
173byte_swap(uint16 &data)
174{
175	data = __swap_int16(data);
176}
177
178
179template<>
180inline void
181byte_swap(entry_ref &data)
182{
183	byte_swap(data.device);
184	byte_swap(data.directory);
185}
186
187#endif	// _MESSAGE_UTILS_H_
188