headers/bsd: Add sys/queue.h.
[haiku.git] / src / kits / storage / ResourceItem.cpp
blob7d708c1e454abfd696f53945e587661de67bebe4
1 //----------------------------------------------------------------------
2 // This software is part of the OpenBeOS distribution and is covered
3 // by the MIT License.
4 //---------------------------------------------------------------------
5 /*!
6 \file ResourcesItem.cpp
7 ResourceItem implementation.
8 */
10 #include "ResourceItem.h"
12 #include <stdio.h>
13 #include <string.h>
15 #include <DataIO.h>
17 namespace BPrivate {
18 namespace Storage {
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)
31 SetBlockSize(1);
34 // destructor
35 ResourceItem::~ResourceItem()
39 // WriteAt
40 ssize_t
41 ResourceItem::WriteAt(off_t pos, const void *buffer, size_t size)
43 ssize_t result = BMallocIO::WriteAt(pos, buffer, size);
44 if (result >= 0)
45 SetModified(true);
46 return result;
49 // SetSize
50 status_t
51 ResourceItem::SetSize(off_t size)
53 status_t error = BMallocIO::SetSize(size);
54 if (error == B_OK)
55 SetModified(true);
56 return error;
59 // SetLocation
60 void
61 ResourceItem::SetLocation(int32 offset, size_t initialSize)
63 SetOffset(offset);
64 fInitialSize = initialSize;
67 // SetIdentity
68 void
69 ResourceItem::SetIdentity(type_code type, int32 id, const char *name)
71 fType = type;
72 fID = id;
73 fName = name;
76 // SetOffset
77 void
78 ResourceItem::SetOffset(int32 offset)
80 fOffset = offset;
83 // Offset
84 int32
85 ResourceItem::Offset() const
87 return fOffset;
90 // InitialSize
91 size_t
92 ResourceItem::InitialSize() const
94 return fInitialSize;
97 // DataSize
98 size_t
99 ResourceItem::DataSize() const
101 if (IsModified())
102 return BufferLength();
103 return fInitialSize;
106 // SetType
107 void
108 ResourceItem::SetType(type_code type)
110 fType = type;
113 // Type
114 type_code
115 ResourceItem::Type() const
117 return fType;
120 // SetID
121 void
122 ResourceItem::SetID(int32 id)
124 fID = id;
127 // ID
128 int32
129 ResourceItem::ID() const
131 return fID;
134 // SetName
135 void
136 ResourceItem::SetName(const char *name)
138 fName = name;
141 // Name
142 const char *
143 ResourceItem::Name() const
145 return fName.String();
148 // Data
149 void *
150 ResourceItem::Data() const
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());
160 // SetLoaded
161 void
162 ResourceItem::SetLoaded(bool loaded)
164 fIsLoaded = loaded;
167 // IsLoaded
168 bool
169 ResourceItem::IsLoaded() const
171 return (BufferLength() > 0 || fIsLoaded);
174 // SetModified
175 void
176 ResourceItem::SetModified(bool modified)
178 fIsModified = modified;
181 // IsModified
182 bool
183 ResourceItem::IsModified() const
185 return fIsModified;
189 }; // namespace Storage
190 }; // namespace BPrivate