|
|
1.1 ! root 1: /* $Header: XrmConvert.c,v 1.2 87/09/13 20:07:28 jg 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: Screen *screen; ! 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(screen, fromType, from, toType, to, pHash) ! 125: register Screen *screen; ! 126: register XrmRepresentation fromType; ! 127: XrmValuePtr from; ! 128: register XrmRepresentation toType; ! 129: XrmValuePtr 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->screen == screen) ! 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(screen, fromType, from, toType, to, hash) ! 155: Screen *screen; ! 156: XrmRepresentation fromType; ! 157: XrmValuePtr from; ! 158: XrmRepresentation toType; ! 159: XrmValuePtr 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->screen = screen; ! 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(screen, fromType, from, toType, to) ! 184: register Screen *screen; ! 185: register XrmRepresentation fromType; ! 186: XrmValuePtr from; ! 187: register XrmRepresentation toType; ! 188: register XrmValuePtr to; ! 189: { ! 190: XrmTypeConverter proc; ! 191: int hash; ! 192: char msg[100]; ! 193: ! 194: if (fromType != toType) { ! 195: if (CacheLookup(screen, fromType, from, toType, to, &hash)) ! 196: return; ! 197: if (LookupConverter(fromType, toType, &proc)) { ! 198: (*proc)(screen, from, to); ! 199: if ((*to).addr != NULL) { ! 200: CacheEnter(screen, fromType, from, toType, to, hash); ! 201: } else { ! 202: #ifdef notdef ! 203: (void)sprintf(msg, "Bad source value for '%s' to '%s' conversion.\n", ! 204: XrmRepresentationToAtom(fromType), ! 205: XrmRepresentationToAtom(toType)); ! 206: /* ||| Some sort of pluggable warning call */ ! 207: (void) fprintf(stderr, "%s", msg); ! 208: #endif ! 209: } ! 210: } else { ! 211: (void)sprintf(msg, "No type converter registered for '%s' to '%s'.\n", ! 212: XrmRepresentationToAtom(fromType), ! 213: XrmRepresentationToAtom(toType)); ! 214: /* ||| Some sort of pluggable warning call */ ! 215: (void) fprintf(stderr, "%s", msg); ! 216: } ! 217: } else { ! 218: (*to).size = from->size; ! 219: (*to).addr = from->addr; ! 220: } ! 221: } ! 222: ! 223: void XrmConvert(screen, fromType, from, toType, to) ! 224: Screen *screen; ! 225: XrmAtom fromType; ! 226: XrmValuePtr from; ! 227: XrmAtom toType; ! 228: XrmValuePtr to; ! 229: { ! 230: _XrmConvert(screen, XrmAtomToRepresentation(fromType), from, ! 231: XrmAtomToRepresentation(toType), to); ! 232: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.