WriterImplBase.h revision b3263ad3
1/*
2 * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _PACKAGE__HPKG__PRIVATE__WRITER_IMPL_BASE_H_
6#define _PACKAGE__HPKG__PRIVATE__WRITER_IMPL_BASE_H_
7
8
9#include <util/DoublyLinkedList.h>
10
11#include <package/hpkg/HPKGDefsPrivate.h>
12
13#include <package/hpkg/PackageWriter.h>
14#include <package/hpkg/Strings.h>
15
16#include <package/PackageInfo.h>
17
18#include <package/hpkg/PackageFileHeapWriter.h>
19
20
21class BCompressionAlgorithm;
22class BCompressionParameters;
23class BDecompressionParameters;
24
25
26namespace BPackageKit {
27
28namespace BHPKG {
29
30
31class BDataReader;
32class BErrorOutput;
33
34
35namespace BPrivate {
36
37
38struct hpkg_header;
39
40
41class WriterImplBase {
42public:
43								WriterImplBase(const char* fileType,
44									BErrorOutput* errorOutput);
45								~WriterImplBase();
46
47protected:
48			struct AttributeValue {
49				union {
50					int64			signedInt;
51					uint64			unsignedInt;
52					CachedString*	string;
53					struct {
54						uint64		size;
55						union {
56							uint64	offset;
57							uint8	raw[B_HPKG_MAX_INLINE_DATA_SIZE];
58						};
59					} data;
60				};
61				uint8		type;
62				int8		encoding;
63
64				AttributeValue();
65				~AttributeValue();
66
67				void SetTo(int8 value);
68				void SetTo(uint8 value);
69				void SetTo(int16 value);
70				void SetTo(uint16 value);
71				void SetTo(int32 value);
72				void SetTo(uint32 value);
73				void SetTo(int64 value);
74				void SetTo(uint64 value);
75				void SetTo(CachedString* value);
76				void SetToData(uint64 size, uint64 offset);
77				void SetToData(uint64 size, const void* rawData);
78				uint8 ApplicableEncoding() const;
79
80			private:
81				static uint8 _ApplicableIntEncoding(uint64 value);
82			};
83
84
85			struct PackageAttribute :
86					public DoublyLinkedListLinkImpl<PackageAttribute>,
87					public AttributeValue {
88				BHPKGAttributeID 					id;
89				DoublyLinkedList<PackageAttribute>	children;
90
91				PackageAttribute(BHPKGAttributeID id_, uint8 type,
92					uint8 encoding);
93				~PackageAttribute();
94
95				void AddChild(PackageAttribute* child);
96
97			private:
98				void _DeleteChildren();
99			};
100
101			typedef DoublyLinkedList<PackageAttribute> PackageAttributeList;
102
103protected:
104			status_t			Init(const char* fileName, size_t headerSize,
105									const BPackageWriterParameters& parameters);
106
107			void				RegisterPackageInfo(
108									PackageAttributeList& attributeList,
109									const BPackageInfo& packageInfo);
110			void				RegisterPackageVersion(
111									PackageAttributeList& attributeList,
112									const BPackageVersion& version,
113									BHPKGAttributeID attributeID
114										= kDefaultVersionAttributeID);
115			void				RegisterPackageResolvableExpressionList(
116									PackageAttributeList& attributeList,
117									const BObjectList<
118										BPackageResolvableExpression>& list,
119									uint8 id);
120
121			PackageAttribute*	AddStringAttribute(BHPKGAttributeID id,
122									const BString& value,
123									DoublyLinkedList<PackageAttribute>& list);
124
125			int32				WriteCachedStrings(const StringCache& cache,
126									uint32 minUsageCount);
127
128			int32				WritePackageAttributes(
129									const PackageAttributeList& attributes,
130									uint32& _stringsLengthUncompressed);
131			void				WritePackageVersion(
132									const BPackageVersion& version);
133			void				WritePackageResolvableExpressionList(
134									const BObjectList<
135										BPackageResolvableExpression>& list,
136									uint8 id);
137
138			void				WriteAttributeValue(const AttributeValue& value,
139									uint8 encoding);
140			void				WriteUnsignedLEB128(uint64 value);
141
142	template<typename Type>
143	inline	void				Write(const Type& value);
144	inline	void				WriteString(const char* string);
145	inline	void				WriteBuffer(const void* data, size_t size);
146									// appends data to the heap
147
148			void				RawWriteBuffer(const void* buffer, size_t size,
149									off_t offset);
150									// writes to the file directly
151
152	inline	int					FD() const;
153	inline	uint32				Flags() const;
154
155	inline	const PackageAttributeList&	PackageAttributes() const;
156	inline	PackageAttributeList&	PackageAttributes();
157
158	inline	const StringCache&	PackageStringCache() const;
159	inline	StringCache&		PackageStringCache();
160
161	inline	void				SetFinished(bool finished);
162
163protected:
164			PackageFileHeapWriter* fHeapWriter;
165			BCompressionAlgorithm* fCompressionAlgorithm;
166			BCompressionParameters* fCompressionParameters;
167			BCompressionAlgorithm* fDecompressionAlgorithm;
168			BDecompressionParameters* fDecompressionParameters;
169
170private:
171	static const BHPKGAttributeID kDefaultVersionAttributeID
172		= B_HPKG_ATTRIBUTE_ID_PACKAGE_VERSION_MAJOR;
173
174private:
175	inline	PackageAttribute*	_AddStringAttributeIfNotEmpty(
176									BHPKGAttributeID id, const BString& value,
177									DoublyLinkedList<PackageAttribute>& list);
178			void				_AddStringAttributeList(BHPKGAttributeID id,
179									const BStringList& value,
180									DoublyLinkedList<PackageAttribute>& list);
181			void				_WritePackageAttributes(
182									const PackageAttributeList& attributes);
183
184private:
185			const char*			fFileType;
186			BErrorOutput*		fErrorOutput;
187			const char*			fFileName;
188			BPackageWriterParameters fParameters;
189			int					fFD;
190			bool				fFinished;
191
192			StringCache			fPackageStringCache;
193			PackageAttributeList	fPackageAttributes;
194};
195
196
197template<typename Type>
198inline void
199WriterImplBase::Write(const Type& value)
200{
201	WriteBuffer(&value, sizeof(Type));
202}
203
204
205inline void
206WriterImplBase::WriteString(const char* string)
207{
208	WriteBuffer(string, strlen(string) + 1);
209}
210
211
212inline void
213WriterImplBase::WriteBuffer(const void* data, size_t size)
214{
215	fHeapWriter->AddDataThrows(data, size);
216}
217
218
219inline int
220WriterImplBase::FD() const
221{
222	return fFD;
223}
224
225
226inline uint32
227WriterImplBase::Flags() const
228{
229	return fParameters.Flags();
230}
231
232
233inline const WriterImplBase::PackageAttributeList&
234WriterImplBase::PackageAttributes() const
235{
236	return fPackageAttributes;
237}
238
239
240inline WriterImplBase::PackageAttributeList&
241WriterImplBase::PackageAttributes()
242{
243	return fPackageAttributes;
244}
245
246
247inline const StringCache&
248WriterImplBase::PackageStringCache() const
249{
250	return fPackageStringCache;
251}
252
253
254inline StringCache&
255WriterImplBase::PackageStringCache()
256{
257	return fPackageStringCache;
258}
259
260
261inline void
262WriterImplBase::SetFinished(bool finished)
263{
264	fFinished = finished;
265}
266
267
268inline WriterImplBase::PackageAttribute*
269WriterImplBase::_AddStringAttributeIfNotEmpty(BHPKGAttributeID id,
270	const BString& value, DoublyLinkedList<PackageAttribute>& list)
271{
272	if (value.IsEmpty())
273		return NULL;
274	return AddStringAttribute(id, value, list);
275}
276
277
278}	// namespace BPrivate
279
280}	// namespace BHPKG
281
282}	// namespace BPackageKit
283
284
285#endif	// _PACKAGE__HPKG__PRIVATE__WRITER_IMPL_BASE_H_
286