summaryrefslogtreecommitdiff
path: root/XMPFiles/source/HandlerRegistry.h
blob: 69509d84078bc34085b06d134200b15e6bb8a0f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// =================================================================================================
// Copyright Adobe
// Copyright 2011 Adobe
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it. If you have received this file from a source other 
// than Adobe, then your use, modification, or distribution of it requires the prior written permission
// of Adobe.
// =================================================================================================

#ifndef _HANDLERREGISTRY_h_
#define _HANDLERREGISTRY_h_

#include "public/include/XMP_Environment.h"	// ! XMP_Environment.h must be the first included header.

#include "public/include/XMP_Const.h"
#include "public/include/XMP_IO.hpp"

#include "XMPFiles/source/XMPFiles_Impl.hpp"
#include "source/XMPFiles_IO.hpp"

#include "XMPFiles/source/FormatSupport/IFF/IChunkBehavior.h"
#include "XMPFiles/source/FormatSupport/IFF/ChunkPath.h"
#include "source/Endian.h"

namespace Common
{

/**
	File handler data
 */
struct XMPFileHandlerInfo 
{
	XMP_FileFormat		format;
	XMP_OptionBits		flags;
	void*				checkProc;
	XMPFileHandlerCTor	handlerCTor;

	XMPFileHandlerInfo() : format(0), flags(0), checkProc(0), handlerCTor(0) 
	{};
	
	XMPFileHandlerInfo( XMP_FileFormat _format, 
						XMP_OptionBits _flags,
						CheckFileFormatProc _checkProc, 
						XMPFileHandlerCTor _handlerCTor )
		: format(_format), flags(_flags), checkProc((void*)_checkProc), handlerCTor(_handlerCTor) 
	{};
	
	XMPFileHandlerInfo( XMP_FileFormat _format, 
						XMP_OptionBits _flags,
						CheckFolderFormatProc _checkProc, 
						XMPFileHandlerCTor _handlerCTor )
		: format(_format), flags(_flags), checkProc((void*)_checkProc), handlerCTor(_handlerCTor) 
	{};
};

/**
	The singleton class HandlerRegistry is responsible to manage all file handler.
	It registers file handlers during initialization time and provides functionality
	to select a file handler based on a given file format.
*/

class HandlerRegistry
{
public:
	static HandlerRegistry&		getInstance();
	static void					terminate();

#if EnableDynamicMediaHandlers
	static XMP_FileFormat		checkTopFolderName( const std::string & rootPath );
	static XMP_FileFormat		checkParentFolderNames( const std::string& rootPath,
														const std::string& gpName,
														const std::string& parentName, 
														const std::string& leafName );
#endif

public:
	/**
	 * Register all file handler
	 */
	void				initialize();

	/**
	 * Register a single folder based file handler.
	 *
	 * @param format			File format identifier
	 * @param flags				Flags
	 * @param checkProc			Check format function pointer
	 * @param handlerCTor		Factory function pointer
	 * @param replaceExisting	Replace an already existing handler
	 */
	bool				registerFolderHandler( XMP_FileFormat			format,
											   XMP_OptionBits			flags,
											   CheckFolderFormatProc	checkProc,
											   XMPFileHandlerCTor		handlerCTor,
											   bool						replaceExisting = false );

	/**
	 * Register a single normal file handler.
	 *
	 * @param format			File format identifier
	 * @param flags				Flags
	 * @param checkProc			Check format function pointer
	 * @param handlerCTor		Factory function pointer
	 * @param replaceExisting	Replace an already existing handler
	 */
	bool				registerNormalHandler( XMP_FileFormat			format,
											   XMP_OptionBits			flags,
											   CheckFileFormatProc		checkProc,
											   XMPFileHandlerCTor		handlerCTor,
											   bool						replaceExisting = false );

	/**
	 * Register a single owning file handler.
	 *
	 * @param format			File format identifier
	 * @param flags				Flags
	 * @param checkProc			Check format function pointer
	 * @param handlerCTor		Factory function pointer
	 * @param replaceExisting	Replace an already existing handler
	 */
	bool				registerOwningHandler( XMP_FileFormat			format,
											   XMP_OptionBits			flags,
											   CheckFileFormatProc		checkProc,
											   XMPFileHandlerCTor		handlerCTor,
											   bool						replaceExisting = false );

