2 * Copyright 2009-2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
14 #include <package/hpkg/HPKGDefs.h>
15 #include <package/hpkg/PackageAttributeValue.h>
16 #include <package/hpkg/PackageContentHandler.h>
17 #include <package/hpkg/PackageEntry.h>
18 #include <package/hpkg/PackageEntryAttribute.h>
19 #include <package/hpkg/PackageReader.h>
20 #include <package/hpkg/StandardErrorOutput.h>
25 using namespace BPackageKit::BHPKG
;
28 struct PackageContentDumpHandler
: BLowLevelPackageContentHandler
{
29 PackageContentDumpHandler()
32 fErrorOccurred(false),
37 virtual status_t
HandleSectionStart(BHPKGPackageSectionID sectionID
,
40 const char* sectionName
;
43 case B_HPKG_SECTION_HEADER
:
44 sectionName
= "header";
46 case B_HPKG_SECTION_HEAP
:
49 case B_HPKG_SECTION_PACKAGE_TOC
:
52 case B_HPKG_SECTION_PACKAGE_ATTRIBUTES
:
53 sectionName
= "package attributes";
55 case B_HPKG_SECTION_REPOSITORY_INFO
:
56 sectionName
= "repository info";
59 sectionName
= "<unknown section>";
63 printf("\n==== SECTION: %s ====\n", sectionName
);
65 _handleSection
= true;
69 virtual status_t
HandleSectionEnd(BHPKGPackageSectionID sectionID
)
74 virtual status_t
HandleAttribute(BHPKGAttributeID attributeID
,
75 const BPackageAttributeValue
& value
, void* parentToken
, void*& _token
)
80 printf("%*s>%s: ", fLevel
* 2, "", AttributeNameForID(attributeID
));
89 virtual status_t
HandleAttributeDone(BHPKGAttributeID attributeID
,
90 const BPackageAttributeValue
& value
, void* parentToken
, void* token
)
98 printf("%*s<%s\n", fLevel
* 2, "", AttributeNameForID(attributeID
));
104 virtual void HandleErrorOccurred()
106 fErrorOccurred
= true;
110 void _PrintValue(const BPackageAttributeValue
& value
)
112 switch (value
.type
) {
113 case B_HPKG_ATTRIBUTE_TYPE_INT
:
114 printf("%lld (%#llx)", (long long)value
.signedInt
,
115 (long long)value
.signedInt
);
117 case B_HPKG_ATTRIBUTE_TYPE_UINT
:
118 printf("%llu (%#llx)", (unsigned long long)value
.unsignedInt
,
119 (unsigned long long)value
.unsignedInt
);
121 case B_HPKG_ATTRIBUTE_TYPE_STRING
:
122 printf("\"%s\"", value
.string
);
124 case B_HPKG_ATTRIBUTE_TYPE_RAW
:
125 switch (value
.encoding
) {
126 case B_HPKG_ATTRIBUTE_ENCODING_RAW_INLINE
:
127 printf("data: size: %llu, inline",
128 (unsigned long long)value
.data
.size
);
129 // TODO: Print the data bytes!
131 case B_HPKG_ATTRIBUTE_ENCODING_RAW_HEAP
:
132 printf("data: size: %llu, offset: %llu",
133 (unsigned long long)value
.data
.size
,
134 (unsigned long long)value
.data
.offset
);
141 printf("<unknown type %u>\n", value
.type
);
154 command_dump(int argc
, const char* const* argv
)
157 static struct option sLongOptions
[] = {
158 { "help", no_argument
, 0, 'h' },
162 opterr
= 0; // don't print errors
163 int c
= getopt_long(argc
, (char**)argv
, "+h", sLongOptions
, NULL
);
169 print_usage_and_exit(false);
173 print_usage_and_exit(true);
178 // One argument should remain -- the package file name.
179 if (optind
+ 1 != argc
)
180 print_usage_and_exit(true);
182 const char* packageFileName
= argv
[optind
++];
185 BStandardErrorOutput errorOutput
;
186 BPackageReader
packageReader(&errorOutput
);
187 status_t error
= packageReader
.Init(packageFileName
);
192 PackageContentDumpHandler handler
;
193 error
= packageReader
.ParseContent(&handler
);