xref: /haiku/src/kits/tracker/AttributeStream.h (revision 49d5d59cab39e03705287a2705b6839d110a1a59)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
28 
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
33 */
34 
35 //	attribute streams allow copying/filtering/transformation of attributes
36 //	between file and/or memory nodes
37 //
38 //	for example one can use constructs of nodes like:
39 //
40 // 	destinationNode << transformer << buffer << filter << sourceNode
41 //
42 //	transformer may for instance perform endian-swapping or offsetting of
43 //	a B_RECT attribute filter may withold certain attributes buffer is a
44 //	memory allocated snapshot of attributes, may be repeatedly streamed into
45 //	other files, buffers
46 //
47 //	In addition to the whacky (but usefull) << syntax, calls like Read, Write
48 //	are also available
49 #ifndef _ATTRIBUTE_STREAM_H
50 #define _ATTRIBUTE_STREAM_H
51 
52 
53 #include <ObjectList.h>
54 #include <Node.h>
55 #include <Rect.h>
56 #include <String.h>
57 #include <TypeConstants.h>
58 
59 #include <fs_attr.h>
60 
61 
62 namespace BPrivate {
63 
64 struct AttributeTemplate {
65 	// used for read-only attribute source
66 	const char* fAttributeName;
67 	uint32 fAttributeType;
68 	off_t fSize;
69 	const char* fBits;
70 };
71 
72 
73 class AttributeInfo {
74 	// utility class for internal attribute description
75 public:
76 	AttributeInfo();
77 	AttributeInfo(const AttributeInfo& other);
78 	AttributeInfo(const char* name, attr_info info);
79 	AttributeInfo(const char* name, uint32 type, off_t size);
80 
81 	void SetTo(const AttributeInfo& other);
82 	void SetTo(const char* name, attr_info info);
83 	void SetTo(const char* name, uint32 type, off_t size);
84 	const char* Name() const;
85 	uint32 Type() const;
86 	off_t Size() const;
87 
88 private:
89 	BString fName;
90 	attr_info fInfo;
91 };
92 
93 
94 class AttributeStreamNode {
95 public:
96 	AttributeStreamNode();
97 	virtual ~AttributeStreamNode();
98 
99 	AttributeStreamNode &operator<<(AttributeStreamNode &source);
100 		// workhorse call
101 		// to the outside makes this node a part of the stream, passing on
102 		// any data it has, gets, transforms, doesn't filter out
103 		//
104 		// under the hood sets up streaming into the next node; hooking
105 		// up source and destination, forces the stream head to start
106 		// streaming
107 
108 	virtual void Rewind();
109 		// get ready to start all over again
110 	virtual void MakeEmpty() {}
111 		// remove any attributes the node may have
112 
113 	virtual off_t Contains(const char*, uint32);
114 		// returns size of attribute if found
115 
116 	virtual off_t Read(const char* name, const char* foreignName,
117 		uint32 type, off_t size, void* buffer, void (*swapFunc)(void*) = 0);
118 		// read from this node
119 	virtual off_t Write(const char* name, const char* foreignName,
120 		uint32 type, off_t size, const void* buffer);
121 		// write to this node
122 
123 	// work calls
124 	virtual bool Drive();
125 		// node at the head of the stream makes the entire stream
126 		// feed it
127 	virtual const AttributeInfo* Next();
128 		// give me the next attribute in the stream
129 	virtual const char* Get();
130 		// give me the data of the attribute in the stream that was just
131 		// returned by Next assumes there is a buffering node somewhere on the
132 		// way to the source, from which the resulting buffer is borrowed
133 	virtual bool Fill(char* buffer) const;
134 		// fill the buffer with data of the attribute in the stream that was
135 		// just returned by next <buffer> is big enough to hold the entire
136 		// attribute data
137 
138 	virtual bool CanFeed() const { return false; }
139 		// return true if can work as a source for the entire stream
140 
141 private:
142 	bool Start();
143 		// utility call, used to start up the stream by finding the ultimate
144 		// target of the stream and calling Drive on it
145 
146 	void Detach();
147 
148 protected:
149 	AttributeStreamNode* fReadFrom;
150 	AttributeStreamNode* fWriteTo;
151 };
152 
153 
154 class AttributeStreamFileNode : public AttributeStreamNode {
155 	// handles reading and writing attributes to and from the
156 	// stream
157 public:
158 	AttributeStreamFileNode();
159 	AttributeStreamFileNode(BNode*);
160 
161 	virtual void MakeEmpty();
162 	virtual void Rewind();
163 	virtual off_t Contains(const char* name, uint32 type);
164 	virtual off_t Read(const char* name, const char* foreignName, uint32 type,
165 		off_t size, void* buffer, void (*swapFunc)(void*) = 0);
166 	virtual off_t Write(const char* name, const char* foreignName, uint32 type,
167 		off_t size, const void* buffer);
168 
169 	void SetTo(BNode*);
170 
171 	BNode* Node()
172 		{ return fNode; }
173 
174 protected:
175 	virtual bool CanFeed() const { return true; }
176 
177 	virtual bool Drive();
178 		// give me all the attributes, I'll write them into myself
179 	virtual const AttributeInfo* Next();
180 		// return the info for the next attribute I can read for you
181 	virtual const char* Get();
182 	virtual bool Fill(char* buffer) const;
183 
184 private:
185 	AttributeInfo fCurrentAttr;
186 	BNode* fNode;
187 
188 	typedef AttributeStreamNode _inherited;
189 };
190 
191 
192 class AttributeStreamMemoryNode : public AttributeStreamNode {
193 	// in memory attribute buffer; can be both target of writing and source
194 	// of reading at the same time
195 public:
196 	AttributeStreamMemoryNode();
197 
198 	virtual void MakeEmpty();
199 	virtual off_t Contains(const char* name, uint32 type);
200 	virtual off_t Read(const char* name, const char* foreignName,
201 		uint32 type, off_t size, void* buffer, void (*swapFunc)(void*) = 0);
202 	virtual off_t Write(const char* name, const char* foreignName,
203 		uint32 type, off_t size, const void* buffer);
204 
205 protected:
206 	virtual bool CanFeed() const { return true; }
207 	virtual void Rewind();
208 	virtual bool Drive();
209 	virtual const AttributeInfo* Next();
210 	virtual const char* Get();
211 	virtual bool Fill(char* buffer) const;
212 
213 	class AttrNode {
214 	public:
215 		AttrNode(const char* name, uint32 type, off_t size, char* data)
216 		:
217 		fAttr(name, type, size),
218 		fData(data)
219 		{
220 		}
221 
222 		~AttrNode()
223 		{
224 			delete[] fData;
225 		}
226 
227 		AttributeInfo fAttr;
228 		char* fData;
229 	};
230 
231 		// utility calls
232 	virtual AttrNode* BufferingGet();
233 	virtual AttrNode* BufferingGet(const char* name, uint32 type, off_t size);
234 	int32 Find(const char* name, uint32 type) const;
235 
236 private:
237 	BObjectList<AttrNode> fAttributes;
238 	int32 fCurrentIndex;
239 
240 	typedef AttributeStreamNode _inherited;
241 };
242 
243 
244 class AttributeStreamTemplateNode : public AttributeStreamNode {
245 	// in read-only memory attribute source
246 	// can only be used as a source for Next and Get
247 public:
248 	AttributeStreamTemplateNode(const AttributeTemplate*, int32 count);
249 
250 	virtual off_t Contains(const char* name, uint32 type);
251 
252 protected:
253 	virtual bool CanFeed() const { return true; }
254 	virtual void Rewind();
255 	virtual const AttributeInfo* Next();
256 	virtual const char* Get();
257 	virtual bool Fill(char* buffer) const;
258 
259 	int32 Find(const char* name, uint32 type) const;
260 
261 private:
262 	AttributeInfo fCurrentAttr;
263 	const AttributeTemplate* fAttributes;
264 	int32 fCurrentIndex;
265 	int32 fCount;
266 
267 	typedef AttributeStreamNode _inherited;
268 };
269 
270 
271 class AttributeStreamFilterNode : public AttributeStreamNode {
272 	// filter node may not pass thru specified attributes
273 public:
274 	AttributeStreamFilterNode()
275 		{}
276 	virtual off_t Contains(const char* name, uint32 type);
277 	virtual off_t Read(const char* name, const char* foreignName,
278 		uint32 type, off_t size, void* buffer, void (*swapFunc)(void*) = 0);
279 	virtual off_t Write(const char* name, const char* foreignName,
280 		uint32 type, off_t size, const void* buffer);
281 
282 protected:
283 	virtual bool Reject(const char* name, uint32 type, off_t size);
284 		// override to implement filtering
285 	virtual const AttributeInfo* Next();
286 
287 private:
288 	typedef AttributeStreamNode _inherited;
289 };
290 
291 
292 class NamesToAcceptAttrFilter : public AttributeStreamFilterNode {
293 	// filter node that only passes thru attributes that match
294 	// a list of names
295 public:
296 	NamesToAcceptAttrFilter(const char**);
297 
298 protected:
299 	virtual bool Reject(const char* name, uint32 type, off_t size);
300 
301 private:
302 	const char** fNameList;
303 };
304 
305 
306 class SelectiveAttributeTransformer : public AttributeStreamNode {
307 	// node applies a transformation on specified attributes
308 public:
309 	SelectiveAttributeTransformer(const char* attributeName,
310 		bool (*)(const char*, uint32 , off_t , void*, void*), void* params);
311 	virtual ~SelectiveAttributeTransformer();
312 
313 	virtual off_t Read(const char* name, const char* foreignName, uint32 type,
314 		off_t size, void* buffer, void (*swapFunc)(void*) = 0);
315 
316 	virtual void Rewind();
317 
318 protected:
319 	virtual bool WillTransform(const char* name, uint32 type, off_t size,
320 		const char* data) const;
321 		// override to implement filtering, should only return true if
322 		// transformation will occur
323 	virtual char* CopyAndApplyTransformer(const char* name, uint32 type,
324 		off_t size, const char* data);
325 		// makes a copy of data
326 	virtual bool ApplyTransformer(const char* name, uint32 type, off_t size,
327 		char* data);
328 		// transforms in place
329 	virtual const AttributeInfo* Next();
330 	virtual const char* Get();
331 
332 private:
333 	AttributeInfo fCurrentAttr;
334 	const char* fAttributeNameToTransform;
335 	bool (*fTransformFunc)(const char*, uint32 , off_t , void*, void*);
336 	void* fTransformParams;
337 
338 	BObjectList<char> fTransformedBuffers;
339 
340 	typedef AttributeStreamNode _inherited;
341 };
342 
343 
344 template <class Type>
345 class AttributeStreamConstValue : public AttributeStreamNode {
346 public:
347 	AttributeStreamConstValue(const char* name, uint32 attributeType,
348 		Type value);
349 
350 protected:
351 	virtual bool CanFeed() const { return true; }
352 	virtual void Rewind() { fRewound = true; }
353 	virtual const AttributeInfo* Next();
354 	virtual const char* Get();
355 	virtual bool Fill(char* buffer) const;
356 
357 	int32 Find(const char* name, uint32 type) const;
358 
359 private:
360 	AttributeInfo fAttr;
361 	Type fValue;
362 	bool fRewound;
363 
364 	typedef AttributeStreamNode _inherited;
365 };
366 
367 
368 template<class Type>
369 AttributeStreamConstValue<Type>::AttributeStreamConstValue(const char* name,
370 	uint32 attributeType, Type value)
371 	:
372 	fAttr(name, attributeType, sizeof(Type)),
373 	fValue(value),
374 	fRewound(true)
375 {
376 }
377 
378 
379 template<class Type>
380 const AttributeInfo*
381 AttributeStreamConstValue<Type>::Next()
382 {
383 	if (!fRewound)
384 		return NULL;
385 
386 	fRewound = false;
387 	return &fAttr;
388 }
389 
390 
391 template<class Type>
392 const char*
393 AttributeStreamConstValue<Type>::Get()
394 {
395 	return (const char*)&fValue;
396 }
397 
398 
399 template<class Type>
400 bool
401 AttributeStreamConstValue<Type>::Fill(char* buffer) const
402 {
403 	memcpy(buffer, &fValue, sizeof(Type));
404 	return true;
405 }
406 
407 
408 template<class Type>
409 int32
410 AttributeStreamConstValue<Type>::Find(const char* name, uint32 type) const
411 {
412 	if (strcmp(fAttr.Name(), name) == 0 && type == fAttr.Type())
413 		return 0;
414 
415 	return -1;
416 }
417 
418 
419 class AttributeStreamBoolValue : public AttributeStreamConstValue<bool> {
420 public:
421 	AttributeStreamBoolValue(const char* name, bool value)
422 		:
423 		AttributeStreamConstValue<bool>(name, B_BOOL_TYPE, value)
424 	{
425 	}
426 };
427 
428 
429 class AttributeStreamInt32Value : public AttributeStreamConstValue<int32> {
430 public:
431 	AttributeStreamInt32Value(const char* name, int32 value)
432 		:
433 		AttributeStreamConstValue<int32>(name, B_INT32_TYPE, value)
434 	{
435 	}
436 };
437 
438 
439 class AttributeStreamInt64Value : public AttributeStreamConstValue<int64> {
440 public:
441 	AttributeStreamInt64Value(const char* name, int64 value)
442 		:
443 		AttributeStreamConstValue<int64>(name, B_INT64_TYPE, value)
444 	{
445 	}
446 };
447 
448 
449 class AttributeStreamRectValue : public AttributeStreamConstValue<BRect> {
450 public:
451 	AttributeStreamRectValue(const char* name, BRect value)
452 		:
453 		AttributeStreamConstValue<BRect>(name, B_RECT_TYPE, value)
454 	{
455 	}
456 };
457 
458 
459 class AttributeStreamFloatValue : public AttributeStreamConstValue<float> {
460 public:
461 	AttributeStreamFloatValue(const char* name, float value)
462 		:
463 		AttributeStreamConstValue<float>(name, B_FLOAT_TYPE, value)
464 	{
465 	}
466 };
467 
468 } // namespace BPrivate
469 
470 using namespace BPrivate;
471 
472 
473 #endif	// _ATTRIBUTE_STREAM_H
474