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