Fixed off-by-one ordinals in MPR.
[wine/multimedia.git] / misc / debugstr.c
blob73a4a712be0cd7239541e829a63f0166313a53a6
1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <stdlib.h>
5 #include "debugstr.h"
6 #include "debugtools.h"
7 #include "xmalloc.h"
9 /* ---------------------------------------------------------------------- */
11 #define SAVE_STRING_COUNT 50
12 static void *strings[SAVE_STRING_COUNT];
13 static int nextstring;
15 /* ---------------------------------------------------------------------- */
17 static void *
18 gimme1 (int n)
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;
27 /* ---------------------------------------------------------------------- */
29 LPSTR
30 debugstr_an (LPCSTR src, int n)
32 LPSTR dst, res;
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)
40 BYTE c = *src++;
41 switch (c)
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
53 *dst++ = '\\';
54 *dst++ = '0' + ((c >> 6) & 7);
55 *dst++ = '0' + ((c >> 3) & 7);
56 *dst++ = '0' + ((c >> 0) & 7);
60 if (*src)
62 *dst++ = '.';
63 *dst++ = '.';
64 *dst++ = '.';
66 *dst++ = '"';
67 *dst = 0;
68 return res;
71 /* ---------------------------------------------------------------------- */
73 LPSTR
74 debugstr_a (LPCSTR s)
76 return debugstr_an (s, 80);
79 /* ---------------------------------------------------------------------- */
81 LPSTR
82 debugstr_wn (LPCWSTR src, int n)
84 LPSTR dst, res;
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)
92 WORD c = *src++;
93 switch (c)
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
105 *dst++ = '\\';
106 *dst++ = '0' + ((c >> 6) & 7);
107 *dst++ = '0' + ((c >> 3) & 7);
108 *dst++ = '0' + ((c >> 0) & 7);
112 if (*src)
114 *dst++ = '.';
115 *dst++ = '.';
116 *dst++ = '.';
118 *dst++ = '"';
119 *dst = 0;
120 return res;
123 /* ---------------------------------------------------------------------- */
125 LPSTR
126 debugstr_w (LPCWSTR s)
128 return debugstr_wn (s, 80);
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 )
137 char resname[10];
138 if (HIWORD(res)) return debugstr_a(res);
139 sprintf(resname, "#%04x", LOWORD(res));
140 return debugstr_a (resname);
143 LPSTR debugres_w( LPCWSTR res )
145 char resname[10];
146 if (HIWORD(res)) return debugstr_w(res);
147 sprintf(resname, "#%04x", LOWORD(res));
148 return debugstr_a (resname);
151 /* ---------------------------------------------------------------------- */
153 void debug_dumpstr (LPCSTR s)
155 fputc ('"', stderr);
156 while (*s)
158 switch (*s)
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);
183 s++;
185 fputc ('"', stderr);
188 /* ---------------------------------------------------------------------- */
190 int dbg_printf(const char *format, ...)
192 int ret;
193 va_list valist;
195 va_start(valist, format);
196 ret = vfprintf(stderr, format, valist);
197 va_end(valist);
198 return ret;