xpcom/ds/nsProperties.cpp

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:b64c216b9e26
1 /* -*- Mode: C++; tab-width: 2; 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 "nsProperties.h"
7
8 ////////////////////////////////////////////////////////////////////////////////
9
10 NS_IMPL_AGGREGATED(nsProperties)
11 NS_INTERFACE_MAP_BEGIN_AGGREGATED(nsProperties)
12 NS_INTERFACE_MAP_ENTRY(nsIProperties)
13 NS_INTERFACE_MAP_END
14
15 NS_IMETHODIMP
16 nsProperties::Get(const char* prop, const nsIID & uuid, void* *result)
17 {
18 if (NS_WARN_IF(!prop))
19 return NS_ERROR_INVALID_ARG;
20
21 nsCOMPtr<nsISupports> value;
22 if (!nsProperties_HashBase::Get(prop, getter_AddRefs(value))) {
23 return NS_ERROR_FAILURE;
24 }
25 return (value) ? value->QueryInterface(uuid, result) : NS_ERROR_NO_INTERFACE;
26 }
27
28 NS_IMETHODIMP
29 nsProperties::Set(const char* prop, nsISupports* value)
30 {
31 if (NS_WARN_IF(!prop))
32 return NS_ERROR_INVALID_ARG;
33 Put(prop, value);
34 return NS_OK;
35 }
36
37 NS_IMETHODIMP
38 nsProperties::Undefine(const char* prop)
39 {
40 if (NS_WARN_IF(!prop))
41 return NS_ERROR_INVALID_ARG;
42
43 nsCOMPtr<nsISupports> value;
44 if (!nsProperties_HashBase::Get(prop, getter_AddRefs(value)))
45 return NS_ERROR_FAILURE;
46
47 Remove(prop);
48 return NS_OK;
49 }
50
51 NS_IMETHODIMP
52 nsProperties::Has(const char* prop, bool *result)
53 {
54 if (NS_WARN_IF(!prop))
55 return NS_ERROR_INVALID_ARG;
56
57 nsCOMPtr<nsISupports> value;
58 *result = nsProperties_HashBase::Get(prop,
59 getter_AddRefs(value));
60 return NS_OK;
61 }
62
63 struct GetKeysEnumData
64 {
65 char **keys;
66 uint32_t next;
67 nsresult res;
68 };
69
70 PLDHashOperator
71 GetKeysEnumerate(const char *key, nsISupports* data,
72 void *arg)
73 {
74 GetKeysEnumData *gkedp = (GetKeysEnumData *)arg;
75 gkedp->keys[gkedp->next] = strdup(key);
76
77 if (!gkedp->keys[gkedp->next]) {
78 gkedp->res = NS_ERROR_OUT_OF_MEMORY;
79 return PL_DHASH_STOP;
80 }
81
82 gkedp->next++;
83 return PL_DHASH_NEXT;
84 }
85
86 NS_IMETHODIMP
87 nsProperties::GetKeys(uint32_t *count, char ***keys)
88 {
89 if (NS_WARN_IF(!count) || NS_WARN_IF(!keys))
90 return NS_ERROR_INVALID_ARG;
91
92 uint32_t n = Count();
93 char ** k = (char **) nsMemory::Alloc(n * sizeof(char *));
94
95 GetKeysEnumData gked;
96 gked.keys = k;
97 gked.next = 0;
98 gked.res = NS_OK;
99
100 EnumerateRead(GetKeysEnumerate, &gked);
101
102 nsresult rv = gked.res;
103 if (NS_FAILED(rv)) {
104 // Free 'em all
105 for (uint32_t i = 0; i < gked.next; i++)
106 nsMemory::Free(k[i]);
107 nsMemory::Free(k);
108 return rv;
109 }
110
111 *count = n;
112 *keys = k;
113 return NS_OK;
114 }
115
116 ////////////////////////////////////////////////////////////////////////////////

mercurial