|
|
1.1 root 1: /* $Header: XrmConvert.c,v 1.1 87/09/11 08:09:36 toddb Exp $ */
2: #ifndef lint
3: static char *sccsid = "@(#)XrmConvert.c 1.6 2/25/87";
4: #endif lint
5:
6: /*
7: * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
8: *
9: * All Rights Reserved
10: *
11: * Permission to use, copy, modify, and distribute this software and its
12: * documentation for any purpose and without fee is hereby granted,
13: * provided that the above copyright notice appear in all copies and that
14: * both that copyright notice and this permission notice appear in
15: * supporting documentation, and that the name of Digital Equipment
16: * Corporation not be used in advertising or publicity pertaining to
17: * distribution of the software without specific, written prior permission.
18: *
19: *
20: * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
21: * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
22: * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
23: * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
24: * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
25: * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
26: * SOFTWARE.
27: */
28:
29:
30: #include "Xlib.h"
31: #include "Xlibos.h"
32: #include "Xresource.h"
33: #include "XrmConvert.h"
34: #include "Conversion.h"
35:
36: /* ||| */
37: #include <stdio.h>
38:
39: extern int bcmp();
40: extern void bcopy();
41:
42: /* Conversion procedure hash table */
43:
44: typedef struct _ConverterRec *ConverterPtr;
45:
46: typedef struct _ConverterRec {
47: ConverterPtr next;
48: XrmRepresentation from, to;
49: XrmTypeConverter proc;
50: } ConverterRec;
51:
52: #define CONVERTERHASHSIZE 256
53: #define CONVERTERHASHMASK 255
54: #define ProcHash(from, to) (2 * (from) + to)
55:
56: typedef ConverterPtr ConverterTable[CONVERTERHASHSIZE];
57:
58: static ConverterTable converterTable;
59:
60:
61: /* Data cache hash table */
62:
63: typedef struct _CacheRec *CachePtr;
64:
65: typedef struct _CacheRec {
66: CachePtr next;
67: int hash;
68: Display *display;
69: XrmRepresentation fromType;
70: XrmValue from;
71: XrmRepresentation toType;
72: XrmValue to;
73: } CacheRec;
74:
75: #define CACHEHASHSIZE 256
76: #define CACHEHASHMASK 255
77: #define CacheHash(from, to, data) (2 * (from) + to + (int)(data))
78:
79: typedef CachePtr CacheHashTable[CACHEHASHSIZE];
80:
81: static CacheHashTable cacheHashTable;
82:
83: void _XrmRegisterTypeConverter(fromType, toType, convertProc)
84: XrmRepresentation fromType, toType;
85: XrmTypeConverter convertProc;
86: {
87: register ConverterPtr *pHashEntry;
88: register ConverterPtr p;
89:
90: pHashEntry = &converterTable[ProcHash(fromType,toType) & CONVERTERHASHMASK];
91: p = (ConverterPtr) Xmalloc(sizeof(ConverterRec));
92: p->next = *pHashEntry;
93: *pHashEntry = p;
94: p->from = fromType;
95: p->to = toType;
96: p->proc = convertProc;
97: }
98:
99: void XrmRegisterTypeConverter(fromType, toType, convertProc)
100: register XrmAtom fromType, toType;
101: XrmTypeConverter convertProc;
102: {
103: _XrmRegisterTypeConverter(XrmAtomToRepresentation(fromType),
104: XrmAtomToRepresentation(toType), convertProc);
105: }
106:
107: Bool LookupConverter(fromType, toType, convertProc)
108: register XrmRepresentation fromType, toType;
109: XrmTypeConverter *convertProc;
110: {
111: register ConverterPtr p;
112:
113: p = converterTable[ProcHash(fromType, toType) & CONVERTERHASHMASK];
114: while (p != NULL) {
115: if ((fromType == p->from) && (toType == p->to)) {
116: *convertProc = p->proc;
117: return True;
118: }
119: p = p->next;
120: }
121: return False;
122: }
123:
124: Bool CacheLookup(dpy, fromType, from, toType, to, pHash)
125: register Display *dpy;
126: register XrmRepresentation fromType;
127: XrmValue from;
128: register XrmRepresentation toType;
129: XrmValue *to;
130: int *pHash;
131: {
132: register CachePtr p;
133: register int hash;
134:
135: hash = CacheHash(fromType, toType, *((char *)from.addr));
136: for (p = cacheHashTable[hash & CACHEHASHMASK]; p != NULL; p = p->next) {
137: if ((p->hash == hash)
138: && (p->toType == toType)
139: && (p->fromType == fromType)
140: && (p->display == dpy)
141: && (p->from.size == from.size)
142: && (! bcmp((char *)p->from.addr, (char *)from.addr, (int)from.size))) {
143: /* Perfect match */
144: *to = p->to;
145: return True;
146: }
147: }
148: (*to).size = 0;
149: (*to).addr = NULL;
150: (*pHash) = hash;
151: return False;
152: }
153:
154: static void CacheEnter(dpy, fromType, from, toType, to, hash)
155: Display *dpy;
156: XrmRepresentation fromType;
157: XrmValue from;
158: XrmRepresentation toType;
159: XrmValue to;
160: register int hash;
161: {
162: register CachePtr *pHashEntry;
163: register CachePtr p;
164:
165: pHashEntry = &cacheHashTable[hash & CACHEHASHMASK];
166:
167: p = (CachePtr) Xmalloc(sizeof(CacheRec));
168: p->next = *pHashEntry;
169: *pHashEntry = p;
170: p->hash = hash;
171: p->display = dpy;
172: p->fromType = fromType;
173: p->from = from;
174: p->from.addr = (caddr_t) Xmalloc(from.size);
175: bcopy((char *) from.addr, (char *)p->from.addr, (int) from.size);
176:
177: p->toType = toType;
178: p->to = to;
179: p->to.addr = (caddr_t) Xmalloc(to.size);
180: bcopy((char *) to.addr, (char *) p->to.addr, (int) to.size);
181: }
182:
183: void _XrmConvert(dpy, fromType, from, toType, to)
184: register Display *dpy;
185: register XrmRepresentation fromType;
186: XrmValue from;
187: register XrmRepresentation toType;
188: register XrmValue *to;
189: {
190: XrmTypeConverter proc;
191: int hash;
192: char msg[100];
193:
194: if (fromType != toType) {
195: if (CacheLookup(dpy, fromType, from, toType, to, &hash))
196: return;
197: if (LookupConverter(fromType, toType, &proc)) {
198: (*proc)(dpy, from, to);
199: if ((*to).addr != NULL) {
200: CacheEnter(dpy, fromType, from, toType, (*to), hash);
201: } else {
202: (void)sprintf(msg, "Bad source value for '%s' to '%s' conversion.\n",
203: XrmRepresentationToAtom(fromType),
204: XrmRepresentationToAtom(toType));
205: /* ||| Some sort of pluggable warning call */
206: (void) fprintf(stderr, "%s", msg);
207: }
208: } else {
209: (void)sprintf(msg, "No type converter registered for '%s' to '%s'.\n",
210: XrmRepresentationToAtom(fromType),
211: XrmRepresentationToAtom(toType));
212: /* ||| Some sort of pluggable warning call */
213: (void) fprintf(stderr, "%s", msg);
214: }
215: } else {
216: (*to).size = from.size;
217: (*to).addr = from.addr;
218: }
219: }
220: void XrmConvert(dpy, fromType, from, toType, to)
221: Display *dpy;
222: XrmAtom fromType;
223: XrmValue from;
224: XrmAtom toType;
225: XrmValue *to;
226: {
227: _XrmConvert(dpy, XrmAtomToRepresentation(fromType), from,
228: XrmAtomToRepresentation(toType), to);
229: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.