xref: /haiku/src/kits/tracker/AttributeStream.h (revision 9437e677ba3d5f813929944ae2b09fc37feb8d2d)
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__
50 #define __ATTRIBUTE_STREAM__
51 
52 
53 #include <Node.h>
54 #include <Rect.h>
55 #include <String.h>
56 #include <TypeConstants.h>
57 
58 #include <fs_attr.h>
59 
60 #include "ObjectList.h"
61 
62 
63 namespace BPrivate {
64 
65 struct AttributeTemplate {
66 	// used for read-only attribute source
67 	const char* fAttributeName;
68 	uint32 fAttributeType;
69 	off_t fSize;
70 	const char* fBits;
71 };
72 
73 
74 class AttributeInfo {
75 	// utility class for internal attribute description
76 public:
77 	AttributeInfo()
78 		{}
79 	AttributeInfo(const AttributeInfo &);
80 	AttributeInfo(const char*, attr_info);
81 	AttributeInfo(const char*, uint32, off_t);
82 
83 	void SetTo(const AttributeInfo &);
84 	void SetTo(const char*, attr_info);
85 	void SetTo(const char*, uint32, off_t);
86 	const char* Name() const;
87 	uint32 Type() const;
88 	off_t Size() const;
89 
90 private:
91 	BString fName;
92 	attr_info fInfo;
93 };
94 
95 
96 class AttributeStreamNode {
97 public:
98 	AttributeStreamNode();
99 	virtual ~AttributeStreamNode();
100 
101 	AttributeStreamNode &operator<<(AttributeStreamNode &source);
102 		// workhorse call
103 		// to the outside makes this node a part of the stream, passing on
104 		// any data it has, gets, transforms, doesn't filter out
105 		//
106 		// under the hood sets up streaming into the next node; hooking
107 		// up source and destination, forces the stream head to start
108 		// streaming
109 
110 	virtual void Rewind();
111 		// get ready to start all over again
112 	virtual void MakeEmpty() {}
113 		// remove any attributes the node may have
114 
115 	virtual off_t Contains(const char*, uint32);
116 		// returns size of attribute if found
117 
118 	virtual off_t Read(const char* name, const char* foreignName,
119 		uint32 type, off_t size, void* buffer, void (*swapFunc)(void*) = 0);
120 		// read from this node
121 	virtual off_t Write(const char* name, const char* foreignName,
122 		uint32 type, off_t size, const void* buffer);
123 		// write to this node
124 
125 	// work calls
126 	virtual bool Drive();
127 		// node at the head of the stream makes the entire stream
128 		// feed it
129 	virtual const AttributeInfo* Next();
130 		// give me the next attribute in the stream
131 	virtual const char* Get();
132 		// give me the data of the attribute in the stream that was just
133 		// returned by Next assumes there is a buffering node somewhere on the
134 		// way to the source, from which the resulting buffer is borrowed
135 	virtual bool Fill(char* buffer) const;
136 		// fill the buffer with data of the attribute in the stream that was
137 		// just returned by next <buffer> is big enough to hold the entire
138 		// attribute data
139 
140 	virtual bool CanFeed() const { return false; }
141 		// return true if can work as a source for the entire stream
142 
143 private:
144 	bool Start();
145 		// utility call, used to start up the stream by finding the ultimate
146 		// target of the stream and calling Drive on it
147 
148 	void Detach();
149 
150 protected:
151 	AttributeStreamNode* fReadFrom;
152 	AttributeStreamNode* fWriteTo;
153 };
154 
155 
156 class AttributeStreamFileNode : public AttributeStreamNode {
157 	// handles reading and writing attributes to and from the
158 	// stream
159 public:
160 	AttributeStreamFileNode();
161 	AttributeStreamFileNode(BNode*);
162 
163 	virtual void MakeEmpty();
164 	virtual void Rewind();
165 	virtual off_t Contains(const char* name, uint32 type);
166 	virtual off_t Read(const char* name, const char* foreignName, uint32 type,
167 		off_t size, void* buffer, void (*swapFunc)(void*) = 0);
168 	virtual off_t Write(const char* name, const char* foreignName, uint32 type,
169 		off_t size, const void* buffer);
170 
171 	void SetTo(BNode*);
172 
173 	BNode* Node()
174 		{ return fNode; }
175 
176 protected:
177 	virtual bool CanFeed() const { return true; }
178 
179 	virtual bool Drive();
180 		// give me all the attributes, I'll write them into myself
181 	virtual const AttributeInfo* Next();
182 		// return the info for the next attribute I can read for you
183 	virtual const char* Get();
184 	virtual bool Fill(char* buffer) const;
185 
186 private:
187 	AttributeInfo fCurrentAttr;
188 	BNode* fNode;
189 
190 	typedef AttributeStreamNode _inherited;
191 };
192 
193 
194 class AttributeStreamMemoryNode : public AttributeStreamNode {
195 	// in memory attribute buffer; can be both target of writing and source
196 	// of reading at the same time
197 public:
198 	AttributeStreamMemoryNode();
199 
200 	virtual void MakeEmpty();
201 	virtual off_t Contains(const char* name, uint32 type);
202 	virtual off_t Read(const char* name, const char* foreignName,
203 		uint32 type, off_t size, void* buffer, void (*swapFunc)(void*) = 0);
204 	virtual off_t Write(const char* name, const char* foreignName,
205 		uint32 type, off_t size, const void* buffer);
206 
207 protected:
208 	virtual bool CanFeed() const { return true; }
209 	virtual void Rewind();
210 	virtual bool Drive();
211 	virtual const AttributeInfo* Next();
212 	virtual const char* Get();
213 	virtual bool Fill(char* buffer) const;
214 
215 	class AttrNode {
216 	public:
217 		AttrNode(const char* name, uint32 type, off_t size, char* data)
218 		:
219 		fAttr(name, type, size),
220 		fData(data)
221 		{
222 		}
223 
224 		~AttrNode()
225 		{
226 			delete[] fData;
227 		}
228 
229 		AttributeInfo fAttr;
230 		char* fData;
231 	};
232 
233 		// utility calls
234 	virtual AttrNode* BufferingGet();
235 	virtual AttrNode* BufferingGet(const char* name, uint32 type, off_t size);
236 	int32 Find(const char* name, uint32 type) const;
237 
238 private:
239 	BObjectList<AttrNode> fAttributes;
240 	int32 fCurrentIndex;
241 
242 	typedef AttributeStreamNode _inherited;
243 };
244 
245 
246 class AttributeStreamTemplateNode : public AttributeStreamNode {
247 	// in read-only memory attribute source
248 	// can only be used as a source for Next and Get
249 public:
250 	AttributeStreamTemplateNode(const AttributeTemplate*, int32 count);
251 
252 	virtual off_t Contains(const char* name, uint32 type);
253 
254 protected:
255 	virtual bool CanFeed() const { return true; }
256 	virtual void Rewind();
257 	virtual const AttributeInfo* Next();
258 	virtual const char* Get();
259 	virtual bool Fill(char* buffer) const;
260 
261 	int32 Find(const char* name, uint32 type) const;
262 
263 private:
264 	AttributeInfo fCurrentAttr;
265 	const AttributeTemplate* fAttributes;
266 	int32 fCurrentIndex;
267 	int32 fCount;
268 
269 	typedef AttributeStreamNode _inherited;
270 };
271 
272 
273 class AttributeStreamFilterNode : public AttributeStreamNode {
274 	// filter node may not pass thru specified attributes
275 public:
276 	AttributeStreamFilterNode()
277 		{}
278 	virtual off_t Contains(const char* name, uint32 type);
279 	virtual off_t Read(const char* name, const char* foreignName,
280 		uint32 type, off_t size, void* buffer, void (*swapFunc)(void*) = 0);
281 	virtual off_t Write(const char* name, const char* foreignName,
282 		uint32 type, off_t size, const void* buffer);
283 
284 protected:
285 	virtual bool Reject(const char* name, uint32 type, off_t size);
286 		// override to implement filtering
287 	virtual const AttributeInfo* Next();
288 
289 private:
290 	typedef AttributeStreamNode _inherited;
291 };
292 
293 
294 class NamesToAcceptAttrFilter : public AttributeStreamFilterNode {
295 	// filter node that only passes thru attributes that match
296 	// a list of names
297 public:
298 	NamesToAcceptAttrFilter(const char**);
299 
300 protected:
301 	virtual bool Reject(const char* name, uint32 type, off_t size);
302 
303 private:
304 	const char** fNameList;
305 };
306 
307 
308 class SelectiveAttributeTransformer : public AttributeStreamNode {
309 	// node applies a transformation on specified attributes
310 public:
311 	SelectiveAttributeTransformer(const char* attributeName,
312 		bool (*)(const char*, uint32 , off_t , void*, void*), void* params);
313 	virtual ~SelectiveAttributeTransformer();
314 
315 	virtual off_t Read(const char* name, const char* foreignName, uint32 type,
316 		off_t size, void* buffer, void (*swapFunc)(void*) = 0);
317 
318 	virtual void Rewind();
319 
320 protected:
321 	virtual bool WillTransform(const char* name, uint32 type, off_t size,
322 		const char* data) const;
323 		// override to implement filtering, should only return true if
324 		// transformation will occur
325 	virtual char* CopyAndApplyTransformer(const char* name, uint32 type,
326 		off_t size, const char* data);
327 		// makes a copy of data
328 	virtual bool ApplyTransformer(const char* name, uint32 type, off_t size,
329 		char* data);
330 		// transforms in place
331 	virtual const AttributeInfo* Next();
332 	virtual const char* Get();
333 
334 private:
335 	AttributeInfo fCurrentAttr;
336 	const char* fAttributeNameToTransform;
337 	bool (*fTransformFunc)(const char*, uint32 , off_t , void*, void*);
338 	void* fTransformParams;
339 
340 	BObjectList<char> fTransformedBuffers;
341 
342 	typedef AttributeStreamNode _inherited;
343 };
344 
345 
346 template <class Type>
347 class AttributeStreamConstValue : public AttributeStreamNode {
348 public:
349 	AttributeStreamConstValue(const char* name, uint32 attributeType,
350 		Type value);
351 
352 protected:
353 	virtual bool CanFeed() const { return true; }
354 	virtual void Rewind() { fRewound = true; }
355 	virtual const AttributeInfo* Next();
356 	virtual const char* Get();
357 	virtual bool Fill(char* buffer) const;
358 
359 	int32 Find(const char* name, uint32 type) const;
360 
361 private:
362 	AttributeInfo fAttr;
363 	Type fValue;
364 	bool fRewound;
365 
366 	typedef AttributeStreamNode _inherited;
367 };
368 
369 
370 template<class Type>
371 AttributeStreamConstValue<Type>::AttributeStreamConstValue(const char* name,
372 	uint32 attributeType, Type value)
373 	:	fAttr(name, attributeType, sizeof(Type)),
374 		fValue(value),
375 		fRewound(true)
376 {
377 }
378 
379 
380 template<class Type>
381 const AttributeInfo*
382 AttributeStreamConstValue<Type>::Next()
383 {
384 	if (!fRewound)
385 		return NULL;
386 
387 	fRewound = false;
388 	return &fAttr;
389 }
390 
391 
392 template<class Type>
393 const char*
394 AttributeStreamConstValue<Type>::Get()
395 {
396 	return (const char*)&fValue;
397 }
398 
399 
400 template<class Type>
401 bool
402 AttributeStreamConstValue<Type>::Fill(char* buffer) const
403 {
404 	memcpy(buffer, &fValue, sizeof(Type));
405 	return true;
406 }
407 
408 
409 template<class Type>
410 int32
411 AttributeStreamConstValue<Type>::Find(const char* name, uint32 type) const
412 {
413 	if (strcmp(fAttr.Name(), name) == 0 && type == fAttr.Type())
414 		return 0;
415 
416 	return -1;
417 }
418 
419 
420 class AttributeStreamBoolValue : public AttributeStreamConstValue<bool> {
421 public:
422 	AttributeStreamBoolValue(const char* name, bool value)
423 		:	AttributeStreamConstValue<bool>(name, B_BOOL_TYPE, value)
424 		{}
425 };
426 
427 
428 class AttributeStreamInt32Value : public AttributeStreamConstValue<int32> {
429 public:
430 	AttributeStreamInt32Value(const char* name, int32 value)
431 		:	AttributeStreamConstValue<int32>(name, B_INT32_TYPE, value)
432 		{}
433 };
434 
435 
436 class AttributeStreamInt64Value : public AttributeStreamConstValue<int64> {
437 public:
438 	AttributeStreamInt64Value(const char* name, int64 value)
439 		:	AttributeStreamConstValue<int64>(name, B_INT64_TYPE, value)
440 		{}
441 };
442 
443 
444 class AttributeStreamRectValue : public AttributeStreamConstValue<BRect> {
445 public:
446 	AttributeStreamRectValue(const char* name, BRect value)
447 		:	AttributeStreamConstValue<BRect>(name, B_RECT_TYPE, value)
448 		{}
449 };
450 
451 
452 class AttributeStreamFloatValue : public AttributeStreamConstValue<float> {
453 public:
454 	AttributeStreamFloatValue(const char* name, float value)
455 		:	AttributeStreamConstValue<float>(name, B_FLOAT_TYPE, value)
456 		{}
457 };
458 
459 } // namespace BPrivate
460 
461 using namespace BPrivate;
462 
463 #endif
464