5 #include <glib/gstdio.h>
8 #include "chimara-glk-private.h"
11 extern GPrivate *glk_data_key;
13 /* Internal function: create a fileref using the given parameters. */
15 fileref_new(gchar *filename, glui32 rock, glui32 usage, glui32 orig_filemode)
17 g_return_val_if_fail(filename != NULL, NULL);
19 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
21 frefid_t f = g_new0(struct glk_fileref_struct, 1);
22 f->magic = MAGIC_FILEREF;
24 if(glk_data->register_obj)
25 f->disprock = (*glk_data->register_obj)(f, gidisp_Class_Fileref);
27 f->filename = g_strdup(filename);
29 f->orig_filemode = orig_filemode;
31 /* Add it to the global fileref list */
32 glk_data->fileref_list = g_list_prepend(glk_data->fileref_list, f);
33 f->fileref_list = glk_data->fileref_list;
39 fileref_close_common(frefid_t fref)
41 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
43 glk_data->fileref_list = g_list_delete_link(glk_data->fileref_list, fref->fileref_list);
45 if(glk_data->unregister_obj)
47 (*glk_data->unregister_obj)(fref, gidisp_Class_Fileref, fref->disprock);
48 fref->disprock.ptr = NULL;
52 g_free(fref->filename);
54 fref->magic = MAGIC_FREE;
59 * glk_fileref_iterate:
60 * @fref: A file reference, or %NULL.
61 * @rockptr: Return location for the next fileref's rock, or %NULL.
63 * Iterates through all the existing filerefs. See <link
64 * linkend="chimara-Iterating-Through-Opaque-Objects">Iterating Through Opaque
67 * Returns: the next file reference, or %NULL if there are no more.
70 glk_fileref_iterate(frefid_t fref, glui32 *rockptr)
72 VALID_FILEREF_OR_NULL(fref, return NULL);
74 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
78 retnode = glk_data->fileref_list;
80 retnode = fref->fileref_list->next;
81 frefid_t retval = retnode? (frefid_t)retnode->data : NULL;
83 /* Store the fileref's rock in rockptr */
85 *rockptr = glk_fileref_get_rock(retval);
91 * glk_fileref_get_rock:
92 * @fref: A file reference.
94 * Retrieves the file reference @fref's rock value. See <link
95 * linkend="chimara-Rocks">Rocks</link>.
97 * Returns: A rock value.
100 glk_fileref_get_rock(frefid_t fref)
102 VALID_FILEREF(fref, return 0);
107 * glk_fileref_create_temp:
108 * @usage: Bitfield with one or more of the <code>fileusage_</code> constants.
109 * @rock: The new fileref's rock value.
111 * Creates a reference to a temporary file. It is always a new file (one which
112 * does not yet exist). The file (once created) will be somewhere out of the
116 * This is why no name is specified; the player will never need to know it.
119 * A temporary file should never be used for long-term storage. It may be
120 * deleted automatically when the program exits, or at some later time, say
121 * when the machine is turned off or rebooted. You do not have to worry about
122 * deleting it yourself.
124 * Returns: A new fileref, or #NULL if the fileref creation failed.
127 glk_fileref_create_temp(glui32 usage, glui32 rock)
129 /* Get a temp file */
130 GError *error = NULL;
131 gchar *filename = NULL;
132 gint handle = g_file_open_tmp("glkXXXXXX", &filename, &error);
135 WARNING_S("Error creating temporary file", error->message);
140 if(close(handle) == -1) /* There is no g_close() */
142 IO_WARNING( "Error closing temporary file", filename, g_strerror(errno) );
148 frefid_t f = fileref_new(filename, rock, usage, filemode_Write);
154 * glk_fileref_create_by_prompt:
155 * @usage: Bitfield with one or more of the <code>fileusage_</code> constants.
156 * @fmode: File mode, contolling the dialog's behavior.
157 * @rock: The new fileref's rock value.
159 * Creates a reference to a file by asking the player to locate it. The library
160 * may simply prompt the player to type a name, or may use a platform-native
161 * file navigation tool. (The prompt, if any, is inferred from the usage
164 * <note><title>Chimara</title>
166 * Chimara uses a <link
167 * linkend="gtk-GtkFileChooserDialog">GtkFileChooserDialog</link>. The default
168 * starting location for the dialog may be set with glkunix_set_base_file().
171 * @fmode must be one of these values:
174 * <term>%filemode_Read</term>
175 * <listitem><para>The file must already exist; and the player will be asked
176 * to select from existing files which match the usage.</para></listitem>
179 * <term>%filemode_Write</term>
180 * <listitem><para>The file should not exist; if the player selects an
181 * existing file, he will be warned that it will be replaced.
185 * <term>%filemode_ReadWrite</term>
186 * <listitem><para>The file may or may not exist; if it already exists, the
187 * player will be warned that it will be modified.</para></listitem>
190 * <term>%filemode_WriteAppend</term>
191 * <listitem><para>Same behavior as %filemode_ReadWrite.</para></listitem>
195 * The @fmode argument should generally match the @fmode which will be used to
199 * It is possible that the prompt or file tool will have a
200 * <quote>cancel</quote> option. If the player chooses this,
201 * glk_fileref_create_by_prompt() will return %NULL. This is a major reason
202 * why you should make sure the return value is valid before you use it.
205 * Returns: A new fileref, or #NULL if the fileref creation failed or the
206 * dialog was canceled.
209 glk_fileref_create_by_prompt(glui32 usage, glui32 fmode, glui32 rock)
211 /* TODO: Remember current working directory and last used filename
215 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
222 chooser = gtk_file_chooser_dialog_new("Select a file to open", NULL,
223 GTK_FILE_CHOOSER_ACTION_OPEN,
224 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
225 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
227 gtk_file_chooser_set_action(GTK_FILE_CHOOSER(chooser), GTK_FILE_CHOOSER_ACTION_OPEN);
230 chooser = gtk_file_chooser_dialog_new("Select a file to save to", NULL,
231 GTK_FILE_CHOOSER_ACTION_SAVE,
232 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
233 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
235 gtk_file_chooser_set_action(GTK_FILE_CHOOSER(chooser), GTK_FILE_CHOOSER_ACTION_SAVE);
236 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(chooser), TRUE);
238 case filemode_ReadWrite:
239 case filemode_WriteAppend:
240 chooser = gtk_file_chooser_dialog_new("Select a file to save to", NULL,
241 GTK_FILE_CHOOSER_ACTION_SAVE,
242 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
243 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
245 gtk_file_chooser_set_action(GTK_FILE_CHOOSER(chooser), GTK_FILE_CHOOSER_ACTION_SAVE);
248 ILLEGAL_PARAM("Unknown file mode: %u", fmode);
253 if(glk_data->current_dir)
254 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), glk_data->current_dir);
256 if(gtk_dialog_run( GTK_DIALOG(chooser) ) != GTK_RESPONSE_ACCEPT)
258 gtk_widget_destroy(chooser);
262 gchar *filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(chooser) );
263 frefid_t f = fileref_new(filename, rock, usage, fmode);
265 gtk_widget_destroy(chooser);
272 * glk_fileref_create_by_name:
273 * @usage: Bitfield with one or more of the <code>fileusage_</code> constants.
275 * @rock: The new fileref's rock value.
277 * This creates a reference to a file with a specific name. The file will be
278 * in a fixed location relevant to your program, and visible to the player.
281 * This usually means <quote>in the same directory as your program.</quote>
283 * <note><title>Chimara</title>
285 * In Chimara, the file is created in the directory last set by
286 * glkunix_set_base_file(), and otherwise in the current working directory.
289 * Since filenames are highly platform-specific, you should use
290 * glk_fileref_create_by_name() with care. It is legal to pass any string in the
291 * name argument. However, the library may have to mangle, transform, or
292 * truncate the string to make it a legal native filename.
295 * For example, if you create two filerefs with the names <quote>File</quote>
296 * and <quote>FILE</quote>, they may wind up pointing to the same file; the
297 * platform may not support case distinctions in file names. Another example:
298 * on a platform where file type is specified by filename suffix, the library
299 * will add an appropriate suffix based on the usage; any suffix in the string
300 * will be overwritten or added to. For that matter, remember that the period
301 * is not a legal character in Acorn filenames...
304 * The most conservative approach is to pass a string of no more than 8
305 * characters, consisting entirely of upper-case letters and numbers, starting
306 * with a letter. You can then be reasonably sure that the resulting filename
307 * will display all the characters you specify — in some form.
309 * Returns: A new fileref, or %NULL if the fileref creation failed.
312 glk_fileref_create_by_name(glui32 usage, char *name, glui32 rock)
314 g_return_val_if_fail(name != NULL && strlen(name) > 0, NULL);
316 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
318 /* Do any string-munging here to remove illegal Latin-1 characters from
319 filename. On ext3, the only illegal characters are '/' and '\0'. */
320 g_strdelimit(name, "/", '_');
322 /* Find out what encoding filenames are in */
323 const gchar **charsets; /* Do not free */
324 g_get_filename_charsets(&charsets);
326 /* Convert name to that encoding */
327 GError *error = NULL;
328 gchar *osname = g_convert(name, -1, charsets[0], "ISO-8859-1", NULL, NULL,
332 WARNING_S("Error during latin1->filename conversion", error->message);
337 if(glk_data->current_dir)
338 path = g_build_filename(glk_data->current_dir, osname, NULL);
340 path = g_strdup(osname);
343 frefid_t f = fileref_new(path, rock, usage, filemode_ReadWrite);
349 * glk_fileref_create_from_fileref:
350 * @usage: Bitfield with one or more of the <code>fileusage_</code> constants.
351 * @fref: Fileref to copy.
352 * @rock: The new fileref's rock value.
354 * This copies an existing file reference @fref, but changes the usage. (The
355 * original fileref is not modified.)
357 * The use of this function can be tricky. If you change the type of the fileref
358 * (%fileusage_Data, %fileusage_SavedGame, etc), the new reference may or may
359 * not point to the same actual disk file.
362 * This generally depends on whether the platform uses suffixes to indicate
366 * If you do this, and open both file references for writing, the results are
367 * unpredictable. It is safest to change the type of a fileref only if it refers
368 * to a nonexistent file.
370 * If you change the mode of a fileref (%fileusage_TextMode,
371 * %fileusage_BinaryMode), but leave the rest of the type unchanged, the new
372 * fileref will definitely point to the same disk file as the old one.
374 * Obviously, if you write to a file in text mode and then read from it in
375 * binary mode, the results are platform-dependent.
377 * Returns: A new fileref, or %NULL if the fileref creation failed.
380 glk_fileref_create_from_fileref(glui32 usage, frefid_t fref, glui32 rock)
382 VALID_FILEREF(fref, return NULL);
383 return fileref_new(fref->filename, rock, usage, fref->orig_filemode);
387 * glk_fileref_destroy:
388 * @fref: Fileref to destroy.
390 * Destroys a fileref which you have created. This does <emphasis>not</emphasis>
391 * affect the disk file; it just reclaims the resources allocated by the
392 * <code>glk_fileref_create...</code> function.
394 * It is legal to destroy a fileref after opening a file with it (while the
395 * file is still open.) The fileref is only used for the opening operation,
396 * not for accessing the file stream.
399 glk_fileref_destroy(frefid_t fref)
401 VALID_FILEREF(fref, return);
402 fileref_close_common(fref);
406 * glk_fileref_delete_file:
407 * @fref: A refrence to the file to delete.
409 * Deletes the file referred to by @fref. It does not destroy @fref itself.
412 glk_fileref_delete_file(frefid_t fref)
414 VALID_FILEREF(fref, return);
415 if( glk_fileref_does_file_exist(fref) )
416 if(g_unlink(fref->filename) == -1)
417 IO_WARNING( "Error deleting file", fref->filename, g_strerror(errno) );
421 * glk_fileref_does_file_exist:
422 * @fref: A fileref to check.
424 * Checks whether the file referred to by @fref exists.
426 * Returns: %TRUE (1) if @fref refers to an existing file, and %FALSE (0) if
430 glk_fileref_does_file_exist(frefid_t fref)
432 VALID_FILEREF(fref, return 0);
433 if( g_file_test(fref->filename, G_FILE_TEST_EXISTS) )