xpcom/ds/nsProperties.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/ds/nsProperties.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,116 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsProperties.h"
    1.10 +
    1.11 +////////////////////////////////////////////////////////////////////////////////
    1.12 +
    1.13 +NS_IMPL_AGGREGATED(nsProperties)
    1.14 +NS_INTERFACE_MAP_BEGIN_AGGREGATED(nsProperties)
    1.15 +    NS_INTERFACE_MAP_ENTRY(nsIProperties)
    1.16 +NS_INTERFACE_MAP_END
    1.17 +
    1.18 +NS_IMETHODIMP
    1.19 +nsProperties::Get(const char* prop, const nsIID & uuid, void* *result)
    1.20 +{
    1.21 +    if (NS_WARN_IF(!prop))
    1.22 +        return NS_ERROR_INVALID_ARG;
    1.23 +
    1.24 +    nsCOMPtr<nsISupports> value;
    1.25 +    if (!nsProperties_HashBase::Get(prop, getter_AddRefs(value))) {
    1.26 +        return NS_ERROR_FAILURE;
    1.27 +    }
    1.28 +    return (value) ? value->QueryInterface(uuid, result) : NS_ERROR_NO_INTERFACE;
    1.29 +}
    1.30 +
    1.31 +NS_IMETHODIMP
    1.32 +nsProperties::Set(const char* prop, nsISupports* value)
    1.33 +{
    1.34 +    if (NS_WARN_IF(!prop))
    1.35 +        return NS_ERROR_INVALID_ARG;
    1.36 +    Put(prop, value);
    1.37 +    return NS_OK;
    1.38 +}
    1.39 +
    1.40 +NS_IMETHODIMP
    1.41 +nsProperties::Undefine(const char* prop)
    1.42 +{
    1.43 +    if (NS_WARN_IF(!prop))
    1.44 +        return NS_ERROR_INVALID_ARG;
    1.45 +
    1.46 +    nsCOMPtr<nsISupports> value;
    1.47 +    if (!nsProperties_HashBase::Get(prop, getter_AddRefs(value)))
    1.48 +        return NS_ERROR_FAILURE;
    1.49 +
    1.50 +    Remove(prop);
    1.51 +    return NS_OK;
    1.52 +}
    1.53 +
    1.54 +NS_IMETHODIMP
    1.55 +nsProperties::Has(const char* prop, bool *result)
    1.56 +{
    1.57 +    if (NS_WARN_IF(!prop))
    1.58 +        return NS_ERROR_INVALID_ARG;
    1.59 +
    1.60 +    nsCOMPtr<nsISupports> value;
    1.61 +    *result = nsProperties_HashBase::Get(prop,
    1.62 +                                         getter_AddRefs(value));
    1.63 +    return NS_OK;
    1.64 +}
    1.65 +
    1.66 +struct GetKeysEnumData
    1.67 +{
    1.68 +    char **keys;
    1.69 +    uint32_t next;
    1.70 +    nsresult res;
    1.71 +};
    1.72 +
    1.73 + PLDHashOperator
    1.74 +GetKeysEnumerate(const char *key, nsISupports* data,
    1.75 +                 void *arg)
    1.76 +{
    1.77 +    GetKeysEnumData *gkedp = (GetKeysEnumData *)arg;
    1.78 +    gkedp->keys[gkedp->next] = strdup(key);
    1.79 +
    1.80 +    if (!gkedp->keys[gkedp->next]) {
    1.81 +        gkedp->res = NS_ERROR_OUT_OF_MEMORY;
    1.82 +        return PL_DHASH_STOP;
    1.83 +    }
    1.84 +
    1.85 +    gkedp->next++;
    1.86 +    return PL_DHASH_NEXT;
    1.87 +}
    1.88 +
    1.89 +NS_IMETHODIMP 
    1.90 +nsProperties::GetKeys(uint32_t *count, char ***keys)
    1.91 +{
    1.92 +    if (NS_WARN_IF(!count) || NS_WARN_IF(!keys))
    1.93 +        return NS_ERROR_INVALID_ARG;
    1.94 +
    1.95 +    uint32_t n = Count();
    1.96 +    char ** k = (char **) nsMemory::Alloc(n * sizeof(char *));
    1.97 +
    1.98 +    GetKeysEnumData gked;
    1.99 +    gked.keys = k;
   1.100 +    gked.next = 0;
   1.101 +    gked.res = NS_OK;
   1.102 +
   1.103 +    EnumerateRead(GetKeysEnumerate, &gked);
   1.104 +
   1.105 +    nsresult rv = gked.res;
   1.106 +    if (NS_FAILED(rv)) {
   1.107 +        // Free 'em all
   1.108 +        for (uint32_t i = 0; i < gked.next; i++)
   1.109 +            nsMemory::Free(k[i]);
   1.110 +        nsMemory::Free(k);
   1.111 +        return rv;
   1.112 +    }
   1.113 +
   1.114 +    *count = n;
   1.115 +    *keys = k;
   1.116 +    return NS_OK;
   1.117 +}
   1.118 +
   1.119 +////////////////////////////////////////////////////////////////////////////////

mercurial