vfs: check userland buffers before reading them.
[haiku.git] / headers / private / locale / HashMapCatalog.h
blob09d18f341efd01bea9f0bb470be479def8c80927
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 */
12 #ifndef _HASH_MAP_CATALOG_H_
13 #define _HASH_MAP_CATALOG_H_
16 #include <assert.h>
18 #include <CatalogData.h>
19 #include <HashMap.h>
20 #include <String.h>
23 namespace BPrivate {
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.
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.
49 public:
50 CatKey(const char *str, const char *ctx, const char *cmt);
51 CatKey(uint32 id);
52 CatKey();
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; }
64 class HashMapCatalog: public BCatalogData {
65 protected:
66 uint32 ComputeFingerprint() const;
67 typedef HashMap<CatKey, BString> CatMap;
68 CatMap fCatMap;
70 public:
71 HashMapCatalog(const char* signature, const char* language,
72 uint32 fingerprint);
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();
102 void MakeEmpty();
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).
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;
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)
140 inline
141 HashMapCatalog::CatWalker::CatWalker(HashMapCatalog* catalog)
143 fPos(catalog->fCatMap.GetIterator())
145 if (fPos.HasNext()) {
146 current = fPos.Next();
147 atEnd = false;
148 } else
149 atEnd = true;
153 inline bool
154 HashMapCatalog::CatWalker::AtEnd() const
156 return atEnd;
160 inline const CatKey &
161 HashMapCatalog::CatWalker::GetKey() const
163 assert(!atEnd);
164 return current.key;
168 inline const char *
169 HashMapCatalog::CatWalker::GetValue() const
171 assert(!atEnd);
172 return current.value.String();
176 inline void
177 HashMapCatalog::CatWalker::Next()
179 if (fPos.HasNext()) {
180 current = fPos.Next();
181 atEnd = false;
182 } else
183 atEnd = true;
187 inline status_t
188 HashMapCatalog::GetWalker(CatWalker *walker)
190 if (!walker)
191 return B_BAD_VALUE;
192 *walker = CatWalker(this);
193 return B_OK;
197 } // namespace BPrivate
199 #endif // _HASH_MAP_CATALOG_H_