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 a B_RECT attribute 43 // filter may withold certain attributes 44 // buffer is a 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 are also 48 // available 49 50 51 #ifndef __ATTRIBUTE_STREAM__ 52 #define __ATTRIBUTE_STREAM__ 53 54 #include <Node.h> 55 #include <Rect.h> 56 #include <String.h> 57 #include <TypeConstants.h> 58 59 #include <fs_attr.h> 60 61 #include "ObjectList.h" 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 class AttributeStreamNode { 96 public: 97 AttributeStreamNode(); 98 virtual ~AttributeStreamNode(); 99 100 AttributeStreamNode &operator<<(AttributeStreamNode &source); 101 // workhorse call 102 // to the outside makes this node a part of the stream, passing on 103 // any data it has, gets, transforms, doesn't filter out 104 // 105 // under the hood sets up streaming into the next node; hooking 106 // up source and destination, forces the stream head to start 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, uint32 type, off_t size, 117 void *buffer, void (*swapFunc)(void *) = 0); 118 // read from this node 119 virtual off_t Write(const char *name, const char *foreignName, uint32 type, off_t size, 120 const void *buffer); 121 // write to this node 122 123 124 // work calls 125 virtual bool Drive(); 126 // node at the head of the stream makes the entire stream 127 // feed it 128 virtual const AttributeInfo *Next(); 129 // give me the next attribute in the stream 130 virtual const char *Get(); 131 // give me the data of the attribute in the stream that was just returned 132 // by Next 133 // assumes there is a buffering node somewhere on the way to 134 // 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 just returned 137 // by next 138 // <buffer> is big enough to hold the entire 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 class AttributeStreamFileNode : public AttributeStreamNode { 156 // handles reading and writing attributes to and from the 157 // stream 158 public: 159 AttributeStreamFileNode(); 160 AttributeStreamFileNode(BNode *); 161 162 virtual void MakeEmpty(); 163 virtual void Rewind(); 164 virtual off_t Contains(const char *name, uint32 type); 165 virtual off_t Read(const char *name, const char *foreignName, uint32 type, off_t size, 166 void *buffer, void (*swapFunc)(void *) = 0); 167 virtual off_t Write(const char *name, const char *foreignName, uint32 type, off_t size, 168 const void *buffer); 169 170 void SetTo(BNode *); 171 172 BNode *Node() 173 { return fNode; } 174 175 protected: 176 virtual bool CanFeed() const { return true; } 177 178 virtual bool Drive(); 179 // give me all the attributes, I'll write them into myself 180 virtual const AttributeInfo *Next(); 181 // return the info for the next attribute I can read for you 182 virtual const char *Get(); 183 virtual bool Fill(char *buffer) const; 184 185 private: 186 AttributeInfo fCurrentAttr; 187 BNode *fNode; 188 189 typedef AttributeStreamNode _inherited; 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, uint32 type, off_t size, 201 void *buffer, void (*swapFunc)(void *) = 0); 202 virtual off_t Write(const char *name, const char *foreignName, uint32 type, off_t size, 203 const void *buffer); 204 205 protected: 206 207 virtual bool CanFeed() const { return true; } 208 virtual void Rewind(); 209 virtual bool Drive(); 210 virtual const AttributeInfo *Next(); 211 virtual const char *Get(); 212 virtual bool Fill(char *buffer) const; 213 214 class AttrNode { 215 public: 216 AttrNode(const char *name, uint32 type, off_t size, char *data) 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 238 BObjectList<AttrNode> fAttributes; 239 int32 fCurrentIndex; 240 241 typedef AttributeStreamNode _inherited; 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 254 virtual bool CanFeed() const { return true; } 255 virtual void Rewind(); 256 virtual const AttributeInfo *Next(); 257 virtual const char *Get(); 258 virtual bool Fill(char *buffer) const; 259 260 int32 Find(const char *name, uint32 type) const; 261 262 private: 263 AttributeInfo fCurrentAttr; 264 const AttributeTemplate *fAttributes; 265 int32 fCurrentIndex; 266 int32 fCount; 267 268 typedef AttributeStreamNode _inherited; 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, uint32 type, off_t size, 278 void *buffer, void (*swapFunc)(void *) = 0); 279 virtual off_t Write(const char *name, const char *foreignName, uint32 type, off_t size, 280 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 class NamesToAcceptAttrFilter : public AttributeStreamFilterNode { 292 // filter node that only passes thru attributes that match 293 // a list of names 294 public: 295 NamesToAcceptAttrFilter(const char **); 296 297 protected: 298 virtual bool Reject(const char *name, uint32 type, off_t size); 299 300 private: 301 const char **fNameList; 302 }; 303 304 class SelectiveAttributeTransformer : public AttributeStreamNode { 305 // node applies a transformation on specified attributes 306 public: 307 SelectiveAttributeTransformer(const char *attributeName, bool (*)(const char *, 308 uint32 , off_t , void *, void *), void *params); 309 virtual ~SelectiveAttributeTransformer(); 310 311 virtual off_t Read(const char *name, const char *foreignName, uint32 type, off_t size, 312 void *buffer, void (*swapFunc)(void *) = 0); 313 314 virtual void Rewind(); 315 316 protected: 317 virtual bool WillTransform(const char *name, uint32 type, off_t size, const char *data) const; 318 // override to implement filtering; should only return true if transformation will 319 // occur 320 virtual char *CopyAndApplyTransformer(const char *name, uint32 type, off_t size, const char *data); 321 // makes a copy of data 322 virtual bool ApplyTransformer(const char *name, uint32 type, off_t size, char *data); 323 // transforms in place 324 virtual const AttributeInfo *Next(); 325 virtual const char *Get(); 326 327 private: 328 AttributeInfo fCurrentAttr; 329 const char *fAttributeNameToTransform; 330 bool (*fTransformFunc)(const char *, uint32 , off_t , void *, void *); 331 void *fTransformParams; 332 333 BObjectList<char> fTransformedBuffers; 334 335 typedef AttributeStreamNode _inherited; 336 }; 337 338 template <class Type> 339 class AttributeStreamConstValue : public AttributeStreamNode { 340 public: 341 AttributeStreamConstValue(const char *name, uint32 attributeType, Type value); 342 protected: 343 344 virtual bool CanFeed() const { return true; } 345 virtual void Rewind() { fRewound = true; } 346 virtual const AttributeInfo *Next(); 347 virtual const char *Get(); 348 virtual bool Fill(char *buffer) const; 349 350 int32 Find(const char *name, uint32 type) const; 351 352 private: 353 AttributeInfo fAttr; 354 Type fValue; 355 bool fRewound; 356 357 typedef AttributeStreamNode _inherited; 358 }; 359 360 template<class Type> 361 AttributeStreamConstValue<Type>::AttributeStreamConstValue(const char *name, 362 uint32 attributeType, Type value) 363 : fAttr(name, attributeType, sizeof(Type)), 364 fValue(value), 365 fRewound(true) 366 { 367 } 368 369 template<class Type> 370 const AttributeInfo * 371 AttributeStreamConstValue<Type>::Next() 372 { 373 if (!fRewound) 374 return NULL; 375 376 fRewound = false; 377 return &fAttr; 378 } 379 380 template<class Type> 381 const char * 382 AttributeStreamConstValue<Type>::Get() 383 { 384 return (const char *)&fValue; 385 } 386 387 template<class Type> 388 bool 389 AttributeStreamConstValue<Type>::Fill(char *buffer) const 390 { 391 memcpy(buffer, &fValue, sizeof(Type)); 392 return true; 393 } 394 395 template<class Type> 396 int32 397 AttributeStreamConstValue<Type>::Find(const char *name, uint32 type) const 398 { 399 if (strcmp(fAttr.Name(), name) == 0 && type = fAttr.Type()) 400 return 0; 401 402 return -1; 403 } 404 405 class AttributeStreamBoolValue : public AttributeStreamConstValue<bool> { 406 public: 407 AttributeStreamBoolValue(const char *name, bool value) 408 : AttributeStreamConstValue<bool>(name, B_BOOL_TYPE, value) 409 {} 410 }; 411 412 class AttributeStreamInt32Value : public AttributeStreamConstValue<int32> { 413 public: 414 AttributeStreamInt32Value(const char *name, int32 value) 415 : AttributeStreamConstValue<int32>(name, B_INT32_TYPE, value) 416 {} 417 }; 418 419 class AttributeStreamInt64Value : public AttributeStreamConstValue<int64> { 420 public: 421 AttributeStreamInt64Value(const char *name, int64 value) 422 : AttributeStreamConstValue<int64>(name, B_INT64_TYPE, value) 423 {} 424 }; 425 426 class AttributeStreamRectValue : public AttributeStreamConstValue<BRect> { 427 public: 428 AttributeStreamRectValue(const char *name, BRect value) 429 : AttributeStreamConstValue<BRect>(name, B_RECT_TYPE, value) 430 {} 431 }; 432 433 class AttributeStreamFloatValue : public AttributeStreamConstValue<float> { 434 public: 435 AttributeStreamFloatValue(const char *name, float value) 436 : AttributeStreamConstValue<float>(name, B_FLOAT_TYPE, value) 437 {} 438 }; 439 440 } 441 442 using namespace BPrivate; 443 444 #endif 445