xpcom/ds/nsINIParserImpl.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial