4 #include <glib/gstdio.h>
6 #include "chimara-glk-private.h"
8 extern ChimaraGlkPrivate *glk_data;
11 * glk_fileref_iterate:
12 * @fref: A file reference, or #NULL.
13 * @rockptr: Return location for the next window's rock, or #NULL.
15 * Iterates over the list of file references; if @fref is #NULL, it returns the
16 * first file reference, otherwise the next file reference after @fref. If
17 * there are no more, it returns #NULL. The file reference's rock is stored in
18 * @rockptr. If you don't want the rocks to be returned, you may set @rockptr
21 * The order in which file references are returned is arbitrary. The order may
22 * change every time you create or destroy a file reference, invalidating the
25 * Returns: the next file reference, or #NULL if there are no more.
28 glk_fileref_iterate(frefid_t fref, glui32 *rockptr)
33 retnode = glk_data->fileref_list;
35 retnode = fref->fileref_list->next;
36 frefid_t retval = retnode? (frefid_t)retnode->data : NULL;
38 /* Store the fileref's rock in rockptr */
40 *rockptr = glk_fileref_get_rock(retval);
46 * glk_fileref_get_rock:
47 * @fref: A file reference.
49 * Returns the file reference @fref's rock value.
51 * Returns: A rock value.
54 glk_fileref_get_rock(frefid_t fref)
56 g_return_val_if_fail(fref != NULL, 0);
60 /* Internal function: create a fileref using the given parameters. */
62 fileref_new(gchar *filename, glui32 rock, glui32 usage, glui32 orig_filemode)
64 g_return_val_if_fail(filename != NULL, NULL);
66 frefid_t f = g_new0(struct glk_fileref_struct, 1);
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 #fileusage_ 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
88 * A temporary file should never be used for long-term storage. It may be
89 * deleted automatically when the program exits, or at some later time, say
90 * when the machine is turned off or rebooted. You do not have to worry about
91 * deleting it yourself.
93 * Returns: A new fileref, or #NULL if the fileref creation failed.
96 glk_fileref_create_temp(glui32 usage, glui32 rock)
100 gchar *filename = NULL;
101 gint handle = g_file_open_tmp("glkXXXXXX", &filename, &error);
104 g_warning("Error creating temporary file: %s", error->message);
109 if(close(handle) == -1) /* There is no g_close()? */
111 g_warning("Error closing temporary file.");
117 frefid_t f = fileref_new(filename, rock, usage, filemode_Write);
123 * glk_fileref_create_by_prompt:
124 * @usage: Bitfield with one or more of the #fileusage_ constants.
125 * @fmode: File mode, contolling the dialog's behavior.
126 * @rock: The new fileref's rock value.
128 * Creates a reference to a file by opening a file chooser dialog. If @fmode is
129 * #filemode_Read, then the file must already exist and the user will be asked
130 * to select from existing files. If @fmode is #filemode_Write, then the file
131 * should not exist; if the user selects an existing file, he or she will be
132 * warned that it will be replaced. If @fmode is #filemode_ReadWrite, then the
133 * file may or may not exist; if it already exists, the user will be warned
134 * that it will be modified. The @fmode argument should generally match the
135 * @fmode which will be used to open the file.
137 * Returns: A new fileref, or #NULL if the fileref creation failed or the
138 * dialog was canceled.
141 glk_fileref_create_by_prompt(glui32 usage, glui32 fmode, glui32 rock)
143 /* TODO: Remember current working directory and last used filename
152 chooser = gtk_file_chooser_dialog_new("Select a file to open", NULL,
153 GTK_FILE_CHOOSER_ACTION_OPEN,
154 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
155 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
157 gtk_file_chooser_set_action(GTK_FILE_CHOOSER(chooser),
158 GTK_FILE_CHOOSER_ACTION_OPEN);
161 chooser = gtk_file_chooser_dialog_new("Select a file to save to", NULL,
162 GTK_FILE_CHOOSER_ACTION_SAVE,
163 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
164 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
166 gtk_file_chooser_set_action(GTK_FILE_CHOOSER(chooser),
167 GTK_FILE_CHOOSER_ACTION_SAVE);
168 gtk_file_chooser_set_do_overwrite_confirmation(
169 GTK_FILE_CHOOSER(chooser), TRUE);
171 case filemode_ReadWrite:
172 case filemode_WriteAppend:
173 chooser = gtk_file_chooser_dialog_new("Select a file to save to", NULL,
174 GTK_FILE_CHOOSER_ACTION_SAVE,
175 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
176 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
178 gtk_file_chooser_set_action(GTK_FILE_CHOOSER(chooser),
179 GTK_FILE_CHOOSER_ACTION_SAVE);
182 g_warning("glk_fileref_create_by_prompt: Unsupported mode");
187 if(gtk_dialog_run( GTK_DIALOG(chooser) ) != GTK_RESPONSE_ACCEPT)
189 gtk_widget_destroy(chooser);
194 gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(chooser) );
195 frefid_t f = fileref_new(filename, rock, usage, fmode);
197 gtk_widget_destroy(chooser);
204 * glk_fileref_create_by_name:
205 * @usage: Bitfield with one or more of the #fileusage_ constants.
207 * @rock: The new fileref's rock value.
209 * This creates a reference to a file with a specific name. The file will be
210 * in the same directory as your program, and visible to the player.
212 * Returns: A new fileref, or #NULL if the fileref creation failed.
215 glk_fileref_create_by_name(glui32 usage, char *name, glui32 rock)
217 g_return_val_if_fail(name != NULL && strlen(name) > 0, NULL);
219 /* Find out what encoding filenames are in */
220 const gchar **charsets; /* Do not free */
221 g_get_filename_charsets(&charsets);
223 /* Convert name to that encoding */
224 GError *error = NULL;
225 gchar *osname = g_convert(name, -1, charsets[0], "ISO-8859-1", NULL, NULL,
229 g_warning("Error during latin1->filename conversion: %s", error->message);
233 /* Do any string-munging here to remove illegal characters from filename.
234 On ext3, the only illegal characters are '/' and '\0'. TODO: Should this
235 function be allowed to reference files in other directories, or should we
238 frefid_t f = fileref_new(osname, rock, usage, filemode_ReadWrite);
244 * glk_fileref_create_from_fileref:
245 * @usage: Bitfield with one or more of the #fileusage_ constants.
246 * @fref: Fileref to copy.
247 * @rock: The new fileref's rock value.
249 * This copies an existing file reference @fref, but changes the usage. (The
250 * original @fref is not modified.)
252 * If you write to a file in text mode and then read from it in binary mode,
253 * the results are platform-dependent.
255 * Returns: A new fileref, or #NULL if the fileref creation failed.
258 glk_fileref_create_from_fileref(glui32 usage, frefid_t fref, glui32 rock)
260 return fileref_new(fref->filename, rock, usage, fref->orig_filemode);
264 * glk_fileref_destroy:
265 * @fref: Fileref to destroy.
267 * Destroys a fileref which you have created. This does not affect the disk
270 * It is legal to destroy a fileref after opening a file with it (while the
271 * file is still open.) The fileref is only used for the opening operation,
272 * not for accessing the file stream.
275 glk_fileref_destroy(frefid_t fref)
277 glk_data->fileref_list = g_list_delete_link(glk_data->fileref_list, fref->fileref_list);
279 g_free(fref->filename);
284 * glk_fileref_delete_file:
285 * @fref: A refrence to the file to delete.
287 * Deletes the file referred to by @fref. Does not destroy @fref itself.
290 glk_fileref_delete_file(frefid_t fref)
292 if( glk_fileref_does_file_exist(fref) )
293 if(g_unlink(fref->filename) == -1)
294 g_warning("Error deleting file %s", fref->filename);
298 * glk_fileref_does_file_exist:
299 * @fref: A fileref to check.
301 * Checks whether the file referred to by @fref exists.
303 * Returns: #TRUE (1) if @fref refers to an existing file, #FALSE (0) if not.
306 glk_fileref_does_file_exist(frefid_t fref)
308 if( g_file_test(fref->filename, G_FILE_TEST_EXISTS) )