import source from 1.3.40
[external/swig.git] / Tools / WAD / Wad / io.c
1 /* ----------------------------------------------------------------------------- 
2  * io.c
3  *
4  *     This file provides some I/O routines so that WAD can produce 
5  *     debugging output without using buffered I/O.
6  * 
7  * Author(s) : David Beazley (beazley@cs.uchicago.edu)
8  *
9  * Copyright (C) 2000.  The University of Chicago. 
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  * 
25  * See the file COPYING for a complete copy of the LGPL.
26  * ----------------------------------------------------------------------------- */
27
28 #include "wad.h"
29 #include <stdarg.h>
30
31 static char cvs[] = "$Id: io.c 10001 2007-10-17 21:33:57Z wsfulton $";
32
33 /* Utility functions used to generate strings that are guaranteed not to 
34    rely upon malloc() and related functions */
35
36 char *wad_format_hex(unsigned long u, int leading) {
37   static char result[64];
38   int i;
39   char *c;
40   c = &result[63];
41   *c = 0;
42   for (i = 0; i < (sizeof(unsigned long)*2); i++) {
43     int d;
44     d = (int) (u & 0xf);
45     c--;
46     if (d < 10) {
47       *c = '0' + d;
48     } else {
49       *c = 'a' + (d-10);
50     }
51     if (!u && !leading) break;
52     u = u >> 4;
53   }
54   return c;
55 }
56
57 char *wad_format_unsigned(unsigned long u, int width) {
58   static char result[128];
59   static char digits[] = "0123456789";
60   char *c, *w;
61   int count = 0;
62   int i;
63   c = &result[64];
64   while (u) {
65     int digit = u % 10;
66     *(--c) = digits[digit];
67     count++;
68     u = u / 10;
69   }
70   if (!count) {
71     *(--c) = '0';
72     count++;
73   }
74   w = &result[64];
75   for (i = count; i < width; i++) {
76     *(w++) = ' ';
77   }
78   *w = 0;
79   return c;
80 }
81
82 char *wad_format_signed(signed long s, int width) {
83   static char result[128];
84   unsigned long u;
85   char *c = result;
86   if (s < 0) {
87     *(c++) = '-';
88     width--;
89     u = (unsigned long) (-s);
90     if (u == 0) {
91       u = (unsigned long) s;
92     }
93   } else {
94     u = (unsigned long) s;
95   }
96   *c = 0;
97   wad_strcat(result, wad_format_unsigned(u,width));
98   return result;
99 }
100
101
102 void wad_printf(const char *fmt, ...) {
103   va_list ap;
104   va_start(ap, fmt);
105   vfprintf(stderr,fmt,ap);
106   va_end(ap);
107 }