gecko-dev/layout/style/nsCSSKeywords.cpp

139 lines
3.7 KiB
C++
Raw Normal View History

/* -*- 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.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
* Reserved.
*/
1998-04-13 20:24:54 +00:00
#include "nsCSSKeywords.h"
#include "nsString.h"
#include "nsAVLTree.h"
1998-04-13 20:24:54 +00:00
// define an array of all CSS keywords
#define CSS_KEY(_key) #_key,
const char* kCSSRawKeywords[] = {
#include "nsCSSKeywordList.h"
1998-04-13 20:24:54 +00:00
};
#undef CSS_KEY
1998-04-13 20:24:54 +00:00
struct KeywordNode {
KeywordNode(void)
: mStr(),
mEnum(eCSSKeyword_UNKNOWN)
{}
1998-04-13 20:24:54 +00: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-13 20:24:54 +00:00
nsCAutoString mStr;
nsCSSKeyword mEnum;
1998-04-13 20:24:54 +00:00
};
class KeywordComparitor: public nsAVLNodeComparitor {
public:
virtual ~KeywordComparitor(void) {}
virtual PRInt32 operator()(void* anItem1,void* anItem2) {
KeywordNode* one = (KeywordNode*)anItem1;
KeywordNode* two = (KeywordNode*)anItem2;
return one->mStr.Compare(two->mStr, PR_TRUE);
}
};
1998-04-13 20:24:54 +00:00
static PRInt32 gTableRefCount;
static KeywordNode* gKeywordArray;
static nsAVLTree* gKeywordTree;
static KeywordComparitor* gComparitor;
1998-04-13 20:24:54 +00: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]));
}
}
}
}
}
void
nsCSSKeywords::ReleaseTable(void)
{
if (0 == --gTableRefCount) {
if (gKeywordArray) {
delete [] gKeywordArray;
gKeywordArray = nsnull;
1998-04-13 20:24:54 +00:00
}
if (gKeywordTree) {
delete gKeywordTree;
gKeywordTree = nsnull;
}
if (gComparitor) {
delete gComparitor;
gComparitor = nsnull;
}
}
}
1998-04-13 20:24:54 +00:00
nsCSSKeyword
nsCSSKeywords::LookupKeyword(const nsStr& 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-13 20:24:54 +00:00
}
}
return eCSSKeyword_UNKNOWN;
}
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 {
static const nsCString kNullStr;
return kNullStr;
}
1998-04-13 20:24:54 +00:00
}