]> git.donarmstrong.com Git - lilypond.git/blob - ttftool/util.c
(open_library): dlopen libkpathsea.so
[lilypond.git] / ttftool / util.c
1 /* Copyright (c) 1997-1998 by Juliusz Chroboczek */
2
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include <malloc.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include "types.h"
10 #include "proto.h"
11
12 #define ALIAS_FILE_TO_FILECOOKIE
13
14 #include "libc-extension.hh"
15
16 void *
17 mymalloc (size_t size)
18 {
19   void *p;
20   if ((p = malloc (size)) == NULL)
21     error ("Unable to allocate memory\n");
22   return p;
23 }
24
25 void *
26 mycalloc (size_t nelem, size_t elsize)
27 {
28   void *p;
29   if ((p = calloc (nelem, elsize)) == NULL)
30     error ("Unable to allocate memory\n");
31   return p;
32 }
33
34 void *
35 myrealloc (void *ptr, size_t size)
36 {
37   void *p;
38   if ((p = realloc (ptr, size)) == NULL)
39     error ("Unable to allocate memory\n");
40   return p;
41 }
42
43 off_t
44 surely_lseek (int fildes, off_t offset, int whence)
45 {
46   off_t result;
47   if ((result = lseek (fildes, offset, whence)) < 0)
48     error ("Bad TTF file");
49   return result;
50 }
51
52 void
53 error (char *string)
54 {
55   fprintf (stderr, "%s\n", string);
56   exit (3);
57  /*NOTREACHED*/}
58
59 void
60 syserror (char *string)
61 {
62   perror (string);
63   exit (3);
64  /*NOTREACHED*/}
65
66 ssize_t
67 surely_read (int fildes, void *buf, size_t nbyte)
68 {
69   ssize_t n;
70   if ((n = read (fildes, buf, nbyte)) < nbyte)
71     error ("Bad TTF file");
72   return n;
73 }
74
75 char *
76 unistrncpy (char *dst, char *str, size_t length)
77 {
78   int i, j;
79
80   for (i = j = 0; i < length; i += 2)
81     if (str[i] == 0)
82       dst[j++] = str[i + 1];
83   dst[j] = '\0';
84   return dst;
85 }
86
87 void
88 fputpss (char *s, FILE * stream)
89 {
90   while (*s)
91     {
92       if ((*s & 0200) == 0 && *s >= 040 && *s != '(' && *s != ')')
93         putc (*s, stream);
94       else
95         fprintf (stream, "\\%03o", (unsigned char) *s);
96       s++;
97     }
98 }
99
100 /* Hashtables */
101
102 unsigned
103 hash (char *string)
104 {
105   int i;
106   unsigned u = 0;
107   for (i = 0; string[i] != '\0'; i++)
108     u = (u << 2) + string[i];
109   return u;
110 }
111
112 struct hashtable *
113 make_hashtable (int size)
114 {
115   struct hashtable *t;
116
117   t = mymalloc (sizeof (struct hashtable));
118   t->size = size;
119   t->buckets = mycalloc (size, sizeof (struct hashtable_bucket *));
120
121   return t;
122 }
123
124 int
125 puthash (struct hashtable *t, char *key, int value)
126 {
127   int i;
128
129   i = hash (key) % t->size;
130
131   if (t->buckets[i] == 0)
132     {
133       t->buckets[i] = mymalloc (sizeof (struct hashtable_bucket));
134       t->buckets[i]->entries = mymalloc (4 * sizeof (struct hashtable_entry));
135       t->buckets[i]->size = 4;
136       t->buckets[i]->nentries = 0;
137     }
138
139   if (t->buckets[i]->nentries >= t->buckets[i]->size)
140     {
141       t->buckets[i]->entries = myrealloc (t->buckets[i]->entries,
142                                           t->buckets[i]->size * 2 *
143                                           sizeof (struct hashtable_entry));
144       t->buckets[i]->size *= 2;
145     }
146
147   t->buckets[i]->entries[t->buckets[i]->nentries].key = key;
148   t->buckets[i]->entries[t->buckets[i]->nentries].value = value;
149   t->buckets[i]->nentries++;
150
151   return value;
152 }
153
154 int
155 gethash (struct hashtable *t, char *key)
156 {
157   int i, j;
158
159   i = hash (key) % t->size;
160   if (t->buckets[i])
161     for (j = 0; j < t->buckets[i]->nentries; j++)
162       if (!strcmp (key, t->buckets[i]->entries[j].key))
163         return t->buckets[i]->entries[j].value;
164   return -1;
165 }