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