1 // ResourceItem.cpp 2 3 #include "ResourceItem.h" 4 5 #include <stdio.h> 6 #include <string.h> 7 8 #include <DataIO.h> 9 10 // constructor 11 ResourceItem::ResourceItem() 12 : fOffset(0), 13 fSize(0), 14 fType(0), 15 fID(0), 16 fName(), 17 fData(NULL) 18 { 19 } 20 21 // destructor 22 ResourceItem::~ResourceItem() 23 { 24 UnsetData(); 25 } 26 27 // SetLocation 28 void 29 ResourceItem::SetLocation(roff_t offset, roff_t size) 30 { 31 SetOffset(offset); 32 SetSize(size); 33 } 34 35 // SetIdentity 36 void 37 ResourceItem::SetIdentity(type_code type, int32 id, const char* name) 38 { 39 fType = type; 40 fID = id; 41 fName = name; 42 } 43 44 // SetOffset 45 void 46 ResourceItem::SetOffset(roff_t offset) 47 { 48 fOffset = offset; 49 } 50 51 // GetOffset 52 ResourceItem::roff_t 53 ResourceItem::GetOffset() const 54 { 55 return fOffset; 56 } 57 58 // SetSize 59 void 60 ResourceItem::SetSize(roff_t size) 61 { 62 if (size != fSize) { 63 UnsetData(); 64 fSize = size; 65 } 66 } 67 68 // GetSize 69 ResourceItem::roff_t 70 ResourceItem::GetSize() const 71 { 72 return fSize; 73 } 74 75 // SetType 76 void 77 ResourceItem::SetType(type_code type) 78 { 79 fType = type; 80 } 81 82 // GetType 83 type_code 84 ResourceItem::GetType() const 85 { 86 return fType; 87 } 88 89 // SetID 90 void 91 ResourceItem::SetID(int32 id) 92 { 93 fID = id; 94 } 95 96 // GetID 97 int32 98 ResourceItem::GetID() const 99 { 100 return fID; 101 } 102 103 // SetName 104 void 105 ResourceItem::SetName(const char* name) 106 { 107 fName = name; 108 } 109 110 // GetName 111 const char* 112 ResourceItem::GetName() const 113 { 114 return fName.String(); 115 } 116 117 // SetData 118 void 119 ResourceItem::SetData(const void* data, roff_t size) 120 { 121 if (size < 0) 122 size = fSize; 123 // set the new data 124 if (data) { 125 AllocData(size); 126 if (fSize > 0) 127 memcpy(fData, data, fSize); 128 } else 129 UnsetData(); 130 } 131 132 // UnsetData 133 void 134 ResourceItem::UnsetData() 135 { 136 if (fData) { 137 delete[] (char*)fData; 138 fData = NULL; 139 } 140 } 141 142 // AllocData 143 void* 144 ResourceItem::AllocData(roff_t size) 145 { 146 if (size >= 0) 147 SetSize(size); 148 if (!fData && fSize > 0) 149 fData = new char*[fSize]; 150 return fData; 151 } 152 153 // GetData 154 void* 155 ResourceItem::GetData() const 156 { 157 return fData; 158 } 159 160 // LoadData 161 status_t 162 ResourceItem::LoadData(BPositionIO& file, roff_t position, roff_t size) 163 { 164 status_t error = B_OK; 165 AllocData(size); 166 if (position >= 0) 167 fOffset = position; 168 if (fData && fSize) { 169 ssize_t read = file.ReadAt(fOffset, fData, fSize); 170 if (read < 0) 171 error = read; 172 else if (read < fSize) 173 error = B_ERROR; 174 } 175 return error; 176 } 177 178 // WriteData 179 status_t 180 ResourceItem::WriteData(BPositionIO& file) const 181 { 182 status_t error = (fData ? B_OK : B_BAD_VALUE); 183 if (error == B_OK && fSize > 0) { 184 ssize_t written = file.WriteAt(fOffset, fData, fSize); 185 if (written < 0) 186 error = written; 187 else if (written < fSize) 188 error = B_FILE_ERROR; 189 } 190 return error; 191 } 192 193 // PrintToStream 194 void 195 ResourceItem::PrintToStream() 196 { 197 char typeName[4] = { fType >> 24, (fType >> 16) & 0xff, 198 (fType >> 8) & 0xff, fType & 0xff }; 199 printf("resource: offset: 0x%08lx (%10ld)\n", fOffset, fOffset); 200 printf(" size : 0x%08lx (%10ld)\n", fSize, fSize); 201 printf(" type : '%.4s' (0x%8lx)\n", typeName, fType); 202 printf(" id : %ld\n", fID); 203 printf(" name : `%s'\n", fName.String()); 204 } 205 206