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 "nsINIParserImpl.h" michael@0: michael@0: #include "nsINIParser.h" michael@0: #include "nsStringEnumerator.h" michael@0: #include "nsTArray.h" michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: class nsINIParserImpl MOZ_FINAL : michael@0: public nsIINIParser michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIINIPARSER michael@0: michael@0: nsresult Init(nsIFile* aINIFile) { michael@0: return mParser.Init(aINIFile); michael@0: } michael@0: michael@0: private: michael@0: nsINIParser mParser; michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(nsINIParserFactory, michael@0: nsIINIParserFactory, michael@0: nsIFactory) michael@0: michael@0: NS_IMETHODIMP michael@0: nsINIParserFactory::CreateINIParser(nsIFile* aINIFile, michael@0: nsIINIParser* *aResult) michael@0: { michael@0: *aResult = nullptr; michael@0: michael@0: nsRefPtr p(new nsINIParserImpl()); michael@0: if (!p) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: nsresult rv = p->Init(aINIFile); michael@0: michael@0: if (NS_SUCCEEDED(rv)) michael@0: NS_ADDREF(*aResult = p); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsINIParserFactory::CreateInstance(nsISupports* aOuter, michael@0: REFNSIID aIID, michael@0: void **aResult) michael@0: { michael@0: if (NS_WARN_IF(aOuter)) michael@0: return NS_ERROR_NO_AGGREGATION; michael@0: michael@0: // We are our own singleton. michael@0: return QueryInterface(aIID, aResult); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsINIParserFactory::LockFactory(bool aLock) michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsINIParserImpl, michael@0: nsIINIParser) michael@0: michael@0: static bool michael@0: SectionCB(const char* aSection, void *aClosure) michael@0: { michael@0: nsTArray *strings = static_cast*>(aClosure); michael@0: michael@0: strings->AppendElement(nsDependentCString(aSection)); michael@0: return true; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsINIParserImpl::GetSections(nsIUTF8StringEnumerator* *aResult) michael@0: { michael@0: nsTArray *strings = new nsTArray; michael@0: if (!strings) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: nsresult rv = mParser.GetSections(SectionCB, strings); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings); michael@0: michael@0: if (NS_FAILED(rv)) michael@0: delete strings; michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: static bool michael@0: KeyCB(const char* aKey, const char *aValue, void *aClosure) michael@0: { michael@0: nsTArray *strings = static_cast*>(aClosure); michael@0: michael@0: strings->AppendElement(nsDependentCString(aKey)); michael@0: return true; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsINIParserImpl::GetKeys(const nsACString& aSection, michael@0: nsIUTF8StringEnumerator* *aResult) michael@0: { michael@0: nsTArray *strings = new nsTArray; michael@0: if (!strings) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: nsresult rv = mParser.GetStrings(PromiseFlatCString(aSection).get(), michael@0: KeyCB, strings); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings); michael@0: michael@0: if (NS_FAILED(rv)) michael@0: delete strings; michael@0: michael@0: return rv; michael@0: michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsINIParserImpl::GetString(const nsACString& aSection, michael@0: const nsACString& aKey, michael@0: nsACString& aResult) michael@0: { michael@0: return mParser.GetString(PromiseFlatCString(aSection).get(), michael@0: PromiseFlatCString(aKey).get(), michael@0: aResult); michael@0: }