summaryrefslogtreecommitdiffstats
path: root/private/oleutest/oletest/output.cpp
blob: da9ef75a90618d990d6ce14860bc30495a4d9c5f (plain) (blame)
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
//+-------------------------------------------------------------------------
//
//  Microsoft Windows
//  Copyright (C) Microsoft Corporation, 1992 - 1993.
//
//  File:	output.cpp
//
//  Contents:	String output functions for displaying text on the main
//		edit window
//
//  Classes:
//
//  Functions: 	OutputString
//		SaveToFile
//
//  History:    dd-mmm-yy Author    Comment
//		22-Mar-94 alexgo    author
//
//--------------------------------------------------------------------------

#include "oletest.h"
#include <commdlg.h>

#ifndef WIN32
#include <stdarg.h>
#endif

//
// handle to memory where the text is stored
//
// Please note this is really burfy (having all these globals).  But for
// the purposes of a simple driver app, it is the easiest.
//
static HGLOBAL	hText;		// handle to the Text
static ULONG	cbText;
static ULONG	iText;

//+-------------------------------------------------------------------------
//
//  Function:	OutputString
//
//  Synopsis:	Dumps the string in printf format to the screen
//
//  Effects:
//
//  Arguments:	[szFormat]	-- the format string
//		[...]		-- variable arguments
//
//  Requires:
//
//  Returns:	int, the number of characters written (returned by sprintf)
//
//  Signals:
//
//  Modifies:
//
//  Algorithm:
//
//  History:    dd-mmm-yy Author    Comment
//
//  Notes:
//
//--------------------------------------------------------------------------

int OutputString( char *szFormat, ... )
{
	LPSTR	psz;
	va_list	ap;
	int	cbWritten;

	va_start(ap, szFormat);

	if( !hText )
	{
		hText = GlobalAlloc( GMEM_MOVEABLE , 2048 );
		assert(hText);
		cbText = 2048;
	}

	// double the size of the array if we need to

	if( iText > cbText / 2 )
	{
		hText = GlobalReAlloc(hText, cbText * 2, GMEM_MOVEABLE );
		assert(hText);
		cbText *= 2;
	}

	psz = (LPSTR)GlobalLock(hText);

	assert(psz);

	cbWritten = wvsprintf( psz + iText, szFormat, ap);

	iText += cbWritten;

	va_end(ap);

	SetWindowText(vApp.m_hwndEdit, psz);

	GlobalUnlock(hText);


	return cbWritten;

}

//+-------------------------------------------------------------------------
//
//  Function: 	SaveToFile
//
//  Synopsis: 	Gets a filename from the user and save the text buffer into it
//
//  Effects:
//
//  Arguments:	void
//
//  Requires:
//
//  Returns: 	void
//
//  Signals:
//
//  Modifies:
//
//  Algorithm:
//
//  History:    dd-mmm-yy Author    Comment
//		24-Mar-94 alexgo    author
//
//  Notes:
//
//--------------------------------------------------------------------------

void SaveToFile( void )
{
	char 		szFileName[MAX_PATH];
	OPENFILENAME	ofn;
	static char *	szFilter[] = { "Log Files (*.log)", "*.log",
				"All Files (*.*)", "*.*", ""};
	FILE *		fp;
	LPSTR		psz;


	memset(&ofn, 0, sizeof(OPENFILENAME));
	
	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner = vApp.m_hwndMain;
	ofn.lpstrFilter = szFilter[0];
	ofn.nFilterIndex = 0;
	
	szFileName[0] = '\0';
	
	ofn.lpstrFile = szFileName;
	ofn.nMaxFile = MAX_PATH;
	
	ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
	
	//
	// Get the file
	//
	
	if (GetSaveFileName(&ofn) == FALSE)
	{
		// user hit cancel
	       return;
	}

	// the 'b' specifies binary mode, so \n --> \r\n translations are
	// not done.
	if( !(fp = fopen( szFileName, "wb")) )
	{
		MessageBox( NULL, "Can't open file!", "OleTest Driver",
			MB_ICONEXCLAMATION );
		return;
	}

	psz = (LPSTR)GlobalLock(hText);

	assert(psz);

	fwrite(psz, iText, sizeof(char), fp);

	fclose(fp);

	GlobalUnlock(hText);

	return;
}