Line data Source code
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/. */
6 :
7 : #include "nsINIParserImpl.h"
8 :
9 : #include "nsINIParser.h"
10 : #include "nsStringEnumerator.h"
11 : #include "nsTArray.h"
12 : #include "mozilla/Attributes.h"
13 :
14 0 : class nsINIParserImpl final
15 : : public nsIINIParser
16 : {
17 0 : ~nsINIParserImpl() {}
18 :
19 : public:
20 : NS_DECL_ISUPPORTS
21 : NS_DECL_NSIINIPARSER
22 :
23 0 : nsresult Init(nsIFile* aINIFile) { return mParser.Init(aINIFile); }
24 :
25 : private:
26 : nsINIParser mParser;
27 : };
28 :
29 0 : NS_IMPL_ISUPPORTS(nsINIParserFactory,
30 : nsIINIParserFactory,
31 : nsIFactory)
32 :
33 : NS_IMETHODIMP
34 0 : nsINIParserFactory::CreateINIParser(nsIFile* aINIFile,
35 : nsIINIParser** aResult)
36 : {
37 0 : *aResult = nullptr;
38 :
39 0 : RefPtr<nsINIParserImpl> p(new nsINIParserImpl());
40 0 : if (!p) {
41 0 : return NS_ERROR_OUT_OF_MEMORY;
42 : }
43 :
44 0 : nsresult rv = p->Init(aINIFile);
45 :
46 0 : if (NS_SUCCEEDED(rv)) {
47 0 : NS_ADDREF(*aResult = p);
48 : }
49 :
50 0 : return rv;
51 : }
52 :
53 : NS_IMETHODIMP
54 0 : nsINIParserFactory::CreateInstance(nsISupports* aOuter,
55 : REFNSIID aIID,
56 : void** aResult)
57 : {
58 0 : if (NS_WARN_IF(aOuter)) {
59 0 : return NS_ERROR_NO_AGGREGATION;
60 : }
61 :
62 : // We are our own singleton.
63 0 : return QueryInterface(aIID, aResult);
64 : }
65 :
66 : NS_IMETHODIMP
67 0 : nsINIParserFactory::LockFactory(bool aLock)
68 : {
69 0 : return NS_OK;
70 : }
71 :
72 0 : NS_IMPL_ISUPPORTS(nsINIParserImpl,
73 : nsIINIParser)
74 :
75 : static bool
76 0 : SectionCB(const char* aSection, void* aClosure)
77 : {
78 0 : nsTArray<nsCString>* strings = static_cast<nsTArray<nsCString>*>(aClosure);
79 0 : strings->AppendElement()->Assign(aSection);
80 0 : return true;
81 : }
82 :
83 : NS_IMETHODIMP
84 0 : nsINIParserImpl::GetSections(nsIUTF8StringEnumerator** aResult)
85 : {
86 0 : nsTArray<nsCString>* strings = new nsTArray<nsCString>;
87 0 : if (!strings) {
88 0 : return NS_ERROR_OUT_OF_MEMORY;
89 : }
90 :
91 0 : nsresult rv = mParser.GetSections(SectionCB, strings);
92 0 : if (NS_SUCCEEDED(rv)) {
93 0 : rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
94 : }
95 :
96 0 : if (NS_FAILED(rv)) {
97 0 : delete strings;
98 : }
99 :
100 0 : return rv;
101 : }
102 :
103 : static bool
104 0 : KeyCB(const char* aKey, const char* aValue, void* aClosure)
105 : {
106 0 : nsTArray<nsCString>* strings = static_cast<nsTArray<nsCString>*>(aClosure);
107 0 : strings->AppendElement()->Assign(aKey);
108 0 : return true;
109 : }
110 :
111 : NS_IMETHODIMP
112 0 : nsINIParserImpl::GetKeys(const nsACString& aSection,
113 : nsIUTF8StringEnumerator** aResult)
114 : {
115 0 : nsTArray<nsCString>* strings = new nsTArray<nsCString>;
116 0 : if (!strings) {
117 0 : return NS_ERROR_OUT_OF_MEMORY;
118 : }
119 :
120 0 : nsresult rv = mParser.GetStrings(PromiseFlatCString(aSection).get(),
121 0 : KeyCB, strings);
122 0 : if (NS_SUCCEEDED(rv)) {
123 0 : rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
124 : }
125 :
126 0 : if (NS_FAILED(rv)) {
127 0 : delete strings;
128 : }
129 :
130 0 : return rv;
131 :
132 : }
133 :
134 : NS_IMETHODIMP
135 0 : nsINIParserImpl::GetString(const nsACString& aSection,
136 : const nsACString& aKey,
137 : nsACString& aResult)
138 : {
139 0 : return mParser.GetString(PromiseFlatCString(aSection).get(),
140 0 : PromiseFlatCString(aKey).get(),
141 0 : aResult);
142 : }
|