1 /* 2 * Copyright (c) 2005-2006, Haiku, Inc. 3 * Distributed under the terms of the MIT license. 4 * 5 * Author: 6 * DarkWyrm <darkwyrm@earthlink.net> 7 */ 8 #include "ResourceData.h" 9 #include <stdlib.h> 10 11 ResourceData::ResourceData(void) 12 : fType(0), 13 fTypeString("Invalid"), 14 fID(-1), 15 fIDString("Invalid"), 16 fName(""), 17 fData(NULL), 18 fLength(0), 19 fFree(false) 20 { 21 } 22 23 24 ResourceData::ResourceData(const type_code &code, const int32 &id, 25 const char *name, char *data, 26 const size_t &length) 27 : fType(code), 28 fID(id), 29 fName(name), 30 fData(data), 31 fLength(length), 32 fFree(false) 33 { 34 fIDString = ""; 35 fIDString << fID; 36 MakeTypeString(); 37 } 38 39 40 ResourceData::ResourceData(const ResourceData &data) 41 { 42 *this = data; 43 } 44 45 46 ResourceData::~ResourceData(void) 47 { 48 if (fFree) 49 free(fData); 50 } 51 52 53 ResourceData & 54 ResourceData::operator=(const ResourceData &data) 55 { 56 fType = data.fType; 57 fTypeString = data.fTypeString; 58 fID = data.fID; 59 fIDString = data.fIDString; 60 fName = data.fName; 61 fLength = data.fLength; 62 fFree = data.fFree; 63 64 // This is an attribute, so we need to allocate and free the data 65 if (fFree) { 66 fData = data.fData; 67 } 68 return *this; 69 } 70 71 bool 72 ResourceData::SetFromResource(const int32 &index, BResources &res) 73 { 74 char *name; 75 if (!res.GetResourceInfo(index, (type_code*)&fType, &fID, 76 (const char **)&name, &fLength)) { 77 *this = ResourceData(); 78 return false; 79 } 80 fName = name; 81 MakeTypeString(); 82 fIDString = ""; 83 fIDString << fID; 84 fFree = false; 85 fData = (char *)res.LoadResource(fType,fID,&fLength); 86 87 return true; 88 } 89 90 91 bool 92 ResourceData::SetFromAttribute(const char *name, BNode &node) 93 { 94 attr_info info; 95 if (node.GetAttrInfo(name, &info) != B_OK) { 96 *this = ResourceData(); 97 return false; 98 } 99 100 fType = info.type; 101 fID = -1; 102 fIDString = "(attr)"; 103 fName = name; 104 fLength = info.size; 105 fFree = true; 106 107 MakeTypeString(); 108 109 fData = (char *)malloc(fLength); 110 if (fData) { 111 ssize_t size = node.ReadAttr(name, info.type, 0, (void*)fData, fLength); 112 if (size >= 0) { 113 fLength = (size_t) size; 114 return true; 115 } 116 } 117 118 *this = ResourceData(); 119 return false; 120 } 121 122 123 void 124 ResourceData::SetTo(const type_code &code, const int32 &id, 125 const char *name, char *data, const size_t &length) 126 { 127 fType = code; 128 fID = id; 129 fName = name; 130 fData = data; 131 fLength = length; 132 133 MakeTypeString(); 134 } 135 136 137 void 138 ResourceData::MakeTypeString(void) 139 { 140 char typestring[7]; 141 char *typeptr = (char *) &fType; 142 typestring[0] = '\''; 143 typestring[1] = typeptr[3]; 144 typestring[2] = typeptr[2]; 145 typestring[3] = typeptr[1]; 146 typestring[4] = typeptr[0]; 147 typestring[5] = '\''; 148 typestring[6] = '\0'; 149 fTypeString = typestring; 150 } 151 152