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 : public nsIINIParser, public nsIINIParserWriter {
|
2014-07-01 02:11:53 +04:00
|
|
|
~nsINIParserImpl() {}
|
|
|
|
|
2008-02-21 23:39:20 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIINIPARSER
|
2017-10-13 00:20:57 +03:00
|
|
|
NS_DECL_NSIINIPARSERWRITER
|
2008-02-21 23:39:20 +03:00
|
|
|
|
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;
|
2017-10-13 00:20:57 +03:00
|
|
|
bool ContainsNull(const nsACString& aStr);
|
2008-02-21 23:39:20 +03:00
|
|
|
};
|
|
|
|
|
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, nsIINIParser** aResult) {
|
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
|
|
|
|
2017-10-13 00:20:57 +03:00
|
|
|
if (aINIFile) {
|
|
|
|
nsresult rv = p->Init(aINIFile);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2005-10-04 21:19:14 +04:00
|
|
|
|
2017-10-13 00:20:57 +03:00
|
|
|
p.forget(aResult);
|
|
|
|
return NS_OK;
|
2005-10-04 21:19:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINIParserFactory::CreateInstance(nsISupports* aOuter, REFNSIID aIID,
|
2014-07-09 19:15:21 +04:00
|
|
|
void** aResult) {
|
|
|
|
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) { return NS_OK; }
|
2005-10-04 21:19:14 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsINIParserImpl, nsIINIParser, nsIINIParserWriter)
|
2017-10-13 00:20:57 +03:00
|
|
|
|
|
|
|
bool nsINIParserImpl::ContainsNull(const nsACString& aStr) {
|
|
|
|
return aStr.CountChar('\0') > 0;
|
|
|
|
}
|
2008-02-21 23:39:20 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
static bool SectionCB(const char* aSection, void* aClosure) {
|
|
|
|
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) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
static bool KeyCB(const char* aKey, const char* aValue, void* aClosure) {
|
|
|
|
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) {
|
2017-10-13 00:20:57 +03:00
|
|
|
if (ContainsNull(aSection)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-10-13 00:20:57 +03:00
|
|
|
if (ContainsNull(aSection) || ContainsNull(aKey)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2005-10-04 21:19:14 +04:00
|
|
|
return mParser.GetString(PromiseFlatCString(aSection).get(),
|
|
|
|
PromiseFlatCString(aKey).get(), aResult);
|
|
|
|
}
|
2017-10-13 00:20:57 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINIParserImpl::SetString(const nsACString& aSection, const nsACString& aKey,
|
|
|
|
const nsACString& aValue) {
|
|
|
|
if (ContainsNull(aSection) || ContainsNull(aKey) || ContainsNull(aValue)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mParser.SetString(PromiseFlatCString(aSection).get(),
|
|
|
|
PromiseFlatCString(aKey).get(),
|
|
|
|
PromiseFlatCString(aValue).get());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsINIParserImpl::WriteFile(nsIFile* aINIFile) {
|
|
|
|
return mParser.WriteToFile(aINIFile);
|
|
|
|
}
|