michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsErrorService.h" michael@0: #include "nsCRTGlue.h" michael@0: #include "nsAutoPtr.h" michael@0: michael@0: static void* michael@0: CloneCString(nsHashKey *aKey, void *aData, void* closure) michael@0: { michael@0: return NS_strdup((const char*)aData); michael@0: } michael@0: michael@0: static bool michael@0: DeleteCString(nsHashKey *aKey, void *aData, void* closure) michael@0: { michael@0: NS_Free(aData); michael@0: return true; michael@0: } michael@0: michael@0: nsInt2StrHashtable::nsInt2StrHashtable() michael@0: : mHashtable(CloneCString, nullptr, DeleteCString, nullptr, 16) michael@0: { michael@0: } michael@0: michael@0: nsresult michael@0: nsInt2StrHashtable::Put(uint32_t key, const char* aData) michael@0: { michael@0: char* value = NS_strdup(aData); michael@0: if (value == nullptr) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: nsPRUint32Key k(key); michael@0: char* oldValue = (char*)mHashtable.Put(&k, value); michael@0: if (oldValue) michael@0: NS_Free(oldValue); michael@0: return NS_OK; michael@0: } michael@0: michael@0: char* michael@0: nsInt2StrHashtable::Get(uint32_t key) michael@0: { michael@0: nsPRUint32Key k(key); michael@0: const char* value = (const char*)mHashtable.Get(&k); michael@0: if (value == nullptr) michael@0: return nullptr; michael@0: return NS_strdup(value); michael@0: } michael@0: michael@0: nsresult michael@0: nsInt2StrHashtable::Remove(uint32_t key) michael@0: { michael@0: nsPRUint32Key k(key); michael@0: char* oldValue = (char*)mHashtable.Remove(&k); michael@0: if (oldValue) michael@0: NS_Free(oldValue); michael@0: return NS_OK; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: NS_IMPL_ISUPPORTS(nsErrorService, nsIErrorService) michael@0: michael@0: nsresult michael@0: nsErrorService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr) michael@0: { michael@0: if (NS_WARN_IF(outer)) michael@0: return NS_ERROR_NO_AGGREGATION; michael@0: nsRefPtr serv = new nsErrorService(); michael@0: return serv->QueryInterface(aIID, aInstancePtr); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsErrorService::RegisterErrorStringBundle(int16_t errorModule, const char *stringBundleURL) michael@0: { michael@0: return mErrorStringBundleURLMap.Put(errorModule, stringBundleURL); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsErrorService::UnregisterErrorStringBundle(int16_t errorModule) michael@0: { michael@0: return mErrorStringBundleURLMap.Remove(errorModule); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsErrorService::GetErrorStringBundle(int16_t errorModule, char **result) michael@0: { michael@0: char* value = mErrorStringBundleURLMap.Get(errorModule); michael@0: if (value == nullptr) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: *result = value; michael@0: return NS_OK; michael@0: } michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////////