-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgfile.cpp
269 lines (237 loc) · 7.42 KB
/
gfile.cpp
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
* ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*
* Copyright (c) Microsoft Corporation. All Rights Reserved.
*/
/*
* File Open/Create dialogs
*
*/
/*
* these dialog functions exist because they were written and
* used before the commmon dialogs existed.
*
* they have now been reduced to just calls to the common file dialog
* functions
*/
/*---includes-----------------------------------------------------------*/
#include "precomp.h"
#include "gutilsrc.h"
#include "errorout.h"
/*--functions----------------------------------------------------------*/
/*
* gfile_open
* dialog asking the user to select an existing file to open.
*
* parameters
*
* prompt - message to user indicating purpose of file
* (to be displayed somewhere in dialog box.
*
* ext - default file extension if user enters file without
* extension.
*
* spec - default file spec (eg *.*)
*
* pszFull - buffer where full filename (including path) is returned.
*
* cchMax - size of pszFull buffer.
*
* fn - buffer where filename (just final element) is returned.
*
* returns - true if file selected and exists (tested with OF_EXIST).
* FALSE if dialog cancelled. If user selects a file that we cannot
* open, we complain and restart the dialog.
*
* if TRUE is returned, the file will have been successfully opened,
* for reading and then closed again.
*/
BOOL
FAR
PASCAL
gfile_open(
HWND hwnd,
LPSTR prompt,
LPSTR ext,
LPSTR spec,
LPSTR pszFull,
int cchMax,
LPSTR fn
)
{
OPENFILENAME ofn;
char achFilters[MAX_PATH];
char szTmp[MAX_PATH * 2] = {0};
HANDLE fh;
HRESULT hr;
if (!pszFull)
{
pszFull = szTmp;
cchMax = sizeof(szTmp) / sizeof(szTmp[0]);
}
if (cchMax < 1)
return FALSE;
/* build filter-pair buffer to contain one pair - the spec filter,
* twice (one of the pair should be the filter, the second should be
* the title of the filter - we don't have a title so we use the
* filter both times.
*/
hr = StringCchPrintf(achFilters, (sizeof(achFilters)/sizeof(achFilters[0])) - 1, "%s%c%s", spec, 0, spec);
if (FAILED(hr))
OutputError(hr, IDS_SAFE_PRINTF);
/*
* initialise arguments to dialog proc
*/
memset(&ofn, 0, sizeof(ofn));
// GetOpenFileName ang GetSaveFileName unfortunately
// validate the size of the structue. So we need to lie to
// the function if we were built for >=Win2000 and
// running on an earlier OS
#if (_WIN32_WINNT >= 0x0500)
ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
#else
ofn.lStructSize = sizeof(OPENFILENAME);
#endif
ofn.hwndOwner = hwnd;
ofn.hInstance = NULL;
ofn.lpstrFilter = achFilters;
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = 0;
ofn.nFilterIndex = 1; // first filter pair in list
pszFull[0] = '\0';
ofn.lpstrFile = pszFull; // we need to get the full path to open
ofn.nMaxFile = cchMax;
ofn.lpstrFileTitle = fn; // return final elem of name here
ofn.nMaxFileTitle = 13; // assume just big enough for 8.3+null
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = prompt; // dialog title is good place for prompt text
ofn.Flags = OFN_FILEMUSTEXIST |
OFN_HIDEREADONLY |
OFN_PATHMUSTEXIST;
ofn.lpstrDefExt = ext;
/*
* loop until the user cancels, or selects a file that we can open
*/
do {
if (!GetOpenFileName(&ofn)) {
return(FALSE);
}
fh = CreateFile(pszFull, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (fh == INVALID_HANDLE_VALUE) {
if (MessageBox(NULL, "File Could Not Be Opened", "File Open",
MB_OKCANCEL|MB_ICONSTOP) == IDCANCEL) {
return(FALSE);
}
}
} while (fh == INVALID_HANDLE_VALUE);
CloseHandle(fh);
return(TRUE);
}
/*
* gfile_new
* dialog asking the user to name a file for writing to.
*
* parameters
*
* prompt - message to user indicating purpose of file
* (to be displayed somewhere in dialog box.
*
* ext - default file extension if user enters file without
* extension.
*
* spec - default file spec (eg *.*)
*
* pszFull - buffer where full filename (including path) is returned.
*
* cchMax - size of pszFull buffer.
*
* fn - buffer where filename (just final element) is returned.
*
* returns - true if file selected and exists (tested with OF_EXIST).
* FALSE if dialog cancelled. If user selects a file that we cannot
* open, we complain and restart the dialog.
*
* if TRUE is returned, the file will have been successfully
* created and opened for writing and then closed again.
*/
BOOL
FAR
PASCAL
gfile_new(
LPSTR prompt,
LPSTR ext,
LPSTR spec,
LPSTR pszFull,
int cchMax,
LPSTR fn
)
{
OPENFILENAME ofn;
char achFilters[MAX_PATH] = {0};
char szTmp[MAX_PATH * 2];
HANDLE fh;
HRESULT hr;
if (!pszFull)
{
pszFull = szTmp;
cchMax = sizeof(szTmp) / sizeof(szTmp[0]);
}
if (cchMax < 1)
return FALSE;
/* build filter-pair buffer to contain one pair - the spec filter,
* twice (one of the pair should be the filter, the second should be
* the title of the filter - we don't have a title so we use the
* filter both times. remember double null at end of list of strings.
*/
hr = StringCchPrintf(achFilters,(sizeof(achFilters)/sizeof(achFilters[0])) - 1, "%s%c%s", spec, 0, spec);
if (FAILED(hr))
OutputError(hr, IDS_SAFE_PRINTF);
/*
* initialise arguments to dialog proc
*/
memset(&ofn, 0, sizeof(ofn));
// GetOpenFileName ang GetSaveFileName unfortunately
// validate the size of the structue. So we need to lie to
// the function if we were built for >=Win2000 and
// running on an earlier OS
#if (_WIN32_WINNT >= 0x0500)
ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
#else
ofn.lStructSize = sizeof(OPENFILENAME);
#endif
ofn.hwndOwner = NULL;
ofn.hInstance = NULL;
ofn.lpstrFilter = achFilters;
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = 0;
ofn.nFilterIndex = 1; // first filter pair in list
pszFull[0] = '\0';
ofn.lpstrFile = pszFull; // we need to get the full path to open
ofn.nMaxFile = cchMax;
ofn.lpstrFileTitle = fn; // return final elem of name here
ofn.nMaxFileTitle = 13; // assume just big enough for 8.3+null
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = prompt; // dialog title is good place for prompt text
ofn.Flags = OFN_HIDEREADONLY;
ofn.lpstrDefExt = ext;
/*
* loop until the user cancels, or selects a file that we can create/write
*/
do {
if (!GetSaveFileName(&ofn)) {
return(FALSE);
}
fh = CreateFile(pszFull, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
if (fh == INVALID_HANDLE_VALUE) {
if (MessageBox(NULL, "File Could Not Be Created", "File Open",
MB_OKCANCEL|MB_ICONSTOP) == IDCANCEL) {
return(FALSE);
}
}
} while (fh == INVALID_HANDLE_VALUE);
CloseHandle(fh);
return(TRUE);
}