2014-06-30 19:39:45 +04: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-08-15 22:29:55 +04:00
|
|
|
|
2012-05-23 21:09:10 +04:00
|
|
|
// Moz headers (alphabetical)
|
|
|
|
#include "nsCRTGlue.h"
|
2005-08-15 22:29:55 +04:00
|
|
|
#include "nsError.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2012-05-23 21:09:10 +04:00
|
|
|
#include "nsINIParser.h"
|
2017-09-01 03:02:10 +03:00
|
|
|
#include "mozilla/ResultExtensions.h"
|
|
|
|
#include "mozilla/URLPreloader.h"
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2015-10-31 20:47:14 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2005-08-15 22:29:55 +04:00
|
|
|
nsresult
|
2012-06-06 06:08:30 +04:00
|
|
|
nsINIParser::Init(nsIFile* aFile)
|
2005-08-15 22:29:55 +04:00
|
|
|
{
|
2017-09-01 03:02:10 +03:00
|
|
|
nsCString result;
|
|
|
|
MOZ_TRY_VAR(result, URLPreloader::ReadFile(aFile));
|
2007-05-25 19:05:11 +04:00
|
|
|
|
2017-09-01 03:02:10 +03:00
|
|
|
return InitFromString(result);
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char kNL[] = "\r\n";
|
|
|
|
static const char kEquals[] = "=";
|
|
|
|
static const char kWhitespace[] = " \t";
|
|
|
|
static const char kRBracket[] = "]";
|
|
|
|
|
|
|
|
nsresult
|
2017-09-01 03:02:10 +03:00
|
|
|
nsINIParser::InitFromString(const nsCString& aStr)
|
2005-08-15 22:29:55 +04:00
|
|
|
{
|
2017-10-13 00:20:57 +03:00
|
|
|
nsCString fileContents;
|
2017-09-01 03:02:10 +03:00
|
|
|
char* buffer;
|
2012-05-23 21:09:10 +04:00
|
|
|
|
2017-09-01 03:02:10 +03:00
|
|
|
if (StringHead(aStr, 3) == "\xEF\xBB\xBF") {
|
2014-06-27 05:35:39 +04:00
|
|
|
// Someone set us up the Utf-8 BOM
|
|
|
|
// This case is easy, since we assume that BOM-less
|
|
|
|
// files are Utf-8 anyway. Just skip the BOM and process as usual.
|
2017-10-13 00:20:57 +03:00
|
|
|
fileContents.Append(aStr);
|
|
|
|
buffer = fileContents.BeginWriting() + 3;
|
2017-09-01 03:02:10 +03:00
|
|
|
} else {
|
|
|
|
if (StringHead(aStr, 2) == "\xFF\xFE") {
|
|
|
|
// Someone set us up the Utf-16LE BOM
|
|
|
|
nsDependentSubstring str(reinterpret_cast<const char16_t*>(aStr.get()),
|
|
|
|
aStr.Length() / 2);
|
|
|
|
|
2017-10-13 00:20:57 +03:00
|
|
|
AppendUTF16toUTF8(Substring(str, 1), fileContents);
|
2017-09-01 03:02:10 +03:00
|
|
|
} else {
|
2017-10-13 00:20:57 +03:00
|
|
|
fileContents.Append(aStr);
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2012-05-23 21:09:10 +04:00
|
|
|
|
2017-10-13 00:20:57 +03:00
|
|
|
buffer = fileContents.BeginWriting();
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
char* currSection = nullptr;
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// outer loop tokenizes into lines
|
|
|
|
while (char* token = NS_strtok(kNL, &buffer)) {
|
|
|
|
if (token[0] == '#' || token[0] == ';') { // it's a comment
|
|
|
|
continue;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
token = (char*)NS_strspnp(kWhitespace, token);
|
|
|
|
if (!*token) { // empty line
|
|
|
|
continue;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (token[0] == '[') { // section header!
|
|
|
|
++token;
|
|
|
|
currSection = token;
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
char* rb = NS_strtok(kRBracket, &token);
|
|
|
|
if (!rb || NS_strtok(kWhitespace, &token)) {
|
|
|
|
// there's either an unclosed [Section or a [Section]Moretext!
|
|
|
|
// we could frankly decide that this INI file is malformed right
|
|
|
|
// here and stop, but we won't... keep going, looking for
|
|
|
|
// a well-formed [section] to continue working with
|
|
|
|
currSection = nullptr;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
continue;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!currSection) {
|
|
|
|
// If we haven't found a section header (or we found a malformed
|
|
|
|
// section header), don't bother parsing this line.
|
|
|
|
continue;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
char* key = token;
|
|
|
|
char* e = NS_strtok(kEquals, &token);
|
|
|
|
if (!e || !token) {
|
|
|
|
continue;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2017-10-13 00:20:57 +03:00
|
|
|
SetString(currSection, key, token);
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2017-10-13 00:20:57 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2017-10-13 00:20:57 +03:00
|
|
|
bool
|
|
|
|
nsINIParser::IsValidSection(const char* aSection)
|
|
|
|
{
|
|
|
|
if (aSection[0] == '\0') {
|
|
|
|
return false;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2017-10-13 00:20:57 +03:00
|
|
|
const char* found = strpbrk(aSection, "\r\n[]");
|
|
|
|
return found == nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsINIParser::IsValidKey(const char* aKey)
|
|
|
|
{
|
|
|
|
if (aKey[0] == '\0') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* found = strpbrk(aKey, "\r\n=");
|
|
|
|
return found == nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsINIParser::IsValidValue(const char* aValue)
|
|
|
|
{
|
|
|
|
const char* found = strpbrk(aValue, "\r\n");
|
|
|
|
return found == nullptr;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-27 05:35:39 +04:00
|
|
|
nsINIParser::GetString(const char* aSection, const char* aKey,
|
|
|
|
nsACString& aResult)
|
2005-08-15 22:29:55 +04:00
|
|
|
{
|
2017-10-13 00:20:57 +03:00
|
|
|
if (!IsValidSection(aSection) || !IsValidKey(aKey)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
INIValue* val;
|
|
|
|
mSections.Get(aSection, &val);
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
while (val) {
|
|
|
|
if (strcmp(val->key, aKey) == 0) {
|
|
|
|
aResult.Assign(val->value);
|
|
|
|
return NS_OK;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
2015-10-31 20:47:14 +03:00
|
|
|
val = val->next.get();
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-27 05:35:39 +04:00
|
|
|
nsINIParser::GetString(const char* aSection, const char* aKey,
|
|
|
|
char* aResult, uint32_t aResultLen)
|
2005-08-15 22:29:55 +04:00
|
|
|
{
|
2017-10-13 00:20:57 +03:00
|
|
|
if (!IsValidSection(aSection) || !IsValidKey(aKey)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
INIValue* val;
|
|
|
|
mSections.Get(aSection, &val);
|
|
|
|
|
|
|
|
while (val) {
|
|
|
|
if (strcmp(val->key, aKey) == 0) {
|
|
|
|
strncpy(aResult, val->value, aResultLen);
|
|
|
|
aResult[aResultLen - 1] = '\0';
|
|
|
|
if (strlen(val->value) >= aResultLen) {
|
|
|
|
return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
2015-10-31 20:47:14 +03:00
|
|
|
val = val->next.get();
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-27 05:35:39 +04:00
|
|
|
nsINIParser::GetSections(INISectionCallback aCB, void* aClosure)
|
2005-08-15 22:29:55 +04:00
|
|
|
{
|
2015-07-10 02:54:59 +03:00
|
|
|
for (auto iter = mSections.Iter(); !iter.Done(); iter.Next()) {
|
2015-07-20 15:21:28 +03:00
|
|
|
if (!aCB(iter.Key(), aClosure)) {
|
2015-07-10 02:54:59 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
return NS_OK;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-27 05:35:39 +04:00
|
|
|
nsINIParser::GetStrings(const char* aSection,
|
|
|
|
INIStringCallback aCB, void* aClosure)
|
2005-08-15 22:29:55 +04:00
|
|
|
{
|
2017-10-13 00:20:57 +03:00
|
|
|
if (!IsValidSection(aSection)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
INIValue* val;
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
for (mSections.Get(aSection, &val);
|
|
|
|
val;
|
2015-10-31 20:47:14 +03:00
|
|
|
val = val->next.get()) {
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!aCB(val->key, val->value, aClosure)) {
|
|
|
|
return NS_OK;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2005-08-15 22:29:55 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return NS_OK;
|
2005-08-15 22:29:55 +04:00
|
|
|
}
|
2017-10-13 00:20:57 +03:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsINIParser::SetString(const char* aSection, const char* aKey, const char* aValue)
|
|
|
|
{
|
|
|
|
if (!IsValidSection(aSection) || !IsValidKey(aKey) || !IsValidValue(aValue)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
INIValue* v;
|
|
|
|
if (!mSections.Get(aSection, &v)) {
|
|
|
|
v = new INIValue(aKey, aValue);
|
|
|
|
|
|
|
|
mSections.Put(aSection, v);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether this key has already been specified; overwrite
|
|
|
|
// if so, or append if not.
|
|
|
|
while (v) {
|
|
|
|
if (!strcmp(aKey, v->key)) {
|
|
|
|
v->SetValue(aValue);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!v->next) {
|
|
|
|
v->next = MakeUnique<INIValue>(aKey, aValue);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
v = v->next.get();
|
|
|
|
}
|
|
|
|
NS_ASSERTION(v, "v should never be null coming out of this loop");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsINIParser::DeleteString(const char* aSection, const char* aKey)
|
|
|
|
{
|
|
|
|
if (!IsValidSection(aSection) || !IsValidKey(aKey)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
INIValue* val;
|
|
|
|
if (!mSections.Get(aSection, &val)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special case the first result
|
|
|
|
if (strcmp(val->key, aKey) == 0) {
|
|
|
|
if (!val->next) {
|
|
|
|
mSections.Remove(aSection);
|
|
|
|
} else {
|
|
|
|
mSections.Put(aSection, val->next.release());
|
|
|
|
delete val;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (val->next) {
|
|
|
|
if (strcmp(val->next->key, aKey) == 0) {
|
|
|
|
val->next = std::move(val->next->next);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = val->next.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsINIParser::DeleteSection(const char* aSection)
|
|
|
|
{
|
|
|
|
if (!IsValidSection(aSection)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mSections.Remove(aSection)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsINIParser::WriteToFile(nsIFile *aFile) {
|
|
|
|
nsCString buffer;
|
|
|
|
|
|
|
|
for (auto iter = mSections.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
buffer.AppendPrintf("[%s]\n", iter.Key());
|
|
|
|
INIValue* val = iter.Data();
|
|
|
|
while (val) {
|
|
|
|
buffer.AppendPrintf("%s=%s\n", val->key, val->value);
|
|
|
|
val = val->next.get();
|
|
|
|
}
|
|
|
|
buffer.AppendLiteral("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* writeFile;
|
|
|
|
nsresult rv = aFile->OpenANSIFileDesc("w", &writeFile);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
unsigned int length = buffer.Length();
|
|
|
|
|
|
|
|
if (fwrite(buffer.get(), sizeof(char), length, writeFile) != length) {
|
|
|
|
fclose(writeFile);
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(writeFile);
|
|
|
|
return NS_OK;
|
|
|
|
}
|