2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsINIParserImpl.h"
|
|
|
|
|
|
|
|
#include "nsINIParser.h"
|
|
|
|
#include "nsStringEnumerator.h"
|
2009-01-21 20:15:34 -08:00
|
|
|
#include "nsTArray.h"
|
2012-06-05 16:51:58 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-05 16:51:58 -07:00
|
|
|
class nsINIParserImpl MOZ_FINAL :
|
2008-02-21 12:39:20 -08:00
|
|
|
public nsIINIParser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIINIPARSER
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
nsresult Init(nsIFile* aINIFile) {
|
2008-02-21 12:39:20 -08:00
|
|
|
return mParser.Init(aINIFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsINIParser mParser;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(nsINIParserFactory,
|
|
|
|
nsIINIParserFactory,
|
|
|
|
nsIFactory)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2012-06-05 19:08:30 -07:00
|
|
|
nsINIParserFactory::CreateINIParser(nsIFile* aINIFile,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIINIParser* *aResult)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aResult = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-17 17:23:03 -07:00
|
|
|
nsRefPtr<nsINIParserImpl> p(new nsINIParserImpl());
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!p)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv = p->Init(aINIFile);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*aResult = p);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINIParserFactory::CreateInstance(nsISupports* aOuter,
|
|
|
|
REFNSIID aIID,
|
|
|
|
void **aResult)
|
|
|
|
{
|
2013-11-19 13:27:37 -08:00
|
|
|
if (NS_WARN_IF(aOuter))
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// We are our own singleton.
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsINIParserFactory::LockFactory(bool aLock)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-02-21 12:39:20 -08:00
|
|
|
NS_IMPL_ISUPPORTS1(nsINIParserImpl,
|
|
|
|
nsIINIParser)
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
SectionCB(const char* aSection, void *aClosure)
|
|
|
|
{
|
2009-01-21 20:15:34 -08:00
|
|
|
nsTArray<nsCString> *strings = static_cast<nsTArray<nsCString>*>(aClosure);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-01-21 20:15:34 -08:00
|
|
|
strings->AppendElement(nsDependentCString(aSection));
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINIParserImpl::GetSections(nsIUTF8StringEnumerator* *aResult)
|
|
|
|
{
|
2009-01-21 20:15:34 -08:00
|
|
|
nsTArray<nsCString> *strings = new nsTArray<nsCString>;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!strings)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv = mParser.GetSections(SectionCB, strings);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2009-08-18 08:52:23 -07:00
|
|
|
rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
delete strings;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
KeyCB(const char* aKey, const char *aValue, void *aClosure)
|
|
|
|
{
|
2009-01-21 20:15:34 -08:00
|
|
|
nsTArray<nsCString> *strings = static_cast<nsTArray<nsCString>*>(aClosure);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-01-21 20:15:34 -08:00
|
|
|
strings->AppendElement(nsDependentCString(aKey));
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINIParserImpl::GetKeys(const nsACString& aSection,
|
|
|
|
nsIUTF8StringEnumerator* *aResult)
|
|
|
|
{
|
2009-01-21 20:15:34 -08:00
|
|
|
nsTArray<nsCString> *strings = new nsTArray<nsCString>;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!strings)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv = mParser.GetStrings(PromiseFlatCString(aSection).get(),
|
|
|
|
KeyCB, strings);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2009-08-18 08:52:23 -07:00
|
|
|
rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
delete strings;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINIParserImpl::GetString(const nsACString& aSection,
|
|
|
|
const nsACString& aKey,
|
|
|
|
nsACString& aResult)
|
|
|
|
{
|
|
|
|
return mParser.GetString(PromiseFlatCString(aSection).get(),
|
|
|
|
PromiseFlatCString(aKey).get(),
|
|
|
|
aResult);
|
|
|
|
}
|