xref: /haiku/headers/private/storage/mime/SnifferRules.h (revision 579f1dbca962a2a03df54f69fdc6e9423f91f20e)
1 /*
2  * Copyright 2002-2007, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _MIME_SNIFFER_RULES_H
6 #define _MIME_SNIFFER_RULES_H
7 
8 
9 #include <SupportDefs.h>
10 
11 #include <list>
12 #include <string>
13 
14 class BFile;
15 class BString;
16 struct entry_ref;
17 
18 
19 namespace BPrivate {
20 namespace Storage {
21 
22 namespace Sniffer {
23 	class Rule;
24 }
25 
26 namespace Mime {
27 
28 
29 class DatabaseLocation;
30 class MimeSniffer;
31 
32 
33 class SnifferRules {
34 public:
35 	SnifferRules(DatabaseLocation* databaseLocation, MimeSniffer* mimeSniffer);
36 	~SnifferRules();
37 
38 	status_t GuessMimeType(const entry_ref *ref, BString *type);
39 	status_t GuessMimeType(const void *buffer, int32 length, BString *type);
40 
41 	status_t SetSnifferRule(const char *type, const char *rule);
42 	status_t DeleteSnifferRule(const char *type);
43 
44 	void PrintToStream() const;
45 
46 	struct sniffer_rule {
47 		std::string type;							// The mime type that own the rule
48 		std::string rule_string;					// The unparsed string version of the rule
49 		BPrivate::Storage::Sniffer::Rule *rule;		// The parsed rule
50 
51 		sniffer_rule(BPrivate::Storage::Sniffer::Rule *rule = NULL);
52 		~sniffer_rule();
53 	};
54 private:
55 	status_t BuildRuleList();
56 	status_t GuessMimeType(BFile* file, const void *buffer, int32 length,
57 		BString *type);
58 	ssize_t MaxBytesNeeded();
59 	status_t ProcessType(const char *type, ssize_t *bytesNeeded);
60 
61 	std::list<sniffer_rule> fRuleList;
62 
63 private:
64 	DatabaseLocation*	fDatabaseLocation;
65 	MimeSniffer*		fMimeSniffer;
66 	ssize_t				fMaxBytesNeeded;
67 	bool				fHaveDoneFullBuild;
68 };
69 
70 } // namespace Mime
71 } // namespace Storage
72 } // namespace BPrivate
73 
74 #endif	// _MIME_SNIFFER_H
75