|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "nsErrorService.h" |
|
7 #include "nsCRTGlue.h" |
|
8 #include "nsAutoPtr.h" |
|
9 |
|
10 static void* |
|
11 CloneCString(nsHashKey *aKey, void *aData, void* closure) |
|
12 { |
|
13 return NS_strdup((const char*)aData); |
|
14 } |
|
15 |
|
16 static bool |
|
17 DeleteCString(nsHashKey *aKey, void *aData, void* closure) |
|
18 { |
|
19 NS_Free(aData); |
|
20 return true; |
|
21 } |
|
22 |
|
23 nsInt2StrHashtable::nsInt2StrHashtable() |
|
24 : mHashtable(CloneCString, nullptr, DeleteCString, nullptr, 16) |
|
25 { |
|
26 } |
|
27 |
|
28 nsresult |
|
29 nsInt2StrHashtable::Put(uint32_t key, const char* aData) |
|
30 { |
|
31 char* value = NS_strdup(aData); |
|
32 if (value == nullptr) |
|
33 return NS_ERROR_OUT_OF_MEMORY; |
|
34 nsPRUint32Key k(key); |
|
35 char* oldValue = (char*)mHashtable.Put(&k, value); |
|
36 if (oldValue) |
|
37 NS_Free(oldValue); |
|
38 return NS_OK; |
|
39 } |
|
40 |
|
41 char* |
|
42 nsInt2StrHashtable::Get(uint32_t key) |
|
43 { |
|
44 nsPRUint32Key k(key); |
|
45 const char* value = (const char*)mHashtable.Get(&k); |
|
46 if (value == nullptr) |
|
47 return nullptr; |
|
48 return NS_strdup(value); |
|
49 } |
|
50 |
|
51 nsresult |
|
52 nsInt2StrHashtable::Remove(uint32_t key) |
|
53 { |
|
54 nsPRUint32Key k(key); |
|
55 char* oldValue = (char*)mHashtable.Remove(&k); |
|
56 if (oldValue) |
|
57 NS_Free(oldValue); |
|
58 return NS_OK; |
|
59 } |
|
60 |
|
61 //////////////////////////////////////////////////////////////////////////////// |
|
62 |
|
63 NS_IMPL_ISUPPORTS(nsErrorService, nsIErrorService) |
|
64 |
|
65 nsresult |
|
66 nsErrorService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr) |
|
67 { |
|
68 if (NS_WARN_IF(outer)) |
|
69 return NS_ERROR_NO_AGGREGATION; |
|
70 nsRefPtr<nsErrorService> serv = new nsErrorService(); |
|
71 return serv->QueryInterface(aIID, aInstancePtr); |
|
72 } |
|
73 |
|
74 NS_IMETHODIMP |
|
75 nsErrorService::RegisterErrorStringBundle(int16_t errorModule, const char *stringBundleURL) |
|
76 { |
|
77 return mErrorStringBundleURLMap.Put(errorModule, stringBundleURL); |
|
78 } |
|
79 |
|
80 NS_IMETHODIMP |
|
81 nsErrorService::UnregisterErrorStringBundle(int16_t errorModule) |
|
82 { |
|
83 return mErrorStringBundleURLMap.Remove(errorModule); |
|
84 } |
|
85 |
|
86 NS_IMETHODIMP |
|
87 nsErrorService::GetErrorStringBundle(int16_t errorModule, char **result) |
|
88 { |
|
89 char* value = mErrorStringBundleURLMap.Get(errorModule); |
|
90 if (value == nullptr) |
|
91 return NS_ERROR_OUT_OF_MEMORY; |
|
92 *result = value; |
|
93 return NS_OK; |
|
94 } |
|
95 |
|
96 //////////////////////////////////////////////////////////////////////////////// |