1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsINIParserImpl.h"
9 #include "nsINIParser.h"
10 #include "nsStringEnumerator.h"
12 #include "mozilla/Attributes.h"
14 class nsINIParserImpl final
: public nsIINIParser
, public nsIINIParserWriter
{
15 ~nsINIParserImpl() = default;
20 NS_DECL_NSIINIPARSERWRITER
22 nsresult
Init(nsIFile
* aINIFile
) { return mParser
.Init(aINIFile
); }
26 bool ContainsNull(const nsACString
& aStr
);
29 NS_IMPL_ISUPPORTS(nsINIParserFactory
, nsIINIParserFactory
)
32 nsINIParserFactory::CreateINIParser(nsIFile
* aINIFile
, nsIINIParser
** aResult
) {
35 RefPtr
<nsINIParserImpl
> p(new nsINIParserImpl());
38 nsresult rv
= p
->Init(aINIFile
);
48 NS_IMPL_ISUPPORTS(nsINIParserImpl
, nsIINIParser
, nsIINIParserWriter
)
50 bool nsINIParserImpl::ContainsNull(const nsACString
& aStr
) {
51 return aStr
.CountChar('\0') > 0;
54 static bool SectionCB(const char* aSection
, void* aClosure
) {
55 nsTArray
<nsCString
>* strings
= static_cast<nsTArray
<nsCString
>*>(aClosure
);
56 strings
->AppendElement()->Assign(aSection
);
61 nsINIParserImpl::GetSections(nsIUTF8StringEnumerator
** aResult
) {
62 nsTArray
<nsCString
>* strings
= new nsTArray
<nsCString
>;
64 nsresult rv
= mParser
.GetSections(SectionCB
, strings
);
65 if (NS_SUCCEEDED(rv
)) {
66 rv
= NS_NewAdoptingUTF8StringEnumerator(aResult
, strings
);
76 static bool KeyCB(const char* aKey
, const char* aValue
, void* aClosure
) {
77 nsTArray
<nsCString
>* strings
= static_cast<nsTArray
<nsCString
>*>(aClosure
);
78 strings
->AppendElement()->Assign(aKey
);
83 nsINIParserImpl::GetKeys(const nsACString
& aSection
,
84 nsIUTF8StringEnumerator
** aResult
) {
85 if (ContainsNull(aSection
)) {
86 return NS_ERROR_INVALID_ARG
;
89 nsTArray
<nsCString
>* strings
= new nsTArray
<nsCString
>;
92 mParser
.GetStrings(PromiseFlatCString(aSection
).get(), KeyCB
, strings
);
93 if (NS_SUCCEEDED(rv
)) {
94 rv
= NS_NewAdoptingUTF8StringEnumerator(aResult
, strings
);
105 nsINIParserImpl::GetString(const nsACString
& aSection
, const nsACString
& aKey
,
106 nsACString
& aResult
) {
107 if (ContainsNull(aSection
) || ContainsNull(aKey
)) {
108 return NS_ERROR_INVALID_ARG
;
111 return mParser
.GetString(PromiseFlatCString(aSection
).get(),
112 PromiseFlatCString(aKey
).get(), aResult
);
116 nsINIParserImpl::InitFromString(const nsACString
& aData
) {
117 return mParser
.InitFromString(nsCString(aData
));
121 nsINIParserImpl::SetString(const nsACString
& aSection
, const nsACString
& aKey
,
122 const nsACString
& aValue
) {
123 if (ContainsNull(aSection
) || ContainsNull(aKey
) || ContainsNull(aValue
)) {
124 return NS_ERROR_INVALID_ARG
;
127 return mParser
.SetString(PromiseFlatCString(aSection
).get(),
128 PromiseFlatCString(aKey
).get(),
129 PromiseFlatCString(aValue
).get());
133 nsINIParserImpl::WriteFile(nsIFile
* aINIFile
) {
134 return mParser
.WriteToFile(aINIFile
);
138 nsINIParserImpl::WriteToString(nsACString
& aOutput
) {
140 mParser
.WriteToString(aOutput
);