OSDN Git Service

libcpp/ChangeLog:
[pf3gnuchains/gcc-fork.git] / libbanshee / engine / util.c
1 /*
2  * Copyright (c) 2000-2001
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30
31 #include <ctype.h>
32 #include <math.h>
33 #include <signal.h>
34 #include <stdio.h>
35 #include <stdarg.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include "buffer.h"
39 #include "util.h"
40
41 /* Panic with a message */
42 static void vfail(const char *fmt, va_list args) __attribute__((__noreturn__));
43
44 static void vfail(const char *fmt, va_list args)
45 {
46   vfprintf(stderr, fmt, args);
47   fflush(stdin);
48   fflush(stderr);
49   fflush(stdout);
50   sync();
51   fsync(STDIN_FILENO);
52   fsync(STDERR_FILENO);
53   fsync(STDOUT_FILENO);
54   abort();
55   while (1); /* Work around stupid gcc-2.96-85 bug */
56 }
57
58 /* Panic with a nice message */
59 void __fail(const char *file, unsigned int line,
60             const char *func __attribute__((unused)),
61             const char *fmt, ...)
62 {
63   va_list args;
64   va_start(args, fmt);
65   fprintf(stderr, "\n%s:%u ", file, line);
66   vfail(fmt, args);
67 }
68
69 #ifndef HAVE_VARIADIC_MACROS
70 /* Panic with a not-quite-as-nice message */
71 void fail(const char *fmt, ...)
72 {
73   va_list args;
74   va_start(args, fmt);
75   vfail(fmt, args);
76 }
77 #endif
78
79 void failure(const char *message)
80 {
81   fprintf(stderr,message);
82   exit(1);
83 }
84
85 /* Concatenate 2 strings, allocating space in r for the result */
86 char *rstrcat(region r, const char *s1, const char *s2)
87 {
88   char *result = rarrayalloc(r, strlen(s1)+strlen(s2)+1, char);
89   result[0] = '\0';
90   strcat(result, s1);
91   strcat(result, s2);
92   return result;
93 }
94
95 /* Concatenate n strings, allocating space in r for the result.  The
96    last argument should be a null pointer. */
97 char *rstrscat(region r, ...)
98 {
99   char *result;
100   int len = 0;
101   const char *s;
102   va_list args;
103
104   va_start(args, r);
105   while ((s = va_arg(args, const char *)))
106     len += strlen(s);
107   result = rarrayalloc(r, len+1, char);
108   result[0] = '\0';
109
110   va_start(args, r);
111   while ((s = va_arg(args, const char *)))
112     strcat(result, s);
113
114   return result;
115 }
116 #if 0
117 /* Convert an integer to a string, storing the result in r */
118 const char *inttostr(region r, int i)
119 {
120   char *result;
121   int width;
122
123   if (i == 0)
124     width = 1;
125   else
126     width = (int) (floor(log10(abs((double) i))) + 1);
127   if (i<0) width++;
128
129   printf("i=%d, width=%d\n", i, width);
130   assert(width >0);
131
132   result = rarrayalloc(r, width + 1, char);
133   if (snprintf(result, width + 1, "%d", i) == -1) {
134     printf("i=%d, width=%d\n", i, width);
135     fail ("inttostr width wrong\n");
136   }
137   return result;
138 }
139 #endif
140
141 /* sprintf a string, allocating space in r for the result */
142 char *rsprintf(region r, const char *fmt, ...)
143 {
144   va_list args;
145
146   va_start(args, fmt);
147   return rvsprintf(r, fmt, args);
148 }
149
150 char *rvsprintf(region r, const char *fmt, va_list args)
151 {
152   growbuf buf = growbuf_new(r, 100);
153   gvprintf(buf, fmt, args);
154   return growbuf_contents(buf);
155 }
156
157 /* Space for the ASCII representation of a pointer -- 2 hex chars per
158    byte, plus 3 chars for 0x prefix and trailing \0 */
159 #define PTR_ASCII_SIZE  ((int) (3 + sizeof(void *)*2))
160
161 /* Convert a pointer to an ascii string with leading 0x.  Re-uses
162    internal buffer. */
163 char *ptr_to_ascii(void *ptr) {
164   static char addr[PTR_ASCII_SIZE];
165   int nchars;
166
167   nchars = snprintf(addr, PTR_ASCII_SIZE, "%p", ptr);
168   if (nchars == -1 || nchars >= PTR_ASCII_SIZE)
169     fail("Unable to convert ptr to ascii (need %d bytes, have %d)\n",
170          nchars, PTR_ASCII_SIZE);
171   return addr;
172 }
173
174 /* Convert a pointer to an integer */
175 long ptr_hash(void *ptr)
176 {
177   return (long) ptr;
178 }
179
180 /* Return TRUE iff ptr1 == ptr2 */
181 bool ptr_eq(void *ptr1, void *ptr2)
182 {
183   return ptr1 == ptr2;
184 }
185
186 /* Return TRUE iff s1 == s2 */
187 bool str_eq(const char *s1, const char *s2)
188 {
189   return (strcmp(s1, s2) == 0);
190 }
191
192 /* A total ordering on pointers.  Returns 0 if ptr1 = ptr2, a value <0
193    if ptr1 < ptr2, or a value >0 if ptr1 > ptr2. */
194 int ptr_cmp(const void *ptr1, const void *ptr2)
195 {
196   return (char *) ptr1 - (char *) ptr2;
197 }
198
199 /* int abs(int a) { if (a < 0) return -a; else return a; } */