Added ability to specify X fonts used for Helv and Tms Rmn typefaces
[wine.git] / misc / debugstr.c
blobb751b46e43702304797ae7a7af263cbc0e4a7436
1 #include "debugstr.h"
2 #include "xmalloc.h"
3 #include <stdlib.h>
4 #include <stdio.h>
6 /* ---------------------------------------------------------------------- */
8 #define SAVE_STRING_COUNT 50
9 static void *strings[SAVE_STRING_COUNT];
10 static int nextstring;
12 /* ---------------------------------------------------------------------- */
14 static void *
15 gimme1 (int n)
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;
24 /* ---------------------------------------------------------------------- */
26 LPSTR
27 debugstr_an (LPCSTR src, int n)
29 LPSTR dst, res;
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)
37 BYTE c = *src++;
38 switch (c)
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
50 *dst++ = '\\';
51 *dst++ = '0' + ((c >> 6) & 7);
52 *dst++ = '0' + ((c >> 3) & 7);
53 *dst++ = '0' + ((c >> 0) & 7);
57 if (*src)
59 *dst++ = '.';
60 *dst++ = '.';
61 *dst++ = '.';
63 *dst++ = '"';
64 *dst = 0;
65 return res;
68 /* ---------------------------------------------------------------------- */
70 LPSTR
71 debugstr_a (LPCSTR s)
73 return debugstr_an (s, 80);
76 /* ---------------------------------------------------------------------- */
78 LPSTR
79 debugstr_wn (LPCWSTR src, int n)
81 LPSTR dst, res;
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)
89 WORD c = *src++;
90 switch (c)
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
102 *dst++ = '\\';
103 *dst++ = '0' + ((c >> 6) & 7);
104 *dst++ = '0' + ((c >> 3) & 7);
105 *dst++ = '0' + ((c >> 0) & 7);
109 if (*src)
111 *dst++ = '.';
112 *dst++ = '.';
113 *dst++ = '.';
115 *dst++ = '"';
116 *dst = 0;
117 return res;
120 /* ---------------------------------------------------------------------- */
122 LPSTR
123 debugstr_w (LPCWSTR s)
125 return debugstr_wn (s, 80);
128 /* ---------------------------------------------------------------------- */
129 /* 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> */
132 LPSTR debugres_a( LPCSTR res )
134 char resname[10];
135 if (HIWORD(res)) return debugstr_a(res);
136 sprintf(resname, "#%04x", LOWORD(res));
137 return debugstr_a (resname);
140 LPSTR debugres_w( LPCWSTR res )
142 char resname[10];
143 if (HIWORD(res)) return debugstr_w(res);
144 sprintf(resname, "#%04x", LOWORD(res));
145 return debugstr_a (resname);
148 /* ---------------------------------------------------------------------- */
150 void debug_dumpstr (LPCSTR s)
152 fputc ('"', stderr);
153 while (*s)
155 switch (*s)
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<' ')
176 fprintf (stderr, "\\0x%02x", *s);
177 else
178 fputc (*s, stderr);
180 s++;
182 fputc ('"', stderr);