Got Gtk-Doc working. Now all the fancy /** comments before the functions
[rodin/chimara.git] / src / stream.c
1 #include "stream.h"
2 #include "fileref.h"
3 #include <glib.h>
4 #include <glib/gstdio.h>
5
6 #include "chimara-glk-private.h"
7 extern ChimaraGlkPrivate *glk_data;
8
9 /* Internal function: create a window stream to go with window. */
10 strid_t
11 window_stream_new(winid_t window)
12 {
13         /* Create stream and connect it to window */
14         strid_t str = g_new0(struct glk_stream_struct, 1);
15         str->file_mode = filemode_Write;
16         str->type = STREAM_TYPE_WINDOW;
17         str->window = window;
18         
19         /* Add it to the global stream list */
20         glk_data->stream_list = g_list_prepend(glk_data->stream_list, str);
21         str->stream_list = glk_data->stream_list;
22
23         return str;
24 }
25
26 /**
27  * glk_stream_iterate:
28  * @str: A stream, or %NULL.
29  * @rockptr: Return location for the next window's rock, or %NULL.
30  *
31  * Iterates through all the existing streams. See <link
32  * linkend="chimara-Iterating-Through-Opaque-Objects">Iterating Through Opaque
33  * Objects</link>.
34  *
35  * Returns: the next stream, or %NULL if there are no more.
36  */
37 strid_t
38 glk_stream_iterate(strid_t str, glui32 *rockptr)
39 {
40         GList *retnode;
41         
42         if(str == NULL)
43                 retnode = glk_data->stream_list;
44         else
45                 retnode = str->stream_list->next;
46         strid_t retval = retnode? (strid_t)retnode->data : NULL;
47                 
48         /* Store the stream's rock in rockptr */
49         if(retval && rockptr)
50                 *rockptr = glk_stream_get_rock(retval);
51                 
52         return retval;
53 }
54
55 /**
56  * glk_stream_get_rock:
57  * @str: A stream.
58  * 
59  * Retrieves the stream @str's rock value. See <link 
60  * linkend="chimara-Rocks">Rocks</link>.
61  *
62  * Returns: A rock value.
63  */
64 glui32
65 glk_stream_get_rock(strid_t str)
66 {
67         g_return_val_if_fail(str != NULL, 0);
68         return str->rock;
69 }
70
71 /**
72  * glk_stream_set_current:
73  * @str: An output stream, or %NULL.
74  *
75  * Sets the current stream to @str, which must be an output stream. You may set
76  * the current stream to %NULL, which means the current stream is not set to
77  * anything. 
78  */
79 void
80 glk_stream_set_current(strid_t str)
81 {
82         if(str != NULL && str->file_mode == filemode_Read)
83         {
84                 g_warning("%s: Cannot set current stream to non output stream", __func__);
85                 return;
86         }
87
88         glk_data->current_stream = str;
89 }
90
91 /**
92  * glk_stream_get_current:
93  * 
94  * Returns the current stream, or %NULL if there is none.
95  *
96  * Returns: A stream, or %NULL.
97  */
98 strid_t
99 glk_stream_get_current()
100 {
101         return glk_data->current_stream;
102 }
103
104 /**
105  * glk_put_char:
106  * @ch: A character in Latin-1 encoding.
107  *
108  * Prints one character to the current stream. As with all basic functions, the
109  * character is assumed to be in the Latin-1 character encoding. See <link
110  * linkend="chimara-Character-Encoding">Character Encoding</link>.
111  */
112 void
113 glk_put_char(unsigned char ch)
114 {
115         g_return_if_fail(glk_data->current_stream != NULL);
116         glk_put_char_stream(glk_data->current_stream, ch);
117 }
118
119 /**
120  * glk_put_char_uni:
121  * @ch: A Unicode code point.
122  *
123  * Prints one character to the current stream. The character is assumed to be a
124  * Unicode code point. See <link linkend="chimara-Character-Encoding">Character
125  * Encoding</link>.
126  */
127 void
128 glk_put_char_uni(glui32 ch)
129 {
130         g_return_if_fail(glk_data->current_stream != NULL);
131         glk_put_char_stream_uni(glk_data->current_stream, ch);
132 }
133
134 /**
135  * glk_put_string:
136  * @s: A null-terminated string in Latin-1 encoding.
137  *
138  * Prints a null-terminated string to the current stream. It is exactly
139  * equivalent to
140  * <informalexample><programlisting>
141  * for (ptr = @s; *ptr; ptr++)
142  *      #glk_put_char(*ptr);
143  * </programlisting></informalexample>
144  * However, it may be more efficient.
145  */
146 void
147 glk_put_string(char *s)
148 {
149         g_return_if_fail(glk_data->current_stream != NULL);
150         glk_put_string_stream(glk_data->current_stream, s);
151 }
152
153 /**
154  * glk_put_string_uni:
155  * @s: A zero-terminated string of Unicode code points.
156  * 
157  * Prints a string of Unicode characters to the current stream. It is equivalent
158  * to a series of glk_put_char_uni() calls. A string ends on a #glui32 whose
159  * value is 0.
160  */
161 void
162 glk_put_string_uni(glui32 *s)
163 {
164         g_return_if_fail(glk_data->current_stream != NULL);
165         glk_put_string_stream_uni(glk_data->current_stream, s);
166 }
167
168 /**
169  * glk_put_buffer:
170  * @buf: An array of characters in Latin-1 encoding.
171  * @len: Length of @buf.
172  *
173  * Prints a block of characters to the current stream. It is exactly equivalent
174  * to:
175  * <informalexample><programlisting>
176  * for (i = 0; i < @len; i++)
177  *      #glk_put_char(@buf[i]);
178  * </programlisting></informalexample>
179  * However, it may be more efficient.
180  */
181 void
182 glk_put_buffer(char *buf, glui32 len)
183 {
184         g_return_if_fail(glk_data->current_stream != NULL);
185         glk_put_buffer_stream(glk_data->current_stream, buf, len);
186 }
187
188 /**
189  * glk_put_buffer_uni:
190  * @buf: An array of Unicode code points.
191  * @len: Length of @buf.
192  *
193  * Prints a block of Unicode characters to the current stream. It is equivalent
194  * to a series of glk_put_char_uni() calls.
195  */
196 void
197 glk_put_buffer_uni(glui32 *buf, glui32 len)
198 {
199         g_return_if_fail(glk_data->current_stream != NULL);
200         glk_put_buffer_stream_uni(glk_data->current_stream, buf, len);
201 }
202
203 /**
204  * glk_stream_open_memory:
205  * @buf: An allocated buffer, or %NULL.
206  * @buflen: Length of @buf.
207  * @fmode: Mode in which the buffer will be opened. Must be one of 
208  * #filemode_Read, #filemode_Write, or #filemode_ReadWrite.
209  * @rock: The new stream's rock value.
210  *
211  * Opens a stream which reads from or writes to a space in memory. @buf points
212  * to the buffer where output will be read from or written to. @buflen is the
213  * length of the buffer.
214  *
215  * Unicode values (characters greater than 255) cannot be written to the buffer.
216  * If you try, they will be stored as 0x3F (<code>"?"</code>) characters.
217  *
218  * Returns: the new stream, or %NULL on error.
219  */
220 strid_t
221 glk_stream_open_memory(char *buf, glui32 buflen, glui32 fmode, glui32 rock)
222 {
223         g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
224         
225         strid_t str = g_new0(struct glk_stream_struct, 1);
226         str->rock = rock;
227         str->file_mode = fmode;
228         str->type = STREAM_TYPE_MEMORY;
229         str->buffer = buf;
230         str->mark = 0;
231         str->buflen = buflen;
232         str->unicode = FALSE;
233
234         /* Add it to the global stream list */
235         glk_data->stream_list = g_list_prepend(glk_data->stream_list, str);
236         str->stream_list = glk_data->stream_list;
237
238         return str;
239 }
240
241 /**
242  * glk_stream_open_memory_uni:
243  * @buf: An allocated buffer, or %NULL.
244  * @buflen: Length of @buf.
245  * @fmode: Mode in which the buffer will be opened. Must be one of 
246  * #filemode_Read, #filemode_Write, or #filemode_ReadWrite.
247  * @rock: The new stream's rock value.
248  *
249  * Works just like glk_stream_open_memory(), except that the buffer is an array
250  * of 32-bit words, instead of bytes. This allows you to write and read any
251  * Unicode character. The @buflen is the number of words, not the number of
252  * bytes.
253  * 
254  * Returns: the new stream, or %NULL on error.
255  */
256 strid_t
257 glk_stream_open_memory_uni(glui32 *buf, glui32 buflen, glui32 fmode, glui32 rock)
258 {
259         g_return_val_if_fail(fmode != filemode_WriteAppend, NULL);
260         
261         strid_t str = g_new0(struct glk_stream_struct, 1);
262         str->rock = rock;
263         str->file_mode = fmode;
264         str->type = STREAM_TYPE_MEMORY;
265         str->ubuffer = buf;
266         str->mark = 0;
267         str->buflen = buflen;
268         str->unicode = TRUE;
269
270         /* Add it to the global stream list */
271         glk_data->stream_list = g_list_prepend(glk_data->stream_list, str);
272         str->stream_list = glk_data->stream_list;
273
274         return str;
275 }
276
277 /* Internal function: create a stream using the given parameters. */
278 static strid_t
279 file_stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
280 {
281         g_return_val_if_fail(fileref != NULL, NULL);
282         
283         gchar *modestr;
284         /* Binary mode is 0x000, text mode 0x100 */
285         gboolean binary = !(fileref->usage & fileusage_TextMode);
286         switch(fmode) 
287         {
288                 case filemode_Read:
289                         if(!g_file_test(fileref->filename, G_FILE_TEST_EXISTS)) {
290                                 g_warning("glk_stream_open_file: Tried to open a file in read "
291                                                   "mode that didn't exist!");
292                                 return NULL;
293                         }
294                         modestr = g_strdup(binary? "rb" : "r");
295                         break;
296                 case filemode_Write:
297                         modestr = g_strdup(binary? "wb" : "w");
298                         break;
299                 case filemode_WriteAppend:
300                         modestr = g_strdup(binary? "ab" : "a");
301                         break;
302                 case filemode_ReadWrite:
303                         if( g_file_test(fileref->filename, G_FILE_TEST_EXISTS) ) {
304                                 modestr = g_strdup(binary? "r+b" : "r+");
305                         } else {
306                                 modestr = g_strdup(binary? "w+b" : "w+");
307                         }
308                         break;
309                 default:
310                         g_warning("glk_stream_open_file: Invalid file mode");
311                         return NULL;
312         }
313         
314         FILE *fp = g_fopen(fileref->filename, modestr);
315         g_free(modestr);
316         if(fp == NULL) {
317                 g_warning("glk_stream_open_file: Error opening file");
318                 return NULL;
319         }
320         
321         /* If they opened a file in write mode but didn't specifically get
322         permission to do so, complain if the file already exists */
323         if(fileref->orig_filemode == filemode_Read && fmode != filemode_Read) {
324                 gdk_threads_enter();
325
326                 GtkWidget *dialog = gtk_message_dialog_new(NULL, 0,
327                         GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
328                         "File %s already exists. Overwrite?", fileref->filename);
329                 gint response = gtk_dialog_run(GTK_DIALOG(dialog));
330                 gtk_widget_destroy(dialog);
331
332                 gdk_threads_leave();
333
334                 if(response != GTK_RESPONSE_YES) {
335                         fclose(fp);
336                         return NULL;
337                 }
338         }
339         
340         strid_t str = g_new0(struct glk_stream_struct, 1);
341         str->rock = rock;
342         str->file_mode = fmode;
343         str->type = STREAM_TYPE_FILE;
344         str->file_pointer = fp;
345         str->binary = binary;
346         str->unicode = unicode;
347         str->filename = g_filename_to_utf8(fileref->filename, -1, NULL, NULL, NULL);
348         if(str->filename == NULL)
349                 str->filename = g_strdup("Unknown file name"); /* fail silently */
350         /* Add it to the global stream list */
351         glk_data->stream_list = g_list_prepend(glk_data->stream_list, str);
352         str->stream_list = glk_data->stream_list;
353
354         return str;
355 }
356
357 /**
358  * glk_stream_open_file:
359  * @fileref: Indicates the file which will be opened.
360  * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
361  * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
362  * @rock: The new stream's rock value.
363  *
364  * Opens a stream which reads to or writes from a disk file. If @fmode is
365  * #filemode_Read, the file must already exist; for the other modes, an empty
366  * file is created if none exists. If @fmode is #filemode_Write, and the file
367  * already exists, it is truncated down to zero length (an empty file). If
368  * @fmode is #filemode_WriteAppend, the file mark is set to the end of the 
369  * file.
370  *
371  * When writing in binary mode, Unicode values (characters greater than 255)
372  * cannot be written to the file. If you try, they will be stored as 0x3F
373  * (<code>"?"</code>) characters. In text mode, Unicode values may be stored
374  * exactly, approximated, or abbreviated, depending on what the platform's text
375  * files support.
376  *
377  * Returns: A new stream, or %NULL if the file operation failed.
378  */
379 strid_t
380 glk_stream_open_file(frefid_t fileref, glui32 fmode, glui32 rock)
381 {
382         return file_stream_new(fileref, fmode, rock, FALSE);
383 }
384
385 /**
386  * glk_stream_open_file_uni:
387  * @fileref: Indicates the file which will be opened.
388  * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
389  * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
390  * @rock: The new stream's rock value.
391  *
392  * This works just like glk_stream_open_file(), except that in binary mode,
393  * characters are written and read as four-byte (big-endian) values. This
394  * allows you to write any Unicode character.
395  *
396  * In text mode, the file is written and read in a platform-dependent way, which
397  * may or may not handle all Unicode characters. A text-mode file created with
398  * glk_stream_open_file_uni() may have the same format as a text-mode file
399  * created with glk_stream_open_file(); or it may use a more Unicode-friendly
400  * format.
401  *
402  * Returns: A new stream, or %NULL if the file operation failed.
403  */
404 strid_t
405 glk_stream_open_file_uni(frefid_t fileref, glui32 fmode, glui32 rock)
406 {
407         return file_stream_new(fileref, fmode, rock, TRUE);
408 }
409
410 /**
411  * glk_stream_close:
412  * @str: Stream to close.
413  * @result: Pointer to a #stream_result_t, or %NULL.
414  *
415  * Closes the stream @str. The @result argument points to a structure which is
416  * filled in with the final character counts of the stream. If you do not care
417  * about these, you may pass %NULL as the @result argument.
418  *
419  * If @str is the current output stream, the current output stream is set to
420  * %NULL.
421  *
422  * You cannot close window streams; use glk_window_close() instead. See <link
423  * linkend="chimara-Window-Opening-Closing-and-Constraints">Window Opening,
424  * Closing, and Constraints</link>.
425  */
426 void 
427 glk_stream_close(strid_t str, stream_result_t *result)
428 {
429         g_return_if_fail(str != NULL);
430         
431         /* Free resources associated with one specific type of stream */
432         switch(str->type)
433         {
434                 case STREAM_TYPE_WINDOW:
435                         g_warning("%s: Attempted to close a window stream. Use glk_window_"
436                                 "close() instead.", __func__);
437                         return;
438                         
439                 case STREAM_TYPE_MEMORY:
440                         /* Do nothing */
441                         break;
442                         
443                 case STREAM_TYPE_FILE:
444                         if(fclose(str->file_pointer) != 0)
445                                 g_warning("%s: Failed to close file '%s'.", __func__, 
446                                         str->filename);
447                         g_free(str->filename);
448                         break;
449                 default:
450                         g_warning("%s: Closing this type of stream not supported.", 
451                                 __func__);
452                         return;
453         }
454
455         stream_close_common(str, result);
456 }
457
458 /* Internal function: Stuff to do upon closing any type of stream. */
459 void
460 stream_close_common(strid_t str, stream_result_t *result)
461 {
462         /* Remove the stream from the global stream list */
463         glk_data->stream_list = g_list_delete_link(glk_data->stream_list, str->stream_list);
464         
465         /* If it was the current output stream, set that to NULL */
466         if(glk_data->current_stream == str)
467                 glk_data->current_stream = NULL;
468                 
469         /* If it was one or more windows' echo streams, set those to NULL */
470         winid_t win;
471         for(win = glk_window_iterate(NULL, NULL); win; 
472                 win = glk_window_iterate(win, NULL))
473                 if(win->echo_stream == str)
474                         win->echo_stream = NULL;
475                         
476         /* Return the character counts */
477         if(result) 
478         {
479                 result->readcount = str->read_count;
480                 result->writecount = str->write_count;
481         }
482         g_free(str);
483 }