From 5466043190dac5b352df63942d26c1e2614dd9af Mon Sep 17 00:00:00 2001 From: chatty Date: Tue, 19 Jan 1999 18:20:36 +0000 Subject: Added collision handling in tables. Added initialisation of arrays --- irtable.c | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) (limited to 'irtable.c') diff --git a/irtable.c b/irtable.c index 9be02fa..c62aa95 100644 --- a/irtable.c +++ b/irtable.c @@ -1,3 +1,16 @@ +/* + * + * IRBOX, an Ivy driver for infra-red remote controls + * + * Copyright 1998-1999 + * Centre d'Etudes de la Navigation Aerienne + * + * Tables for decoding events + * + * $Id$ + * + */ + #include "irtable.h" #include "irdev.h" #include @@ -14,11 +27,23 @@ struct _irtablecell { }; +#define KEYCELLS 127 +#define CODECELLS 1023 + +static IrTableCell* KeyToCell [KEYCELLS]; +static IrTableCell* CodeToCell [CODECELLS]; +static int Initialized = 0; + IrTable* IrCreateTable () { + if (!Initialized) { + Initialized = 1; + memset (KeyToCell, 0, sizeof (KeyToCell)); + memset (CodeToCell, 0, sizeof (CodeToCell)); + } return (IrTable*) malloc (sizeof (IrTable)); } @@ -41,12 +66,6 @@ IrTableAddBrand (IrTable* t, const char* b) -#define KEYCELLS 127 -#define CODECELLS 1023 - -static IrTableCell* KeyToCell [KEYCELLS]; -static IrTableCell* CodeToCell [CODECELLS]; - static unsigned int IrHashKey (const char* key) { @@ -73,9 +92,9 @@ IrTableAddKey (IrTable* t, const char* k, int c0, int c1, int c2, int c3 , int c { IrTableCell* c = (IrTableCell*) malloc (sizeof (IrTableCell)); IrTableCell** cc; + IrTableCell* p; c->table = t; - c->key = strdup (k); c->code[0] = c0; c->code[1] = c1; c->code[2] = c2; @@ -83,7 +102,22 @@ IrTableAddKey (IrTable* t, const char* k, int c0, int c1, int c2, int c3 , int c c->code[4] = c4; c->code[5] = c5; cc = CodeToCell + IrHashCode (c->code); - /* GERER LES COLLISIONS */ + + /* collision handling */ + p = *cc; + while (p) { + if (strncmp (p->code, c->code, 6) == 0) { + fprintf (stderr, "Code for key \'%s\' on %s", k, t->name); + fprintf (stderr, " was already registered"); + fprintf (stderr, " for key \'%s\' on %s\n", p->key, p->table->name); + free (c); + return; + } + p = p->next; + } + + /* fine, we can keep on storing that key */ + c->key = strdup (k); c->next = *cc; *cc = c; } -- cgit v1.1