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
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
class nsINIParserImpl MOZ_FINAL
|
|
|
|
: public nsIINIParser
|
2008-02-21 12:39:20 -08:00
|
|
|
{
|
2014-06-30 15:11:53 -07:00
|
|
|
~nsINIParserImpl() {}
|
|
|
|
|
2008-02-21 12:39:20 -08:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIINIPARSER
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
nsresult Init(nsIFile* aINIFile) { return mParser.Init(aINIFile); }
|
2008-02-21 12:39:20 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsINIParser mParser;
|
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsINIParserFactory,
|
|
|
|
nsIINIParserFactory,
|
|
|
|
nsIFactory)
|
2008-02-21 12:39:20 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2012-06-05 19:08:30 -07:00
|
|
|
nsINIParserFactory::CreateINIParser(nsIFile* aINIFile,
|
2014-07-09 08:15:21 -07:00
|
|
|
nsIINIParser** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
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());
|
2014-07-09 08:15:21 -07:00
|
|
|
if (!p) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv = p->Init(aINIFile);
|
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ADDREF(*aResult = p);
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINIParserFactory::CreateInstance(nsISupports* aOuter,
|
|
|
|
REFNSIID aIID,
|
2014-07-09 08:15:21 -07:00
|
|
|
void** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_WARN_IF(aOuter)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsINIParserImpl,
|
|
|
|
nsIINIParser)
|
2008-02-21 12:39:20 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2014-07-09 08:15:21 -07:00
|
|
|
SectionCB(const char* aSection, void* aClosure)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
nsTArray<nsCString>* strings = static_cast<nsTArray<nsCString>*>(aClosure);
|
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
|
2014-07-09 08:15:21 -07:00
|
|
|
nsINIParserImpl::GetSections(nsIUTF8StringEnumerator** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
nsTArray<nsCString>* strings = new nsTArray<nsCString>;
|
|
|
|
if (!strings) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv = mParser.GetSections(SectionCB, strings);
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-08-18 08:52:23 -07:00
|
|
|
rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
delete strings;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2014-07-09 08:15:21 -07:00
|
|
|
KeyCB(const char* aKey, const char* aValue, void* aClosure)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
nsTArray<nsCString>* strings = static_cast<nsTArray<nsCString>*>(aClosure);
|
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,
|
2014-07-09 08:15:21 -07:00
|
|
|
nsIUTF8StringEnumerator** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-07-09 08:15:21 -07:00
|
|
|
nsTArray<nsCString>* strings = new nsTArray<nsCString>;
|
|
|
|
if (!strings) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv = mParser.GetStrings(PromiseFlatCString(aSection).get(),
|
|
|
|
KeyCB, strings);
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-08-18 08:52:23 -07:00
|
|
|
rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-09 08:15:21 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
delete strings;
|
2014-07-09 08:15:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07: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);
|
|
|
|
}
|