Add 2007 to copyright years.
[bpt/emacs.git] / oldXMenu / XMakeAssoc.c
CommitLineData
e745ede7 1/* Copyright Massachusetts Institute of Technology 1985 */
61e1e4e8 2/* Copyright (C) 2001, 2002, 2003, 2004, 2005,
4e6835db 3 2006, 2007 Free Software Foundation, Inc. */
e745ede7
DL
4
5/*
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation, and that the name of M.I.T. not be used in advertising or
11publicity pertaining to distribution of the software without specific,
12written prior permission. M.I.T. makes no representations about the
13suitability of this software for any purpose. It is provided "as is"
14without express or implied warranty.
15*/
16
17#include <config.h>
18#include <X11/Xlib.h>
19#include <X11/Xresource.h>
20#include "X10.h"
21#include <errno.h>
22
23#ifndef NULL
24#define NULL 0
25#endif
26
27extern int errno;
28
29void emacs_insque();
30struct qelem {
31 struct qelem *q_forw;
32 struct qelem *q_back;
33 char q_data[1];
34};
35/*
36 * XMakeAssoc - Insert data into an XAssocTable keyed on an XId.
37 * Data is inserted into the table only once. Redundant inserts are
38 * meaningless (but cause no problems). The queue in each association
39 * bucket is sorted (lowest XId to highest XId).
40 */
41XMakeAssoc(dpy, table, x_id, data)
42 register Display *dpy;
43 register XAssocTable *table;
44 register XID x_id;
45 register caddr_t data;
46{
47 int hash;
48 register XAssoc *bucket;
49 register XAssoc *Entry;
50 register XAssoc *new_entry;
177c0ea7 51
e745ede7
DL
52 /* Hash the XId to get the bucket number. */
53 hash = x_id & (table->size - 1);
54 /* Look up the bucket to get the entries in that bucket. */
55 bucket = &table->buckets[hash];
56 /* Get the first entry in the bucket. */
57 Entry = bucket->next;
58
59 /* If (Entry != bucket), the bucket is empty so make */
60 /* the new entry the first entry in the bucket. */
61 /* if (Entry == bucket), the we have to search the */
62 /* bucket. */
63 if (Entry != bucket) {
64 /* The bucket isn't empty, begin searching. */
65 /* If we leave the for loop then we have either passed */
66 /* where the entry should be or hit the end of the bucket. */
67 /* In either case we should then insert the new entry */
68 /* before the current value of "Entry". */
69 for (; Entry != bucket; Entry = Entry->next) {
70 if (Entry->x_id == x_id) {
71 /* Entry has the same XId... */
72 if (Entry->display == dpy) {
73 /* Entry has the same Display... */
74 /* Therefore there is already an */
75 /* entry with this XId and Display, */
76 /* reset its data value and return. */
77 Entry->data = data;
78 return;
79 }
80 /* We found an association with the right */
81 /* id but the wrong display! */
82 continue;
83 }
84 /* If the current entry's XId is greater than the */
85 /* XId of the entry to be inserted then we have */
86 /* passed the location where the new XId should */
87 /* be inserted. */
88 if (Entry->x_id > x_id) break;
89 }
90 }
91
92 /* If we are here then the new entry should be inserted just */
93 /* before the current value of "Entry". */
94 /* Create a new XAssoc and load it with new provided data. */
95 new_entry = (XAssoc *) xmalloc(sizeof(XAssoc));
96 new_entry->display = dpy;
97 new_entry->x_id = x_id;
98 new_entry->data = data;
99
100 /* Insert the new entry. */
101 emacs_insque((struct qelem *)new_entry, (struct qelem *)Entry->prev);
102}
103
ab5796a9
MB
104/* arch-tag: d7e3fb8a-f3b3-4c5d-a307-75ca67ec1b49
105 (do not change this comment) */