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 <CatalogData.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
GetHashCode()60 uint32 GetHashCode() const { return fHashVal; }
61 };
62
63
64 class HashMapCatalog: public BCatalogData {
65 protected:
66 uint32 ComputeFingerprint() const;
67 typedef HashMap<CatKey, BString> CatMap;
68 CatMap fCatMap;
69
70 public:
71 HashMapCatalog(const char* signature, const char* language,
72 uint32 fingerprint);
73 // Constructor for normal use
74 //
75 // overrides of BCatalogData:
76 const char *GetString(const char *string, const char *context = NULL,
77 const char *comment = NULL);
78 const char *GetString(uint32 id);
79 const char *GetString(const CatKey& key);
80 //
81 status_t SetString(const char *string, const char *translated,
82 const char *context = NULL, const char *comment = NULL);
83 status_t SetString(int32 id, const char *translated);
84 status_t SetString(const CatKey& key, const char *translated);
85
86 // implementation for editor-interface
87 virtual status_t ReadFromFile(const char *path = NULL)
88 {return B_NOT_SUPPORTED;}
ReadFromAttribute(const entry_ref & appOrAddOnRef)89 virtual status_t ReadFromAttribute(const entry_ref &appOrAddOnRef)
90 {return B_NOT_SUPPORTED;}
ReadFromResource(const entry_ref & appOrAddOnRef)91 virtual status_t ReadFromResource(const entry_ref &appOrAddOnRef)
92 {return B_NOT_SUPPORTED;}
93 virtual status_t WriteToFile(const char *path = NULL)
94 {return B_NOT_SUPPORTED;}
WriteToAttribute(const entry_ref & appOrAddOnRef)95 virtual status_t WriteToAttribute(const entry_ref &appOrAddOnRef)
96 {return B_NOT_SUPPORTED;}
WriteToResource(const entry_ref & appOrAddOnRef)97 virtual status_t WriteToResource(const entry_ref &appOrAddOnRef)
98 {return B_NOT_SUPPORTED;}
99
100 void UpdateFingerprint();
101 //
102 void MakeEmpty();
103 int32 CountItems() const;
104
105 /*
106 * CatWalker allows to walk trough all the strings stored in the
107 * catalog. We need that for dumpcatalog, linkcatkeys (to extract the
108 * data from the plaintext catalog) and in the catalog editor (to
109 * display the list of strings in a given catalog).
110 */
111 class CatWalker {
112 public:
113 //CatWalker() {}; // if you use this there is no way to set fPos
114 // properly.
115 CatWalker(HashMapCatalog* catalog);
116 bool AtEnd() const;
117 const CatKey& GetKey() const;
118 const char *GetValue() const;
119 void Next();
120 private:
121 CatMap::Iterator fPos;
122 CatMap::Entry current;
123 bool atEnd;
124 };
125 friend class CatWalker;
126 status_t GetWalker(CatWalker *walker);
127
128
129 };
130
131
HashMapCatalog(const char * signature,const char * language,uint32 fingerprint)132 inline HashMapCatalog::HashMapCatalog(const char* signature,
133 const char* language, uint32 fingerprint)
134 :
135 BCatalogData(signature, language, fingerprint)
136 {
137 }
138
139
140 inline
CatWalker(HashMapCatalog * catalog)141 HashMapCatalog::CatWalker::CatWalker(HashMapCatalog* catalog)
142 :
143 fPos(catalog->fCatMap.GetIterator())
144 {
145 if (fPos.HasNext()) {
146 current = fPos.Next();
147 atEnd = false;
148 } else
149 atEnd = true;
150 }
151
152
153 inline bool
AtEnd()154 HashMapCatalog::CatWalker::AtEnd() const
155 {
156 return atEnd;
157 }
158
159
160 inline const CatKey &
GetKey()161 HashMapCatalog::CatWalker::GetKey() const
162 {
163 assert(!atEnd);
164 return current.key;
165 }
166
167
168 inline const char *
GetValue()169 HashMapCatalog::CatWalker::GetValue() const
170 {
171 assert(!atEnd);
172 return current.value.String();
173 }
174
175
176 inline void
Next()177 HashMapCatalog::CatWalker::Next()
178 {
179 if (fPos.HasNext()) {
180 current = fPos.Next();
181 atEnd = false;
182 } else
183 atEnd = true;
184 }
185
186
187 inline status_t
GetWalker(CatWalker * walker)188 HashMapCatalog::GetWalker(CatWalker *walker)
189 {
190 if (!walker)
191 return B_BAD_VALUE;
192 *walker = CatWalker(this);
193 return B_OK;
194 }
195
196
197 } // namespace BPrivate
198
199 #endif // _HASH_MAP_CATALOG_H_
200