blob: b751b46e43702304797ae7a7af263cbc0e4a7436 [file] [log] [blame]
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +00001#include "debugstr.h"
2#include "xmalloc.h"
3#include <stdlib.h>
Alexandre Julliarda11d7b11998-03-01 20:05:02 +00004#include <stdio.h>
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +00005
6/* ---------------------------------------------------------------------- */
7
8#define SAVE_STRING_COUNT 50
9static void *strings[SAVE_STRING_COUNT];
10static int nextstring;
11
12/* ---------------------------------------------------------------------- */
13
14static void *
15gimme1 (int n)
16{
17 void *res;
18 if (strings[nextstring]) free (strings[nextstring]);
19 res = strings[nextstring] = xmalloc (n);
20 if (++nextstring == SAVE_STRING_COUNT) nextstring = 0;
21 return res;
22}
23
24/* ---------------------------------------------------------------------- */
25
26LPSTR
27debugstr_an (LPCSTR src, int n)
28{
29 LPSTR dst, res;
30
31 if (!src) return "(null)";
32 if (n < 0) n = 0;
33 dst = res = gimme1 (n * 4 + 10);
34 *dst++ = '"';
35 while (n-- > 0 && *src)
36 {
37 BYTE c = *src++;
38 switch (c)
39 {
40 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
41 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
42 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
43 case '"': *dst++ = '\\'; *dst++ = '"'; break;
44 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
45 default:
46 if (c >= ' ' && c <= 126)
47 *dst++ = c;
48 else
49 {
50 *dst++ = '\\';
51 *dst++ = '0' + ((c >> 6) & 7);
52 *dst++ = '0' + ((c >> 3) & 7);
53 *dst++ = '0' + ((c >> 0) & 7);
54 }
55 }
56 }
57 if (*src)
58 {
59 *dst++ = '.';
60 *dst++ = '.';
61 *dst++ = '.';
62 }
63 *dst++ = '"';
64 *dst = 0;
65 return res;
66}
67
68/* ---------------------------------------------------------------------- */
69
70LPSTR
71debugstr_a (LPCSTR s)
72{
73 return debugstr_an (s, 80);
74}
75
76/* ---------------------------------------------------------------------- */
77
78LPSTR
79debugstr_wn (LPCWSTR src, int n)
80{
81 LPSTR dst, res;
82
83 if (!src) return "(null)";
84 if (n < 0) n = 0;
85 dst = res = gimme1 (n * 4 + 10);
86 *dst++ = '"';
87 while (n-- > 0 && *src)
88 {
89 WORD c = *src++;
90 switch (c)
91 {
92 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
93 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
94 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
95 case '"': *dst++ = '\\'; *dst++ = '"'; break;
96 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
97 default:
98 if (c >= ' ' && c <= 126)
99 *dst++ = c;
100 else
101 {
102 *dst++ = '\\';
103 *dst++ = '0' + ((c >> 6) & 7);
104 *dst++ = '0' + ((c >> 3) & 7);
105 *dst++ = '0' + ((c >> 0) & 7);
106 }
107 }
108 }
109 if (*src)
110 {
111 *dst++ = '.';
112 *dst++ = '.';
113 *dst++ = '.';
114 }
115 *dst++ = '"';
116 *dst = 0;
117 return res;
118}
119
120/* ---------------------------------------------------------------------- */
121
122LPSTR
123debugstr_w (LPCWSTR s)
124{
125 return debugstr_wn (s, 80);
126}
127
128/* ---------------------------------------------------------------------- */
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000129/* This routine returns a nicely formated name of the resource res
130 If the resource name is a string, it will return '<res-name>'
131 If it is a number, it will return #<4-digit-hex-number> */
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000132LPSTR debugres_a( LPCSTR res )
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000133{
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000134 char resname[10];
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000135 if (HIWORD(res)) return debugstr_a(res);
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000136 sprintf(resname, "#%04x", LOWORD(res));
137 return debugstr_a (resname);
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000138}
139
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000140LPSTR debugres_w( LPCWSTR res )
141{
142 char resname[10];
143 if (HIWORD(res)) return debugstr_w(res);
144 sprintf(resname, "#%04x", LOWORD(res));
145 return debugstr_a (resname);
146}
147
148/* ---------------------------------------------------------------------- */
149
150void debug_dumpstr (LPCSTR s)
151{
152 fputc ('"', stderr);
153 while (*s)
154 {
155 switch (*s)
156 {
157 case '\\':
158 case '"':
159 fputc ('\\', stderr);
160 fputc (*s, stderr);
161 break;
162 case '\n':
163 fputc ('\\', stderr);
164 fputc ('n', stderr);
165 break;
166 case '\r':
167 fputc ('\\', stderr);
168 fputc ('r', stderr);
169 break;
170 case '\t':
171 fputc ('\\', stderr);
172 fputc ('t', stderr);
173 break;
174 default:
175 if (*s<' ')
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000176 fprintf (stderr, "\\0x%02x", *s);
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000177 else
178 fputc (*s, stderr);
179 }
180 s++;
181 }
182 fputc ('"', stderr);
183}
184