atom: Coding-style cleanup and refactor
Some coding style nits were cleaned up. Additionally, most of the functions have been collapsed from the libxkbfile version where there's distinction with the Xlib atom functions when Display was set. Finally, the InitAtoms function tests whether the table has already been created by testing the pointer rather than using a static int.master
parent
c88c0ba725
commit
433a405c98
75
src/atom.c
75
src/atom.c
|
@ -88,68 +88,54 @@ typedef struct _Node {
|
|||
#define BAD_RESOURCE 0xe0000000
|
||||
|
||||
static Atom lastAtom = None;
|
||||
static NodePtr atomRoot = (NodePtr)NULL;
|
||||
static NodePtr atomRoot = NULL;
|
||||
static unsigned long tableLength;
|
||||
static NodePtr *nodeTable;
|
||||
|
||||
static void
|
||||
_XkbInitAtoms(void)
|
||||
{
|
||||
tableLength = InitialTableSize;
|
||||
nodeTable = (NodePtr *)_XkbAlloc(InitialTableSize*sizeof(NodePtr));
|
||||
nodeTable[None] = (NodePtr)NULL;
|
||||
}
|
||||
static NodePtr *nodeTable = NULL;
|
||||
|
||||
void
|
||||
XkbcInitAtoms(void)
|
||||
{
|
||||
static int been_here= 0;
|
||||
if (!been_here) {
|
||||
_XkbInitAtoms();
|
||||
been_here= 1;
|
||||
}
|
||||
if (nodeTable)
|
||||
return;
|
||||
}
|
||||
|
||||
static char *
|
||||
_XkbNameForAtom(Atom atom)
|
||||
{
|
||||
NodePtr node;
|
||||
if (atom > lastAtom) return NULL;
|
||||
if ((node = nodeTable[atom]) == (NodePtr)NULL) return NULL;
|
||||
return strdup(node->string);
|
||||
tableLength = InitialTableSize;
|
||||
nodeTable = (NodePtr *)_XkbAlloc(InitialTableSize * sizeof(NodePtr));
|
||||
nodeTable[None] = NULL;
|
||||
}
|
||||
|
||||
char *
|
||||
XkbcAtomGetString(Atom atm)
|
||||
XkbcAtomGetString(Atom atom)
|
||||
{
|
||||
if (atm==None)
|
||||
NodePtr node;
|
||||
|
||||
if ((atom == None) || (atom > lastAtom))
|
||||
return NULL;
|
||||
return _XkbNameForAtom(atm);
|
||||
if (!(node = nodeTable[atom]))
|
||||
return NULL;
|
||||
return strdup(node->string);
|
||||
}
|
||||
|
||||
static Atom
|
||||
_XkbMakeAtom(char *string,unsigned len,Bool makeit)
|
||||
_XkbcMakeAtom(char *string, unsigned len, Bool makeit)
|
||||
{
|
||||
register NodePtr * np;
|
||||
NodePtr *np;
|
||||
unsigned i;
|
||||
int comp;
|
||||
register unsigned int fp = 0;
|
||||
unsigned int fp = 0;
|
||||
|
||||
np = &atomRoot;
|
||||
for (i = 0; i < (len+1)/2; i++)
|
||||
{
|
||||
for (i = 0; i < (len + 1) / 2; i++) {
|
||||
fp = fp * 27 + string[i];
|
||||
fp = fp * 27 + string[len - 1 - i];
|
||||
}
|
||||
while (*np != (NodePtr) NULL)
|
||||
{
|
||||
|
||||
while (*np) {
|
||||
if (fp < (*np)->fingerPrint)
|
||||
np = &((*np)->left);
|
||||
else if (fp > (*np)->fingerPrint)
|
||||
np = &((*np)->right);
|
||||
else
|
||||
{ /* now start testing the strings */
|
||||
else {
|
||||
/* now start testing the strings */
|
||||
comp = strncmp(string, (*np)->string, (int)len);
|
||||
if ((comp < 0) || ((comp == 0) && (len < strlen((*np)->string))))
|
||||
np = &((*np)->left);
|
||||
|
@ -159,13 +145,14 @@ _XkbMakeAtom(char *string,unsigned len,Bool makeit)
|
|||
return(*np)->a;
|
||||
}
|
||||
}
|
||||
if (makeit)
|
||||
{
|
||||
register NodePtr nd;
|
||||
|
||||
if (makeit) {
|
||||
NodePtr nd;
|
||||
|
||||
nd = (NodePtr)_XkbAlloc(sizeof(NodeRec));
|
||||
if (!nd)
|
||||
return BAD_RESOURCE;
|
||||
|
||||
nd->string = (char *)_XkbAlloc(len + 1);
|
||||
if (!nd->string) {
|
||||
_XkbFree(nd);
|
||||
|
@ -173,11 +160,12 @@ _XkbMakeAtom(char *string,unsigned len,Bool makeit)
|
|||
}
|
||||
strncpy(nd->string, string, (int)len);
|
||||
nd->string[len] = 0;
|
||||
|
||||
if ((lastAtom + 1) >= tableLength) {
|
||||
NodePtr *table;
|
||||
|
||||
table = (NodePtr *)_XkbRealloc(nodeTable,
|
||||
tableLength * (2 * sizeof(NodePtr)));
|
||||
tableLength * 2 * sizeof(NodePtr));
|
||||
if (!table) {
|
||||
if (nd->string != string)
|
||||
_XkbFree(nd->string);
|
||||
|
@ -185,13 +173,16 @@ _XkbMakeAtom(char *string,unsigned len,Bool makeit)
|
|||
return BAD_RESOURCE;
|
||||
}
|
||||
tableLength <<= 1;
|
||||
|
||||
nodeTable = table;
|
||||
}
|
||||
|
||||
*np = nd;
|
||||
nd->left = nd->right = (NodePtr) NULL;
|
||||
nd->left = nd->right = NULL;
|
||||
nd->fingerPrint = fp;
|
||||
nd->a = (++lastAtom);
|
||||
*(nodeTable + lastAtom) = nd;
|
||||
|
||||
return nd->a;
|
||||
}
|
||||
else
|
||||
|
@ -201,7 +192,7 @@ _XkbMakeAtom(char *string,unsigned len,Bool makeit)
|
|||
Atom
|
||||
XkbcInternAtom(char *name, Bool onlyIfExists)
|
||||
{
|
||||
if (name==NULL)
|
||||
if (!name)
|
||||
return None;
|
||||
return _XkbMakeAtom(name, strlen(name), (!onlyIfExists));
|
||||
return _XkbcMakeAtom(name, strlen(name), !onlyIfExists);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue