2 * Copyright 2009, Adrien Destugues, pulkomandy@gmail.com.
3 * Distributed under the terms of the MIT License.
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.
12 #ifndef _HASH_MAP_CATALOG_H_
13 #define _HASH_MAP_CATALOG_H_
18 #include <CatalogData.h>
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.
37 // The context of the string's usage
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)
43 // the hash-value of the key, computed from the three strings
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.
50 CatKey(const char *str
, const char *ctx
, const char *cmt
);
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
60 uint32
GetHashCode() const { return fHashVal
; }
64 class HashMapCatalog
: public BCatalogData
{
66 uint32
ComputeFingerprint() const;
67 typedef HashMap
<CatKey
, BString
> CatMap
;
71 HashMapCatalog(const char* signature
, const char* language
,
73 // Constructor for normal use
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
);
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
);
86 // implementation for editor-interface
87 virtual status_t
ReadFromFile(const char *path
= NULL
)
88 {return B_NOT_SUPPORTED
;}
89 virtual status_t
ReadFromAttribute(const entry_ref
&appOrAddOnRef
)
90 {return B_NOT_SUPPORTED
;}
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
;}
95 virtual status_t
WriteToAttribute(const entry_ref
&appOrAddOnRef
)
96 {return B_NOT_SUPPORTED
;}
97 virtual status_t
WriteToResource(const entry_ref
&appOrAddOnRef
)
98 {return B_NOT_SUPPORTED
;}
100 void UpdateFingerprint();
103 int32
CountItems() const;
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).
113 //CatWalker() {}; // if you use this there is no way to set fPos
115 CatWalker(HashMapCatalog
* catalog
);
117 const CatKey
& GetKey() const;
118 const char *GetValue() const;
121 CatMap::Iterator fPos
;
122 CatMap::Entry current
;
125 friend class CatWalker
;
126 status_t
GetWalker(CatWalker
*walker
);
132 inline HashMapCatalog::HashMapCatalog(const char* signature
,
133 const char* language
, uint32 fingerprint
)
135 BCatalogData(signature
, language
, fingerprint
)
141 HashMapCatalog::CatWalker::CatWalker(HashMapCatalog
* catalog
)
143 fPos(catalog
->fCatMap
.GetIterator())
145 if (fPos
.HasNext()) {
146 current
= fPos
.Next();
154 HashMapCatalog::CatWalker::AtEnd() const
160 inline const CatKey
&
161 HashMapCatalog::CatWalker::GetKey() const
169 HashMapCatalog::CatWalker::GetValue() const
172 return current
.value
.String();
177 HashMapCatalog::CatWalker::Next()
179 if (fPos
.HasNext()) {
180 current
= fPos
.Next();
188 HashMapCatalog::GetWalker(CatWalker
*walker
)
192 *walker
= CatWalker(this);
197 } // namespace BPrivate
199 #endif // _HASH_MAP_CATALOG_H_