	// Remove a handler. Does nothing if no such handler exists.
	void removeHandler ( XMP_FileFormat format );
	
	/**
	 * Get file format identifier for filename extension.
	 *
	 * @param fileExt			Filename extension
	 * @param addIfNotFound		If true if handler doesn't exists then add now
	 */
	XMP_FileFormat		getFileFormat( const std::string & fileExt, bool addIfNotFound = false );

	/**
	 * Get handler flags for file format.
	 *
	 * @param format	File format identifier
	 * @param flags		Return handler flag
	 * @return			True on success
	 */
	bool				getFormatInfo( XMP_FileFormat format, XMP_OptionBits* flags = NULL );

	/**
	 * Get handler information for passed format. 
	 * The returned file handler is the default handler. I.e. that handler
	 * that is used when called from outside using the XMPFiles API.
	 *
	 * @param format	File format identifier
	 * @return			Return handler info if available, otherwise NULL
	 */
	XMPFileHandlerInfo*	getHandlerInfo( XMP_FileFormat format );

	/**
	 * Get file handler information of the standard file handler for the
	 * file format identifier.
	 * If there is a replacement for this format then the standard handler
	 * is the replaced handler. Otherwise the standard handler and the 
	 * default handler are the same.
	 *
	 * @param format	File format identifier
	 * @return			Return handler info if available, otherwise NULL
	 */
	XMPFileHandlerInfo*	getStandardHandlerInfo( XMP_FileFormat format );

	/**
	 * Return true if there is a replacement for the file format
	 */
	bool isReplaced( XMP_FileFormat format );

	/**
	 * Select file handler based on passed information and setup XMPFiles instance with related data.
	 *
	 * @param session		XMPFiles instance
	 * @param clientPath	Path to file
	 * @param format		File format identifier
	 * @param openFlags		Flags
	 * @return				File handler structure
	 */
	XMPFileHandlerInfo*	selectSmartHandler( XMPFiles* session, XMP_StringPtr clientPath, XMP_FileFormat format, XMP_OptionBits openFlags );

private:
	/**
	 * Return default file handler for file format identifier or filename extension
	 *
	 * @param format	File format identifier
	 * @param fileExt	Filename extension
	 * @return			File handler structure for passed format/extension
	 */
	XMPFileHandlerInfo* pickDefaultHandler ( XMP_FileFormat format, const std::string & fileExt );

#if EnableDynamicMediaHandlers
	/**
	 * Try to find folder based file handler.
	 *
	 * @param format		File format identifier
	 * @param rootPath		Path to root folder
	 * @param gpName		Grand parent folder name
	 * @param parentName	Parent folder name
	 * @param leafName		File name
	 * @param parentObj		XMPFiles instance
	 * @return				File handler structure
	 */
	XMPFileHandlerInfo* tryFolderHandlers( XMP_FileFormat format,
										   const std::string& rootPath,
										   const std::string& gpName,
										   const std::string& parentName,
										   const std::string& leafName,
										   XMPFiles* parentObj );
#endif

private:
	/**
	 * ctor/dtor
	 */
	 HandlerRegistry();
	~HandlerRegistry();

private:
	typedef std::map <XMP_FileFormat, XMPFileHandlerInfo>	XMPFileHandlerTable;
	typedef XMPFileHandlerTable::iterator					XMPFileHandlerTablePos;
	typedef std::pair <XMP_FileFormat, XMPFileHandlerInfo>	XMPFileHandlerTablePair;

	XMPFileHandlerTable*	mFolderHandlers;	// The directory-oriented handlers.
	XMPFileHandlerTable*	mNormalHandlers;	// The normal file-oriented handlers.
	XMPFileHandlerTable*	mOwningHandlers;	// The file-oriented handlers that "own" the file.

	XMPFileHandlerTable*	mReplacedHandlers;	// All file handler that where replaced by a later one

	static HandlerRegistry*	sInstance;			// singleton instance
};

} // Common

#endif	// _HANDLERREGISTRY_h_