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