Fixed ANSI compabillity.
[wine] / misc / debugstr.c
1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "debugstr.h"
6 #include "debugtools.h"
7 #include "xmalloc.h"
8
9 /* ---------------------------------------------------------------------- */
10
11 #define SAVE_STRING_COUNT 50
12 static void *strings[SAVE_STRING_COUNT];
13 static int nextstring;
14
15 /* ---------------------------------------------------------------------- */
16
17 static void *
18 gimme1 (int n)
19 {
20   void *res;
21   if (strings[nextstring]) free (strings[nextstring]);
22   res = strings[nextstring] = xmalloc (n);
23   if (++nextstring == SAVE_STRING_COUNT) nextstring = 0;
24   return res;
25 }
26
27 /* ---------------------------------------------------------------------- */
28
29 LPSTR
30 debugstr_an (LPCSTR src, int n)
31 {
32   LPSTR dst, res;
33
34   if (!src) return "(null)";
35   if (n < 0) n = 0;
36   dst = res = gimme1 (n * 4 + 10);
37   *dst++ = '"';
38   while (n-- > 0 && *src)
39     {
40       BYTE c = *src++;
41       switch (c)
42         {
43         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
44         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
45         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
46         case '"': *dst++ = '\\'; *dst++ = '"'; break;
47         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
48         default:
49           if (c >= ' ' && c <= 126)
50             *dst++ = c;
51           else
52             {
53               *dst++ = '\\';
54               *dst++ = '0' + ((c >> 6) & 7);
55               *dst++ = '0' + ((c >> 3) & 7);
56               *dst++ = '0' + ((c >> 0) & 7);
57             }
58         }
59     }
60   if (*src)
61     {
62       *dst++ = '.';
63       *dst++ = '.';
64       *dst++ = '.';
65     }
66   *dst++ = '"';
67   *dst = 0;
68   return res;
69 }
70
71 /* ---------------------------------------------------------------------- */
72
73 LPSTR
74 debugstr_a (LPCSTR s)
75 {
76   return debugstr_an (s, 80);
77 }
78
79 /* ---------------------------------------------------------------------- */
80
81 LPSTR
82 debugstr_wn (LPCWSTR src, int n)
83 {
84   LPSTR dst, res;
85
86   if (!src) return "(null)";
87   if (n < 0) n = 0;
88   dst = res = gimme1 (n * 4 + 10);
89   *dst++ = '"';
90   while (n-- > 0 && *src)
91     {
92       WORD c = *src++;
93       switch (c)
94         {
95         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
96         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
97         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
98         case '"': *dst++ = '\\'; *dst++ = '"'; break;
99         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
100         default:
101           if (c >= ' ' && c <= 126)
102             *dst++ = c;
103           else
104             {
105               *dst++ = '\\';
106               *dst++ = '0' + ((c >> 6) & 7);
107               *dst++ = '0' + ((c >> 3) & 7);
108               *dst++ = '0' + ((c >> 0) & 7);
109             }
110         }
111     }
112   if (*src)
113     {
114       *dst++ = '.';
115       *dst++ = '.';
116       *dst++ = '.';
117     }
118   *dst++ = '"';
119   *dst = 0;
120   return res;
121 }
122
123 /* ---------------------------------------------------------------------- */
124
125 LPSTR
126 debugstr_w (LPCWSTR s)
127 {
128   return debugstr_wn (s, 80);
129 }
130
131 /* ---------------------------------------------------------------------- */
132 /* This routine returns a nicely formated name of the resource res
133    If the resource name is a string, it will return '<res-name>'
134    If it is a number, it will return #<4-digit-hex-number> */
135 LPSTR debugres_a( LPCSTR res )
136 {
137     char resname[10];
138     if (HIWORD(res)) return debugstr_a(res);
139     sprintf(resname, "#%04x", LOWORD(res));
140     return debugstr_a (resname);
141 }
142
143 LPSTR debugres_w( LPCWSTR res )
144 {
145     char resname[10];
146     if (HIWORD(res)) return debugstr_w(res);
147     sprintf(resname, "#%04x", LOWORD(res));
148     return debugstr_a (resname);
149 }
150
151 /* ---------------------------------------------------------------------- */
152
153 void debug_dumpstr (LPCSTR s)
154 {
155   fputc ('"', stderr);
156   while (*s)
157     {
158       switch (*s)
159         {
160         case '\\':
161         case '"':
162           fputc ('\\', stderr);
163           fputc (*s, stderr);
164           break;
165         case '\n':
166           fputc ('\\', stderr);
167           fputc ('n', stderr);
168           break;
169         case '\r':
170           fputc ('\\', stderr);
171           fputc ('r', stderr);
172           break;
173         case '\t':
174           fputc ('\\', stderr);
175           fputc ('t', stderr);
176           break;
177         default:
178           if (*s<' ')
179             fprintf (stderr, "\\0x%02x", *s);
180           else
181             fputc (*s, stderr);
182         }
183       s++;
184     }
185   fputc ('"', stderr);
186 }
187
188 /* ---------------------------------------------------------------------- */
189
190 int dbg_printf(const char *format, ...)
191 {
192     int ret;
193     va_list valist;
194
195     va_start(valist, format);
196     ret = vfprintf(stderr, format, valist);
197     va_end(valist);
198     return ret;
199 }
200