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"
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2005-10-04 21:19:14 +04:00
|
|
|
|
|
|
|
#include "nsINIParser.h"
|
|
|
|
#include "nsStringEnumerator.h"
|
2009-01-22 07:15:34 +03:00
|
|
|
#include "nsTArray.h"
|
2005-10-04 21:19:14 +04:00
|
|
|
|
2008-02-21 23:39:20 +03:00
|
|
|
class nsINIParserImpl :
|
|
|
|
public nsIINIParser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIINIPARSER
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
nsresult Init(nsIFile* aINIFile) {
|
2008-02-21 23:39:20 +03:00
|
|
|
return mParser.Init(aINIFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsINIParser mParser;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(nsINIParserFactory,
|
|
|
|
nsIINIParserFactory,
|
|
|
|
nsIFactory)
|
|
|
|
|
2005-10-04 21:19:14 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-06 06:08:30 +04:00
|
|
|
nsINIParserFactory::CreateINIParser(nsIFile* aINIFile,
|
2005-10-04 21:19:14 +04:00
|
|
|
nsIINIParser* *aResult)
|
|
|
|
{
|
|
|
|
*aResult = nsnull;
|
|
|
|
|
|
|
|
nsCOMPtr<nsINIParserImpl> p(new nsINIParserImpl());
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
NS_ENSURE_NO_AGGREGATION(aOuter);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2008-02-21 23:39:20 +03:00
|
|
|
NS_IMPL_ISUPPORTS1(nsINIParserImpl,
|
|
|
|
nsIINIParser)
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2005-10-04 21:19:14 +04:00
|
|
|
SectionCB(const char* aSection, void *aClosure)
|
|
|
|
{
|
2009-01-22 07:15:34 +03:00
|
|
|
nsTArray<nsCString> *strings = static_cast<nsTArray<nsCString>*>(aClosure);
|
2005-10-04 21:19:14 +04:00
|
|
|
|
2009-01-22 07:15:34 +03:00
|
|
|
strings->AppendElement(nsDependentCString(aSection));
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-10-04 21:19:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINIParserImpl::GetSections(nsIUTF8StringEnumerator* *aResult)
|
|
|
|
{
|
2009-01-22 07:15:34 +03:00
|
|
|
nsTArray<nsCString> *strings = new nsTArray<nsCString>;
|
2005-10-04 21:19:14 +04:00
|
|
|
if (!strings)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv = mParser.GetSections(SectionCB, strings);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2009-08-18 19:52:23 +04:00
|
|
|
rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
|
2005-10-04 21:19:14 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
delete strings;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2005-10-04 21:19:14 +04:00
|
|
|
KeyCB(const char* aKey, const char *aValue, void *aClosure)
|
|
|
|
{
|
2009-01-22 07:15:34 +03:00
|
|
|
nsTArray<nsCString> *strings = static_cast<nsTArray<nsCString>*>(aClosure);
|
2005-10-04 21:19:14 +04:00
|
|
|
|
2009-01-22 07:15:34 +03:00
|
|
|
strings->AppendElement(nsDependentCString(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,
|
|
|
|
nsIUTF8StringEnumerator* *aResult)
|
|
|
|
{
|
2009-01-22 07:15:34 +03:00
|
|
|
nsTArray<nsCString> *strings = new nsTArray<nsCString>;
|
2005-10-04 21:19:14 +04: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 19:52:23 +04:00
|
|
|
rv = NS_NewAdoptingUTF8StringEnumerator(aResult, strings);
|
2005-10-04 21:19:14 +04: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);
|
|
|
|
}
|