1 /* Copyright (c) 1997-1998 by Juliusz Chroboczek */
11 #define ALIAS_FILE_TO_FILECOOKIE
13 #include "libc-extension.hh"
16 mymalloc (size_t size)
19 if ((p = malloc (size)) == NULL)
20 error ("Unable to allocate memory\n");
25 mycalloc (size_t nelem, size_t elsize)
28 if ((p = calloc (nelem, elsize)) == NULL)
29 error ("Unable to allocate memory\n");
34 myrealloc (void *ptr, size_t size)
37 if ((p = realloc (ptr, size)) == NULL)
38 error ("Unable to allocate memory\n");
43 surely_lseek (int fildes, off_t offset, int whence)
46 if ((result = lseek (fildes, offset, whence)) < 0)
47 error ("Bad TTF file");
54 fprintf (stderr, "%s\n", string);
59 syserror (char *string)
66 surely_read (int fildes, void *buf, size_t nbyte)
69 if ((n = read (fildes, buf, nbyte)) < nbyte)
70 error ("Bad TTF file");
75 unistrncpy (char *dst, char *str, size_t length)
79 for (i = j = 0; i < length; i += 2)
81 dst[j++] = str[i + 1];
87 fputpss (char *s, FILE * stream)
91 if ((*s & 0200) == 0 && *s >= 040 && *s != '(' && *s != ')')
94 fprintf (stream, "\\%03o", (unsigned char) *s);
106 for (i = 0; string[i] != '\0'; i++)
107 u = (u << 2) + string[i];
112 make_hashtable (int size)
116 t = mymalloc (sizeof (struct hashtable));
118 t->buckets = mycalloc (size, sizeof (struct hashtable_bucket *));
124 puthash (struct hashtable *t, char *key, int value)
128 i = hash (key) % t->size;
130 if (t->buckets[i] == 0)
132 t->buckets[i] = mymalloc (sizeof (struct hashtable_bucket));
133 t->buckets[i]->entries = mymalloc (4 * sizeof (struct hashtable_entry));
134 t->buckets[i]->size = 4;
135 t->buckets[i]->nentries = 0;
138 if (t->buckets[i]->nentries >= t->buckets[i]->size)
140 t->buckets[i]->entries = myrealloc (t->buckets[i]->entries,
141 t->buckets[i]->size * 2 *
142 sizeof (struct hashtable_entry));
143 t->buckets[i]->size *= 2;
146 t->buckets[i]->entries[t->buckets[i]->nentries].key = key;
147 t->buckets[i]->entries[t->buckets[i]->nentries].value = value;
148 t->buckets[i]->nentries++;
154 gethash (struct hashtable *t, char *key)
158 i = hash (key) % t->size;
160 for (j = 0; j < t->buckets[i]->nentries; j++)
161 if (!strcmp (key, t->buckets[i]->entries[j].key))
162 return t->buckets[i]->entries[j].value;