xref: /haiku/headers/private/locale/HashMapCatalog.h (revision 362efe0c9f36d3dd38b22d2c24ac02e54b189d7c)
1 /*
2  * Copyright 2009, Adrien Destugues, pulkomandy@gmail.com.
3  * Distributed under the terms of the MIT License.
4  */
5 /*
6  * This file declares all the things we need to add to a catalog add-on to be
7  * able to use it in the developper tools (linkcatkeys, dumpcatalog, and the
8  * catalog editor, when we will have one.
9  */
10 
11 
12 #ifndef _HASH_MAP_CATALOG_H_
13 #define _HASH_MAP_CATALOG_H_
14 
15 
16 #include <assert.h>
17 
18 #include <Catalog.h>
19 #include <HashMap.h>
20 #include <String.h>
21 
22 
23 namespace BPrivate {
24 
25 /*
26  * The key-type for the hash_map which maps native strings or IDs to
27  * the corresponding translated string.
28  * The key-type should be efficient to use if it is just created by an ID
29  * but it should also support being created from up to three strings,
30  * which as a whole specify the key to the translated string.
31  */
32 class CatKey {
33 	public:
34 		BString fString;
35 			// The native string
36 		BString fContext;
37 			// The context of the string's usage
38 		BString fComment;
39 			// A comment that can be used to separate strings that
40 			// are identical otherwise in the native language, but different in
41 			// the translation (useful for the translator)
42 		uint32 fHashVal;
43 			// the hash-value of the key, computed from the three strings
44 		uint32 fFlags;
45 			// with respect to the catalog-editor, each translation can be
46 			// in different states (empty, unchecked, checked, etc.). This
47 			// state (and potential other flags) lives in the fFlags member.
48 
49 	public:
50 		CatKey(const char *str, const char *ctx, const char *cmt);
51 		CatKey(uint32 id);
52 		CatKey();
53 
54 		bool operator== (const CatKey& right) const;
55 		bool operator!= (const CatKey& right) const;
56 		status_t GetStringParts(BString* str, BString* ctx, BString* cmt) const;
57 		static uint32 HashFun(const char* s, int startvalue = 0);
58 			// The hash function is called 3 times, cumulating the 3 strings to
59 			// calculate the key
60 		uint32 GetHashCode() const { return fHashVal; }
61 };
62 
63 } // namespace BPrivate
64 
65 
66 namespace BPrivate {
67 
68 class BHashMapCatalog: public BCatalogAddOn {
69 	protected:
70 		uint32 ComputeFingerprint() const;
71 		typedef HashMap<CatKey, BString> CatMap;
72 		CatMap 				fCatMap;
73 
74 	public:
75 		BHashMapCatalog(const char* signature, const char* language,
76 			uint32 fingerprint);
77 			// Constructor for normal use
78 			//
79 		// overrides of BCatalogAddOn:
80 		const char *GetString(const char *string, const char *context = NULL,
81 						const char *comment = NULL);
82 		const char *GetString(uint32 id);
83 		const char *GetString(const CatKey& key);
84 		//
85 		status_t SetString(const char *string, const char *translated,
86 					const char *context = NULL, const char *comment = NULL);
87 		status_t SetString(int32 id, const char *translated);
88 		status_t SetString(const CatKey& key, const char *translated);
89 
90 		// implementation for editor-interface
91 		virtual status_t ReadFromFile(const char *path = NULL)
92 			{return B_NOT_SUPPORTED;}
93 		virtual status_t ReadFromAttribute(const entry_ref &appOrAddOnRef)
94 			{return B_NOT_SUPPORTED;}
95 		virtual status_t ReadFromResource(const entry_ref &appOrAddOnRef)
96 			{return B_NOT_SUPPORTED;}
97 		virtual status_t WriteToFile(const char *path = NULL)
98 			{return B_NOT_SUPPORTED;}
99 		virtual status_t WriteToAttribute(const entry_ref &appOrAddOnRef)
100 			{return B_NOT_SUPPORTED;}
101 		virtual status_t WriteToResource(const entry_ref &appOrAddOnRef)
102 			{return B_NOT_SUPPORTED;}
103 
104 		void UpdateFingerprint();
105 		//
106 		void MakeEmpty();
107 		int32 CountItems() const;
108 
109 		/*
110 		 * CatWalker allows to walk trough all the strings stored in the
111 		 * catalog. We need that for dumpcatalog, linkcatkeys (to extract the
112 		 * data from the plaintext catalog) and in the catalog editor (to
113 		 * display the list of strings in a given catalog).
114 		 */
115 		class CatWalker {
116 			public:
117 				//CatWalker() {}; // if you use this there is no way to set fPos
118 				// properly.
119 				CatWalker(BHashMapCatalog* catalog);
120 				bool AtEnd() const;
121 				const CatKey& GetKey() const;
122 				const char *GetValue() const;
123 				void Next();
124 			private:
125 				CatMap::Iterator fPos;
126 				CatMap::Entry current;
127 				bool atEnd;
128 		};
129 		friend class CatWalker;
130 		status_t GetWalker(CatWalker *walker);
131 
132 
133 };
134 
135 
136 inline BHashMapCatalog::BHashMapCatalog(const char* signature,
137 	const char* language, uint32 fingerprint)
138 	:
139 	BCatalogAddOn(signature, language, fingerprint)
140 {
141 }
142 
143 
144 inline
145 BHashMapCatalog::CatWalker::CatWalker(BHashMapCatalog* catalog)
146 	:
147 	fPos(catalog->fCatMap.GetIterator())
148 {
149 	if (fPos.HasNext()) {
150 		current = fPos.Next();
151 		atEnd = false;
152 	} else
153 		atEnd = true;
154 }
155 
156 
157 inline bool
158 BHashMapCatalog::CatWalker::AtEnd() const
159 {
160 	return atEnd;
161 }
162 
163 
164 inline const CatKey &
165 BHashMapCatalog::CatWalker::GetKey() const
166 {
167 	assert(!atEnd);
168 	return current.key;
169 }
170 
171 
172 inline const char *
173 BHashMapCatalog::CatWalker::GetValue() const
174 {
175 	assert(!atEnd);
176 	return current.value.String();
177 }
178 
179 
180 inline void
181 BHashMapCatalog::CatWalker::Next()
182 {
183 	if (fPos.HasNext()) {
184 		current = fPos.Next();
185 		atEnd = false;
186 	} else
187 		atEnd = true;
188 }
189 
190 
191 inline status_t
192 BHashMapCatalog::GetWalker(CatWalker *walker)
193 {
194 	if (!walker)
195 		return B_BAD_VALUE;
196 	*walker = CatWalker(this);
197 	return B_OK;
198 }
199 
200 
201 } // namespace BPrivate
202 
203 using namespace BPrivate;
204 
205 #endif // _HASH_MAP_CATALOG_H_
206