gecko-dev/layout/style/nsCSSKeywords.cpp

151 строка
4.0 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
1998-04-14 00:24:54 +04:00
#include "nsCSSKeywords.h"
#include "nsString.h"
#include "nsAVLTree.h"
1998-04-14 00:24:54 +04:00
// define an array of all CSS keywords
#define CSS_KEY(_key) #_key,
const char* kCSSRawKeywords[] = {
#include "nsCSSKeywordList.h"
1998-04-14 00:24:54 +04:00
};
#undef CSS_KEY
1998-04-14 00:24:54 +04:00
struct KeywordNode {
KeywordNode(void)
: mStr(),
mEnum(eCSSKeyword_UNKNOWN)
{}
1998-04-14 00:24:54 +04:00
KeywordNode(const nsStr& aStringValue, nsCSSKeyword aEnumValue)
: mStr(),
mEnum(aEnumValue)
{ // point to the incomming buffer
// note that the incomming buffer may really be 2 byte
nsStr::Initialize(mStr, aStringValue.mStr, aStringValue.mCapacity,
aStringValue.mLength, aStringValue.mCharSize, PR_FALSE);
}
1998-04-14 00:24:54 +04:00
nsCAutoString mStr;
nsCSSKeyword mEnum;
1998-04-14 00:24:54 +04:00
};
class KeywordComparitor: public nsAVLNodeComparitor {
public:
virtual ~KeywordComparitor(void) {}
virtual PRInt32 operator()(void* anItem1,void* anItem2) {
KeywordNode* one = (KeywordNode*)anItem1;
KeywordNode* two = (KeywordNode*)anItem2;
2000-05-15 03:06:18 +04:00
return one->mStr.CompareWithConversion(two->mStr, PR_TRUE);
}
};
1998-04-14 00:24:54 +04:00
static PRInt32 gTableRefCount;
static KeywordNode* gKeywordArray;
static nsAVLTree* gKeywordTree;
static KeywordComparitor* gComparitor;
static nsCString* kNullStr;
1998-04-14 00:24:54 +04:00
void
nsCSSKeywords::AddRefTable(void)
{
if (0 == gTableRefCount++) {
if (! gKeywordArray) {
gKeywordArray = new KeywordNode[eCSSKeyword_COUNT];
gComparitor = new KeywordComparitor();
if (gComparitor) {
gKeywordTree = new nsAVLTree(*gComparitor, nsnull);
}
if (gKeywordArray && gKeywordTree) {
PRInt32 index = -1;
while (++index < PRInt32(eCSSKeyword_COUNT)) {
gKeywordArray[index].mStr = kCSSRawKeywords[index];
gKeywordArray[index].mStr.ReplaceChar('_', '-');
gKeywordArray[index].mEnum = nsCSSKeyword(index);
gKeywordTree->AddItem(&(gKeywordArray[index]));
}
}
}
1999-10-05 18:51:56 +04:00
kNullStr = new nsCString();
}
}
void
nsCSSKeywords::ReleaseTable(void)
{
if (0 == --gTableRefCount) {
if (gKeywordArray) {
delete [] gKeywordArray;
gKeywordArray = nsnull;
1998-04-14 00:24:54 +04:00
}
if (gKeywordTree) {
delete gKeywordTree;
gKeywordTree = nsnull;
}
if (gComparitor) {
delete gComparitor;
gComparitor = nsnull;
}
1999-10-05 18:51:56 +04:00
delete kNullStr;
}
}
1998-04-14 00:24:54 +04:00
nsCSSKeyword
2000-03-12 13:07:57 +03:00
nsCSSKeywords::LookupKeyword(const nsCString& aKeyword)
{
NS_ASSERTION(gKeywordTree, "no lookup table, needs addref");
if (gKeywordTree) {
KeywordNode node(aKeyword, eCSSKeyword_UNKNOWN);
KeywordNode* found = (KeywordNode*)gKeywordTree->FindItem(&node);
if (found) {
NS_ASSERTION(found->mStr.EqualsIgnoreCase(aKeyword), "bad tree");
return found->mEnum;
1998-04-14 00:24:54 +04:00
}
}
return eCSSKeyword_UNKNOWN;
}
2000-03-12 13:07:57 +03:00
nsCSSKeyword
nsCSSKeywords::LookupKeyword(const nsString& aKeyword) {
nsCAutoString theKeyword; theKeyword.AssignWithConversion(aKeyword);
2000-03-12 13:07:57 +03:00
return LookupKeyword(theKeyword);
}
const nsCString&
nsCSSKeywords::GetStringValue(nsCSSKeyword aKeyword)
{
NS_ASSERTION(gKeywordArray, "no lookup table, needs addref");
if ((eCSSKeyword_UNKNOWN < aKeyword) &&
(aKeyword < eCSSKeyword_COUNT) && gKeywordArray) {
return gKeywordArray[aKeyword].mStr;
}
else {
1999-10-05 18:51:56 +04:00
return *kNullStr;
}
1998-04-14 00:24:54 +04:00
}