BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / resedit / ResourceData.cpp
blobe79926857229fc6bdf8ac6de501cde9466a67e2a
1 /*
2 * Copyright (c) 2005-2010, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
5 * Author:
6 * DarkWyrm <darkwyrm@gmail.com>
7 */
8 #include "ResourceData.h"
9 #include "ResFields.h"
10 #include <stdlib.h>
12 ResourceData::ResourceData(void)
13 : fType(0),
14 fTypeString("Invalid"),
15 fID(-1),
16 fIDString("Invalid"),
17 fName(""),
18 fData(NULL),
19 fLength(0),
20 fAttr(false)
25 ResourceData::ResourceData(const type_code &code, const int32 &id,
26 const char *name, char *data,
27 const size_t &length)
28 : fType(code),
29 fID(id),
30 fName(name),
31 fData(data),
32 fLength(length),
33 fAttr(false)
35 fIDString = "";
36 fIDString << fID;
37 fTypeString = MakeTypeString(code);
41 ResourceData::ResourceData(const ResourceData &data)
43 *this = data;
47 ResourceData::~ResourceData(void)
49 free(fData);
53 ResourceData &
54 ResourceData::operator=(const ResourceData &data)
56 fType = data.fType;
57 fTypeString = data.fTypeString;
58 fID = data.fID;
59 fIDString = data.fIDString;
60 fName = data.fName;
61 fAttr = data.fAttr;
62 SetData(data.fData, data.fLength);
64 return *this;
67 bool
68 ResourceData::SetFromResource(const int32 &index, BResources &res)
70 char *name;
71 if (!res.GetResourceInfo(index, (type_code*)&fType, &fID,
72 (const char **)&name, &fLength)) {
73 *this = ResourceData();
74 return false;
76 fName = name;
77 fTypeString = MakeTypeString(fType);
78 fIDString = "";
79 fIDString << fID;
80 fAttr = false;
81 char *data = (char *)res.LoadResource(fType, fID, &fLength);
82 SetData(data, fLength);
84 return true;
88 bool
89 ResourceData::SetFromAttribute(const char *name, BNode &node)
91 attr_info info;
92 if (node.GetAttrInfo(name, &info) != B_OK) {
93 *this = ResourceData();
94 return false;
97 fType = info.type;
98 fID = -1;
99 fIDString = "(attr)";
100 fName = name;
101 fLength = info.size;
102 fAttr = true;
104 fTypeString = MakeTypeString(fType);
106 fData = (char *)malloc(fLength);
107 if (fData) {
108 ssize_t size = node.ReadAttr(name, info.type, 0, (void*)fData, fLength);
109 if (size >= 0) {
110 fLength = (size_t) size;
111 return true;
115 *this = ResourceData();
116 return false;
120 void
121 ResourceData::SetTo(const type_code &code, const int32 &id,
122 const char *name, char *data, const size_t &length)
124 fType = code;
125 fTypeString = MakeTypeString(code);
126 fID = id;
127 fIDString = "";
128 fIDString << fID;
129 fName = name;
130 SetData(data, length);
135 void
136 ResourceData::SetType(const type_code &code)
138 fType = code;
139 fTypeString = MakeTypeString(code);
143 void
144 ResourceData::SetID(const int32 &id)
146 fID = id;
147 fIDString = "";
148 fIDString << fID;
152 void
153 ResourceData::SetData(const char *data, const size_t &size)
155 free(fData);
157 fLength = size;
159 if (size > 0) {
160 fData = (char *)malloc(size);
161 memcpy(fData, data, size);
163 else
164 fData = NULL;