1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is Mozilla XPCOM.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Benjamin Smedberg <benjamin@smedbergs.us>
18 : *
19 : * Portions created by the Initial Developer are Copyright (C) 2005
20 : * the Mozilla Foundation <http://www.mozilla.org/>. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsINIParserImpl.h"
39 :
40 : #include "nsILocalFile.h"
41 :
42 : #include "nsINIParser.h"
43 : #include "nsStringEnumerator.h"
44 : #include "nsTArray.h"
45 :
46 : class nsINIParserImpl :
47 : public nsIINIParser
48 2426 : {
49 : public:
50 : NS_DECL_ISUPPORTS
51 : NS_DECL_NSIINIPARSER
52 :
53 1213 : nsresult Init(nsILocalFile* aINIFile) {
54 1213 : return mParser.Init(aINIFile);
55 : }
56 :
57 : private:
58 : nsINIParser mParser;
59 : };
60 :
61 15580 : NS_IMPL_ISUPPORTS2(nsINIParserFactory,
62 : nsIINIParserFactory,
63 : nsIFactory)
64 :
65 : NS_IMETHODIMP
66 1213 : nsINIParserFactory::CreateINIParser(nsILocalFile* aINIFile,
67 : nsIINIParser* *aResult)
68 : {
69 1213 : *aResult = nsnull;
70 :
71 2426 : nsCOMPtr<nsINIParserImpl> p(new nsINIParserImpl());
72 1213 : if (!p)
73 0 : return NS_ERROR_OUT_OF_MEMORY;
74 :
75 1213 : nsresult rv = p->Init(aINIFile);
76 :
77 1213 : if (NS_SUCCEEDED(rv))
78 1213 : NS_ADDREF(*aResult = p);
79 :
80 1213 : return rv;
81 : }
82 :
83 : NS_IMETHODIMP
84 147 : nsINIParserFactory::CreateInstance(nsISupports* aOuter,
85 : REFNSIID aIID,
86 : void **aResult)
87 : {
88 147 : NS_ENSURE_NO_AGGREGATION(aOuter);
89 :
90 : // We are our own singleton.
91 147 : return QueryInterface(aIID, aResult);
92 : }
93 :
94 : NS_IMETHODIMP
95 0 : nsINIParserFactory::LockFactory(bool aLock)
96 : {
97 0 : return NS_OK;
98 : }
99 :
100 25120 : NS_IMPL_ISUPPORTS1(nsINIParserImpl,
101 : nsIINIParser)
102 :
103 : static bool
104 31 : SectionCB(const char* aSection, void *aClosure)
105 : {
106 31 : nsTArray<nsCString> *strings = static_cast<nsTArray<nsCString>*>(aClosure);
107 :
108 31 : strings->AppendElement(nsDependentCString(aSection));
109 31 : return true;
110 : }
111 :
112 : NS_IMETHODIMP
113 27 : nsINIParserImpl::GetSections(nsIUTF8StringEnumerator* *aResult)
114 : {
115 27 : nsTArray<nsCString> *strings = new nsTArray<nsCString>;
116 27 : if (!strings)
117 0 : return NS_ERROR_OUT_OF_MEMORY;
118 :
119 27 : nsresult rv = mParser.GetSections(SectionCB, strings);
120 27 : if (NS_SUCCEEDED(rv))
121 27 : rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
122 :
123 27 : if (NS_FAILED(rv))
124 0 : delete strings;
125 :
126 27 : return rv;
127 : }
128 :
129 : static bool
130 2818 : KeyCB(const char* aKey, const char *aValue, void *aClosure)
131 : {
132 2818 : nsTArray<nsCString> *strings = static_cast<nsTArray<nsCString>*>(aClosure);
133 :
134 2818 : strings->AppendElement(nsDependentCString(aKey));
135 2818 : return true;
136 : }
137 :
138 : NS_IMETHODIMP
139 2389 : nsINIParserImpl::GetKeys(const nsACString& aSection,
140 : nsIUTF8StringEnumerator* *aResult)
141 : {
142 2389 : nsTArray<nsCString> *strings = new nsTArray<nsCString>;
143 2389 : if (!strings)
144 0 : return NS_ERROR_OUT_OF_MEMORY;
145 :
146 2389 : nsresult rv = mParser.GetStrings(PromiseFlatCString(aSection).get(),
147 2389 : KeyCB, strings);
148 2389 : if (NS_SUCCEEDED(rv))
149 2389 : rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
150 :
151 2389 : if (NS_FAILED(rv))
152 0 : delete strings;
153 :
154 2389 : return rv;
155 :
156 : }
157 :
158 : NS_IMETHODIMP
159 2818 : nsINIParserImpl::GetString(const nsACString& aSection,
160 : const nsACString& aKey,
161 : nsACString& aResult)
162 : {
163 2818 : return mParser.GetString(PromiseFlatCString(aSection).get(),
164 2818 : PromiseFlatCString(aKey).get(),
165 2818 : aResult);
166 : }
|