a5d1be0a150b3c062eabde34f10af1f2a26d0d10
[m17n/libotf.git] / src / otfutil.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "otf.h"
6
7 static char *error_message;
8 int otf_error;
9
10 static char *error_string[] =
11   {
12     "Memory shortage",
13     "File error",
14     "Invalid OTF table contents"
15   };
16
17 int
18 otf__error (int err, char *fmt, void *arg)
19 {
20   if (! error_message)
21     error_message = (char *) malloc (256);
22   sprintf (error_message, "OTF-Error (%s): ", error_string[-err - 1]);
23   sprintf (error_message + strlen (error_message), fmt, arg);
24   otf_error = err;
25   return 0;
26 }
27
28 void
29 otf_perror (char *prefix, int exit_code)
30 {
31   if (otf_error < 0)
32     {
33       if (prefix)
34         fprintf (stderr, "%s%s\n", prefix, error_message);
35       else
36         fprintf (stderr, "%s\n", error_message);
37       if (exit_code > 0)
38         exit (exit_code);
39     }
40 }
41
42
43 OTF_Tag
44 otf_tag (char *str)
45 {
46   unsigned char *p = (unsigned char *) str;
47
48   if (! str)
49     return (OTF_Tag) 0;
50   return (OTF_Tag) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
51 }
52