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 #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 streaming 108 109 virtual void Rewind(); 110 // get ready to start all over again 111 virtual void MakeEmpty() {} 112 // remove any attributes the node may have 113 114 virtual off_t Contains(const char*, uint32); 115 // returns size of attribute if found 116 117 virtual off_t Read(const char* name, const char* foreignName, uint32 type, off_t size, 118 void* buffer, void (*swapFunc)(void*) = 0); 119 // read from this node 120 virtual off_t Write(const char* name, const char* foreignName, uint32 type, off_t size, 121 const void* buffer); 122 // write to this node 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 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, uint32 type, off_t size, 203 void* buffer, void (*swapFunc)(void*) = 0); 204 virtual off_t Write(const char* name, const char* foreignName, uint32 type, off_t size, 205 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 : fAttr(name, type, size), 219 fData(data) 220 { 221 } 222 223 ~AttrNode() 224 { 225 delete [] fData; 226 } 227 228 AttributeInfo fAttr; 229 char* fData; 230 }; 231 232 // utility calls 233 virtual AttrNode* BufferingGet(); 234 virtual AttrNode* BufferingGet(const char* name, uint32 type, off_t size); 235 int32 Find(const char* name, uint32 type) const; 236 237 private: 238 BObjectList<AttrNode> fAttributes; 239 int32 fCurrentIndex; 240 241 typedef AttributeStreamNode _inherited; 242 }; 243 244 245 class AttributeStreamTemplateNode : public AttributeStreamNode { 246 // in read-only memory attribute source 247 // can only be used as a source for Next and Get 248 public: 249 AttributeStreamTemplateNode(const AttributeTemplate*, int32 count); 250 251 virtual off_t Contains(const char* name, uint32 type); 252 253 protected: 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 272 class AttributeStreamFilterNode : public AttributeStreamNode { 273 // filter node may not pass thru specified attributes 274 public: 275 AttributeStreamFilterNode() 276 {} 277 virtual off_t Contains(const char* name, uint32 type); 278 virtual off_t Read(const char* name, const char* foreignName, uint32 type, off_t size, 279 void* buffer, void (*swapFunc)(void*) = 0); 280 virtual off_t Write(const char* name, const char* foreignName, uint32 type, off_t size, 281 const void* buffer); 282 283 protected: 284 virtual bool Reject(const char* name, uint32 type, off_t size); 285 // override to implement filtering 286 virtual const AttributeInfo* Next(); 287 288 private: 289 typedef AttributeStreamNode _inherited; 290 }; 291 292 293 class NamesToAcceptAttrFilter : public AttributeStreamFilterNode { 294 // filter node that only passes thru attributes that match 295 // a list of names 296 public: 297 NamesToAcceptAttrFilter(const char**); 298 299 protected: 300 virtual bool Reject(const char* name, uint32 type, off_t size); 301 302 private: 303 const char** fNameList; 304 }; 305 306 307 class SelectiveAttributeTransformer : public AttributeStreamNode { 308 // node applies a transformation on specified attributes 309 public: 310 SelectiveAttributeTransformer(const char* attributeName, bool (*)(const char*, 311 uint32 , off_t , void*, void*), void* params); 312 virtual ~SelectiveAttributeTransformer(); 313 314 virtual off_t Read(const char* name, const char* foreignName, uint32 type, off_t size, 315 void* buffer, void (*swapFunc)(void*) = 0); 316 317 virtual void Rewind(); 318 319 protected: 320 virtual bool WillTransform(const char* name, uint32 type, off_t size, const char* data) const; 321 // override to implement filtering; should only return true if transformation will 322 // occur 323 virtual char* CopyAndApplyTransformer(const char* name, uint32 type, off_t size, const char* data); 324 // makes a copy of data 325 virtual bool ApplyTransformer(const char* name, uint32 type, off_t size, char* data); 326 // transforms in place 327 virtual const AttributeInfo* Next(); 328 virtual const char* Get(); 329 330 private: 331 AttributeInfo fCurrentAttr; 332 const char* fAttributeNameToTransform; 333 bool (*fTransformFunc)(const char*, uint32 , off_t , void*, void*); 334 void* fTransformParams; 335 336 BObjectList<char> fTransformedBuffers; 337 338 typedef AttributeStreamNode _inherited; 339 }; 340 341 342 template <class Type> 343 class AttributeStreamConstValue : public AttributeStreamNode { 344 public: 345 AttributeStreamConstValue(const char* name, uint32 attributeType, Type value); 346 347 protected: 348 virtual bool CanFeed() const { return true; } 349 virtual void Rewind() { fRewound = true; } 350 virtual const AttributeInfo* Next(); 351 virtual const char* Get(); 352 virtual bool Fill(char* buffer) const; 353 354 int32 Find(const char* name, uint32 type) const; 355 356 private: 357 AttributeInfo fAttr; 358 Type fValue; 359 bool fRewound; 360 361 typedef AttributeStreamNode _inherited; 362 }; 363 364 365 template<class Type> 366 AttributeStreamConstValue<Type>::AttributeStreamConstValue(const char* name, 367 uint32 attributeType, Type value) 368 : fAttr(name, attributeType, sizeof(Type)), 369 fValue(value), 370 fRewound(true) 371 { 372 } 373 374 375 template<class Type> 376 const AttributeInfo* 377 AttributeStreamConstValue<Type>::Next() 378 { 379 if (!fRewound) 380 return NULL; 381 382 fRewound = false; 383 return &fAttr; 384 } 385 386 387 template<class Type> 388 const char* 389 AttributeStreamConstValue<Type>::Get() 390 { 391 return (const char*)&fValue; 392 } 393 394 395 template<class Type> 396 bool 397 AttributeStreamConstValue<Type>::Fill(char* buffer) const 398 { 399 memcpy(buffer, &fValue, sizeof(Type)); 400 return true; 401 } 402 403 404 template<class Type> 405 int32 406 AttributeStreamConstValue<Type>::Find(const char* name, uint32 type) const 407 { 408 if (strcmp(fAttr.Name(), name) == 0 && type == fAttr.Type()) 409 return 0; 410 411 return -1; 412 } 413 414 415 class AttributeStreamBoolValue : public AttributeStreamConstValue<bool> { 416 public: 417 AttributeStreamBoolValue(const char* name, bool value) 418 : AttributeStreamConstValue<bool>(name, B_BOOL_TYPE, value) 419 {} 420 }; 421 422 423 class AttributeStreamInt32Value : public AttributeStreamConstValue<int32> { 424 public: 425 AttributeStreamInt32Value(const char* name, int32 value) 426 : AttributeStreamConstValue<int32>(name, B_INT32_TYPE, value) 427 {} 428 }; 429 430 431 class AttributeStreamInt64Value : public AttributeStreamConstValue<int64> { 432 public: 433 AttributeStreamInt64Value(const char* name, int64 value) 434 : AttributeStreamConstValue<int64>(name, B_INT64_TYPE, value) 435 {} 436 }; 437 438 439 class AttributeStreamRectValue : public AttributeStreamConstValue<BRect> { 440 public: 441 AttributeStreamRectValue(const char* name, BRect value) 442 : AttributeStreamConstValue<BRect>(name, B_RECT_TYPE, value) 443 {} 444 }; 445 446 447 class AttributeStreamFloatValue : public AttributeStreamConstValue<float> { 448 public: 449 AttributeStreamFloatValue(const char* name, float value) 450 : AttributeStreamConstValue<float>(name, B_FLOAT_TYPE, value) 451 {} 452 }; 453 454 } // namespace BPrivate 455 456 using namespace BPrivate; 457 458 #endif 459