multipage_hp2ps
[ghc.git] / utils / hp2ps / Utilities.c
1 #include "Main.h"
2 #include <stdio.h>
3 #include <string.h>
4 #include "Error.h"
5
6 extern void* malloc();
7
8 char*
9 Basename(name)
10 char* name;
11 {
12 char* t;
13
14 t = name;
15
16 while (*name) {
17 if (*name == '/') {
18 t = name+1;
19 }
20 name++;
21 }
22
23 return t;
24 }
25
26 void
27 DropSuffix(name, suffix)
28 char* name; char* suffix;
29 {
30 char* t;
31
32 t = (char*) 0;
33
34 while (*name) {
35 if (*name == '.') {
36 t = name;
37 }
38 name++;
39 }
40
41 if (t != (char*) 0 && strcmp(t, suffix) == 0) {
42 *t = '\0';
43 }
44 }
45
46 FILE*
47 OpenFile(s, mode)
48 char* s; char* mode;
49 {
50 FILE* r;
51
52 if ((r = fopen(s, mode)) == NULL) {
53 /*NOTREACHED*/
54 Error("cannot open %s", s);
55 }
56
57 return r;
58 }
59
60
61 #define ONETHOUSAND 1000
62
63 /*
64 * Print a positive integer with commas
65 */
66
67 void
68 CommaPrint(fp,n)
69 FILE* fp;
70 intish n;
71 {
72 if (n < ONETHOUSAND) {
73 fprintf(fp, "%d", (int)n);
74 } else {
75 CommaPrint(fp, n / ONETHOUSAND);
76 fprintf(fp, ",%03d", (int)(n % ONETHOUSAND));
77 }
78 }
79
80 void *
81 xmalloc(n)
82 size_t n;
83 {
84 void *r;
85
86 r = (void*) malloc(n);
87 if (!r) {
88 /*NOTREACHED*/
89 Disaster("%s, sorry, out of memory", hpfile);
90 }
91 return r;
92 }
93
94 void *
95 xrealloc(p, n)
96 void *p;
97 size_t n;
98 {
99 void *r;
100 extern void *realloc();
101
102 r = realloc(p, n);
103 if (!r) {
104 /*NOTREACHED*/
105 Disaster("%s, sorry, out of memory", hpfile);
106 }
107 return r;
108 }
109
110 char *
111 copystring(s)
112 char *s;
113 {
114 char *r;
115
116 r = (char*) xmalloc(strlen(s)+1);
117 strcpy(r, s);
118 return r;
119 }
120
121 char *
122 copystring2(s, t)
123 char *s, *t;
124 {
125 char *r;
126
127 r = (char*) xmalloc(strlen(s)+strlen(t)+1);
128 strcpy(r, s);
129 strcat(r, t);
130 return r;
131 }
132