xref: /haiku/src/kits/storage/ResourceItem.cpp (revision d5cd5d63ff0ad395989db6cf4841a64d5b545d1d)
1 //----------------------------------------------------------------------
2 //  This software is part of the OpenBeOS distribution and is covered
3 //  by the OpenBeOS 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 	if (fName.Length() > 0)
146 		return fName.String();
147 	return NULL;
148 }
149 
150 // Data
151 void *
152 ResourceItem::Data() const
153 {
154 	// Since MallocIO may have a NULL buffer, if the data size is 0,
155 	// we return a pointer to ourselves in this case. This ensures, that
156 	// the resource item still can be uniquely identified by its data pointer.
157 	if (DataSize() == 0)
158 		return const_cast<ResourceItem*>(this);
159 	return const_cast<void*>(Buffer());
160 }
161 
162 // SetLoaded
163 void
164 ResourceItem::SetLoaded(bool loaded)
165 {
166 	fIsLoaded = loaded;
167 }
168 
169 // IsLoaded
170 bool
171 ResourceItem::IsLoaded() const
172 {
173 	return (BufferLength() > 0 || fIsLoaded);
174 }
175 
176 // SetModified
177 void
178 ResourceItem::SetModified(bool modified)
179 {
180 	fIsModified = modified;
181 }
182 
183 // IsModified
184 bool
185 ResourceItem::IsModified() const
186 {
187 	return fIsModified;
188 }
189 
190 
191 };	// namespace Storage
192 };	// namespace BPrivate
193 
194 
195 
196 
197