1 /* Copyright (c) 1997-1998 by Juliusz Chroboczek */
13 mymalloc (size_t size)
16 if ((p = malloc (size)) == NULL)
17 error ("Unable to allocate memory\n");
22 mycalloc (size_t nelem, size_t elsize)
25 if ((p = calloc (nelem, elsize)) == NULL)
26 error ("Unable to allocate memory\n");
31 myrealloc (void *ptr, size_t size)
34 if ((p = realloc (ptr, size)) == NULL)
35 error ("Unable to allocate memory\n");
40 surely_lseek (int fildes, off_t offset, int whence)
43 if ((result = lseek (fildes, offset, whence)) < 0)
44 error ("Bad TTF file");
51 fprintf (stderr, "%s\n", string);
56 syserror (char *string)
63 surely_read (int fildes, void *buf, size_t nbyte)
66 if ((n = read (fildes, buf, nbyte)) < nbyte)
67 error ("Bad TTF file");
72 unistrncpy (char *dst, char *str, size_t length)
76 for (i = j = 0; i < length; i += 2)
78 dst[j++] = str[i + 1];
84 fputpss (char *s, FILE * stream)
88 if ((*s & 0200) == 0 && *s >= 040 && *s != '(' && *s != ')')
91 fprintf (stream, "\\%03o", (unsigned char) *s);
103 for (i = 0; string[i] != '\0'; i++)
104 u = (u << 2) + string[i];
109 make_hashtable (int size)
113 t = mymalloc (sizeof (struct hashtable));
115 t->buckets = mycalloc (size, sizeof (struct hashtable_bucket *));
121 puthash (struct hashtable *t, char *key, int value)
125 i = hash (key) % t->size;
127 if (t->buckets[i] == 0)
129 t->buckets[i] = mymalloc (sizeof (struct hashtable_bucket));
130 t->buckets[i]->entries = mymalloc (4 * sizeof (struct hashtable_entry));
131 t->buckets[i]->size = 4;
132 t->buckets[i]->nentries = 0;
135 if (t->buckets[i]->nentries >= t->buckets[i]->size)
137 t->buckets[i]->entries = myrealloc (t->buckets[i]->entries,
138 t->buckets[i]->size * 2 *
139 sizeof (struct hashtable_entry));
140 t->buckets[i]->size *= 2;
143 t->buckets[i]->entries[t->buckets[i]->nentries].key = key;
144 t->buckets[i]->entries[t->buckets[i]->nentries].value = value;
145 t->buckets[i]->nentries++;
151 gethash (struct hashtable *t, char *key)
155 i = hash (key) % t->size;
157 for (j = 0; j < t->buckets[i]->nentries; j++)
158 if (!strcmp (key, t->buckets[i]->entries[j].key))
159 return t->buckets[i]->entries[j].value;