Pulled reusable stuff from PackageReaderImpl into new class ReaderImplBase.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40485 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Oliver Tappe
2011-02-14 13:09:09 +00:00
parent e28490dfa1
commit ad6a8dbe2b
6 changed files with 1408 additions and 1076 deletions
+13 -102
View File
@@ -7,14 +7,7 @@
#define _PACKAGE__HPKG__PRIVATE__PACKAGE_READER_IMPL_H_ #define _PACKAGE__HPKG__PRIVATE__PACKAGE_READER_IMPL_H_
#include <SupportDefs.h> #include <package/hpkg/ReaderImplBase.h>
#include <util/SinglyLinkedList.h>
#include <package/hpkg/PackageAttributeValue.h>
#include <package/hpkg/PackageContentHandler.h>
#include <package/hpkg/PackageInfoAttributeValue.h>
#include <package/hpkg/PackageReader.h>
namespace BPackageKit { namespace BPackageKit {
@@ -24,13 +17,13 @@ namespace BHPKG {
class BPackageEntry; class BPackageEntry;
class BPackageEntryAttribute; class BPackageEntryAttribute;
class BErrorOutput;
namespace BPrivate { namespace BPrivate {
class PackageReaderImpl { class PackageReaderImpl : public ReaderImplBase {
typedef ReaderImplBase inherited;
public: public:
PackageReaderImpl( PackageReaderImpl(
BErrorOutput* errorOutput); BErrorOutput* errorOutput);
@@ -43,120 +36,38 @@ public:
status_t ParseContent(BLowLevelPackageContentHandler* status_t ParseContent(BLowLevelPackageContentHandler*
contentHandler); contentHandler);
int PackageFileFD() { return fFD; } int PackageFileFD() const;
protected:
// from ReaderImplBase
virtual status_t ReadAttributeValue(uint8 type, uint8 encoding,
AttributeValue& _value);
private: private:
struct AttributeHandlerContext;
struct AttributeHandler;
struct IgnoreAttributeHandler;
struct DataAttributeHandler; struct DataAttributeHandler;
struct AttributeAttributeHandler; struct AttributeAttributeHandler;
struct EntryAttributeHandler; struct EntryAttributeHandler;
struct PackageAttributeHandler;
struct PackageVersionAttributeHandler;
struct PackageResolvableAttributeHandler;
struct PackageResolvableExpressionAttributeHandler;
struct RootAttributeHandler; struct RootAttributeHandler;
struct LowLevelAttributeHandler;
struct PackageContentListHandler;
struct SectionInfo {
uint32 compression;
uint32 compressedLength;
uint32 uncompressedLength;
uint8* data;
uint64 offset;
uint64 currentOffset;
uint64 stringsLength;
uint64 stringsCount;
char** strings;
const char* name;
SectionInfo(const char* _name)
:
data(NULL),
strings(NULL),
name(_name)
{
}
~SectionInfo()
{
delete[] strings;
delete[] data;
}
};
typedef BPackageAttributeValue AttributeValue;
typedef SinglyLinkedList<AttributeHandler> AttributeHandlerList;
private: private:
status_t _Init(const char* fileName); status_t _ParseTOC(AttributeHandlerContext* context,
const char* _CheckCompression(
const SectionInfo& section) const;
status_t _ParseStrings();
status_t _ParseContent(AttributeHandlerContext* context,
AttributeHandler* rootAttributeHandler); AttributeHandler* rootAttributeHandler);
status_t _ParseAttributeTree(
AttributeHandlerContext* context);
status_t _ReadAttribute(uint8& _id,
AttributeValue& _value,
bool* _hasChildren = NULL,
uint64* _tag = NULL);
status_t _ReadAttributeValue(uint8 type, uint8 encoding,
AttributeValue& _value);
status_t _ReadUnsignedLEB128(uint64& _value);
status_t _ReadString(const char*& _string,
size_t* _stringLength = NULL);
template<typename Type>
inline status_t _Read(Type& _value);
status_t _GetTOCBuffer(size_t size, status_t _GetTOCBuffer(size_t size,
const void*& _buffer); const void*& _buffer);
status_t _ReadSectionBuffer(void* buffer, size_t size);
status_t _ReadBuffer(off_t offset, void* buffer,
size_t size);
status_t _ReadCompressedBuffer(
const SectionInfo& section);
inline AttributeHandler* _CurrentAttributeHandler() const;
inline void _PushAttributeHandler(
AttributeHandler* handler);
inline AttributeHandler* _PopAttributeHandler();
private: private:
BErrorOutput* fErrorOutput;
int fFD;
bool fOwnsFD;
uint64 fTotalSize; uint64 fTotalSize;
uint64 fHeapOffset; uint64 fHeapOffset;
uint64 fHeapSize; uint64 fHeapSize;
SectionInfo fTOCSection; SectionInfo fTOCSection;
SectionInfo fPackageAttributesSection;
SectionInfo* fCurrentSection;
AttributeHandlerList* fAttributeHandlerStack;
uint8* fScratchBuffer;
size_t fScratchBufferSize;
}; };
template<typename Type> inline int
status_t PackageReaderImpl::PackageFileFD() const
PackageReaderImpl::_Read(Type& _value)
{ {
return _ReadSectionBuffer(&_value, sizeof(Type)); return FD();
} }
@@ -0,0 +1,324 @@
/*
* Copyright 2009, Ingo Weinhold, [email protected].
* Copyright 2011, Oliver Tappe <[email protected]>
* Distributed under the terms of the MIT License.
*/
#ifndef _PACKAGE__HPKG__PRIVATE__READER_IMPL_BASE_H_
#define _PACKAGE__HPKG__PRIVATE__READER_IMPL_BASE_H_
#include <SupportDefs.h>
#include <util/SinglyLinkedList.h>
#include <package/hpkg/PackageAttributeValue.h>
#include <package/hpkg/PackageContentHandler.h>
#include <package/hpkg/PackageInfoAttributeValue.h>
namespace BPackageKit {
namespace BHPKG {
class BErrorOutput;
namespace BPrivate {
class ReaderImplBase {
protected:
ReaderImplBase(
BErrorOutput* errorOutput);
virtual ~ReaderImplBase();
virtual status_t Init(int fd, bool keepFD);
int FD() const;
BErrorOutput* ErrorOutput() const;
protected:
struct AttributeHandlerContext {
BErrorOutput* errorOutput;
union {
BPackageContentHandler* packageContentHandler;
BLowLevelPackageContentHandler* lowLevelHandler;
};
bool hasLowLevelHandler;
uint64 heapOffset;
uint64 heapSize;
AttributeHandlerContext(BErrorOutput* errorOutput,
BPackageContentHandler* packageContentHandler);
AttributeHandlerContext(BErrorOutput* errorOutput,
BLowLevelPackageContentHandler* lowLevelHandler);
void ErrorOccurred();
};
typedef BPackageAttributeValue AttributeValue;
struct AttributeHandler
: SinglyLinkedListLinkImpl<AttributeHandler> {
virtual ~AttributeHandler();
void SetLevel(int level);
virtual status_t HandleAttribute(
AttributeHandlerContext* context, uint8 id,
const AttributeValue& value, AttributeHandler** _handler);
virtual status_t Delete(AttributeHandlerContext* context);
protected:
int fLevel;
};
struct IgnoreAttributeHandler : AttributeHandler {
};
struct PackageVersionAttributeHandler : AttributeHandler {
PackageVersionAttributeHandler(
BPackageInfoAttributeValue& packageInfoValue,
BPackageVersionData& versionData, bool notify);
virtual status_t HandleAttribute(
AttributeHandlerContext* context, uint8 id,
const AttributeValue& value, AttributeHandler** _handler);
virtual status_t Delete(AttributeHandlerContext* context);
private:
BPackageInfoAttributeValue& fPackageInfoValue;
BPackageVersionData& fPackageVersionData;
bool fNotify;
};
struct PackageResolvableAttributeHandler : AttributeHandler {
PackageResolvableAttributeHandler(
BPackageInfoAttributeValue& packageInfoValue);
virtual status_t HandleAttribute(
AttributeHandlerContext* context, uint8 id,
const AttributeValue& value, AttributeHandler** _handler);
virtual status_t Delete(AttributeHandlerContext* context);
private:
BPackageInfoAttributeValue& fPackageInfoValue;
};
struct PackageResolvableExpressionAttributeHandler
: AttributeHandler {
PackageResolvableExpressionAttributeHandler(
BPackageInfoAttributeValue& packageInfoValue);
virtual status_t HandleAttribute(
AttributeHandlerContext* context, uint8 id,
const AttributeValue& value, AttributeHandler** _handler);
virtual status_t Delete(AttributeHandlerContext* context);
private:
BPackageInfoAttributeValue& fPackageInfoValue;
};
struct PackageAttributeHandler : AttributeHandler {
virtual status_t HandleAttribute(
AttributeHandlerContext* context, uint8 id,
const AttributeValue& value, AttributeHandler** _handler);
private:
BPackageInfoAttributeValue fPackageInfoValue;
};
struct LowLevelAttributeHandler : AttributeHandler {
LowLevelAttributeHandler();
LowLevelAttributeHandler(uint8 id,
const BPackageAttributeValue& value, void* token);
virtual status_t HandleAttribute(
AttributeHandlerContext* context, uint8 id,
const AttributeValue& value, AttributeHandler** _handler);
virtual status_t Delete(AttributeHandlerContext* context);
private:
void* fToken;
uint8 fID;
AttributeValue fValue;
};
struct SectionInfo {
uint32 compression;
uint32 compressedLength;
uint32 uncompressedLength;
uint8* data;
uint64 offset;
uint64 currentOffset;
uint64 stringsLength;
uint64 stringsCount;
char** strings;
const char* name;
SectionInfo(const char* _name)
:
data(NULL),
strings(NULL),
name(_name)
{
}
~SectionInfo()
{
delete[] strings;
delete[] data;
}
};
typedef SinglyLinkedList<AttributeHandler> AttributeHandlerList;
protected:
const char* CheckCompression(
const SectionInfo& section) const;
status_t ParseStrings();
status_t ParsePackageAttributesSection(
AttributeHandlerContext* context,
AttributeHandler* rootAttributeHandler);
status_t ParseAttributeTree(
AttributeHandlerContext* context);
virtual status_t ReadAttributeValue(uint8 type, uint8 encoding,
AttributeValue& _value);
status_t ReadUnsignedLEB128(uint64& _value);
status_t ReadBuffer(off_t offset, void* buffer,
size_t size);
status_t ReadCompressedBuffer(
const SectionInfo& section);
inline AttributeHandler* CurrentAttributeHandler() const;
inline void PushAttributeHandler(
AttributeHandler* handler);
inline AttributeHandler* PopAttributeHandler();
inline void ClearAttributeHandlerStack();
inline SectionInfo* CurrentSection();
inline void SetCurrentSection(SectionInfo* section);
protected:
SectionInfo fPackageAttributesSection;
private:
template<typename Type>
inline status_t _Read(Type& _value);
status_t _ReadSectionBuffer(void* buffer, size_t size);
status_t _ReadAttribute(uint8& _id,
AttributeValue& _value,
bool* _hasChildren = NULL,
uint64* _tag = NULL);
status_t _ReadString(const char*& _string,
size_t* _stringLength = NULL);
private:
BErrorOutput* fErrorOutput;
int fFD;
bool fOwnsFD;
SectionInfo* fCurrentSection;
AttributeHandlerList fAttributeHandlerStack;
uint8* fScratchBuffer;
size_t fScratchBufferSize;
};
inline int
ReaderImplBase::FD() const
{
return fFD;
}
inline BErrorOutput*
ReaderImplBase::ErrorOutput() const
{
return fErrorOutput;
}
ReaderImplBase::SectionInfo*
ReaderImplBase::CurrentSection()
{
return fCurrentSection;
}
void
ReaderImplBase::SetCurrentSection(SectionInfo* section)
{
fCurrentSection = section;
}
template<typename Type>
status_t
ReaderImplBase::_Read(Type& _value)
{
return _ReadSectionBuffer(&_value, sizeof(Type));
}
inline ReaderImplBase::AttributeHandler*
ReaderImplBase::CurrentAttributeHandler() const
{
return fAttributeHandlerStack.Head();
}
inline void
ReaderImplBase::PushAttributeHandler(AttributeHandler* handler)
{
fAttributeHandlerStack.Add(handler);
}
inline ReaderImplBase::AttributeHandler*
ReaderImplBase::PopAttributeHandler()
{
return fAttributeHandlerStack.RemoveHead();
}
inline void
ReaderImplBase::ClearAttributeHandlerStack()
{
fAttributeHandlerStack.MakeEmpty();
}
} // namespace BPrivate
} // namespace BHPKG
} // namespace BPackageKit
#endif // _PACKAGE__HPKG__PRIVATE__READER_IMPL_BASE_H_
@@ -41,6 +41,7 @@ HAIKU_PACKAGE_FS_PACKAGE_READER_SOURCES =
PackageEntry.cpp PackageEntry.cpp
PackageEntryAttribute.cpp PackageEntryAttribute.cpp
PackageReaderImpl.cpp PackageReaderImpl.cpp
ReaderImplBase.cpp
# compression # compression
ZlibCompressionBase.cpp ZlibCompressionBase.cpp
+1
View File
@@ -25,6 +25,7 @@ HPKG_SOURCES =
PackageReaderImpl.cpp PackageReaderImpl.cpp
PackageWriter.cpp PackageWriter.cpp
PackageWriterImpl.cpp PackageWriterImpl.cpp
ReaderImplBase.cpp
RepositoryWriter.cpp RepositoryWriter.cpp
RepositoryWriterImpl.cpp RepositoryWriterImpl.cpp
Strings.cpp Strings.cpp
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff