summaryrefslogtreecommitdiff
path: root/irtable.c
blob: 9be02fa6c069a11fe8fa3d23ded86cc59b8a0fa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "irtable.h"
#include "irdev.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct _irtablecell IrTableCell;

struct _irtablecell {
	IrTable* table;
	unsigned char code[6];
	const char* key;
	IrTableCell*	next;
};




IrTable*
IrCreateTable ()
{
	return (IrTable*) malloc (sizeof (IrTable));
}

void
IrTableName (IrTable* t, const char* n)
{
	t->name = strdup (n);
}

void
IrTableType (IrTable* t, const char* type)
{
	t->type = strdup (type);
}

void
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)
{
	register unsigned int h = 0;
	register const char* p = key;
	while (*p)
		h += (h << 3) + *p++;	/* this hash function was stolen from Tcl */
	return h % KEYCELLS;
}

static unsigned int
IrHashCode (const unsigned char* code)
{
	register unsigned int h = 0;
	register const unsigned char* p = code;
	register const unsigned char* q = p+6;
	while (p < q)
		h += (h << 3) + *p++;	/* A ADAPTER */
	return h % CODECELLS;
}

void
IrTableAddKey (IrTable* t, const char* k, int c0, int c1, int c2, int c3 , int c4, int c5)
{
	IrTableCell* c = (IrTableCell*) malloc (sizeof (IrTableCell));
	IrTableCell** cc;

	c->table = t;
	c->key = strdup (k);
	c->code[0] = c0;
	c->code[1] = c1;
	c->code[2] = c2;
	c->code[3] = c3;
	c->code[4] = c4;
	c->code[5] = c5;
	cc = CodeToCell + IrHashCode (c->code);
	/* GERER LES COLLISIONS */
	c->next = *cc;
	*cc = c;
}

int
IrTableTranslateCode (const unsigned char* code, IrTable** table, const char** key)
{
	IrTableCell* c;

	c = CodeToCell [IrHashCode (code)];
	while (c) {
		if (strncmp (code, c->code, 6) == 0)
			break;
		c = c->next;
	}
	if (c) {
		if (table) *table = c->table;
		if (key) *key = c->key;
	} else {
		if (table) *table = 0;
		if (key) *key = 0;
	}
	return (int) (c);
}