xref: /haiku/src/kits/storage/ResourceItem.cpp (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
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 
17 namespace BPrivate {
18 namespace Storage {
19 
20 // constructor
21 ResourceItem::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
35 ResourceItem::~ResourceItem()
36 {
37 }
38 
39 // WriteAt
40 ssize_t
41 ResourceItem::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
50 status_t
51 ResourceItem::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
60 void
61 ResourceItem::SetLocation(int32 offset, size_t initialSize)
62 {
63 	SetOffset(offset);
64 	fInitialSize = initialSize;
65 }
66 
67 // SetIdentity
68 void
69 ResourceItem::SetIdentity(type_code type, int32 id, const char *name)
70 {
71 	fType = type;
72 	fID = id;
73 	fName = name;
74 }
75 
76 // SetOffset
77 void
78 ResourceItem::SetOffset(int32 offset)
79 {
80 	fOffset = offset;
81 }
82 
83 // Offset
84 int32
85 ResourceItem::Offset() const
86 {
87 	return fOffset;
88 }
89 
90 // InitialSize
91 size_t
92 ResourceItem::InitialSize() const
93 {
94 	return fInitialSize;
95 }
96 
97 // DataSize
98 size_t
99 ResourceItem::DataSize() const
100 {
101 	if (IsModified())
102 		return BufferLength();
103 	return fInitialSize;
104 }
105 
106 // SetType
107 void
108 ResourceItem::SetType(type_code type)
109 {
110 	fType = type;
111 }
112 
113 // Type
114 type_code
115 ResourceItem::Type() const
116 {
117 	return fType;
118 }
119 
120 // SetID
121 void
122 ResourceItem::SetID(int32 id)
123 {
124 	fID = id;
125 }
126 
127 // ID
128 int32
129 ResourceItem::ID() const
130 {
131 	return fID;
132 }
133 
134 // SetName
135 void
136 ResourceItem::SetName(const char *name)
137 {
138 	fName = name;
139 }
140 
141 // Name
142 const char *
143 ResourceItem::Name() const
144 {
145 	return fName.String();
146 }
147 
148 // Data
149 void *
150 ResourceItem::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
161 void
162 ResourceItem::SetLoaded(bool loaded)
163 {
164 	fIsLoaded = loaded;
165 }
166 
167 // IsLoaded
168 bool
169 ResourceItem::IsLoaded() const
170 {
171 	return (BufferLength() > 0 || fIsLoaded);
172 }
173 
174 // SetModified
175 void
176 ResourceItem::SetModified(bool modified)
177 {
178 	fIsModified = modified;
179 }
180 
181 // IsModified
182 bool
183 ResourceItem::IsModified() const
184 {
185 	return fIsModified;
186 }
187 
188 
189 };	// namespace Storage
190 };	// namespace BPrivate
191 
192 
193 
194 
195