]> git.donarmstrong.com Git - lilypond.git/blob - ttftool/util.c
fa6e7b3add7e0311ea7bd083af5fb37e455be80b
[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 <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9
10 #include "types.h"
11 #include "proto.h"
12
13 #include "libc-extension.hh"
14
15
16
17 void *
18 mymalloc (size_t size)
19 {
20   void *p;
21   if ((p = malloc (size)) == NULL)
22     ttf_error ("Unable to allocate memory\n");
23   return p;
24 }
25
26 void *
27 mycalloc (size_t nelem, size_t elsize)
28 {
29   void *p;
30   if ((p = calloc (nelem, elsize)) == NULL)
31     ttf_error ("Unable to allocate memory\n");
32   return p;
33 }
34
35 void
36 ttf_error (char *string)
37 {
38   fprintf (stderr, "TTF tool: %s\n", string);
39   exit (3);
40  /*NOTREACHED*/
41 }
42
43 void
44 syserror (char *string)
45 {
46   char *sys_err = strerror (errno);
47   fprintf (stderr, "TTF tool: %s (%s)\n",
48            string,
49            sys_err);
50   exit (3);
51 }
52
53 void *
54 myrealloc (void *ptr, size_t size)
55 {
56   void *p;
57   if ((p = realloc (ptr, size)) == NULL)
58     ttf_error ("Unable to allocate memory\n");
59   return p;
60 }
61
62 off_t
63 surely_lseek (int fildes, off_t offset, int whence)
64 {
65   off_t result;
66   if ((result = lseek (fildes, offset, whence)) < 0)
67     {
68       char s[100];
69       sprintf (s, "Cannot seek to %d %ld", whence, offset);
70       syserror (s);
71     }
72   return result;
73 }
74
75 ssize_t
76 surely_read (int fildes, void *buf, size_t nbyte)
77 {
78   ssize_t n;
79   if ((n = read (fildes, buf, nbyte)) < nbyte)
80     {
81       char s[100];
82       sprintf (s, "read too little in surely_read(), expect %d got %d", nbyte, n);
83       syserror  (s);
84     }
85   return n;
86 }
87
88 char *
89 unistrncpy (char *dst, char *str, size_t length)
90 {
91   int i, j;
92
93   for (i = j = 0; i < length; i += 2)
94     if (str[i] == 0)
95       dst[j++] = str[i + 1];
96   dst[j] = '\0';
97   return dst;
98 }
99
100 void
101 fputpss (char *s, void *stream)
102 {
103   while (*s)
104     {
105       if ((*s & 0200) == 0 && *s >= 040 && *s != '(' && *s != ')')
106         lily_cookie_putc (*s, stream);
107       else
108         lily_cookie_fprintf (stream, "\\%03o", (unsigned char) *s);
109       s++;
110     }
111 }
112
113 /* Hashtables */
114
115 unsigned
116 hash (char *string)
117 {
118   int i;
119   unsigned u = 0;
120   for (i = 0; string[i] != '\0'; i++)
121     u = (u << 2) + string[i];
122   return u;
123 }
124
125 struct hashtable *
126 make_hashtable (int size)
127 {
128   struct hashtable *t;
129
130   t = mymalloc (sizeof (struct hashtable));
131   t->size = size;
132   t->buckets = mycalloc (size, sizeof (struct hashtable_bucket *));
133
134   return t;
135 }
136
137 int
138 puthash (struct hashtable *t, char *key, int value)
139 {
140   int i;
141
142   i = hash (key) % t->size;
143
144   if (t->buckets[i] == 0)
145     {
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;
150     }
151
152   if (t->buckets[i]->nentries >= t->buckets[i]->size)
153     {
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;
158     }
159
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++;
163
164   return value;
165 }
166
167 int
168 gethash (struct hashtable *t, char *key)
169 {
170   int i, j;
171
172   i = hash (key) % t->size;
173   if (t->buckets[i])
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;
177   return -1;
178 }