1//----------------------------------------------------------------------
2//  This software is part of the Haiku distribution and is covered
3//  by the MIT License.
4//---------------------------------------------------------------------
5/*!
6	\file ResourcesItem.cpp
7	ResourceItem implementation.
8*/
9
10#include "ResourceItem.h"
11
12#include <stdio.h>
13#include <string.h>
14
15#include <DataIO.h>
16
17namespace BPrivate {
18namespace Storage {
19
20// constructor
21ResourceItem::ResourceItem()
22			: BMallocIO(),
23			  fOffset(0),
24			  fInitialSize(0),
25			  fType(0),
26			  fID(0),
27			  fName(),
28			  fIsLoaded(false),
29			  fIsModified(false)
30{
31	SetBlockSize(1);
32}
33
34// destructor
35ResourceItem::~ResourceItem()
36{
37}
38
39// WriteAt
40ssize_t
41ResourceItem::WriteAt(off_t pos, const void *buffer, size_t size)
42{
43	ssize_t result = BMallocIO::WriteAt(pos, buffer, size);
44	if (result >= 0)
45		SetModified(true);
46	return result;
47}
48
49// SetSize
50status_t
51ResourceItem::SetSize(off_t size)
52{
53	status_t error = BMallocIO::SetSize(size);
54	if (error == B_OK)
55		SetModified(true);
56	return error;
57}
58
59// SetLocation
60void
61ResourceItem::SetLocation(int32 offset, size_t initialSize)
62{
63	SetOffset(offset);
64	fInitialSize = initialSize;
65}
66
67// SetIdentity
68void
69ResourceItem::SetIdentity(type_code type, int32 id, const char *name)
70{
71	fType = type;
72	fID = id;
73	fName = name;
74}
75
76// SetOffset
77void
78ResourceItem::SetOffset(int32 offset)
79{
80	fOffset = offset;
81}
82
83// Offset
84int32
85ResourceItem::Offset() const
86{
87	return fOffset;
88}
89
90// InitialSize
91size_t
92ResourceItem::InitialSize() const
93{
94	return fInitialSize;
95}
96
97// DataSize
98size_t
99ResourceItem::DataSize() const
100{
101	if (IsModified())
102		return BufferLength();
103	return fInitialSize;
104}
105
106// SetType
107void
108ResourceItem::SetType(type_code type)
109{
110	fType = type;
111}
112
113// Type
114type_code
115ResourceItem::Type() const
116{
117	return fType;
118}
119
120// SetID
121void
122ResourceItem::SetID(int32 id)
123{
124	fID = id;
125}
126
127// ID
128int32
129ResourceItem::ID() const
130{
131	return fID;
132}
133
134// SetName
135void
136ResourceItem::SetName(const char *name)
137{
138	fName = name;
139}
140
141// Name
142const char *
143ResourceItem::Name() const
144{
145	return fName.String();
146}
147
148// Data
149void *
150ResourceItem::Data() const
151{
152	// Since MallocIO may have a NULL buffer, if the data size is 0,
153	// we return a pointer to ourselves in this case. This ensures, that
154	// the resource item still can be uniquely identified by its data pointer.
155	if (DataSize() == 0)
156		return const_cast<ResourceItem*>(this);
157	return const_cast<void*>(Buffer());
158}
159
160// SetLoaded
161void
162ResourceItem::SetLoaded(bool loaded)
163{
164	fIsLoaded = loaded;
165}
166
167// IsLoaded
168bool
169ResourceItem::IsLoaded() const
170{
171	return (BufferLength() > 0 || fIsLoaded);
172}
173
174// SetModified
175void
176ResourceItem::SetModified(bool modified)
177{
178	fIsModified = modified;
179}
180
181// IsModified
182bool
183ResourceItem::IsModified() const
184{
185	return fIsModified;
186}
187
188
189};	// namespace Storage
190};	// namespace BPrivate
191
192
193
194
195