1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/ds/nsINIParserImpl.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,133 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "nsINIParserImpl.h" 1.9 + 1.10 +#include "nsINIParser.h" 1.11 +#include "nsStringEnumerator.h" 1.12 +#include "nsTArray.h" 1.13 +#include "mozilla/Attributes.h" 1.14 + 1.15 +class nsINIParserImpl MOZ_FINAL : 1.16 + public nsIINIParser 1.17 +{ 1.18 +public: 1.19 + NS_DECL_ISUPPORTS 1.20 + NS_DECL_NSIINIPARSER 1.21 + 1.22 + nsresult Init(nsIFile* aINIFile) { 1.23 + return mParser.Init(aINIFile); 1.24 + } 1.25 + 1.26 +private: 1.27 + nsINIParser mParser; 1.28 +}; 1.29 + 1.30 +NS_IMPL_ISUPPORTS(nsINIParserFactory, 1.31 + nsIINIParserFactory, 1.32 + nsIFactory) 1.33 + 1.34 +NS_IMETHODIMP 1.35 +nsINIParserFactory::CreateINIParser(nsIFile* aINIFile, 1.36 + nsIINIParser* *aResult) 1.37 +{ 1.38 + *aResult = nullptr; 1.39 + 1.40 + nsRefPtr<nsINIParserImpl> p(new nsINIParserImpl()); 1.41 + if (!p) 1.42 + return NS_ERROR_OUT_OF_MEMORY; 1.43 + 1.44 + nsresult rv = p->Init(aINIFile); 1.45 + 1.46 + if (NS_SUCCEEDED(rv)) 1.47 + NS_ADDREF(*aResult = p); 1.48 + 1.49 + return rv; 1.50 +} 1.51 + 1.52 +NS_IMETHODIMP 1.53 +nsINIParserFactory::CreateInstance(nsISupports* aOuter, 1.54 + REFNSIID aIID, 1.55 + void **aResult) 1.56 +{ 1.57 + if (NS_WARN_IF(aOuter)) 1.58 + return NS_ERROR_NO_AGGREGATION; 1.59 + 1.60 + // We are our own singleton. 1.61 + return QueryInterface(aIID, aResult); 1.62 +} 1.63 + 1.64 +NS_IMETHODIMP 1.65 +nsINIParserFactory::LockFactory(bool aLock) 1.66 +{ 1.67 + return NS_OK; 1.68 +} 1.69 + 1.70 +NS_IMPL_ISUPPORTS(nsINIParserImpl, 1.71 + nsIINIParser) 1.72 + 1.73 +static bool 1.74 +SectionCB(const char* aSection, void *aClosure) 1.75 +{ 1.76 + nsTArray<nsCString> *strings = static_cast<nsTArray<nsCString>*>(aClosure); 1.77 + 1.78 + strings->AppendElement(nsDependentCString(aSection)); 1.79 + return true; 1.80 +} 1.81 + 1.82 +NS_IMETHODIMP 1.83 +nsINIParserImpl::GetSections(nsIUTF8StringEnumerator* *aResult) 1.84 +{ 1.85 + nsTArray<nsCString> *strings = new nsTArray<nsCString>; 1.86 + if (!strings) 1.87 + return NS_ERROR_OUT_OF_MEMORY; 1.88 + 1.89 + nsresult rv = mParser.GetSections(SectionCB, strings); 1.90 + if (NS_SUCCEEDED(rv)) 1.91 + rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings); 1.92 + 1.93 + if (NS_FAILED(rv)) 1.94 + delete strings; 1.95 + 1.96 + return rv; 1.97 +} 1.98 + 1.99 +static bool 1.100 +KeyCB(const char* aKey, const char *aValue, void *aClosure) 1.101 +{ 1.102 + nsTArray<nsCString> *strings = static_cast<nsTArray<nsCString>*>(aClosure); 1.103 + 1.104 + strings->AppendElement(nsDependentCString(aKey)); 1.105 + return true; 1.106 +} 1.107 + 1.108 +NS_IMETHODIMP 1.109 +nsINIParserImpl::GetKeys(const nsACString& aSection, 1.110 + nsIUTF8StringEnumerator* *aResult) 1.111 +{ 1.112 + nsTArray<nsCString> *strings = new nsTArray<nsCString>; 1.113 + if (!strings) 1.114 + return NS_ERROR_OUT_OF_MEMORY; 1.115 + 1.116 + nsresult rv = mParser.GetStrings(PromiseFlatCString(aSection).get(), 1.117 + KeyCB, strings); 1.118 + if (NS_SUCCEEDED(rv)) 1.119 + rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings); 1.120 + 1.121 + if (NS_FAILED(rv)) 1.122 + delete strings; 1.123 + 1.124 + return rv; 1.125 + 1.126 +} 1.127 + 1.128 +NS_IMETHODIMP 1.129 +nsINIParserImpl::GetString(const nsACString& aSection, 1.130 + const nsACString& aKey, 1.131 + nsACString& aResult) 1.132 +{ 1.133 + return mParser.GetString(PromiseFlatCString(aSection).get(), 1.134 + PromiseFlatCString(aKey).get(), 1.135 + aResult); 1.136 +}