nethercote | 5912c81 | 2004-02-15 15:38:08 +0000 | [diff] [blame] | 1 | /* 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 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | #include "Main.h" |
| 8 | #include "Error.h" |
| 9 | |
| 10 | extern void* malloc(); |
| 11 | |
| 12 | char* |
| 13 | Basename(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 | |
| 30 | void |
| 31 | DropSuffix(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 | |
| 50 | FILE* |
| 51 | OpenFile(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 | |
| 71 | void |
| 72 | CommaPrint(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); |
nethercote | ed4ad88 | 2004-04-02 13:08:40 +0000 | [diff] [blame] | 80 | fprintf(fp, ",%03d", (int)(n % ONETHOUSAND)); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | void * |
| 85 | xmalloc(n) |
nethercote | a0b7791 | 2004-06-02 20:43:24 +0000 | [diff] [blame] | 86 | size_t n; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 87 | { |
| 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 | |
| 98 | void * |
| 99 | xrealloc(p, n) |
| 100 | void *p; |
nethercote | a0b7791 | 2004-06-02 20:43:24 +0000 | [diff] [blame] | 101 | size_t n; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 102 | { |
| 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 | |
| 114 | char * |
| 115 | copystring(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 | |
| 125 | char * |
| 126 | copystring2(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 | |