Bumping manifests a=b2g-bump
[gecko.git] / xpcom / ds / nsINIParserImpl.cpp
blob3b078f0791995f6c77bfa41b9d4985abdaaf8a38
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "nsINIParserImpl.h"
7 #include "nsINIParser.h"
8 #include "nsStringEnumerator.h"
9 #include "nsTArray.h"
10 #include "mozilla/Attributes.h"
12 class nsINIParserImpl MOZ_FINAL
13 : public nsIINIParser
15 ~nsINIParserImpl() {}
17 public:
18 NS_DECL_ISUPPORTS
19 NS_DECL_NSIINIPARSER
21 nsresult Init(nsIFile* aINIFile) { return mParser.Init(aINIFile); }
23 private:
24 nsINIParser mParser;
27 NS_IMPL_ISUPPORTS(nsINIParserFactory,
28 nsIINIParserFactory,
29 nsIFactory)
31 NS_IMETHODIMP
32 nsINIParserFactory::CreateINIParser(nsIFile* aINIFile,
33 nsIINIParser** aResult)
35 *aResult = nullptr;
37 nsRefPtr<nsINIParserImpl> p(new nsINIParserImpl());
38 if (!p) {
39 return NS_ERROR_OUT_OF_MEMORY;
42 nsresult rv = p->Init(aINIFile);
44 if (NS_SUCCEEDED(rv)) {
45 NS_ADDREF(*aResult = p);
48 return rv;
51 NS_IMETHODIMP
52 nsINIParserFactory::CreateInstance(nsISupports* aOuter,
53 REFNSIID aIID,
54 void** aResult)
56 if (NS_WARN_IF(aOuter)) {
57 return NS_ERROR_NO_AGGREGATION;
60 // We are our own singleton.
61 return QueryInterface(aIID, aResult);
64 NS_IMETHODIMP
65 nsINIParserFactory::LockFactory(bool aLock)
67 return NS_OK;
70 NS_IMPL_ISUPPORTS(nsINIParserImpl,
71 nsIINIParser)
73 static bool
74 SectionCB(const char* aSection, void* aClosure)
76 nsTArray<nsCString>* strings = static_cast<nsTArray<nsCString>*>(aClosure);
77 strings->AppendElement(nsDependentCString(aSection));
78 return true;
81 NS_IMETHODIMP
82 nsINIParserImpl::GetSections(nsIUTF8StringEnumerator** aResult)
84 nsTArray<nsCString>* strings = new nsTArray<nsCString>;
85 if (!strings) {
86 return NS_ERROR_OUT_OF_MEMORY;
89 nsresult rv = mParser.GetSections(SectionCB, strings);
90 if (NS_SUCCEEDED(rv)) {
91 rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
94 if (NS_FAILED(rv)) {
95 delete strings;
98 return rv;
101 static bool
102 KeyCB(const char* aKey, const char* aValue, void* aClosure)
104 nsTArray<nsCString>* strings = static_cast<nsTArray<nsCString>*>(aClosure);
105 strings->AppendElement(nsDependentCString(aKey));
106 return true;
109 NS_IMETHODIMP
110 nsINIParserImpl::GetKeys(const nsACString& aSection,
111 nsIUTF8StringEnumerator** aResult)
113 nsTArray<nsCString>* strings = new nsTArray<nsCString>;
114 if (!strings) {
115 return NS_ERROR_OUT_OF_MEMORY;
118 nsresult rv = mParser.GetStrings(PromiseFlatCString(aSection).get(),
119 KeyCB, strings);
120 if (NS_SUCCEEDED(rv)) {
121 rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
124 if (NS_FAILED(rv)) {
125 delete strings;
128 return rv;
132 NS_IMETHODIMP
133 nsINIParserImpl::GetString(const nsACString& aSection,
134 const nsACString& aKey,
135 nsACString& aResult)
137 return mParser.GetString(PromiseFlatCString(aSection).get(),
138 PromiseFlatCString(aKey).get(),
139 aResult);