1 /* Copyright (c) 1997-1998 by Juliusz Chroboczek */
13 #include "libc-extension.hh"
18 mymalloc (size_t size)
21 if ((p = malloc (size)) == NULL)
22 ttf_error ("Unable to allocate memory\n");
27 mycalloc (size_t nelem, size_t elsize)
30 if ((p = calloc (nelem, elsize)) == NULL)
31 ttf_error ("Unable to allocate memory\n");
36 ttf_error (char *string)
38 fprintf (stderr, "TTF tool: %s\n", string);
44 syserror (char *string)
46 char *sys_err = strerror (errno);
47 fprintf (stderr, "TTF tool: %s (%s)\n",
54 myrealloc (void *ptr, size_t size)
57 if ((p = realloc (ptr, size)) == NULL)
58 ttf_error ("Unable to allocate memory\n");
63 surely_lseek (int fildes, off_t offset, int whence)
66 if ((result = lseek (fildes, offset, whence)) < 0)
69 sprintf (s, "Cannot seek to %d %ld", whence, offset);
76 surely_read (int fildes, void *buf, size_t nbyte)
79 if ((n = read (fildes, buf, nbyte)) < nbyte)
82 sprintf (s, "read too little in surely_read(), expect %d got %d", nbyte, n);
89 unistrncpy (char *dst, char *str, size_t length)
93 for (i = j = 0; i < length; i += 2)
95 dst[j++] = str[i + 1];
101 fputpss (char *s, void *stream)
105 if ((*s & 0200) == 0 && *s >= 040 && *s != '(' && *s != ')')
106 lily_cookie_putc (*s, stream);
108 lily_cookie_fprintf (stream, "\\%03o", (unsigned char) *s);
120 for (i = 0; string[i] != '\0'; i++)
121 u = (u << 2) + string[i];
126 make_hashtable (int size)
130 t = mymalloc (sizeof (struct hashtable));
132 t->buckets = mycalloc (size, sizeof (struct hashtable_bucket *));
138 puthash (struct hashtable *t, char *key, int value)
142 i = hash (key) % t->size;
144 if (t->buckets[i] == 0)
146 t->buckets[i] = mymalloc (sizeof (struct hashtable_bucket));
147 t->buckets[i]->entries = mymalloc (4 * sizeof (struct hashtable_entry));
148 t->buckets[i]->size = 4;
149 t->buckets[i]->nentries = 0;
152 if (t->buckets[i]->nentries >= t->buckets[i]->size)
154 t->buckets[i]->entries = myrealloc (t->buckets[i]->entries,
155 t->buckets[i]->size * 2 *
156 sizeof (struct hashtable_entry));
157 t->buckets[i]->size *= 2;
160 t->buckets[i]->entries[t->buckets[i]->nentries].key = key;
161 t->buckets[i]->entries[t->buckets[i]->nentries].value = value;
162 t->buckets[i]->nentries++;
168 gethash (struct hashtable *t, char *key)
172 i = hash (key) % t->size;
174 for (j = 0; j < t->buckets[i]->nentries; j++)
175 if (!strcmp (key, t->buckets[i]->entries[j].key))
176 return t->buckets[i]->entries[j].value;