Debian Codegraph @

terps/tads/tads2/msdos/oswin_askfile.c

raw symbols SCIP
1 /*
2 * Ask the user for a filename, using a system-dependent dialog or other
3 * mechanism. Returns zero on success, non-zero on error.
4 */
5 int os_askfile(const char *prompt, char *fname_buf, int fname_buf_len,
6 int prompt_type, int file_type)
7 {
8 OPENFILENAME info;
9 int ret;
10 char filter[256];
11 const char *def_ext;
12 const char *save_def_ext;
13 static char lastsave[OSFNMAX] = "";
14
15 /* presume we won't need a default extension */
16 def_ext = 0;
17 save_def_ext = 0;
18
19 /*
20 * Build the appropriate filter. Most of the filters are boilerplate,
21 * but the "saved game" filter (for both TADS 2 and TADS 3 saved game
22 * files) is special because we need to use the dynamically-selected
23 * default saved game extension for our filter. This means that the
24 * "unknown" type is equally complicated, because it needs to include
25 * the dynamic saved game extension as well.
26 */
27 if (file_type == OSFTSAVE
28 || file_type == OSFTT3SAV
29 || file_type == OSFTUNK)
30 {
31 static const char filter_start[] =
32 "Saved Game Positions\0*.";
33 static const char filter_end_sav[] =
34 "All Files\0*.*\0\0";
35 static const char filter_end_unk[] =
36 "TADS Games\0*.GAM\0"
37 "Text Files\0*.TXT\0"
38 "Log Files\0*.LOG\0"
39 "All Files\0*.*\0\0";
40 size_t totlen;
41 size_t extlen;
42 const char *save_filter;
43
44 /*
45 * Get the default saved game extension. If the game program has
46 * explicitly set a custom extension, use that. Otherwise, use the
47 * appropriate extension for the file type.
48 */
49 if (default_saved_game_ext[0] != '\0')
50 {
51 /*
52 * there's an explicit filter, so use it as the filter and as
53 * the default extension
54 */
55 save_filter = default_saved_game_ext;
56 save_def_ext = default_saved_game_ext;
57 }
58 else if (file_type == OSFTSAVE)
59 {
60 /* tads 2 saved game, so use the ".sav" suffix */
61 save_filter = "sav";
62 save_def_ext = "sav";
63 }
64 else if (file_type == OSFTT3SAV)
65 {
66 /* tads 3 saved game, so use the ".t3v" suffix */
67 save_filter = "t3v";
68 save_def_ext = "t3v";
69 }
70 else
71 {
72 /*
73 * Unknown file type - use BOTH tads 2 and tads 3 saved game
74 * extensions for the filter, but do not apply any default
75 * extension to files we're creating.
76 *
77 * Note that we will append this filter to "*.", so the first
78 * extension in our list doesn't need the "*.", but the second
79 * one does.
80 */
81 save_filter = "sav;*.t3v";
82 save_def_ext = 0;
83 }
84
85 /*
86 * Build the filter string for saved game files. Note that we
87 * need to use memcpy rather than strcpy, because we need to
88 * include a bunch of embedded null characters within the
89 * string. First, copy the prefix up to the saved game
90 * extension...
91 */
92 memcpy(filter, filter_start, sizeof(filter_start) - 1);
93 totlen = sizeof(filter_start) - 1;
94
95 /* now add the saved game extension that we're to use... */
96 extlen = strlen(save_filter) + 1;
97 memcpy(filter + totlen, save_filter, extlen);
98 totlen += extlen;
99
100 /* finish with the whole rest of it */
101 if (file_type == OSFTSAVE || file_type == OSFTT3SAV)
102 {
103 /* saved game: use the saved game extra filter list */
104 memcpy(filter + totlen, filter_end_sav, sizeof(filter_end_sav));
105
106 /*
107 * if we're creating a new saved game file, apply the default
108 * suffix to the name the user enters for the new file
109 */
110 if (prompt_type == OS_AFP_SAVE)
111 def_ext = save_def_ext;
112 }
113 else
114 {
115 /* not a saved game: use the unknown file type filter list */
116 memcpy(filter + totlen, filter_end_unk, sizeof(filter_end_unk));
117 }
118 }
119 else
120 {
121 static const struct
122 {
123 const char *disp;
124 const char *pat;
125 const char *def_ext;
126 }
127 filters[] =
128 {
129 { "TADS Games", "*.gam", "gam" }, /* OSFTGAME */
130 { 0, 0 }, /* unused - OSFTSAVE */
131 { "Transcripts", "*.log", "log" }, /* OSFTLOG */
132 { "Swap Files", "*.dat", "dat" }, /* OSFTSWAP */
133 { "Data Files", "*.dat", "dat" }, /* OSFTDATA */
134 { "Command Scripts", "*.cmd", "cmd" }, /* OSFTCMD */
135 { "Message Files", "*.msg", "msg" }, /* OSFTERRS */
136 { "Text Files", "*.txt", "txt" }, /* OSFTTEXT */
137 { "Binary Files", "*.dat", "dat" }, /* OSFTBIN */
138 { "Character Maps", "*.tcp", "tcp" }, /* OSFTCMAP */
139 { "Preference Files","*.dat", "dat" }, /* OSFTPREF */
140 { 0, 0, 0 }, /* unused - OSFTUNK */
141 { "T3 Applications", "*.t3" /* was t3x */, "t3" }, /* OSFTT3IMG */
142 { "T3 Object Files", "*.t3o", "t3o" }, /* OSFTT3OBJ */
143 { "T3 Symbol Files", "*.t3s", "t3s" }, /* OSFTT3SYM */
144 { 0, 0, 0 } /* unused - OSFTT3SAV */
145 };
146 static const char filter_end[] = "All Files\0*.*\0\0";
147 char *p;
148
149 /*
150 * We have an ordinary, static filter -- choose the appropriate
151 * filter from our list. Start with the display name...
152 */
153 strcpy(filter, filters[file_type].disp);
154
155 /* add the file filter pattern after the null character */
156 p = filter + strlen(filter) + 1;
157 strcpy(p, filters[file_type].pat);
158
159 /* add the filter terminator after the null */
160 p += strlen(p) + 1;
161 memcpy(p, filter_end, sizeof(filter_end));
162
163 /* remember the default extension */
164 save_def_ext = def_ext = filters[file_type].def_ext;
165 }
166
167 /* set up the open-file structure */
168 info.lStructSize = sizeof(info);
169 info.hwndOwner = GetActiveWindow();
170 info.hInstance = oss_G_hinstance;
171 info.lpstrFilter = filter;
172 info.lpstrCustomFilter = 0;
173 info.nFilterIndex = 0;
174 info.lpstrFile = fname_buf;
175 fname_buf[0] = '\0';
176 info.nMaxFile = fname_buf_len;
177 info.lpstrFileTitle = 0;
178 info.nMaxFileTitle = 0;
179 info.lpstrInitialDir = (S_open_file_dir[0] != '\0' ? S_open_file_dir : 0);
180 info.lpstrTitle = prompt;
181 info.nFileOffset = 0;
182 info.nFileExtension = 0;
183 info.lpstrDefExt = def_ext;
184 info.lCustData = 0;
185 info.lpfnHook = filedlg_hook;
186 info.Flags = OFN_ENABLEHOOK | OFN_EXPLORER | OFN_NOCHANGEDIR;
187 info.lpTemplateName = 0;
188
189 /*
190 * If we're asking for a saved game file, pick a default. If we've
191 * asked before, use the same default as last time; if not, use the
192 * game name as the default. If we're opening a saved game rather then
193 * saving one, don't apply the default if the file we'd get by applying
194 * the default doesn't exist.
195 */
196 if (file_type == OSFTSAVE || file_type == OSFTT3SAV)
197 {
198 /* try using the last saved game name */
199 strncpy(fname_buf, lastsave, fname_buf_len);
200 fname_buf[fname_buf_len - 1] = '\0';
201
202 /* if that's empty, generate a default name based on the game name */
203 if (fname_buf[0] == '\0' && G_os_gamename[0] != '\0')
204 {
205 /* copy the game name as the base of the save-file name */
206 strncpy(fname_buf, G_os_gamename, fname_buf_len);
207 fname_buf[fname_buf_len - 1] = '\0';
208
209 /* remove the old extension */
210 os_remext(fname_buf);
211
212 /* ... and replace it with the saved-game extension */
213 if (save_def_ext != 0
214 && strlen(fname_buf) + strlen(save_def_ext) + 1
215 < (size_t)fname_buf_len)
216 os_defext(fname_buf, save_def_ext);
217 }
218
219 /*
220 * if we're asking about a file to open, make sure our default
221 * exists - if not, don't offer it as the default, since it's a
222 * waste of time and attention for the user to have to change it
223 */
224 if (prompt_type == OS_AFP_OPEN && osfacc(fname_buf))
225 fname_buf[0] = '\0';
226 }
227
228 /* display the dialog */
229 if (prompt_type == OS_AFP_SAVE)
230 {
231 info.Flags |= OFN_NOCHANGEDIR | OFN_OVERWRITEPROMPT;
232 ret = GetSaveFileName(&info);
233 }
234 else
235 {
236 /* ask for a file to open */
237 ret = GetOpenFileName(&info);
238 }
239
240 /* translate the result code to an OS_AFE_xxx value */
241 if (ret == 0)
242 {
243 /*
244 * an error occurred - check to see what happened: if the extended
245 * error is zero, it means that the user simply canceled the
246 * dialog, otherwise it means that an error occurred
247 */
248 ret = (CommDlgExtendedError() == 0 ? OS_AFE_CANCEL : OS_AFE_FAILURE);
249 }
250 else
251 {
252 char path[OSFNMAX];
253
254 /* success */
255 ret = OS_AFE_SUCCESS;
256
257 /*
258 * remember the file's directory for the next file dialog (we need
259 * to do this explicitly because we're not allowing the dialog to
260 * set the working directory)
261 */
262 os_get_path_name(path, sizeof(path), fname_buf);
263 oss_set_open_file_dir(path);
264
265 /*
266 * if this is a saved game prompt, remember the response as the
267 * default for next time
268 */
269 if (file_type == OSFTSAVE || file_type == OSFTT3SAV)
270 strcpy(lastsave, fname_buf);
271 }
272
273 /* return the result */
274 return ret;
275 }
276