5 #include <glib/gstdio.h>
8 #include "chimara-glk-private.h"
10 extern ChimaraGlkPrivate *glk_data;
13 * glk_fileref_iterate:
14 * @fref: A file reference, or %NULL.
15 * @rockptr: Return location for the next fileref's rock, or %NULL.
17 * Iterates through all the existing filerefs. See <link
18 * linkend="chimara-Iterating-Through-Opaque-Objects">Iterating Through Opaque
21 * Returns: the next file reference, or %NULL if there are no more.
24 glk_fileref_iterate(frefid_t fref, glui32 *rockptr)
26 VALID_FILEREF_OR_NULL(fref, return NULL);
31 retnode = glk_data->fileref_list;
33 retnode = fref->fileref_list->next;
34 frefid_t retval = retnode? (frefid_t)retnode->data : NULL;
36 /* Store the fileref's rock in rockptr */
38 *rockptr = glk_fileref_get_rock(retval);
44 * glk_fileref_get_rock:
45 * @fref: A file reference.
47 * Retrieves the file reference @fref's rock value. See <link
48 * linkend="chimara-Rocks">Rocks</link>.
50 * Returns: A rock value.
53 glk_fileref_get_rock(frefid_t fref)
55 VALID_FILEREF(fref, return 0);
59 /* Internal function: create a fileref using the given parameters. */
61 fileref_new(gchar *filename, glui32 rock, glui32 usage, glui32 orig_filemode)
63 g_return_val_if_fail(filename != NULL, NULL);
65 frefid_t f = g_new0(struct glk_fileref_struct, 1);
66 f->magic = MAGIC_FILEREF;
68 f->filename = g_strdup(filename);
70 f->orig_filemode = orig_filemode;
72 /* Add it to the global fileref list */
73 glk_data->fileref_list = g_list_prepend(glk_data->fileref_list, f);
74 f->fileref_list = glk_data->fileref_list;
80 * glk_fileref_create_temp:
81 * @usage: Bitfield with one or more of the <code>fileusage_</code> constants.
82 * @rock: The new fileref's rock value.
84 * Creates a reference to a temporary file. It is always a new file (one which
85 * does not yet exist). The file (once created) will be somewhere out of the
89 * This is why no name is specified; the player will never need to know it.
92 * A temporary file should never be used for long-term storage. It may be
93 * deleted automatically when the program exits, or at some later time, say
94 * when the machine is turned off or rebooted. You do not have to worry about
95 * deleting it yourself.
97 * Returns: A new fileref, or #NULL if the fileref creation failed.
100 glk_fileref_create_temp(glui32 usage, glui32 rock)
102 /* Get a temp file */
103 GError *error = NULL;
104 gchar *filename = NULL;
105 gint handle = g_file_open_tmp("glkXXXXXX", &filename, &error);
108 WARNING_S("Error creating temporary file", error->message);
113 if(close(handle) == -1) /* There is no g_close() */
115 IO_WARNING( "Error closing temporary file", filename, g_strerror(errno) );
121 frefid_t f = fileref_new(filename, rock, usage, filemode_Write);
127 * glk_fileref_create_by_prompt:
128 * @usage: Bitfield with one or more of the <code>fileusage_</code> constants.
129 * @fmode: File mode, contolling the dialog's behavior.
130 * @rock: The new fileref's rock value.
132 * Creates a reference to a file by asking the player to locate it. The library
133 * may simply prompt the player to type a name, or may use a platform-native
134 * file navigation tool. (The prompt, if any, is inferred from the usage
137 * <note><title>Chimara</title>
139 * Chimara uses a <link
140 * linkend="gtk-GtkFileChooserDialog">GtkFileChooserDialog</link>.
143 * @fmode must be one of these values:
146 * <term>#filemode_Read</term>
147 * <listitem><para>The file must already exist; and the player will be asked
148 * to select from existing files which match the usage.</para></listitem>
151 * <term>#filemode_Write</term>
152 * <listitem><para>The file should not exist; if the player selects an
153 * existing file, he will be warned that it will be replaced.
157 * <term>#filemode_ReadWrite</term>
158 * <listitem><para>The file may or may not exist; if it already exists, the
159 * player will be warned that it will be modified.</para></listitem>
162 * <term>#filemode_WriteAppend</term>
163 * <listitem><para>Same behavior as #filemode_ReadWrite.</para></listitem>
167 * The @fmode argument should generally match the @fmode which will be used to
171 * It is possible that the prompt or file tool will have a
172 * <quote>cancel</quote> option. If the player chooses this,
173 * glk_fileref_create_by_prompt() will return %NULL. This is a major reason
174 * why you should make sure the return value is valid before you use it.
177 * Returns: A new fileref, or #NULL if the fileref creation failed or the
178 * dialog was canceled.
181 glk_fileref_create_by_prompt(glui32 usage, glui32 fmode, glui32 rock)
183 /* TODO: Remember current working directory and last used filename
192 chooser = gtk_file_chooser_dialog_new("Select a file to open", NULL,
193 GTK_FILE_CHOOSER_ACTION_OPEN,
194 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
195 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
197 gtk_file_chooser_set_action(GTK_FILE_CHOOSER(chooser),
198 GTK_FILE_CHOOSER_ACTION_OPEN);
201 chooser = gtk_file_chooser_dialog_new("Select a file to save to", NULL,
202 GTK_FILE_CHOOSER_ACTION_SAVE,
203 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
204 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
206 gtk_file_chooser_set_action(GTK_FILE_CHOOSER(chooser),
207 GTK_FILE_CHOOSER_ACTION_SAVE);
208 gtk_file_chooser_set_do_overwrite_confirmation(
209 GTK_FILE_CHOOSER(chooser), TRUE);
211 case filemode_ReadWrite:
212 case filemode_WriteAppend:
213 chooser = gtk_file_chooser_dialog_new("Select a file to save to", NULL,
214 GTK_FILE_CHOOSER_ACTION_SAVE,
215 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
216 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
218 gtk_file_chooser_set_action(GTK_FILE_CHOOSER(chooser),
219 GTK_FILE_CHOOSER_ACTION_SAVE);
222 ILLEGAL_PARAM("Unknown file mode: %u", fmode);
227 if(gtk_dialog_run( GTK_DIALOG(chooser) ) != GTK_RESPONSE_ACCEPT)
229 gtk_widget_destroy(chooser);
234 gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(chooser) );
235 frefid_t f = fileref_new(filename, rock, usage, fmode);
237 gtk_widget_destroy(chooser);
244 * glk_fileref_create_by_name:
245 * @usage: Bitfield with one or more of the <code>fileusage_</code> constants.
247 * @rock: The new fileref's rock value.
249 * This creates a reference to a file with a specific name. The file will be
250 * in a fixed location relevant to your program, and visible to the player.
253 * This usually means <quote>in the same directory as your program.</quote>
255 * <note><title>Chimara</title>
257 * In Chimara, the file is created in the current working directory.
260 * Since filenames are highly platform-specific, you should use
261 * glk_fileref_create_by_name() with care. It is legal to pass any string in the
262 * name argument. However, the library may have to mangle, transform, or
263 * truncate the string to make it a legal native filename.
266 * For example, if you create two filerefs with the names <quote>File</quote>
267 * and <quote>FILE</quote>, they may wind up pointing to the same file; the
268 * platform may not support case distinctions in file names. Another example:
269 * on a platform where file type is specified by filename suffix, the library
270 * will add an appropriate suffix based on the usage; any suffix in the string
271 * will be overwritten or added to. For that matter, remember that the period
272 * is not a legal character in Acorn filenames...
275 * The most conservative approach is to pass a string of no more than 8
276 * characters, consisting entirely of upper-case letters and numbers, starting
277 * with a letter. You can then be reasonably sure that the resulting filename
278 * will display all the characters you specify — in some form.
280 * Returns: A new fileref, or %NULL if the fileref creation failed.
283 glk_fileref_create_by_name(glui32 usage, char *name, glui32 rock)
285 g_return_val_if_fail(name != NULL && strlen(name) > 0, NULL);
287 /* Find out what encoding filenames are in */
288 const gchar **charsets; /* Do not free */
289 g_get_filename_charsets(&charsets);
291 /* Convert name to that encoding */
292 GError *error = NULL;
293 gchar *osname = g_convert(name, -1, charsets[0], "ISO-8859-1", NULL, NULL,
297 WARNING_S("Error during latin1->filename conversion", error->message);
301 /* Do any string-munging here to remove illegal characters from filename.
302 On ext3, the only illegal characters are '/' and '\0'. TODO: Should this
303 function be allowed to reference files in other directories, or should we
306 frefid_t f = fileref_new(osname, rock, usage, filemode_ReadWrite);
312 * glk_fileref_create_from_fileref:
313 * @usage: Bitfield with one or more of the <code>fileusage_</code> constants.
314 * @fref: Fileref to copy.
315 * @rock: The new fileref's rock value.
317 * This copies an existing file reference @fref, but changes the usage. (The
318 * original fileref is not modified.)
320 * The use of this function can be tricky. If you change the type of the fileref
321 * (#fileusage_Data, #fileusage_SavedGame, etc), the new reference may or may
322 * not point to the same actual disk file.
325 * This generally depends on whether the platform uses suffixes to indicate
329 * If you do this, and open both file references for writing, the results are
330 * unpredictable. It is safest to change the type of a fileref only if it refers
331 * to a nonexistent file.
333 * If you change the mode of a fileref (#fileusage_TextMode,
334 * #fileusage_BinaryMode), but leave the rest of the type unchanged, the new
335 * fileref will definitely point to the same disk file as the old one.
337 * Obviously, if you write to a file in text mode and then read from it in
338 * binary mode, the results are platform-dependent.
340 * Returns: A new fileref, or %NULL if the fileref creation failed.
343 glk_fileref_create_from_fileref(glui32 usage, frefid_t fref, glui32 rock)
345 VALID_FILEREF(fref, return NULL);
346 return fileref_new(fref->filename, rock, usage, fref->orig_filemode);
350 * glk_fileref_destroy:
351 * @fref: Fileref to destroy.
353 * Destroys a fileref which you have created. This does <emphasis>not</emphasis>
354 * affect the disk file; it just reclaims the resources allocated by the
355 * <code>glk_fileref_create...</code> function.
357 * It is legal to destroy a fileref after opening a file with it (while the
358 * file is still open.) The fileref is only used for the opening operation,
359 * not for accessing the file stream.
362 glk_fileref_destroy(frefid_t fref)
364 VALID_FILEREF(fref, return);
366 glk_data->fileref_list = g_list_delete_link(glk_data->fileref_list, fref->fileref_list);
368 g_free(fref->filename);
370 fref->magic = MAGIC_FREE;
375 * glk_fileref_delete_file:
376 * @fref: A refrence to the file to delete.
378 * Deletes the file referred to by @fref. It does not destroy @fref itself.
381 glk_fileref_delete_file(frefid_t fref)
383 VALID_FILEREF(fref, return);
384 if( glk_fileref_does_file_exist(fref) )
385 if(g_unlink(fref->filename) == -1)
386 IO_WARNING( "Error deleting file", fref->filename, g_strerror(errno) );
390 * glk_fileref_does_file_exist:
391 * @fref: A fileref to check.
393 * Checks whether the file referred to by @fref exists.
395 * Returns: %TRUE (1) if @fref refers to an existing file, and %FALSE (0) if
399 glk_fileref_does_file_exist(frefid_t fref)
401 VALID_FILEREF(fref, return 0);
402 if( g_file_test(fref->filename, G_FILE_TEST_EXISTS) )