Back to index

lightning-sunbird  0.9+nobinonly
nsProperties.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsProperties.h"
00039 #include "nsString.h"
00040 #include "nsCRT.h"
00041 
00043 
00044 NS_IMPL_AGGREGATED(nsProperties)
00045 
00046 NS_METHOD
00047 nsProperties::AggregatedQueryInterface(const nsIID& aIID, void** aInstancePtr) 
00048 {
00049     NS_ENSURE_ARG_POINTER(aInstancePtr);
00050 
00051     if (aIID.Equals(NS_GET_IID(nsISupports)))
00052         *aInstancePtr = GetInner();
00053     else if (aIID.Equals(NS_GET_IID(nsIProperties)))
00054         *aInstancePtr = NS_STATIC_CAST(nsIProperties*, this);
00055     else {
00056         *aInstancePtr = nsnull;
00057         return NS_NOINTERFACE;
00058     } 
00059 
00060     NS_ADDREF((nsISupports*)*aInstancePtr);
00061     return NS_OK;
00062 }
00063 
00064 NS_IMETHODIMP
00065 nsProperties::Get(const char* prop, const nsIID & uuid, void* *result)
00066 {
00067     nsCOMPtr<nsISupports> value;
00068     if (!nsProperties_HashBase::Get(prop, getter_AddRefs(value))) {
00069         return NS_ERROR_FAILURE;
00070     }
00071     return (value) ? value->QueryInterface(uuid, result) : NS_ERROR_NO_INTERFACE;
00072 }
00073 
00074 NS_IMETHODIMP
00075 nsProperties::Set(const char* prop, nsISupports* value)
00076 {
00077     return Put(prop, value) ? NS_OK : NS_ERROR_FAILURE;
00078 }
00079 
00080 NS_IMETHODIMP
00081 nsProperties::Undefine(const char* prop)
00082 {
00083     nsCOMPtr<nsISupports> value;
00084     if (!nsProperties_HashBase::Get(prop, getter_AddRefs(value)))
00085         return NS_ERROR_FAILURE;
00086 
00087     Remove(prop);
00088     return NS_OK;
00089 }
00090 
00091 NS_IMETHODIMP
00092 nsProperties::Has(const char* prop, PRBool *result)
00093 {
00094     nsCOMPtr<nsISupports> value;
00095     *result = nsProperties_HashBase::Get(prop,
00096                                          getter_AddRefs(value));
00097     return NS_OK;
00098 }
00099 
00100 struct GetKeysEnumData
00101 {
00102     char **keys;
00103     PRUint32 next;
00104     nsresult res;
00105 };
00106 
00107 PR_CALLBACK PLDHashOperator
00108 GetKeysEnumerate(const char *key, nsISupports* data,
00109                  void *arg)
00110 {
00111     GetKeysEnumData *gkedp = (GetKeysEnumData *)arg;
00112     gkedp->keys[gkedp->next] = nsCRT::strdup(key);
00113 
00114     if (!gkedp->keys[gkedp->next]) {
00115         gkedp->res = NS_ERROR_OUT_OF_MEMORY;
00116         return PL_DHASH_STOP;
00117     }
00118 
00119     gkedp->next++;
00120     return PL_DHASH_NEXT;
00121 }
00122 
00123 NS_IMETHODIMP 
00124 nsProperties::GetKeys(PRUint32 *count, char ***keys)
00125 {
00126     PRUint32 n = Count();
00127     char ** k = (char **) nsMemory::Alloc(n * sizeof(char *));
00128     if (!k)
00129         return NS_ERROR_OUT_OF_MEMORY;
00130 
00131     GetKeysEnumData gked;
00132     gked.keys = k;
00133     gked.next = 0;
00134     gked.res = NS_OK;
00135 
00136     EnumerateRead(GetKeysEnumerate, &gked);
00137 
00138     if (NS_FAILED(gked.res)) {
00139         // Free 'em all
00140         for (PRUint32 i = 0; i < gked.next; i++)
00141             nsMemory::Free(k[i]);
00142         nsMemory::Free(k);
00143         return gked.res;
00144     }
00145 
00146     *count = n;
00147     *keys = k;
00148     return NS_OK;
00149 }
00150 
00151 NS_METHOD
00152 nsProperties::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00153 {
00154     NS_ENSURE_PROPER_AGGREGATION(aOuter, aIID);
00155 
00156     nsProperties* props = new nsProperties(aOuter);
00157     if (props == nsnull)
00158         return NS_ERROR_OUT_OF_MEMORY;
00159 
00160     NS_ADDREF(props);
00161     nsresult rv = props->Init();
00162     if (NS_SUCCEEDED(rv))
00163         rv = props->AggregatedQueryInterface(aIID, aResult);
00164 
00165     NS_RELEASE(props);
00166     return rv;
00167 }
00168