Backed out changeset 4c2bc5ae8f95 (bug 945562) for device image bustage.
[gecko.git] / xpcom / ds / nsPersistentProperties.cpp
blob0b9b2606b64360eb72421b4389ad1610c45b4ec3
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"
7 #include "nsID.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
19 const char *mKey;
20 const PRUnichar *mValue;
23 static PRUnichar*
24 ArenaStrdup(const nsAFlatString& aString, PLArenaPool* aArena)
26 void *mem;
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");
31 if (mem) {
32 memcpy(mem, aString.get(), len);
34 return static_cast<PRUnichar*>(mem);
37 static char*
38 ArenaStrdup(const nsAFlatCString& aString, PLArenaPool* aArena)
40 void *mem;
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");
45 if (mem)
46 memcpy(mem, aString.get(), len);
47 return static_cast<char*>(mem);
50 static const struct PLDHashTableOps property_HashTableOps = {
51 PL_DHashAllocTable,
52 PL_DHashFreeTable,
53 PL_DHashStringKey,
54 PL_DHashMatchStringKey,
55 PL_DHashMoveEntryStub,
56 PL_DHashClearEntryStub,
57 PL_DHashFinalizeStub,
58 nullptr,
62 // parser stuff
64 enum EParserState {
65 eParserState_AwaitingKey,
66 eParserState_Key,
67 eParserState_AwaitingValue,
68 eParserState_Value,
69 eParserState_Comment
72 enum EParserSpecial {
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
80 public:
81 nsPropertiesParser(nsIPersistentProperties* aProps) :
82 mHaveMultiLine(false), mState(eParserState_AwaitingKey),
83 mProps(aProps) {}
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;
92 if (minLength)
94 backup_char = mValue[minLength-1];
95 mValue.SetCharAt('x', minLength-1);
97 mValue.Trim(trimThese, false, true);
98 if (minLength)
99 mValue.SetCharAt(backup_char, minLength-1);
101 mProps->SetStringProperty(NS_ConvertUTF16toUTF8(mKey), mValue, aOldValue);
102 mSpecialState = eParserSpecial_None;
103 WaitForKey();
106 EParserState GetState() { return mState; }
108 static NS_METHOD SegmentWriter(nsIUnicharInputStream* aStream,
109 void* aClosure,
110 const PRUnichar *aFromSegment,
111 uint32_t aToOffset,
112 uint32_t aCount,
113 uint32_t *aWriteCount);
115 nsresult ParseBuffer(const PRUnichar* aBuffer, uint32_t aBufferLength);
117 private:
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
123 // of this block
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
128 void WaitForKey() {
129 mState = eParserState_AwaitingKey;
132 void EnterKeyState() {
133 mKey.Truncate();
134 mState = eParserState_Key;
137 void WaitForValue() {
138 mState = eParserState_AwaitingValue;
141 void EnterValueState() {
142 mValue.Truncate();
143 mMinLength = 0;
144 mState = eParserState_Value;
145 mSpecialState = eParserSpecial_None;
148 void EnterCommentState() {
149 mState = eParserState_Comment;
152 nsAutoString mKey;
153 nsAutoString mValue;
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:
159 // - "\\\r"
160 // - "\\\n"
161 // - "\\\r\n"
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
167 EParserState mState;
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,
187 nsAString& oldValue)
189 switch (mSpecialState) {
191 // the normal state - look for special characters
192 case eParserSpecial_None:
193 switch (c) {
194 case '\\':
195 if (mHaveMultiLine)
196 // there is nothing to append to mValue yet
197 mHaveMultiLine = false;
198 else
199 mValue += Substring(tokenStart, cur);
201 mSpecialState = eParserSpecial_Escaped;
202 break;
204 case '\n':
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.
213 tokenStart = cur+1;
214 break;
216 // no break
218 case '\r':
219 // we're done! We have a key and value
220 mValue += Substring(tokenStart, cur);
221 FinishValueState(oldValue);
222 mHaveMultiLine = false;
223 break;
225 default:
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.
236 tokenStart = cur+1;
237 break;
239 mHaveMultiLine = false;
240 tokenStart = cur;
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
250 tokenStart = cur+1;
251 mSpecialState = eParserSpecial_None;
253 switch (c) {
255 // the easy characters - \t, \n, and so forth
256 case 't':
257 mValue += PRUnichar('\t');
258 mMinLength = mValue.Length();
259 break;
260 case 'n':
261 mValue += PRUnichar('\n');
262 mMinLength = mValue.Length();
263 break;
264 case 'r':
265 mValue += PRUnichar('\r');
266 mMinLength = mValue.Length();
267 break;
268 case '\\':
269 mValue += PRUnichar('\\');
270 break;
272 // switch to unicode mode!
273 case 'u':
274 case 'U':
275 mSpecialState = eParserSpecial_Unicode;
276 mUnicodeValuesRead = 0;
277 mUnicodeValue = 0;
278 break;
280 // a \ immediately followed by a newline means we're going multiline
281 case '\r':
282 case '\n':
283 mHaveMultiLine = true;
284 mMultiLineCanSkipN = (c == '\r');
285 mSpecialState = eParserSpecial_None;
286 break;
288 default:
289 // don't recognize the character, so just append it
290 mValue += c;
291 break;
293 break;
295 // we're in the middle of parsing a 4-character unicode value
296 // like \u5f39
297 case eParserSpecial_Unicode:
299 if(('0' <= c) && (c <= '9'))
300 mUnicodeValue =
301 (mUnicodeValue << 4) | (c - '0');
302 else if(('a' <= c) && (c <= 'f'))
303 mUnicodeValue =
304 (mUnicodeValue << 4) | (c - 'a' + 0x0a);
305 else if(('A' <= c) && (c <= 'F'))
306 mUnicodeValue =
307 (mUnicodeValue << 4) | (c - 'A' + 0x0a);
308 else {
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
315 tokenStart = cur;
317 // ensure parsing this non-hex character again
318 return false;
321 if (++mUnicodeValuesRead >= 4) {
322 tokenStart = cur+1;
323 mSpecialState = eParserSpecial_None;
324 mValue += mUnicodeValue;
325 mMinLength = mValue.Length();
328 break;
331 return true;
334 NS_METHOD nsPropertiesParser::SegmentWriter(nsIUnicharInputStream* aStream,
335 void* aClosure,
336 const PRUnichar *aFromSegment,
337 uint32_t aToOffset,
338 uint32_t aCount,
339 uint32_t *aWriteCount)
341 nsPropertiesParser *parser =
342 static_cast<nsPropertiesParser *>(aClosure);
344 parser->ParseBuffer(aFromSegment, aCount);
346 *aWriteCount = aCount;
347 return NS_OK;
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;
368 while (cur != end) {
370 PRUnichar c = *cur;
372 switch (mState) {
373 case eParserState_AwaitingKey:
374 if (c == '#' || c == '!')
375 EnterCommentState();
377 else if (!IsWhiteSpace(c)) {
378 // not a comment, not whitespace, we must have found a key!
379 EnterKeyState();
380 tokenStart = cur;
382 break;
384 case eParserState_Key:
385 if (c == '=' || c == ':') {
386 mKey += Substring(tokenStart, cur);
387 WaitForValue();
389 break;
391 case eParserState_AwaitingValue:
392 if (IsEOL(c)) {
393 // no value at all! mimic the normal value-ending
394 EnterValueState();
395 FinishValueState(oldValue);
398 // ignore white space leading up to the value
399 else if (!IsWhiteSpace(c)) {
400 tokenStart = cur;
401 EnterValueState();
403 // make sure to handle this first character
404 if (ParseValueCharacter(c, cur, tokenStart, oldValue))
405 cur++;
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
409 // processed again.
410 continue;
412 break;
414 case eParserState_Value:
415 if (ParseValueCharacter(c, cur, tokenStart, oldValue))
416 cur++;
417 // See few lines above for reason of doing this
418 continue;
420 case eParserState_Comment:
421 // stay in this state till we hit EOL
422 if (c == '\r' || c== '\n')
423 WaitForKey();
424 break;
427 // finally, advance to the next character
428 cur++;
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);
442 return NS_OK;
445 nsPersistentProperties::nsPersistentProperties()
446 : mIn(nullptr)
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);
456 if (mTable.ops)
457 PL_DHashTableFinish(&mTable);
460 nsresult
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;
468 return NS_OK;
471 nsresult
472 nsPersistentProperties::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
474 if (aOuter)
475 return NS_ERROR_NO_AGGREGATION;
476 nsPersistentProperties* props = new nsPersistentProperties();
477 if (props == nullptr)
478 return NS_ERROR_OUT_OF_MEMORY;
480 NS_ADDREF(props);
481 nsresult rv = props->Init();
482 if (NS_SUCCEEDED(rv))
483 rv = props->QueryInterface(aIID, aResult);
485 NS_RELEASE(props);
486 return rv;
489 NS_IMPL_ISUPPORTS2(nsPersistentProperties, nsIPersistentProperties, nsIProperties)
491 NS_IMETHODIMP
492 nsPersistentProperties::Load(nsIInputStream *aIn)
494 nsresult rv = nsSimpleUnicharStreamFactory::GetInstance()->
495 CreateInstanceFromUTF8Stream(aIn, getter_AddRefs(mIn));
497 if (rv != NS_OK) {
498 NS_WARNING("Error creating UnicharInputStream");
499 return NS_ERROR_FAILURE;
502 nsPropertiesParser parser(mSubclass);
504 uint32_t nProcessed;
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)) &&
508 nProcessed != 0);
509 mIn = nullptr;
510 if (NS_FAILED(rv))
511 return rv;
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);
520 return NS_OK;
523 NS_IMETHODIMP
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));
533 if (entry->mKey) {
534 aOldValue = entry->mValue;
535 NS_WARNING(nsPrintfCString("the property %s already exists\n",
536 flatKey.get()).get());
538 else {
539 aOldValue.Truncate();
542 entry->mKey = ArenaStrdup(flatKey, &mArena);
543 entry->mValue = ArenaStrdup(PromiseFlatString(aNewValue), &mArena);
545 return NS_OK;
548 NS_IMETHODIMP
549 nsPersistentProperties::Save(nsIOutputStream* aOut, const nsACString& aHeader)
551 return NS_ERROR_NOT_IMPLEMENTED;
554 NS_IMETHODIMP
555 nsPersistentProperties::Subclass(nsIPersistentProperties* aSubclass)
557 if (aSubclass) {
558 mSubclass = aSubclass;
561 return NS_OK;
564 NS_IMETHODIMP
565 nsPersistentProperties::GetStringProperty(const nsACString& aKey,
566 nsAString& aValue)
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;
578 return NS_OK;
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;
600 NS_IMETHODIMP
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
609 uint32_t n =
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...
621 NS_IMETHODIMP
622 nsPersistentProperties::Get(const char* prop, const nsIID & uuid, void* *result)
624 return NS_ERROR_NOT_IMPLEMENTED;
627 NS_IMETHODIMP
628 nsPersistentProperties::Set(const char* prop, nsISupports* value)
630 return NS_ERROR_NOT_IMPLEMENTED;
632 NS_IMETHODIMP
633 nsPersistentProperties::Undefine(const char* prop)
635 return NS_ERROR_NOT_IMPLEMENTED;
638 NS_IMETHODIMP
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));
647 return NS_OK;
650 NS_IMETHODIMP
651 nsPersistentProperties::GetKeys(uint32_t *count, char ***keys)
653 return NS_ERROR_NOT_IMPLEMENTED;
656 ////////////////////////////////////////////////////////////////////////////////
657 // PropertyElement
658 ////////////////////////////////////////////////////////////////////////////////
660 NS_METHOD
661 nsPropertyElement::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
663 if (aOuter)
664 return NS_ERROR_NO_AGGREGATION;
665 nsPropertyElement* propElem = new nsPropertyElement();
666 if (propElem == nullptr)
667 return NS_ERROR_OUT_OF_MEMORY;
668 NS_ADDREF(propElem);
669 nsresult rv = propElem->QueryInterface(aIID, aResult);
670 NS_RELEASE(propElem);
671 return rv;
674 NS_IMPL_ISUPPORTS1(nsPropertyElement, nsIPropertyElement)
676 NS_IMETHODIMP
677 nsPropertyElement::GetKey(nsACString& aReturnKey)
679 aReturnKey = mKey;
680 return NS_OK;
683 NS_IMETHODIMP
684 nsPropertyElement::GetValue(nsAString& aReturnValue)
686 aReturnValue = mValue;
687 return NS_OK;
690 NS_IMETHODIMP
691 nsPropertyElement::SetKey(const nsACString& aKey)
693 mKey = aKey;
694 return NS_OK;
697 NS_IMETHODIMP
698 nsPropertyElement::SetValue(const nsAString& aValue)
700 mValue = aValue;
701 return NS_OK;
704 ////////////////////////////////////////////////////////////////////////////////