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"
10 #include "mozilla/Attributes.h"
12 class nsINIParserImpl MOZ_FINAL
21 nsresult
Init(nsIFile
* aINIFile
) { return mParser
.Init(aINIFile
); }
27 NS_IMPL_ISUPPORTS(nsINIParserFactory
,
32 nsINIParserFactory::CreateINIParser(nsIFile
* aINIFile
,
33 nsIINIParser
** aResult
)
37 nsRefPtr
<nsINIParserImpl
> p(new nsINIParserImpl());
39 return NS_ERROR_OUT_OF_MEMORY
;
42 nsresult rv
= p
->Init(aINIFile
);
44 if (NS_SUCCEEDED(rv
)) {
45 NS_ADDREF(*aResult
= p
);
52 nsINIParserFactory::CreateInstance(nsISupports
* aOuter
,
56 if (NS_WARN_IF(aOuter
)) {
57 return NS_ERROR_NO_AGGREGATION
;
60 // We are our own singleton.
61 return QueryInterface(aIID
, aResult
);
65 nsINIParserFactory::LockFactory(bool aLock
)
70 NS_IMPL_ISUPPORTS(nsINIParserImpl
,
74 SectionCB(const char* aSection
, void* aClosure
)
76 nsTArray
<nsCString
>* strings
= static_cast<nsTArray
<nsCString
>*>(aClosure
);
77 strings
->AppendElement(nsDependentCString(aSection
));
82 nsINIParserImpl::GetSections(nsIUTF8StringEnumerator
** aResult
)
84 nsTArray
<nsCString
>* strings
= new nsTArray
<nsCString
>;
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
);
102 KeyCB(const char* aKey
, const char* aValue
, void* aClosure
)
104 nsTArray
<nsCString
>* strings
= static_cast<nsTArray
<nsCString
>*>(aClosure
);
105 strings
->AppendElement(nsDependentCString(aKey
));
110 nsINIParserImpl::GetKeys(const nsACString
& aSection
,
111 nsIUTF8StringEnumerator
** aResult
)
113 nsTArray
<nsCString
>* strings
= new nsTArray
<nsCString
>;
115 return NS_ERROR_OUT_OF_MEMORY
;
118 nsresult rv
= mParser
.GetStrings(PromiseFlatCString(aSection
).get(),
120 if (NS_SUCCEEDED(rv
)) {
121 rv
= NS_NewAdoptingUTF8StringEnumerator(aResult
, strings
);
133 nsINIParserImpl::GetString(const nsACString
& aSection
,
134 const nsACString
& aKey
,
137 return mParser
.GetString(PromiseFlatCString(aSection
).get(),
138 PromiseFlatCString(aKey
).get(),