Bug 1866894 - Update failing subtest for content-visibility-auto-resize.html. r=fredw
[gecko.git] / xpcom / ds / nsINIParserImpl.cpp
blob6ed52947d8aabfa027f207847a267ade8127d2d5
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"
11 #include "nsTArray.h"
12 #include "mozilla/Attributes.h"
14 class nsINIParserImpl final : public nsIINIParser, public nsIINIParserWriter {
15 ~nsINIParserImpl() = default;
17 public:
18 NS_DECL_ISUPPORTS
19 NS_DECL_NSIINIPARSER
20 NS_DECL_NSIINIPARSERWRITER
22 nsresult Init(nsIFile* aINIFile) { return mParser.Init(aINIFile); }
24 private:
25 nsINIParser mParser;
26 bool ContainsNull(const nsACString& aStr);
29 NS_IMPL_ISUPPORTS(nsINIParserFactory, nsIINIParserFactory)
31 NS_IMETHODIMP
32 nsINIParserFactory::CreateINIParser(nsIFile* aINIFile, nsIINIParser** aResult) {
33 *aResult = nullptr;
35 RefPtr<nsINIParserImpl> p(new nsINIParserImpl());
37 if (aINIFile) {
38 nsresult rv = p->Init(aINIFile);
39 if (NS_FAILED(rv)) {
40 return rv;
44 p.forget(aResult);
45 return NS_OK;
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);
57 return true;
60 NS_IMETHODIMP
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);
69 if (NS_FAILED(rv)) {
70 delete strings;
73 return rv;
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);
79 return true;
82 NS_IMETHODIMP
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>;
91 nsresult rv =
92 mParser.GetStrings(PromiseFlatCString(aSection).get(), KeyCB, strings);
93 if (NS_SUCCEEDED(rv)) {
94 rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
97 if (NS_FAILED(rv)) {
98 delete strings;
101 return rv;
104 NS_IMETHODIMP
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);
115 NS_IMETHODIMP
116 nsINIParserImpl::InitFromString(const nsACString& aData) {
117 return mParser.InitFromString(nsCString(aData));
120 NS_IMETHODIMP
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());
132 NS_IMETHODIMP
133 nsINIParserImpl::WriteFile(nsIFile* aINIFile) {
134 return mParser.WriteToFile(aINIFile);
137 NS_IMETHODIMP
138 nsINIParserImpl::WriteToString(nsACString& aOutput) {
139 aOutput.Truncate();
140 mParser.WriteToString(aOutput);
142 return NS_OK;