1/*
2 * Copyright (c) 2005-2010, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
4 *
5 * Author:
6 *		DarkWyrm <darkwyrm@gmail.com>
7 */
8#include "ResourceData.h"
9#include "ResFields.h"
10#include <stdlib.h>
11
12ResourceData::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)
21{
22}
23
24
25ResourceData::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)
34{
35	fIDString = "";
36	fIDString << fID;
37	fTypeString = MakeTypeString(code);
38}
39
40
41ResourceData::ResourceData(const ResourceData &data)
42{
43	*this = data;
44}
45
46
47ResourceData::~ResourceData(void)
48{
49	free(fData);
50}
51
52
53ResourceData &
54ResourceData::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	fAttr = data.fAttr;
62	SetData(data.fData, data.fLength);
63
64	return *this;
65}
66
67bool
68ResourceData::SetFromResource(const int32 &index, BResources &res)
69{
70	char *name;
71	if (!res.GetResourceInfo(index, (type_code*)&fType, &fID,
72							(const char **)&name, &fLength)) {
73		*this = ResourceData();
74		return false;
75	}
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);
83
84	return true;
85}
86
87
88bool
89ResourceData::SetFromAttribute(const char *name, BNode &node)
90{
91	attr_info info;
92	if (node.GetAttrInfo(name, &info) != B_OK) {
93		*this = ResourceData();
94		return false;
95	}
96
97	fType = info.type;
98	fID = -1;
99	fIDString = "(attr)";
100	fName = name;
101	fLength = info.size;
102	fAttr = true;
103
104	fTypeString = MakeTypeString(fType);
105
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;
112		}
113	}
114
115	*this = ResourceData();
116	return false;
117}
118
119
120void
121ResourceData::SetTo(const type_code &code, const int32 &id,
122					const char *name, char *data, const size_t &length)
123{
124	fType = code;
125	fTypeString = MakeTypeString(code);
126	fID = id;
127	fIDString = "";
128	fIDString << fID;
129	fName = name;
130	SetData(data, length);
131
132}
133
134
135void
136ResourceData::SetType(const type_code &code)
137{
138	fType = code;
139	fTypeString = MakeTypeString(code);
140}
141
142
143void
144ResourceData::SetID(const int32 &id)
145{
146	fID = id;
147	fIDString = "";
148	fIDString << fID;
149}
150
151
152void
153ResourceData::SetData(const char *data, const size_t &size)
154{
155	free(fData);
156
157	fLength = size;
158
159	if (size > 0) {
160		fData = (char *)malloc(size);
161		memcpy(fData, data, size);
162	}
163	else
164		fData = NULL;
165}
166
167