1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsArrayEnumerator.h"
8 #include "nsCOMArray.h"
9 #include "nsUnicharInputStream.h"
10 #include "nsPrintfCString.h"
12 #define PL_ARENA_CONST_ALIGN_MASK 3
13 #include "nsPersistentProperties.h"
14 #include "nsIProperties.h"
16 struct PropertyTableEntry
: public PLDHashEntryHdr
18 // both of these are arena-allocated
20 const PRUnichar
*mValue
;
24 ArenaStrdup(const nsAFlatString
& aString
, PLArenaPool
* aArena
)
27 // add one to include the null terminator
28 int32_t len
= (aString
.Length()+1) * sizeof(PRUnichar
);
29 PL_ARENA_ALLOCATE(mem
, aArena
, len
);
30 NS_ASSERTION(mem
, "Couldn't allocate space!\n");
32 memcpy(mem
, aString
.get(), len
);
34 return static_cast<PRUnichar
*>(mem
);
38 ArenaStrdup(const nsAFlatCString
& aString
, PLArenaPool
* aArena
)
41 // add one to include the null terminator
42 int32_t len
= (aString
.Length()+1) * sizeof(char);
43 PL_ARENA_ALLOCATE(mem
, aArena
, len
);
44 NS_ASSERTION(mem
, "Couldn't allocate space!\n");
46 memcpy(mem
, aString
.get(), len
);
47 return static_cast<char*>(mem
);
50 static const struct PLDHashTableOps property_HashTableOps
= {
54 PL_DHashMatchStringKey
,
55 PL_DHashMoveEntryStub
,
56 PL_DHashClearEntryStub
,
65 eParserState_AwaitingKey
,
67 eParserState_AwaitingValue
,
73 eParserSpecial_None
, // not parsing a special character
74 eParserSpecial_Escaped
, // awaiting a special character
75 eParserSpecial_Unicode
// parsing a \Uxxx value
78 class nsPropertiesParser
81 nsPropertiesParser(nsIPersistentProperties
* aProps
) :
82 mHaveMultiLine(false), mState(eParserState_AwaitingKey
),
85 void FinishValueState(nsAString
& aOldValue
) {
86 static const char trimThese
[] = " \t";
87 mKey
.Trim(trimThese
, false, true);
89 // This is really ugly hack but it should be fast
90 PRUnichar backup_char
;
91 uint32_t minLength
= mMinLength
;
94 backup_char
= mValue
[minLength
-1];
95 mValue
.SetCharAt('x', minLength
-1);
97 mValue
.Trim(trimThese
, false, true);
99 mValue
.SetCharAt(backup_char
, minLength
-1);
101 mProps
->SetStringProperty(NS_ConvertUTF16toUTF8(mKey
), mValue
, aOldValue
);
102 mSpecialState
= eParserSpecial_None
;
106 EParserState
GetState() { return mState
; }
108 static NS_METHOD
SegmentWriter(nsIUnicharInputStream
* aStream
,
110 const PRUnichar
*aFromSegment
,
113 uint32_t *aWriteCount
);
115 nsresult
ParseBuffer(const PRUnichar
* aBuffer
, uint32_t aBufferLength
);
118 bool ParseValueCharacter(
119 PRUnichar c
, // character that is just being parsed
120 const PRUnichar
* cur
, // pointer to character c in the buffer
121 const PRUnichar
* &tokenStart
, // string copying is done in blocks as big as
122 // possible, tokenStart points to the beginning
124 nsAString
& oldValue
); // when duplicate property is found, new value
125 // is stored into hashtable and the old one is
126 // placed in this variable
129 mState
= eParserState_AwaitingKey
;
132 void EnterKeyState() {
134 mState
= eParserState_Key
;
137 void WaitForValue() {
138 mState
= eParserState_AwaitingValue
;
141 void EnterValueState() {
144 mState
= eParserState_Value
;
145 mSpecialState
= eParserSpecial_None
;
148 void EnterCommentState() {
149 mState
= eParserState_Comment
;
155 uint32_t mUnicodeValuesRead
; // should be 4!
156 PRUnichar mUnicodeValue
; // currently parsed unicode value
157 bool mHaveMultiLine
; // is TRUE when last processed characters form
158 // any of following sequences:
162 // - any sequence above followed by any
163 // combination of ' ' and '\t'
164 bool mMultiLineCanSkipN
; // TRUE if "\\\r" was detected
165 uint32_t mMinLength
; // limit right trimming at the end to not trim
166 // escaped whitespaces
168 // if we see a '\' then we enter this special state
169 EParserSpecial mSpecialState
;
170 nsIPersistentProperties
* mProps
;
173 inline bool IsWhiteSpace(PRUnichar aChar
)
175 return (aChar
== ' ') || (aChar
== '\t') ||
176 (aChar
== '\r') || (aChar
== '\n');
179 inline bool IsEOL(PRUnichar aChar
)
181 return (aChar
== '\r') || (aChar
== '\n');
185 bool nsPropertiesParser::ParseValueCharacter(
186 PRUnichar c
, const PRUnichar
* cur
, const PRUnichar
* &tokenStart
,
189 switch (mSpecialState
) {
191 // the normal state - look for special characters
192 case eParserSpecial_None
:
196 // there is nothing to append to mValue yet
197 mHaveMultiLine
= false;
199 mValue
+= Substring(tokenStart
, cur
);
201 mSpecialState
= eParserSpecial_Escaped
;
205 // if we detected multiline and got only "\\\r" ignore next "\n" if any
206 if (mHaveMultiLine
&& mMultiLineCanSkipN
) {
207 // but don't allow another '\n' to be skipped
208 mMultiLineCanSkipN
= false;
209 // Now there is nothing to append to the mValue since we are skipping
210 // whitespaces at the beginning of the new line of the multiline
211 // property. Set tokenStart properly to ensure that nothing is appended
212 // if we find regular line-end or the end of the buffer.
219 // we're done! We have a key and value
220 mValue
+= Substring(tokenStart
, cur
);
221 FinishValueState(oldValue
);
222 mHaveMultiLine
= false;
226 // there is nothing to do with normal characters,
227 // but handle multilines correctly
228 if (mHaveMultiLine
) {
229 if (c
== ' ' || c
== '\t') {
230 // don't allow another '\n' to be skipped
231 mMultiLineCanSkipN
= false;
232 // Now there is nothing to append to the mValue since we are skipping
233 // whitespaces at the beginning of the new line of the multiline
234 // property. Set tokenStart properly to ensure that nothing is appended
235 // if we find regular line-end or the end of the buffer.
239 mHaveMultiLine
= false;
242 break; // from switch on (c)
244 break; // from switch on (mSpecialState)
246 // saw a \ character, so parse the character after that
247 case eParserSpecial_Escaped
:
248 // probably want to start parsing at the next token
249 // other characters, like 'u' might override this
251 mSpecialState
= eParserSpecial_None
;
255 // the easy characters - \t, \n, and so forth
257 mValue
+= PRUnichar('\t');
258 mMinLength
= mValue
.Length();
261 mValue
+= PRUnichar('\n');
262 mMinLength
= mValue
.Length();
265 mValue
+= PRUnichar('\r');
266 mMinLength
= mValue
.Length();
269 mValue
+= PRUnichar('\\');
272 // switch to unicode mode!
275 mSpecialState
= eParserSpecial_Unicode
;
276 mUnicodeValuesRead
= 0;
280 // a \ immediately followed by a newline means we're going multiline
283 mHaveMultiLine
= true;
284 mMultiLineCanSkipN
= (c
== '\r');
285 mSpecialState
= eParserSpecial_None
;
289 // don't recognize the character, so just append it
295 // we're in the middle of parsing a 4-character unicode value
297 case eParserSpecial_Unicode
:
299 if(('0' <= c
) && (c
<= '9'))
301 (mUnicodeValue
<< 4) | (c
- '0');
302 else if(('a' <= c
) && (c
<= 'f'))
304 (mUnicodeValue
<< 4) | (c
- 'a' + 0x0a);
305 else if(('A' <= c
) && (c
<= 'F'))
307 (mUnicodeValue
<< 4) | (c
- 'A' + 0x0a);
309 // non-hex character. Append what we have, and move on.
310 mValue
+= mUnicodeValue
;
311 mMinLength
= mValue
.Length();
312 mSpecialState
= eParserSpecial_None
;
314 // leave tokenStart at this unknown character, so it gets appended
317 // ensure parsing this non-hex character again
321 if (++mUnicodeValuesRead
>= 4) {
323 mSpecialState
= eParserSpecial_None
;
324 mValue
+= mUnicodeValue
;
325 mMinLength
= mValue
.Length();
334 NS_METHOD
nsPropertiesParser::SegmentWriter(nsIUnicharInputStream
* aStream
,
336 const PRUnichar
*aFromSegment
,
339 uint32_t *aWriteCount
)
341 nsPropertiesParser
*parser
=
342 static_cast<nsPropertiesParser
*>(aClosure
);
344 parser
->ParseBuffer(aFromSegment
, aCount
);
346 *aWriteCount
= aCount
;
350 nsresult
nsPropertiesParser::ParseBuffer(const PRUnichar
* aBuffer
,
351 uint32_t aBufferLength
)
353 const PRUnichar
* cur
= aBuffer
;
354 const PRUnichar
* end
= aBuffer
+ aBufferLength
;
356 // points to the start/end of the current key or value
357 const PRUnichar
* tokenStart
= nullptr;
359 // if we're in the middle of parsing a key or value, make sure
360 // the current token points to the beginning of the current buffer
361 if (mState
== eParserState_Key
||
362 mState
== eParserState_Value
) {
363 tokenStart
= aBuffer
;
366 nsAutoString oldValue
;
373 case eParserState_AwaitingKey
:
374 if (c
== '#' || c
== '!')
377 else if (!IsWhiteSpace(c
)) {
378 // not a comment, not whitespace, we must have found a key!
384 case eParserState_Key
:
385 if (c
== '=' || c
== ':') {
386 mKey
+= Substring(tokenStart
, cur
);
391 case eParserState_AwaitingValue
:
393 // no value at all! mimic the normal value-ending
395 FinishValueState(oldValue
);
398 // ignore white space leading up to the value
399 else if (!IsWhiteSpace(c
)) {
403 // make sure to handle this first character
404 if (ParseValueCharacter(c
, cur
, tokenStart
, oldValue
))
406 // If the character isn't consumed, don't do cur++ and parse
407 // the character again. This can happen f.e. for char 'X' in sequence
408 // "\u00X". This character can be control character and must be
414 case eParserState_Value
:
415 if (ParseValueCharacter(c
, cur
, tokenStart
, oldValue
))
417 // See few lines above for reason of doing this
420 case eParserState_Comment
:
421 // stay in this state till we hit EOL
422 if (c
== '\r' || c
== '\n')
427 // finally, advance to the next character
431 // if we're still parsing the value and are in eParserSpecial_None, then
432 // append whatever we have..
433 if (mState
== eParserState_Value
&& tokenStart
&&
434 mSpecialState
== eParserSpecial_None
) {
435 mValue
+= Substring(tokenStart
, cur
);
437 // if we're still parsing the key, then append whatever we have..
438 else if (mState
== eParserState_Key
&& tokenStart
) {
439 mKey
+= Substring(tokenStart
, cur
);
445 nsPersistentProperties::nsPersistentProperties()
448 mSubclass
= static_cast<nsIPersistentProperties
*>(this);
449 mTable
.ops
= nullptr;
450 PL_INIT_ARENA_POOL(&mArena
, "PersistentPropertyArena", 2048);
453 nsPersistentProperties::~nsPersistentProperties()
455 PL_FinishArenaPool(&mArena
);
457 PL_DHashTableFinish(&mTable
);
461 nsPersistentProperties::Init()
463 if (!PL_DHashTableInit(&mTable
, &property_HashTableOps
, nullptr,
464 sizeof(PropertyTableEntry
), 20)) {
465 mTable
.ops
= nullptr;
466 return NS_ERROR_OUT_OF_MEMORY
;
472 nsPersistentProperties::Create(nsISupports
*aOuter
, REFNSIID aIID
, void **aResult
)
475 return NS_ERROR_NO_AGGREGATION
;
476 nsPersistentProperties
* props
= new nsPersistentProperties();
477 if (props
== nullptr)
478 return NS_ERROR_OUT_OF_MEMORY
;
481 nsresult rv
= props
->Init();
482 if (NS_SUCCEEDED(rv
))
483 rv
= props
->QueryInterface(aIID
, aResult
);
489 NS_IMPL_ISUPPORTS2(nsPersistentProperties
, nsIPersistentProperties
, nsIProperties
)
492 nsPersistentProperties::Load(nsIInputStream
*aIn
)
494 nsresult rv
= nsSimpleUnicharStreamFactory::GetInstance()->
495 CreateInstanceFromUTF8Stream(aIn
, getter_AddRefs(mIn
));
498 NS_WARNING("Error creating UnicharInputStream");
499 return NS_ERROR_FAILURE
;
502 nsPropertiesParser
parser(mSubclass
);
505 // If this 4096 is changed to some other value, make sure to adjust
506 // the bug121341.properties test file accordingly.
507 while (NS_SUCCEEDED(rv
= mIn
->ReadSegments(nsPropertiesParser::SegmentWriter
, &parser
, 4096, &nProcessed
)) &&
513 // We may have an unprocessed value at this point
514 // if the last line did not have a proper line ending.
515 if (parser
.GetState() == eParserState_Value
) {
516 nsAutoString oldValue
;
517 parser
.FinishValueState(oldValue
);
524 nsPersistentProperties::SetStringProperty(const nsACString
& aKey
,
525 const nsAString
& aNewValue
,
526 nsAString
& aOldValue
)
528 const nsAFlatCString
& flatKey
= PromiseFlatCString(aKey
);
529 PropertyTableEntry
*entry
=
530 static_cast<PropertyTableEntry
*>
531 (PL_DHashTableOperate(&mTable
, flatKey
.get(), PL_DHASH_ADD
));
534 aOldValue
= entry
->mValue
;
535 NS_WARNING(nsPrintfCString("the property %s already exists\n",
536 flatKey
.get()).get());
539 aOldValue
.Truncate();
542 entry
->mKey
= ArenaStrdup(flatKey
, &mArena
);
543 entry
->mValue
= ArenaStrdup(PromiseFlatString(aNewValue
), &mArena
);
549 nsPersistentProperties::Save(nsIOutputStream
* aOut
, const nsACString
& aHeader
)
551 return NS_ERROR_NOT_IMPLEMENTED
;
555 nsPersistentProperties::Subclass(nsIPersistentProperties
* aSubclass
)
558 mSubclass
= aSubclass
;
565 nsPersistentProperties::GetStringProperty(const nsACString
& aKey
,
568 const nsAFlatCString
& flatKey
= PromiseFlatCString(aKey
);
570 PropertyTableEntry
*entry
=
571 static_cast<PropertyTableEntry
*>
572 (PL_DHashTableOperate(&mTable
, flatKey
.get(), PL_DHASH_LOOKUP
));
574 if (PL_DHASH_ENTRY_IS_FREE(entry
))
575 return NS_ERROR_FAILURE
;
577 aValue
= entry
->mValue
;
581 static PLDHashOperator
582 AddElemToArray(PLDHashTable
* table
, PLDHashEntryHdr
*hdr
,
583 uint32_t i
, void *arg
)
585 nsCOMArray
<nsIPropertyElement
>* props
=
586 static_cast<nsCOMArray
<nsIPropertyElement
>*>(arg
);
587 PropertyTableEntry
* entry
=
588 static_cast<PropertyTableEntry
*>(hdr
);
590 nsPropertyElement
*element
=
591 new nsPropertyElement(nsDependentCString(entry
->mKey
),
592 nsDependentString(entry
->mValue
));
594 props
->AppendObject(element
);
596 return PL_DHASH_NEXT
;
601 nsPersistentProperties::Enumerate(nsISimpleEnumerator
** aResult
)
603 nsCOMArray
<nsIPropertyElement
> props
;
605 // We know the necessary size; we can avoid growing it while adding elements
606 props
.SetCapacity(mTable
.entryCount
);
608 // Step through hash entries populating a transient array
610 PL_DHashTableEnumerate(&mTable
, AddElemToArray
, (void *)&props
);
611 if (n
< mTable
.entryCount
)
612 return NS_ERROR_OUT_OF_MEMORY
;
614 return NS_NewArrayEnumerator(aResult
, props
);
617 ////////////////////////////////////////////////////////////////////////////////
618 // XXX Some day we'll unify the nsIPersistentProperties interface with
619 // nsIProperties, but until now...
622 nsPersistentProperties::Get(const char* prop
, const nsIID
& uuid
, void* *result
)
624 return NS_ERROR_NOT_IMPLEMENTED
;
628 nsPersistentProperties::Set(const char* prop
, nsISupports
* value
)
630 return NS_ERROR_NOT_IMPLEMENTED
;
633 nsPersistentProperties::Undefine(const char* prop
)
635 return NS_ERROR_NOT_IMPLEMENTED
;
639 nsPersistentProperties::Has(const char* prop
, bool *result
)
641 PropertyTableEntry
*entry
=
642 static_cast<PropertyTableEntry
*>
643 (PL_DHashTableOperate(&mTable
, prop
, PL_DHASH_LOOKUP
));
645 *result
= (entry
&& PL_DHASH_ENTRY_IS_BUSY(entry
));
651 nsPersistentProperties::GetKeys(uint32_t *count
, char ***keys
)
653 return NS_ERROR_NOT_IMPLEMENTED
;
656 ////////////////////////////////////////////////////////////////////////////////
658 ////////////////////////////////////////////////////////////////////////////////
661 nsPropertyElement::Create(nsISupports
*aOuter
, REFNSIID aIID
, void **aResult
)
664 return NS_ERROR_NO_AGGREGATION
;
665 nsPropertyElement
* propElem
= new nsPropertyElement();
666 if (propElem
== nullptr)
667 return NS_ERROR_OUT_OF_MEMORY
;
669 nsresult rv
= propElem
->QueryInterface(aIID
, aResult
);
670 NS_RELEASE(propElem
);
674 NS_IMPL_ISUPPORTS1(nsPropertyElement
, nsIPropertyElement
)
677 nsPropertyElement::GetKey(nsACString
& aReturnKey
)
684 nsPropertyElement::GetValue(nsAString
& aReturnValue
)
686 aReturnValue
= mValue
;
691 nsPropertyElement::SetKey(const nsACString
& aKey
)
698 nsPropertyElement::SetValue(const nsAString
& aValue
)
704 ////////////////////////////////////////////////////////////////////////////////