xpcom/base/nsErrorService.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/base/nsErrorService.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,96 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; 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 "nsErrorService.h"
    1.10 +#include "nsCRTGlue.h"
    1.11 +#include "nsAutoPtr.h"
    1.12 +
    1.13 +static void*
    1.14 +CloneCString(nsHashKey *aKey, void *aData, void* closure)
    1.15 +{
    1.16 +  return NS_strdup((const char*)aData);
    1.17 +}
    1.18 +
    1.19 +static bool
    1.20 +DeleteCString(nsHashKey *aKey, void *aData, void* closure)
    1.21 +{
    1.22 +  NS_Free(aData);
    1.23 +  return true;
    1.24 +}
    1.25 +
    1.26 +nsInt2StrHashtable::nsInt2StrHashtable()
    1.27 +    : mHashtable(CloneCString, nullptr, DeleteCString, nullptr, 16)
    1.28 +{
    1.29 +}
    1.30 +
    1.31 +nsresult
    1.32 +nsInt2StrHashtable::Put(uint32_t key, const char* aData)
    1.33 +{
    1.34 +  char* value = NS_strdup(aData);
    1.35 +  if (value == nullptr)
    1.36 +    return NS_ERROR_OUT_OF_MEMORY;
    1.37 +  nsPRUint32Key k(key);
    1.38 +  char* oldValue = (char*)mHashtable.Put(&k, value);
    1.39 +  if (oldValue)
    1.40 +    NS_Free(oldValue);
    1.41 +  return NS_OK;
    1.42 +}
    1.43 +
    1.44 +char* 
    1.45 +nsInt2StrHashtable::Get(uint32_t key)
    1.46 +{
    1.47 +  nsPRUint32Key k(key);
    1.48 +  const char* value = (const char*)mHashtable.Get(&k);
    1.49 +  if (value == nullptr)
    1.50 +    return nullptr;
    1.51 +  return NS_strdup(value);
    1.52 +}
    1.53 +
    1.54 +nsresult
    1.55 +nsInt2StrHashtable::Remove(uint32_t key)
    1.56 +{
    1.57 +  nsPRUint32Key k(key);
    1.58 +  char* oldValue = (char*)mHashtable.Remove(&k);
    1.59 +  if (oldValue)
    1.60 +    NS_Free(oldValue);
    1.61 +  return NS_OK;
    1.62 +}
    1.63 +
    1.64 +////////////////////////////////////////////////////////////////////////////////
    1.65 +
    1.66 +NS_IMPL_ISUPPORTS(nsErrorService, nsIErrorService)
    1.67 +
    1.68 +nsresult
    1.69 +nsErrorService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
    1.70 +{
    1.71 +    if (NS_WARN_IF(outer))
    1.72 +        return NS_ERROR_NO_AGGREGATION;
    1.73 +    nsRefPtr<nsErrorService> serv = new nsErrorService();
    1.74 +    return serv->QueryInterface(aIID, aInstancePtr);
    1.75 +}
    1.76 +
    1.77 +NS_IMETHODIMP
    1.78 +nsErrorService::RegisterErrorStringBundle(int16_t errorModule, const char *stringBundleURL)
    1.79 +{
    1.80 +    return mErrorStringBundleURLMap.Put(errorModule, stringBundleURL);
    1.81 +}
    1.82 +
    1.83 +NS_IMETHODIMP
    1.84 +nsErrorService::UnregisterErrorStringBundle(int16_t errorModule)
    1.85 +{
    1.86 +    return mErrorStringBundleURLMap.Remove(errorModule);
    1.87 +}
    1.88 +
    1.89 +NS_IMETHODIMP
    1.90 +nsErrorService::GetErrorStringBundle(int16_t errorModule, char **result)
    1.91 +{
    1.92 +    char* value = mErrorStringBundleURLMap.Get(errorModule);
    1.93 +    if (value == nullptr)
    1.94 +        return NS_ERROR_OUT_OF_MEMORY;
    1.95 +    *result = value;
    1.96 +    return NS_OK;
    1.97 +}
    1.98 +
    1.99 +////////////////////////////////////////////////////////////////////////////////

mercurial