1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/***
*printf.h - print formatted
*
* Copyright (c) 1985-1991, Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines w4*printf() - print formatted data
* defines w4v*printf() - print formatted output, get data from an
* argument ptr instead of explicit args.
*
*Revision History:
* 09-02-83 RN original sprintf
* 06-17-85 TC rewrote to use new varargs macros, and to be vsprintf
* 04-13-87 JCR added const to declaration
* 11-07-87 JCR Multi-thread support
* 12-11-87 JCR Added "_LOAD_DS" to declaration
* 05-27-88 PHG Merged DLL and normal versions
* 06-13-88 JCR Fake _iob entry is now static so that other routines
* can assume _iob entries are in DGROUP.
* 08-25-88 GJF Define MAXSTR to be INT_MAX (from LIMITS.H).
* 06-06-89 JCR 386 mthread support
* 08-18-89 GJF Clean up, now specific to OS/2 2.0 (i.e., 386 WIN32
* model). Also fixed copyright and indents.
* 02-16-90 GJF Fixed copyright
*
*******************************************************************************/
#include <windows.h>
#include <wchar.h>
#include <stdarg.h>
#include <limits.h>
#include "w4io.h"
#if defined(_W4PRINTF_)
static long fh;
// extern long GetStdHandle(long);
// extern void WriteFile(long fh, char *s, long cch, long * pcchret, long);
# define _PRINTF_
#elif defined(_W4DPRINTF_)
# define _pwritechar _dwritechar
# define _pflushbuf _dflushbuf
# define w4printf w4dprintf
# define w4vprintf w4vdprintf
# define _PRINTF_
#elif defined(_W4SPRINTF_)
# define _pwritechar _swritechar
# define w4printf w4sprintf
# define w4vprintf w4vsprintf
#elif defined(_W4WCSPRINTF_)
# define _TCHAR_ wchar_t
# define _PBUF_ pwcbuf
# define _PSTART_ pwcstart
# define w4printf w4wcsprintf
# define w4vprintf w4vwcsprintf
# define _pwritechar _wwritechar
#else
# error configuration problem
#endif
#ifndef _TCHAR_
# define _TCHAR_ char
# define _PBUF_ pchbuf
# define _PSTART_ pchstart
#endif
#ifdef _PRINTF_
# ifdef WIN32
# undef OutputDebugString
# define OutputDebugString OutputDebugStringA
# else
extern void _pascal OutputDebugString(char *);
# endif
int _cdecl _pflushbuf(struct w4io *f);
# define SPR(a)
# define MAXSTR 128
#else
# define SPR(a) a,
# define MAXSTR INT_MAX
#endif
void _cdecl _pwritechar(int ch, int num, struct w4io *f, int *pcchwritten);
int _cdecl w4vprintf(SPR(_TCHAR_ *string) const char *format, va_list arglist);
/***
*int w4printf(format, ...) - print formatted data
*
*Purpose:
* Prints formatted data using the format string to
* format data and getting as many arguments as called for
* Sets up a w4io so file i/o operations can be used.
* w4iooutput does the real work here
*
*Entry:
* char *format - format string to control data format/number
* of arguments followed by list of arguments, number and type
* controlled by format string
*
*Exit:
* returns number of characters written
*
*Exceptions:
*
*******************************************************************************/
int _cdecl
w4printf(SPR(_TCHAR_ *string) const char *format, ...)
/*
* 'PRINT', 'F'ormatted
*/
{
va_list arglist;
va_start(arglist, format);
return(w4vprintf(SPR(string) format, arglist));
}
/***
*int w4vprintf(format, arglist) - print formatted data from arg ptr
*
*Purpose:
* Prints formatted data, but gets data from an argument pointer.
* Sets up a w4io so file i/o operations can be used, make string look
* like a huge buffer to it, but _flsbuf will refuse to flush it if it
* fills up. Appends '\0' to make it a true string.
*
* Multi-thread: (1) Since there is no stream, this routine must never try
* to get the stream lock (i.e., there is no stream lock either). (2)
* Also, since there is only one staticly allocated 'fake' iob, we must
* lock/unlock to prevent collisions.
*
*Entry:
* char *format - format string, describes format of data
* va_list arglist - varargs argument pointer
*
*Exit:
* returns number of characters written
*
*Exceptions:
*
*******************************************************************************/
int _cdecl
w4vprintf(SPR(_TCHAR_ *string) const char *format, va_list arglist)
/*
* 'V'ariable argument 'PRINT', 'F'ormatted
*/
{
struct w4io outfile;
register int retval;
#ifdef _PRINTF_
char string[MAXSTR + 1]; // leave room for null termination
#else
int dummy;
#endif
#ifdef _W4PRINTF_
long ldummy;
if (fh == 0 || fh == -1)
{
ldummy = -11; // C7 bug workaround
if ((fh = (long)GetStdHandle(ldummy)) == 0 || fh == -1)
{
OutputDebugString("GetStdHandle in " __FILE__ " failed\n");
return(-1);
}
}
#endif
outfile._PBUF_ = outfile._PSTART_ = string;
outfile.cchleft = MAXSTR;
outfile.writechar = _pwritechar;
retval = w4iooutput(&outfile, format, arglist);
#ifdef _PRINTF_
if (_pflushbuf(&outfile) == -1) {
return(-1);
}
#else
_pwritechar('\0', 1, &outfile, &dummy);
#endif
return(retval);
}
void _cdecl _pwritechar(int ch, int num, struct w4io *f, int *pcchwritten)
{
//printf(" char: ch=%c, cnt=%d, cch=%d\n", ch, num, *pcchwritten);
while (num-- > 0) {
#ifdef _PRINTF_
if (f->cchleft < 2 && _pflushbuf(f) == -1) {
*pcchwritten = -1;
return;
}
#endif
#ifdef _W4DPRINTF_
# ifndef WIN32
if (ch == '\n')
{
*f->_PBUF_++ = '\r';
f->cchleft--;
(*pcchwritten)++;
}
# endif
#endif
*f->_PBUF_++ = (char) ch;
f->cchleft--;
(*pcchwritten)++;
}
}
#ifdef _PRINTF_
int _cdecl _pflushbuf(struct w4io *f)
{
int cch;
if (cch = (f->pchbuf - f->pchstart))
{
#ifdef _W4DPRINTF_
*f->pchbuf = '\0'; // null terminate
OutputDebugString(f->pchstart);
#else
long cchret;
//*f->pchbuf = '\0'; // null terminate
//printf("%d chars: \"%s\"\n", cch, f->pchstart);
WriteFile((HANDLE)fh, f->pchstart, cch, &cchret, 0);
if (cch != cchret)
{
OutputDebugString("WriteFile in " __FILE__ " failed\n");
return(-1);
}
#endif
f->pchbuf -= cch; // reset pointer
f->cchleft += cch; // reset count
}
return(0);
}
#endif // _PRINTF_
|