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