xref: /haiku/headers/private/storage/ResourceFile.h (revision f2ced752a08ff5d2618826bcd3ae3976c9f3e92e)
1 //----------------------------------------------------------------------
2 //  This software is part of the OpenBeOS distribution and is covered
3 //  by the OpenBeOS license.
4 //---------------------------------------------------------------------
5 /*!
6 	\file ResourceFile.h
7 	ResourceFile interface declaration.
8 */
9 
10 #ifndef _RESOURCE_FILE_H
11 #define _RESOURCE_FILE_H
12 
13 #include <ByteOrder.h>
14 
15 #include "OffsetFile.h"
16 
17 struct resource_info;
18 struct PEFContainerHeader;
19 
20 namespace BPrivate {
21 namespace Storage {
22 
23 class Exception;
24 struct MemArea;
25 class ResourceItem;
26 struct resource_parse_info;
27 class ResourcesContainer;
28 
29 /*!
30 	\class ResourceFile
31 	\brief Represents a file capable of containing resources.
32 
33 	This class provides access to the resources of a file.
34 	Basically a ResourceFile object can be set to a file, load infos for the
35 	resources without loading their data (InitContainer()), read the data of
36 	one (ReadResource()) or all resources (ReadResources()) and write all
37 	resources to the file (WriteResources()).
38 
39 	Note, that the object does only provide the I/O functionality, it does
40 	not store any information about the resources -- this is done via a
41 	ResourcesContainer. We gain flexibility using this approach, since e.g.
42 	a certain resource may be represented by more than one ResourceItem and
43 	we can have as many ResourcesContainers for the resources as we like.
44 	In particular it is nice, that at any time we can write an arbitrary set
45 	of resources to the file.
46 
47 	\author <a href='mailto:bonefish@users.sf.net'>Ingo Weinhold</a>
48 
49 	\version 0.0.0
50 */
51 class ResourceFile {
52 public:
53 	ResourceFile();
54 	virtual ~ResourceFile();
55 
56 	status_t SetTo(BFile *file, bool clobber = false);
57 	void Unset();
58 	status_t InitCheck() const;
59 
60 	status_t InitContainer(ResourcesContainer &container);
61 	status_t ReadResource(ResourceItem &resource, bool force = false);
62 	status_t ReadResources(ResourcesContainer &container, bool force = false);
63 	status_t WriteResources(ResourcesContainer &container);
64 
65 private:
66 	void _InitFile(BFile &file, bool clobber);
67 	void _InitELFFile(BFile &file);
68 	void _InitPEFFile(BFile &file, const PEFContainerHeader &pefHeader);
69 	void _ReadHeader(resource_parse_info &parseInfo);
70 	void _ReadIndex(resource_parse_info &parseInfo);
71 	bool _ReadIndexEntry(resource_parse_info &parseInfo, int32 index,
72 						 uint32 tableOffset, bool peekAhead);
73 	void _ReadInfoTable(resource_parse_info &parseInfo);
74 	bool _ReadInfoTableEnd(const void *data, int32 dataSize);
75 	const void *_ReadResourceInfo(resource_parse_info &parseInfo,
76 								  const MemArea &area,
77 								  const resource_info *info, type_code type,
78 								  bool *readIndices);
79 
80 	status_t _WriteResources(ResourcesContainer &container);
81 	status_t _MakeEmptyResourceFile();
82 
83 	inline int16 _GetInt16(int16 value);
84 	inline uint16 _GetUInt16(uint16 value);
85 	inline int32 _GetInt32(int32 value);
86 	inline uint32 _GetUInt32(uint32 value);
87 
88 private:
89 	OffsetFile	fFile;
90 	uint32		fFileType;
91 	bool		fHostEndianess;
92 	bool		fEmptyResources;
93 };
94 
95 // _GetInt16
96 inline
97 int16
98 ResourceFile::_GetInt16(int16 value)
99 {
100 	return (fHostEndianess ? value : B_SWAP_INT16(value));
101 }
102 
103 // _GetUInt16
104 inline
105 uint16
106 ResourceFile::_GetUInt16(uint16 value)
107 {
108 	return (fHostEndianess ? value : B_SWAP_INT16(value));
109 }
110 
111 // _GetInt32
112 inline
113 int32
114 ResourceFile::_GetInt32(int32 value)
115 {
116 	return (fHostEndianess ? value : B_SWAP_INT32(value));
117 }
118 
119 // _GetUInt32
120 inline
121 uint32
122 ResourceFile::_GetUInt32(uint32 value)
123 {
124 	return (fHostEndianess ? value : B_SWAP_INT32(value));
125 }
126 
127 };	// namespace Storage
128 };	// namespace BPrivate
129 
130 #endif	// _RESOURCE_FILE_H
131 
132 
133