2015-04-09 20:25:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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 21:19:14 +04:00
|
|
|
|
|
|
|
#include "nsINIParserImpl.h"
|
|
|
|
|
|
|
|
#include "nsINIParser.h"
|
|
|
|
#include "nsStringEnumerator.h"
|
2009-01-22 07:15:34 +03:00
|
|
|
#include "nsTArray.h"
|
2012-06-06 03:51:58 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2005-10-04 21:19:14 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsINIParserImpl final
|
2014-07-09 19:15:21 +04:00
|
|
|
: public nsIINIParser
|
2008-02-21 23:39:20 +03:00
|
|
|
{
|
2014-07-01 02:11:53 +04:00
|
|
|
~nsINIParserImpl() {}
|
|
|
|
|
2008-02-21 23:39:20 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIINIPARSER
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
nsresult Init(nsIFile* aINIFile) { return mParser.Init(aINIFile); }
|
2008-02-21 23:39:20 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsINIParser mParser;
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsINIParserFactory,
|
|
|
|
nsIINIParserFactory,
|
|
|
|
nsIFactory)
|
2008-02-21 23:39:20 +03:00
|
|
|
|
2005-10-04 21:19:14 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-06 06:08:30 +04:00
|
|
|
nsINIParserFactory::CreateINIParser(nsIFile* aINIFile,
|
2014-07-09 19:15:21 +04:00
|
|
|
nsIINIParser** aResult)
|
2005-10-04 21:19:14 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aResult = nullptr;
|
2005-10-04 21:19:14 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsINIParserImpl> p(new nsINIParserImpl());
|
2014-07-09 19:15:21 +04:00
|
|
|
if (!p) {
|
2005-10-04 21:19:14 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2005-10-04 21:19:14 +04:00
|
|
|
|
|
|
|
nsresult rv = p->Init(aINIFile);
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2005-10-04 21:19:14 +04:00
|
|
|
NS_ADDREF(*aResult = p);
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2005-10-04 21:19:14 +04:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINIParserFactory::CreateInstance(nsISupports* aOuter,
|
|
|
|
REFNSIID aIID,
|
2014-07-09 19:15:21 +04:00
|
|
|
void** aResult)
|
2005-10-04 21:19:14 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(aOuter)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2005-10-04 21:19:14 +04:00
|
|
|
|
|
|
|
// We are our own singleton.
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsINIParserFactory::LockFactory(bool aLock)
|
2005-10-04 21:19:14 +04:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsINIParserImpl,
|
|
|
|
nsIINIParser)
|
2008-02-21 23:39:20 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2014-07-09 19:15:21 +04:00
|
|
|
SectionCB(const char* aSection, void* aClosure)
|
2005-10-04 21:19:14 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsTArray<nsCString>* strings = static_cast<nsTArray<nsCString>*>(aClosure);
|
2015-11-18 18:25:19 +03:00
|
|
|
strings->AppendElement()->Assign(aSection);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-10-04 21:19:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-09 19:15:21 +04:00
|
|
|
nsINIParserImpl::GetSections(nsIUTF8StringEnumerator** aResult)
|
2005-10-04 21:19:14 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsTArray<nsCString>* strings = new nsTArray<nsCString>;
|
|
|
|
if (!strings) {
|
2005-10-04 21:19:14 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2005-10-04 21:19:14 +04:00
|
|
|
|
|
|
|
nsresult rv = mParser.GetSections(SectionCB, strings);
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-08-18 19:52:23 +04:00
|
|
|
rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2005-10-04 21:19:14 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2005-10-04 21:19:14 +04:00
|
|
|
delete strings;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2005-10-04 21:19:14 +04:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2014-07-09 19:15:21 +04:00
|
|
|
KeyCB(const char* aKey, const char* aValue, void* aClosure)
|
2005-10-04 21:19:14 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsTArray<nsCString>* strings = static_cast<nsTArray<nsCString>*>(aClosure);
|
2015-11-18 18:25:19 +03:00
|
|
|
strings->AppendElement()->Assign(aKey);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-10-04 21:19:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINIParserImpl::GetKeys(const nsACString& aSection,
|
2014-07-09 19:15:21 +04:00
|
|
|
nsIUTF8StringEnumerator** aResult)
|
2005-10-04 21:19:14 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
nsTArray<nsCString>* strings = new nsTArray<nsCString>;
|
|
|
|
if (!strings) {
|
2005-10-04 21:19:14 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2005-10-04 21:19:14 +04:00
|
|
|
|
|
|
|
nsresult rv = mParser.GetStrings(PromiseFlatCString(aSection).get(),
|
|
|
|
KeyCB, strings);
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-08-18 19:52:23 +04:00
|
|
|
rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2005-10-04 21:19:14 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2005-10-04 21:19:14 +04:00
|
|
|
delete strings;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2005-10-04 21:19:14 +04: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);
|
|
|
|
}
|