2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-10-04 17:19:14 +00:00
|
|
|
|
|
|
|
#include "nsINIParserImpl.h"
|
|
|
|
|
|
|
|
#include "nsINIParser.h"
|
|
|
|
#include "nsStringEnumerator.h"
|
2009-01-22 04:15:34 +00:00
|
|
|
#include "nsTArray.h"
|
2012-06-05 23:51:58 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2005-10-04 17:19:14 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
class nsINIParserImpl MOZ_FINAL
|
|
|
|
: public nsIINIParser
|
2008-02-21 20:39:20 +00:00
|
|
|
{
|
2014-06-30 22:11:53 +00:00
|
|
|
~nsINIParserImpl() {}
|
|
|
|
|
2008-02-21 20:39:20 +00:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIINIPARSER
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
nsresult Init(nsIFile* aINIFile) { return mParser.Init(aINIFile); }
|
2008-02-21 20:39:20 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsINIParser mParser;
|
|
|
|
};
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsINIParserFactory,
|
|
|
|
nsIINIParserFactory,
|
|
|
|
nsIFactory)
|
2008-02-21 20:39:20 +00:00
|
|
|
|
2005-10-04 17:19:14 +00:00
|
|
|
NS_IMETHODIMP
|
2012-06-06 02:08:30 +00:00
|
|
|
nsINIParserFactory::CreateINIParser(nsIFile* aINIFile,
|
2014-07-09 15:15:21 +00:00
|
|
|
nsIINIParser** aResult)
|
2005-10-04 17:19:14 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
*aResult = nullptr;
|
2005-10-04 17:19:14 +00:00
|
|
|
|
2014-03-18 00:23:03 +00:00
|
|
|
nsRefPtr<nsINIParserImpl> p(new nsINIParserImpl());
|
2014-07-09 15:15:21 +00:00
|
|
|
if (!p) {
|
2005-10-04 17:19:14 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2005-10-04 17:19:14 +00:00
|
|
|
|
|
|
|
nsresult rv = p->Init(aINIFile);
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2005-10-04 17:19:14 +00:00
|
|
|
NS_ADDREF(*aResult = p);
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2005-10-04 17:19:14 +00:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINIParserFactory::CreateInstance(nsISupports* aOuter,
|
|
|
|
REFNSIID aIID,
|
2014-07-09 15:15:21 +00:00
|
|
|
void** aResult)
|
2005-10-04 17:19:14 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
if (NS_WARN_IF(aOuter)) {
|
2013-11-19 21:27:37 +00:00
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2005-10-04 17:19:14 +00:00
|
|
|
|
|
|
|
// We are our own singleton.
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsINIParserFactory::LockFactory(bool aLock)
|
2005-10-04 17:19:14 +00:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsINIParserImpl,
|
|
|
|
nsIINIParser)
|
2008-02-21 20:39:20 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool
|
2014-07-09 15:15:21 +00:00
|
|
|
SectionCB(const char* aSection, void* aClosure)
|
2005-10-04 17:19:14 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
nsTArray<nsCString>* strings = static_cast<nsTArray<nsCString>*>(aClosure);
|
2009-01-22 04:15:34 +00:00
|
|
|
strings->AppendElement(nsDependentCString(aSection));
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2005-10-04 17:19:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 15:15:21 +00:00
|
|
|
nsINIParserImpl::GetSections(nsIUTF8StringEnumerator** aResult)
|
2005-10-04 17:19:14 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
nsTArray<nsCString>* strings = new nsTArray<nsCString>;
|
|
|
|
if (!strings) {
|
2005-10-04 17:19:14 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2005-10-04 17:19:14 +00:00
|
|
|
|
|
|
|
nsresult rv = mParser.GetSections(SectionCB, strings);
|
2014-07-09 15:15:21 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-08-18 15:52:23 +00:00
|
|
|
rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2005-10-04 17:19:14 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2005-10-04 17:19:14 +00:00
|
|
|
delete strings;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2005-10-04 17:19:14 +00:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool
|
2014-07-09 15:15:21 +00:00
|
|
|
KeyCB(const char* aKey, const char* aValue, void* aClosure)
|
2005-10-04 17:19:14 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
nsTArray<nsCString>* strings = static_cast<nsTArray<nsCString>*>(aClosure);
|
2009-01-22 04:15:34 +00:00
|
|
|
strings->AppendElement(nsDependentCString(aKey));
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2005-10-04 17:19:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINIParserImpl::GetKeys(const nsACString& aSection,
|
2014-07-09 15:15:21 +00:00
|
|
|
nsIUTF8StringEnumerator** aResult)
|
2005-10-04 17:19:14 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
nsTArray<nsCString>* strings = new nsTArray<nsCString>;
|
|
|
|
if (!strings) {
|
2005-10-04 17:19:14 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2005-10-04 17:19:14 +00:00
|
|
|
|
|
|
|
nsresult rv = mParser.GetStrings(PromiseFlatCString(aSection).get(),
|
|
|
|
KeyCB, strings);
|
2014-07-09 15:15:21 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-08-18 15:52:23 +00:00
|
|
|
rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2005-10-04 17:19:14 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2005-10-04 17:19:14 +00:00
|
|
|
delete strings;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2005-10-04 17:19:14 +00:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINIParserImpl::GetString(const nsACString& aSection,
|
|
|
|
const nsACString& aKey,
|
|
|
|
nsACString& aResult)
|
|
|
|
{
|
|
|
|
return mParser.GetString(PromiseFlatCString(aSection).get(),
|
|
|
|
PromiseFlatCString(aKey).get(),
|
|
|
|
aResult);
|
|
|
|
}
|