1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsProperties.h"
39 : #include "nsString.h"
40 : #include "nsCRT.h"
41 :
42 : ////////////////////////////////////////////////////////////////////////////////
43 :
44 112 : NS_IMPL_AGGREGATED(nsProperties)
45 16 : NS_INTERFACE_MAP_BEGIN_AGGREGATED(nsProperties)
46 16 : NS_INTERFACE_MAP_ENTRY(nsIProperties)
47 0 : NS_INTERFACE_MAP_END
48 :
49 : NS_IMETHODIMP
50 0 : nsProperties::Get(const char* prop, const nsIID & uuid, void* *result)
51 : {
52 0 : NS_ENSURE_ARG(prop);
53 :
54 0 : nsCOMPtr<nsISupports> value;
55 0 : if (!nsProperties_HashBase::Get(prop, getter_AddRefs(value))) {
56 0 : return NS_ERROR_FAILURE;
57 : }
58 0 : return (value) ? value->QueryInterface(uuid, result) : NS_ERROR_NO_INTERFACE;
59 : }
60 :
61 : NS_IMETHODIMP
62 8 : nsProperties::Set(const char* prop, nsISupports* value)
63 : {
64 8 : NS_ENSURE_ARG(prop);
65 :
66 8 : return Put(prop, value) ? NS_OK : NS_ERROR_FAILURE;
67 : }
68 :
69 : NS_IMETHODIMP
70 0 : nsProperties::Undefine(const char* prop)
71 : {
72 0 : NS_ENSURE_ARG(prop);
73 :
74 0 : nsCOMPtr<nsISupports> value;
75 0 : if (!nsProperties_HashBase::Get(prop, getter_AddRefs(value)))
76 0 : return NS_ERROR_FAILURE;
77 :
78 0 : Remove(prop);
79 0 : return NS_OK;
80 : }
81 :
82 : NS_IMETHODIMP
83 0 : nsProperties::Has(const char* prop, bool *result)
84 : {
85 0 : NS_ENSURE_ARG(prop);
86 :
87 0 : nsCOMPtr<nsISupports> value;
88 : *result = nsProperties_HashBase::Get(prop,
89 0 : getter_AddRefs(value));
90 0 : return NS_OK;
91 : }
92 :
93 : struct GetKeysEnumData
94 : {
95 : char **keys;
96 : PRUint32 next;
97 : nsresult res;
98 : };
99 :
100 : PLDHashOperator
101 0 : GetKeysEnumerate(const char *key, nsISupports* data,
102 : void *arg)
103 : {
104 0 : GetKeysEnumData *gkedp = (GetKeysEnumData *)arg;
105 0 : gkedp->keys[gkedp->next] = nsCRT::strdup(key);
106 :
107 0 : if (!gkedp->keys[gkedp->next]) {
108 0 : gkedp->res = NS_ERROR_OUT_OF_MEMORY;
109 0 : return PL_DHASH_STOP;
110 : }
111 :
112 0 : gkedp->next++;
113 0 : return PL_DHASH_NEXT;
114 : }
115 :
116 : NS_IMETHODIMP
117 0 : nsProperties::GetKeys(PRUint32 *count, char ***keys)
118 : {
119 0 : NS_ENSURE_ARG(count);
120 0 : NS_ENSURE_ARG(keys);
121 :
122 0 : PRUint32 n = Count();
123 0 : char ** k = (char **) nsMemory::Alloc(n * sizeof(char *));
124 0 : NS_ENSURE_TRUE(k, NS_ERROR_OUT_OF_MEMORY);
125 :
126 : GetKeysEnumData gked;
127 0 : gked.keys = k;
128 0 : gked.next = 0;
129 0 : gked.res = NS_OK;
130 :
131 0 : EnumerateRead(GetKeysEnumerate, &gked);
132 :
133 0 : nsresult rv = gked.res;
134 0 : if (NS_FAILED(rv)) {
135 : // Free 'em all
136 0 : for (PRUint32 i = 0; i < gked.next; i++)
137 0 : nsMemory::Free(k[i]);
138 0 : nsMemory::Free(k);
139 0 : return rv;
140 : }
141 :
142 0 : *count = n;
143 0 : *keys = k;
144 0 : return NS_OK;
145 : }
146 :
147 : ////////////////////////////////////////////////////////////////////////////////
|