2002-06-26 01:33:07 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00: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/. */
|
1999-12-29 20:55:42 +00:00
|
|
|
|
2012-12-09 10:04:21 +00:00
|
|
|
#include "nsArrayEnumerator.h"
|
1999-12-29 20:55:42 +00:00
|
|
|
#include "nsID.h"
|
2012-12-09 10:04:21 +00:00
|
|
|
#include "nsCOMArray.h"
|
2006-07-21 16:28:51 +00:00
|
|
|
#include "nsUnicharInputStream.h"
|
2002-06-26 01:33:07 +00:00
|
|
|
#include "nsPrintfCString.h"
|
2014-02-27 18:04:10 +00:00
|
|
|
#include "nsAutoPtr.h"
|
1999-12-29 20:55:42 +00:00
|
|
|
|
2002-06-26 01:33:07 +00:00
|
|
|
#define PL_ARENA_CONST_ALIGN_MASK 3
|
|
|
|
#include "nsPersistentProperties.h"
|
|
|
|
#include "nsIProperties.h"
|
|
|
|
|
2003-08-05 20:09:21 +00:00
|
|
|
struct PropertyTableEntry : public PLDHashEntryHdr
|
2002-06-26 01:33:07 +00:00
|
|
|
{
|
2003-08-05 20:09:21 +00:00
|
|
|
// both of these are arena-allocated
|
2014-07-09 15:15:21 +00:00
|
|
|
const char* mKey;
|
|
|
|
const char16_t* mValue;
|
2002-06-26 01:33:07 +00:00
|
|
|
};
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
static char16_t*
|
2002-06-26 01:33:07 +00:00
|
|
|
ArenaStrdup(const nsAFlatString& aString, PLArenaPool* aArena)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
void* mem;
|
2003-08-05 20:09:21 +00:00
|
|
|
// add one to include the null terminator
|
2014-07-09 15:15:21 +00:00
|
|
|
int32_t len = (aString.Length() + 1) * sizeof(char16_t);
|
2003-08-05 20:09:21 +00:00
|
|
|
PL_ARENA_ALLOCATE(mem, aArena, len);
|
|
|
|
NS_ASSERTION(mem, "Couldn't allocate space!\n");
|
|
|
|
if (mem) {
|
|
|
|
memcpy(mem, aString.get(), len);
|
|
|
|
}
|
2014-01-04 15:02:17 +00:00
|
|
|
return static_cast<char16_t*>(mem);
|
1999-12-29 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
2002-06-26 01:33:07 +00:00
|
|
|
static char*
|
|
|
|
ArenaStrdup(const nsAFlatCString& aString, PLArenaPool* aArena)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
void* mem;
|
2003-08-05 20:09:21 +00:00
|
|
|
// add one to include the null terminator
|
2014-07-09 15:15:21 +00:00
|
|
|
int32_t len = (aString.Length() + 1) * sizeof(char);
|
2003-08-05 20:09:21 +00:00
|
|
|
PL_ARENA_ALLOCATE(mem, aArena, len);
|
|
|
|
NS_ASSERTION(mem, "Couldn't allocate space!\n");
|
2014-07-09 15:15:21 +00:00
|
|
|
if (mem) {
|
2003-08-05 20:09:21 +00:00
|
|
|
memcpy(mem, aString.get(), len);
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2007-07-08 07:08:04 +00:00
|
|
|
return static_cast<char*>(mem);
|
2002-06-26 01:33:07 +00:00
|
|
|
}
|
|
|
|
|
2003-04-04 15:10:37 +00:00
|
|
|
static const struct PLDHashTableOps property_HashTableOps = {
|
2003-08-05 20:09:21 +00:00
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
|
|
|
PL_DHashStringKey,
|
|
|
|
PL_DHashMatchStringKey,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
PL_DHashClearEntryStub,
|
|
|
|
PL_DHashFinalizeStub,
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr,
|
2002-06-26 01:33:07 +00:00
|
|
|
};
|
|
|
|
|
2008-08-07 21:05:19 +00:00
|
|
|
//
|
|
|
|
// parser stuff
|
|
|
|
//
|
2014-07-09 15:15:21 +00:00
|
|
|
enum EParserState
|
|
|
|
{
|
2008-08-07 21:05:19 +00:00
|
|
|
eParserState_AwaitingKey,
|
|
|
|
eParserState_Key,
|
|
|
|
eParserState_AwaitingValue,
|
|
|
|
eParserState_Value,
|
|
|
|
eParserState_Comment
|
|
|
|
};
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
enum EParserSpecial
|
|
|
|
{
|
2008-08-07 21:05:19 +00:00
|
|
|
eParserSpecial_None, // not parsing a special character
|
|
|
|
eParserSpecial_Escaped, // awaiting a special character
|
|
|
|
eParserSpecial_Unicode // parsing a \Uxxx value
|
|
|
|
};
|
|
|
|
|
|
|
|
class nsPropertiesParser
|
|
|
|
{
|
|
|
|
public:
|
2014-07-28 17:19:06 +00:00
|
|
|
explicit nsPropertiesParser(nsIPersistentProperties* aProps)
|
2014-07-09 15:15:21 +00:00
|
|
|
: mHaveMultiLine(false)
|
|
|
|
, mState(eParserState_AwaitingKey)
|
|
|
|
, mProps(aProps)
|
|
|
|
{
|
|
|
|
}
|
2008-08-07 21:05:19 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
void FinishValueState(nsAString& aOldValue)
|
|
|
|
{
|
2008-08-07 21:05:19 +00:00
|
|
|
static const char trimThese[] = " \t";
|
2011-10-17 14:59:28 +00:00
|
|
|
mKey.Trim(trimThese, false, true);
|
2008-08-07 21:05:19 +00:00
|
|
|
|
|
|
|
// This is really ugly hack but it should be fast
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t backup_char;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t minLength = mMinLength;
|
2014-07-09 15:15:21 +00:00
|
|
|
if (minLength) {
|
|
|
|
backup_char = mValue[minLength - 1];
|
|
|
|
mValue.SetCharAt('x', minLength - 1);
|
2008-08-07 21:05:19 +00:00
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
mValue.Trim(trimThese, false, true);
|
2014-07-09 15:15:21 +00:00
|
|
|
if (minLength) {
|
|
|
|
mValue.SetCharAt(backup_char, minLength - 1);
|
|
|
|
}
|
2008-08-07 21:05:19 +00:00
|
|
|
|
|
|
|
mProps->SetStringProperty(NS_ConvertUTF16toUTF8(mKey), mValue, aOldValue);
|
|
|
|
mSpecialState = eParserSpecial_None;
|
|
|
|
WaitForKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
EParserState GetState() { return mState; }
|
|
|
|
|
|
|
|
static NS_METHOD SegmentWriter(nsIUnicharInputStream* aStream,
|
|
|
|
void* aClosure,
|
2014-07-09 15:15:21 +00:00
|
|
|
const char16_t* aFromSegment,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aToOffset,
|
|
|
|
uint32_t aCount,
|
2014-07-09 15:15:21 +00:00
|
|
|
uint32_t* aWriteCount);
|
2008-08-07 21:05:19 +00:00
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
nsresult ParseBuffer(const char16_t* aBuffer, uint32_t aBufferLength);
|
2008-08-07 21:05:19 +00:00
|
|
|
|
|
|
|
private:
|
2011-09-29 06:19:26 +00:00
|
|
|
bool ParseValueCharacter(
|
2014-07-09 15:15:21 +00:00
|
|
|
char16_t aChar, // character that is just being parsed
|
|
|
|
const char16_t* aCur, // pointer to character aChar in the buffer
|
|
|
|
const char16_t*& aTokenStart, // string copying is done in blocks as big as
|
|
|
|
// possible, aTokenStart points to the beginning
|
2008-08-07 21:05:19 +00:00
|
|
|
// of this block
|
2014-07-09 15:15:21 +00:00
|
|
|
nsAString& aOldValue); // when duplicate property is found, new value
|
2008-08-07 21:05:19 +00:00
|
|
|
// is stored into hashtable and the old one is
|
|
|
|
// placed in this variable
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
void WaitForKey()
|
|
|
|
{
|
2008-08-07 21:05:19 +00:00
|
|
|
mState = eParserState_AwaitingKey;
|
|
|
|
}
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
void EnterKeyState()
|
|
|
|
{
|
2008-08-07 21:05:19 +00:00
|
|
|
mKey.Truncate();
|
|
|
|
mState = eParserState_Key;
|
|
|
|
}
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
void WaitForValue()
|
|
|
|
{
|
2008-08-07 21:05:19 +00:00
|
|
|
mState = eParserState_AwaitingValue;
|
|
|
|
}
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
void EnterValueState()
|
|
|
|
{
|
2008-08-07 21:05:19 +00:00
|
|
|
mValue.Truncate();
|
|
|
|
mMinLength = 0;
|
|
|
|
mState = eParserState_Value;
|
|
|
|
mSpecialState = eParserSpecial_None;
|
|
|
|
}
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
void EnterCommentState()
|
|
|
|
{
|
2008-08-07 21:05:19 +00:00
|
|
|
mState = eParserState_Comment;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString mKey;
|
|
|
|
nsAutoString mValue;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mUnicodeValuesRead; // should be 4!
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t mUnicodeValue; // currently parsed unicode value
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mHaveMultiLine; // is TRUE when last processed characters form
|
2008-08-07 21:05:19 +00:00
|
|
|
// any of following sequences:
|
|
|
|
// - "\\\r"
|
|
|
|
// - "\\\n"
|
|
|
|
// - "\\\r\n"
|
|
|
|
// - any sequence above followed by any
|
|
|
|
// combination of ' ' and '\t'
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mMultiLineCanSkipN; // TRUE if "\\\r" was detected
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mMinLength; // limit right trimming at the end to not trim
|
2008-08-07 21:05:19 +00:00
|
|
|
// escaped whitespaces
|
|
|
|
EParserState mState;
|
|
|
|
// if we see a '\' then we enter this special state
|
|
|
|
EParserSpecial mSpecialState;
|
|
|
|
nsIPersistentProperties* mProps;
|
|
|
|
};
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
inline bool
|
|
|
|
IsWhiteSpace(char16_t aChar)
|
2008-08-07 21:05:19 +00:00
|
|
|
{
|
|
|
|
return (aChar == ' ') || (aChar == '\t') ||
|
|
|
|
(aChar == '\r') || (aChar == '\n');
|
|
|
|
}
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
inline bool
|
|
|
|
IsEOL(char16_t aChar)
|
2008-08-07 21:05:19 +00:00
|
|
|
{
|
|
|
|
return (aChar == '\r') || (aChar == '\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
bool
|
|
|
|
nsPropertiesParser::ParseValueCharacter(char16_t aChar, const char16_t* aCur,
|
|
|
|
const char16_t*& aTokenStart,
|
|
|
|
nsAString& aOldValue)
|
2008-08-07 21:05:19 +00:00
|
|
|
{
|
|
|
|
switch (mSpecialState) {
|
|
|
|
// the normal state - look for special characters
|
2014-07-09 15:15:21 +00:00
|
|
|
case eParserSpecial_None:
|
|
|
|
switch (aChar) {
|
|
|
|
case '\\':
|
|
|
|
if (mHaveMultiLine) {
|
|
|
|
// there is nothing to append to mValue yet
|
|
|
|
mHaveMultiLine = false;
|
|
|
|
} else {
|
|
|
|
mValue += Substring(aTokenStart, aCur);
|
|
|
|
}
|
|
|
|
|
|
|
|
mSpecialState = eParserSpecial_Escaped;
|
|
|
|
break;
|
2008-08-07 21:05:19 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
case '\n':
|
|
|
|
// if we detected multiline and got only "\\\r" ignore next "\n" if any
|
|
|
|
if (mHaveMultiLine && mMultiLineCanSkipN) {
|
|
|
|
// but don't allow another '\n' to be skipped
|
|
|
|
mMultiLineCanSkipN = false;
|
|
|
|
// Now there is nothing to append to the mValue since we are skipping
|
|
|
|
// whitespaces at the beginning of the new line of the multiline
|
|
|
|
// property. Set aTokenStart properly to ensure that nothing is appended
|
|
|
|
// if we find regular line-end or the end of the buffer.
|
|
|
|
aTokenStart = aCur + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// no break
|
|
|
|
|
|
|
|
case '\r':
|
|
|
|
// we're done! We have a key and value
|
|
|
|
mValue += Substring(aTokenStart, aCur);
|
|
|
|
FinishValueState(aOldValue);
|
|
|
|
mHaveMultiLine = false;
|
2008-08-07 21:05:19 +00:00
|
|
|
break;
|
2014-07-09 15:15:21 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
// there is nothing to do with normal characters,
|
|
|
|
// but handle multilines correctly
|
|
|
|
if (mHaveMultiLine) {
|
|
|
|
if (aChar == ' ' || aChar == '\t') {
|
|
|
|
// don't allow another '\n' to be skipped
|
|
|
|
mMultiLineCanSkipN = false;
|
|
|
|
// Now there is nothing to append to the mValue since we are skipping
|
|
|
|
// whitespaces at the beginning of the new line of the multiline
|
|
|
|
// property. Set aTokenStart properly to ensure that nothing is appended
|
|
|
|
// if we find regular line-end or the end of the buffer.
|
|
|
|
aTokenStart = aCur + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mHaveMultiLine = false;
|
|
|
|
aTokenStart = aCur;
|
|
|
|
}
|
|
|
|
break; // from switch on (aChar)
|
2008-08-07 21:05:19 +00:00
|
|
|
}
|
2014-07-09 15:15:21 +00:00
|
|
|
break; // from switch on (mSpecialState)
|
2008-08-07 21:05:19 +00:00
|
|
|
|
|
|
|
// saw a \ character, so parse the character after that
|
2014-07-09 15:15:21 +00:00
|
|
|
case eParserSpecial_Escaped:
|
|
|
|
// probably want to start parsing at the next token
|
|
|
|
// other characters, like 'u' might override this
|
|
|
|
aTokenStart = aCur + 1;
|
|
|
|
mSpecialState = eParserSpecial_None;
|
2008-08-07 21:05:19 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
switch (aChar) {
|
|
|
|
// the easy characters - \t, \n, and so forth
|
|
|
|
case 't':
|
|
|
|
mValue += char16_t('\t');
|
|
|
|
mMinLength = mValue.Length();
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
mValue += char16_t('\n');
|
|
|
|
mMinLength = mValue.Length();
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
mValue += char16_t('\r');
|
|
|
|
mMinLength = mValue.Length();
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
mValue += char16_t('\\');
|
|
|
|
break;
|
2008-08-07 21:05:19 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
// switch to unicode mode!
|
|
|
|
case 'u':
|
|
|
|
case 'U':
|
|
|
|
mSpecialState = eParserSpecial_Unicode;
|
|
|
|
mUnicodeValuesRead = 0;
|
|
|
|
mUnicodeValue = 0;
|
|
|
|
break;
|
2008-08-07 21:05:19 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
// a \ immediately followed by a newline means we're going multiline
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
mHaveMultiLine = true;
|
|
|
|
mMultiLineCanSkipN = (aChar == '\r');
|
|
|
|
mSpecialState = eParserSpecial_None;
|
|
|
|
break;
|
2008-08-07 21:05:19 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
default:
|
|
|
|
// don't recognize the character, so just append it
|
|
|
|
mValue += aChar;
|
|
|
|
break;
|
|
|
|
}
|
2008-08-07 21:05:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// we're in the middle of parsing a 4-character unicode value
|
|
|
|
// like \u5f39
|
2014-07-09 15:15:21 +00:00
|
|
|
case eParserSpecial_Unicode:
|
|
|
|
if ('0' <= aChar && aChar <= '9') {
|
|
|
|
mUnicodeValue =
|
|
|
|
(mUnicodeValue << 4) | (aChar - '0');
|
|
|
|
} else if ('a' <= aChar && aChar <= 'f') {
|
|
|
|
mUnicodeValue =
|
|
|
|
(mUnicodeValue << 4) | (aChar - 'a' + 0x0a);
|
|
|
|
} else if ('A' <= aChar && aChar <= 'F') {
|
|
|
|
mUnicodeValue =
|
|
|
|
(mUnicodeValue << 4) | (aChar - 'A' + 0x0a);
|
|
|
|
} else {
|
|
|
|
// non-hex character. Append what we have, and move on.
|
|
|
|
mValue += mUnicodeValue;
|
|
|
|
mMinLength = mValue.Length();
|
|
|
|
mSpecialState = eParserSpecial_None;
|
|
|
|
|
|
|
|
// leave aTokenStart at this unknown character, so it gets appended
|
|
|
|
aTokenStart = aCur;
|
|
|
|
|
|
|
|
// ensure parsing this non-hex character again
|
|
|
|
return false;
|
|
|
|
}
|
2008-08-07 21:05:19 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
if (++mUnicodeValuesRead >= 4) {
|
|
|
|
aTokenStart = aCur + 1;
|
|
|
|
mSpecialState = eParserSpecial_None;
|
|
|
|
mValue += mUnicodeValue;
|
|
|
|
mMinLength = mValue.Length();
|
|
|
|
}
|
2008-08-07 21:05:19 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
break;
|
2008-08-07 21:05:19 +00:00
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2008-08-07 21:05:19 +00:00
|
|
|
}
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
NS_METHOD
|
|
|
|
nsPropertiesParser::SegmentWriter(nsIUnicharInputStream* aStream,
|
|
|
|
void* aClosure,
|
|
|
|
const char16_t* aFromSegment,
|
|
|
|
uint32_t aToOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aWriteCount)
|
2008-08-07 21:05:19 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
nsPropertiesParser* parser = static_cast<nsPropertiesParser*>(aClosure);
|
2008-08-07 21:05:19 +00:00
|
|
|
parser->ParseBuffer(aFromSegment, aCount);
|
|
|
|
|
|
|
|
*aWriteCount = aCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
nsresult
|
|
|
|
nsPropertiesParser::ParseBuffer(const char16_t* aBuffer,
|
|
|
|
uint32_t aBufferLength)
|
2008-08-07 21:05:19 +00:00
|
|
|
{
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t* cur = aBuffer;
|
|
|
|
const char16_t* end = aBuffer + aBufferLength;
|
2008-08-07 21:05:19 +00:00
|
|
|
|
|
|
|
// points to the start/end of the current key or value
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t* tokenStart = nullptr;
|
2008-08-07 21:05:19 +00:00
|
|
|
|
|
|
|
// if we're in the middle of parsing a key or value, make sure
|
|
|
|
// the current token points to the beginning of the current buffer
|
|
|
|
if (mState == eParserState_Key ||
|
|
|
|
mState == eParserState_Value) {
|
|
|
|
tokenStart = aBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString oldValue;
|
|
|
|
|
|
|
|
while (cur != end) {
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t c = *cur;
|
2008-08-07 21:05:19 +00:00
|
|
|
|
|
|
|
switch (mState) {
|
2014-07-09 15:15:21 +00:00
|
|
|
case eParserState_AwaitingKey:
|
|
|
|
if (c == '#' || c == '!') {
|
|
|
|
EnterCommentState();
|
|
|
|
}
|
2008-08-07 21:05:19 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
else if (!IsWhiteSpace(c)) {
|
|
|
|
// not a comment, not whitespace, we must have found a key!
|
|
|
|
EnterKeyState();
|
|
|
|
tokenStart = cur;
|
|
|
|
}
|
|
|
|
break;
|
2008-08-07 21:05:19 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
case eParserState_Key:
|
|
|
|
if (c == '=' || c == ':') {
|
|
|
|
mKey += Substring(tokenStart, cur);
|
|
|
|
WaitForValue();
|
|
|
|
}
|
|
|
|
break;
|
2008-08-07 21:05:19 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
case eParserState_AwaitingValue:
|
|
|
|
if (IsEOL(c)) {
|
|
|
|
// no value at all! mimic the normal value-ending
|
|
|
|
EnterValueState();
|
|
|
|
FinishValueState(oldValue);
|
|
|
|
}
|
2008-08-07 21:05:19 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
// ignore white space leading up to the value
|
|
|
|
else if (!IsWhiteSpace(c)) {
|
|
|
|
tokenStart = cur;
|
|
|
|
EnterValueState();
|
|
|
|
|
|
|
|
// make sure to handle this first character
|
|
|
|
if (ParseValueCharacter(c, cur, tokenStart, oldValue)) {
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
// If the character isn't consumed, don't do cur++ and parse
|
|
|
|
// the character again. This can happen f.e. for char 'X' in sequence
|
|
|
|
// "\u00X". This character can be control character and must be
|
|
|
|
// processed again.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eParserState_Value:
|
|
|
|
if (ParseValueCharacter(c, cur, tokenStart, oldValue)) {
|
2008-08-07 21:05:19 +00:00
|
|
|
cur++;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
|
|
|
// See few lines above for reason of doing this
|
2008-08-07 21:05:19 +00:00
|
|
|
continue;
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
case eParserState_Comment:
|
|
|
|
// stay in this state till we hit EOL
|
|
|
|
if (c == '\r' || c == '\n') {
|
|
|
|
WaitForKey();
|
|
|
|
}
|
|
|
|
break;
|
2008-08-07 21:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// finally, advance to the next character
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we're still parsing the value and are in eParserSpecial_None, then
|
|
|
|
// append whatever we have..
|
|
|
|
if (mState == eParserState_Value && tokenStart &&
|
|
|
|
mSpecialState == eParserSpecial_None) {
|
|
|
|
mValue += Substring(tokenStart, cur);
|
|
|
|
}
|
|
|
|
// if we're still parsing the key, then append whatever we have..
|
|
|
|
else if (mState == eParserState_Key && tokenStart) {
|
|
|
|
mKey += Substring(tokenStart, cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-12-29 20:55:42 +00:00
|
|
|
nsPersistentProperties::nsPersistentProperties()
|
2014-07-09 15:15:21 +00:00
|
|
|
: mIn(nullptr)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
mSubclass = static_cast<nsIPersistentProperties*>(this);
|
2014-02-27 18:04:10 +00:00
|
|
|
|
|
|
|
PL_DHashTableInit(&mTable, &property_HashTableOps, nullptr,
|
2014-08-06 13:31:21 +00:00
|
|
|
sizeof(PropertyTableEntry), 16);
|
2014-02-27 18:04:10 +00:00
|
|
|
|
2003-08-05 20:09:21 +00:00
|
|
|
PL_INIT_ARENA_POOL(&mArena, "PersistentPropertyArena", 2048);
|
1999-12-29 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPersistentProperties::~nsPersistentProperties()
|
|
|
|
{
|
2003-08-05 20:09:21 +00:00
|
|
|
PL_FinishArenaPool(&mArena);
|
2014-07-09 15:15:21 +00:00
|
|
|
if (mTable.ops) {
|
2003-08-05 20:09:21 +00:00
|
|
|
PL_DHashTableFinish(&mTable);
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2003-07-14 21:07:31 +00:00
|
|
|
}
|
|
|
|
|
2010-06-10 18:11:11 +00:00
|
|
|
nsresult
|
2014-07-09 15:15:21 +00:00
|
|
|
nsPersistentProperties::Create(nsISupports* aOuter, REFNSIID aIID,
|
|
|
|
void** aResult)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
if (aOuter) {
|
2003-08-05 20:09:21 +00:00
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2014-02-27 18:04:10 +00:00
|
|
|
nsRefPtr<nsPersistentProperties> props = new nsPersistentProperties();
|
|
|
|
return props->QueryInterface(aIID, aResult);
|
1999-12-29 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsPersistentProperties, nsIPersistentProperties, nsIProperties)
|
1999-12-29 20:55:42 +00:00
|
|
|
|
2003-02-04 05:43:57 +00:00
|
|
|
NS_IMETHODIMP
|
2014-07-09 15:15:21 +00:00
|
|
|
nsPersistentProperties::Load(nsIInputStream* aIn)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
2008-08-07 21:05:19 +00:00
|
|
|
nsresult rv = nsSimpleUnicharStreamFactory::GetInstance()->
|
|
|
|
CreateInstanceFromUTF8Stream(aIn, getter_AddRefs(mIn));
|
2003-08-05 20:09:21 +00:00
|
|
|
|
2008-08-07 21:05:19 +00:00
|
|
|
if (rv != NS_OK) {
|
|
|
|
NS_WARNING("Error creating UnicharInputStream");
|
2003-02-04 05:43:57 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2008-08-07 21:05:19 +00:00
|
|
|
|
|
|
|
nsPropertiesParser parser(mSubclass);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t nProcessed;
|
2008-08-07 21:05:19 +00:00
|
|
|
// If this 4096 is changed to some other value, make sure to adjust
|
|
|
|
// the bug121341.properties test file accordingly.
|
2014-07-09 15:15:21 +00:00
|
|
|
while (NS_SUCCEEDED(rv = mIn->ReadSegments(nsPropertiesParser::SegmentWriter,
|
|
|
|
&parser, 4096, &nProcessed)) &&
|
2008-08-07 21:05:19 +00:00
|
|
|
nProcessed != 0);
|
2012-07-30 14:20:58 +00:00
|
|
|
mIn = nullptr;
|
2014-07-09 15:15:21 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2008-08-07 21:05:19 +00:00
|
|
|
return rv;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2008-08-07 21:05:19 +00:00
|
|
|
|
|
|
|
// We may have an unprocessed value at this point
|
|
|
|
// if the last line did not have a proper line ending.
|
|
|
|
if (parser.GetState() == eParserState_Value) {
|
2014-02-27 18:04:10 +00:00
|
|
|
nsAutoString oldValue;
|
2008-08-07 21:05:19 +00:00
|
|
|
parser.FinishValueState(oldValue);
|
2003-02-03 14:59:39 +00:00
|
|
|
}
|
1999-12-29 20:55:42 +00:00
|
|
|
|
2003-01-22 05:15:40 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPersistentProperties::SetStringProperty(const nsACString& aKey,
|
|
|
|
const nsAString& aNewValue,
|
|
|
|
nsAString& aOldValue)
|
|
|
|
{
|
2002-08-30 17:13:34 +00:00
|
|
|
const nsAFlatCString& flatKey = PromiseFlatCString(aKey);
|
2014-07-09 15:15:21 +00:00
|
|
|
PropertyTableEntry* entry = static_cast<PropertyTableEntry*>(
|
|
|
|
PL_DHashTableOperate(&mTable, flatKey.get(), PL_DHASH_ADD));
|
2002-06-26 01:33:07 +00:00
|
|
|
|
|
|
|
if (entry->mKey) {
|
2003-08-05 20:09:21 +00:00
|
|
|
aOldValue = entry->mValue;
|
2012-04-24 18:43:00 +00:00
|
|
|
NS_WARNING(nsPrintfCString("the property %s already exists\n",
|
2003-08-05 20:09:21 +00:00
|
|
|
flatKey.get()).get());
|
2014-07-09 15:15:21 +00:00
|
|
|
} else {
|
2008-12-11 20:13:52 +00:00
|
|
|
aOldValue.Truncate();
|
|
|
|
}
|
1999-12-29 20:55:42 +00:00
|
|
|
|
2002-06-26 01:33:07 +00:00
|
|
|
entry->mKey = ArenaStrdup(flatKey, &mArena);
|
|
|
|
entry->mValue = ArenaStrdup(PromiseFlatString(aNewValue), &mArena);
|
2003-08-05 20:09:21 +00:00
|
|
|
|
1999-12-29 20:55:42 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-08-30 17:13:34 +00:00
|
|
|
nsPersistentProperties::Save(nsIOutputStream* aOut, const nsACString& aHeader)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPersistentProperties::Subclass(nsIPersistentProperties* aSubclass)
|
|
|
|
{
|
|
|
|
if (aSubclass) {
|
|
|
|
mSubclass = aSubclass;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-08-30 17:13:34 +00:00
|
|
|
nsPersistentProperties::GetStringProperty(const nsACString& aKey,
|
|
|
|
nsAString& aValue)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
2002-08-30 17:13:34 +00:00
|
|
|
const nsAFlatCString& flatKey = PromiseFlatCString(aKey);
|
2002-06-26 01:33:07 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
PropertyTableEntry* entry = static_cast<PropertyTableEntry*>(
|
|
|
|
PL_DHashTableOperate(&mTable, flatKey.get(), PL_DHASH_LOOKUP));
|
2002-06-26 01:33:07 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
2002-06-26 01:33:07 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
1999-12-29 20:55:42 +00:00
|
|
|
|
2002-06-26 01:33:07 +00:00
|
|
|
aValue = entry->mValue;
|
|
|
|
return NS_OK;
|
1999-12-29 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static PLDHashOperator
|
2014-07-09 15:15:21 +00:00
|
|
|
AddElemToArray(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
|
|
|
|
uint32_t aIndex, void* aArg)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
2012-12-09 10:04:21 +00:00
|
|
|
nsCOMArray<nsIPropertyElement>* props =
|
2014-07-09 15:15:21 +00:00
|
|
|
static_cast<nsCOMArray<nsIPropertyElement>*>(aArg);
|
2003-08-05 20:09:21 +00:00
|
|
|
PropertyTableEntry* entry =
|
2014-07-09 15:15:21 +00:00
|
|
|
static_cast<PropertyTableEntry*>(aHdr);
|
2003-08-05 20:09:21 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
nsPropertyElement* element =
|
2002-08-30 17:13:34 +00:00
|
|
|
new nsPropertyElement(nsDependentCString(entry->mKey),
|
|
|
|
nsDependentString(entry->mValue));
|
1999-12-29 20:55:42 +00:00
|
|
|
|
2012-12-09 10:04:21 +00:00
|
|
|
props->AppendObject(element);
|
1999-12-29 20:55:42 +00:00
|
|
|
|
2002-06-26 01:33:07 +00:00
|
|
|
return PL_DHASH_NEXT;
|
1999-12-29 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
2000-05-02 05:08:39 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-08-30 17:13:34 +00:00
|
|
|
nsPersistentProperties::Enumerate(nsISimpleEnumerator** aResult)
|
2000-05-02 05:08:39 +00:00
|
|
|
{
|
2012-12-09 10:04:21 +00:00
|
|
|
nsCOMArray<nsIPropertyElement> props;
|
1999-12-29 20:55:42 +00:00
|
|
|
|
2005-11-16 21:27:11 +00:00
|
|
|
// We know the necessary size; we can avoid growing it while adding elements
|
2014-08-25 23:56:33 +00:00
|
|
|
props.SetCapacity(mTable.EntryCount());
|
2005-11-16 21:27:11 +00:00
|
|
|
|
2000-05-02 05:08:39 +00:00
|
|
|
// Step through hash entries populating a transient array
|
2014-07-09 15:15:21 +00:00
|
|
|
uint32_t n = PL_DHashTableEnumerate(&mTable, AddElemToArray, (void*)&props);
|
2014-08-25 23:56:33 +00:00
|
|
|
if (n < mTable.EntryCount()) {
|
2008-08-07 21:05:19 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
1999-12-29 20:55:42 +00:00
|
|
|
|
2012-12-09 10:04:21 +00:00
|
|
|
return NS_NewArrayEnumerator(aResult, props);
|
1999-12-29 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2003-08-05 20:09:21 +00:00
|
|
|
// XXX Some day we'll unify the nsIPersistentProperties interface with
|
1999-12-29 20:55:42 +00:00
|
|
|
// nsIProperties, but until now...
|
|
|
|
|
2003-08-05 20:09:21 +00:00
|
|
|
NS_IMETHODIMP
|
2014-07-09 15:15:21 +00:00
|
|
|
nsPersistentProperties::Get(const char* aProp, const nsIID& aUUID,
|
|
|
|
void** aResult)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2003-08-05 20:09:21 +00:00
|
|
|
NS_IMETHODIMP
|
2014-07-09 15:15:21 +00:00
|
|
|
nsPersistentProperties::Set(const char* aProp, nsISupports* value)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2003-08-05 20:09:21 +00:00
|
|
|
NS_IMETHODIMP
|
2014-07-09 15:15:21 +00:00
|
|
|
nsPersistentProperties::Undefine(const char* aProp)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2003-08-05 20:09:21 +00:00
|
|
|
NS_IMETHODIMP
|
2014-07-09 15:15:21 +00:00
|
|
|
nsPersistentProperties::Has(const char* aProp, bool* aResult)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
PropertyTableEntry* entry = static_cast<PropertyTableEntry*>(
|
|
|
|
PL_DHashTableOperate(&mTable, aProp, PL_DHASH_LOOKUP));
|
|
|
|
*aResult = (entry && PL_DHASH_ENTRY_IS_BUSY(entry));
|
2008-08-07 21:05:19 +00:00
|
|
|
return NS_OK;
|
1999-12-29 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
2003-08-05 20:09:21 +00:00
|
|
|
NS_IMETHODIMP
|
2014-07-09 15:15:21 +00:00
|
|
|
nsPersistentProperties::GetKeys(uint32_t* aCount, char*** aKeys)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
1999-12-29 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// PropertyElement
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_METHOD
|
2014-07-09 15:15:21 +00:00
|
|
|
nsPropertyElement::Create(nsISupports* aOuter, REFNSIID aIID, void** aResult)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
2014-07-09 15:15:21 +00:00
|
|
|
if (aOuter) {
|
2003-08-05 20:09:21 +00:00
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2014-07-09 15:15:21 +00:00
|
|
|
}
|
2014-02-27 18:04:10 +00:00
|
|
|
nsRefPtr<nsPropertyElement> propElem = new nsPropertyElement();
|
|
|
|
return propElem->QueryInterface(aIID, aResult);
|
1999-12-29 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsPropertyElement, nsIPropertyElement)
|
1999-12-29 20:55:42 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-08-30 17:13:34 +00:00
|
|
|
nsPropertyElement::GetKey(nsACString& aReturnKey)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
2002-08-30 17:13:34 +00:00
|
|
|
aReturnKey = mKey;
|
|
|
|
return NS_OK;
|
1999-12-29 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-08-30 17:13:34 +00:00
|
|
|
nsPropertyElement::GetValue(nsAString& aReturnValue)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
2002-08-30 17:13:34 +00:00
|
|
|
aReturnValue = mValue;
|
|
|
|
return NS_OK;
|
1999-12-29 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-08-30 17:13:34 +00:00
|
|
|
nsPropertyElement::SetKey(const nsACString& aKey)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
2000-05-02 05:08:39 +00:00
|
|
|
mKey = aKey;
|
|
|
|
return NS_OK;
|
1999-12-29 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-08-30 17:13:34 +00:00
|
|
|
nsPropertyElement::SetValue(const nsAString& aValue)
|
1999-12-29 20:55:42 +00:00
|
|
|
{
|
2000-05-02 05:08:39 +00:00
|
|
|
mValue = aValue;
|
|
|
|
return NS_OK;
|
1999-12-29 20:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|