blob: 754cc8a01a965bac3e6180b126451af30d6ebf9f [file] [log] [blame]
nethercote5912c812004-02-15 15:38:08 +00001/* This file is part of hp2ps, a graph drawer for memory profiles.
2 Copyright (C) 2002 The University Court of the University of Glasgow.
3 This program is governed by the license contained in the file LICENSE. */
4
nethercotec9f36922004-02-14 16:40:02 +00005#include <stdio.h>
6#include <string.h>
7#include "Main.h"
8#include "Error.h"
9
10extern void* malloc();
11
12char*
13Basename(name)
14 char* name;
15{
16 char* t;
17
18 t = name;
19
20 while (*name) {
21 if (*name == '/') {
22 t = name+1;
23 }
24 name++;
25 }
26
27 return t;
28}
29
30void
31DropSuffix(name, suffix)
32 char* name; char* suffix;
33{
34 char* t;
35
36 t = (char*) 0;
37
38 while (*name) {
39 if (*name == '.') {
40 t = name;
41 }
42 name++;
43 }
44
45 if (t != (char*) 0 && strcmp(t, suffix) == 0) {
46 *t = '\0';
47 }
48}
49
50FILE*
51OpenFile(s, mode)
52 char* s; char* mode;
53{
54 FILE* r;
55
56 if ((r = fopen(s, mode)) == NULL) {
57 /*NOTREACHED*/
58 Error("cannot open %s", s);
59 }
60
61 return r;
62}
63
64
65#define ONETHOUSAND 1000
66
67/*
68 * Print a positive integer with commas
69 */
70
71void
72CommaPrint(fp,n)
73 FILE* fp;
74 intish n;
75{
76 if (n < ONETHOUSAND) {
77 fprintf(fp, "%d", (int)n);
78 } else {
79 CommaPrint(fp, n / ONETHOUSAND);
nethercoteed4ad882004-04-02 13:08:40 +000080 fprintf(fp, ",%03d", (int)(n % ONETHOUSAND));
nethercotec9f36922004-02-14 16:40:02 +000081 }
82}
83
84void *
85xmalloc(n)
nethercotea0b77912004-06-02 20:43:24 +000086 size_t n;
nethercotec9f36922004-02-14 16:40:02 +000087{
88 void *r;
89
90 r = (void*) malloc(n);
91 if (!r) {
92 /*NOTREACHED*/
93 Disaster("%s, sorry, out of memory", hpfile);
94 }
95 return r;
96}
97
98void *
99xrealloc(p, n)
100 void *p;
nethercotea0b77912004-06-02 20:43:24 +0000101 size_t n;
nethercotec9f36922004-02-14 16:40:02 +0000102{
103 void *r;
104 extern void *realloc();
105
106 r = realloc(p, n);
107 if (!r) {
108 /*NOTREACHED*/
109 Disaster("%s, sorry, out of memory", hpfile);
110 }
111 return r;
112}
113
114char *
115copystring(s)
116 char *s;
117{
118 char *r;
119
120 r = (char*) xmalloc(strlen(s)+1);
121 strcpy(r, s);
122 return r;
123}
124
125char *
126copystring2(s, t)
127 char *s, *t;
128{
129 char *r;
130
131 r = (char*) xmalloc(strlen(s)+strlen(t)+1);
132 strcpy(r, s);
133 strcat(r, t);
134 return r;
135}
136