xpcom/ds/nsINIParserImpl.cpp

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

mercurial