Files openen. Niet getest, omdat het schrijven naar files nog niet
[rodin/chimara.git] / src / stream.c
1 #include "stream.h"
2 #include "fileref.h"
3 #include <string.h>
4 #include <stdio.h>
5 #include <glib/gstdio.h>
6
7 /* Global current stream */
8 static strid_t current_stream = NULL;
9 /* List of streams currently in existence */
10 static GList *stream_list = NULL;
11
12 /* Internal function: create a window stream to go with window. */
13 strid_t
14 window_stream_new(winid_t window)
15 {
16         /* Create stream and connect it to window */
17         strid_t s = g_new0(struct glk_stream_struct, 1);
18         s->file_mode = filemode_Write;
19         s->stream_type = STREAM_TYPE_WINDOW;
20         s->window = window;
21         /* Add it to the global stream list */
22         stream_list = g_list_prepend(stream_list, s);
23         s->stream_list = stream_list;
24
25         return s;
26 }
27
28 /**
29  * glk_stream_iterate:
30  * @str: A stream, or #NULL.
31  * @rockptr: Return location for the next window's rock, or #NULL.
32  *
33  * Iterates over the list of streams; if @str is #NULL, it returns the first
34  * stream, otherwise the next stream after @str. If there are no more, it
35  * returns #NULL. The stream's rock is stored in @rockptr. If you don't want
36  * the rocks to be returned, you may set @rockptr to #NULL.
37  *
38  * The order in which streams are returned is arbitrary. The order may change
39  * every time you create or destroy a stream, invalidating the iteration.
40  *
41  * Returns: the next stream, or #NULL if there are no more.
42  */
43 strid_t
44 glk_stream_iterate(strid_t str, glui32 *rockptr)
45 {
46         GList *retnode;
47         
48         if(str == NULL)
49                 retnode = stream_list;
50         else
51                 retnode = str->stream_list->next;
52         strid_t retval = retnode? (strid_t)retnode->data : NULL;
53                 
54         /* Store the stream's rock in rockptr */
55         if(retval && rockptr)
56                 *rockptr = glk_stream_get_rock(retval);
57                 
58         return retval;
59 }
60
61 /**
62  * glk_stream_get_rock:
63  * @str: A stream.
64  * 
65  * Returns the stream @str's rock value.
66  *
67  * Returns: A rock value.
68  */
69 glui32
70 glk_stream_get_rock(strid_t str)
71 {
72         g_return_val_if_fail(str != NULL, 0);
73         return str->rock;
74 }
75
76 /**
77  * glk_stream_set_current:
78  * @str: An output stream, or NULL.
79  *
80  * Sets the current stream to @str, or to nothing if @str is #NULL.
81  */
82 void
83 glk_stream_set_current(strid_t str)
84 {
85         if(str != NULL && str->file_mode != filemode_Write)
86         {
87                 g_warning("glk_stream_set_current: "
88                         "Cannot set current stream to non output stream");
89                 return;
90         }
91
92         current_stream = str;
93 }
94
95 /**
96  * glk_stream_get_current:
97  * 
98  * Returns the current stream, or #NULL if there is none.
99  *
100  * Returns: A stream.
101  */
102 strid_t
103 glk_stream_get_current()
104 {
105         return current_stream;
106 }
107
108 /**
109  * glk_put_char:
110  * @ch: A character in Latin-1 encoding.
111  *
112  * Prints one character @ch to the current stream.
113  */
114 void
115 glk_put_char(unsigned char ch)
116 {
117         /* Illegal to print to the current stream if it is NULL */
118         g_return_if_fail(current_stream != NULL);
119         glk_put_char_stream(current_stream, ch);
120 }
121
122 /**
123  * glk_put_string:
124  * @s: A null-terminated string in Latin-1 encoding.
125  *
126  * Prints @s to the current stream.
127  */
128 void
129 glk_put_string(char *s)
130 {
131         /* Illegal to print to the current stream if it is NULL */
132         g_return_if_fail(current_stream != NULL);
133         glk_put_string_stream(current_stream, s);
134 }
135
136 /**
137  * glk_put_buffer:
138  * @buf: An array of characters in Latin-1 encoding.
139  * @len: Length of @buf.
140  *
141  * Prints @buf to the current stream.
142  */
143 void
144 glk_put_buffer(char *buf, glui32 len)
145 {
146         /* Illegal to print to the current stream if it is NULL */
147         g_return_if_fail(current_stream != NULL);
148         glk_put_buffer_stream(current_stream, buf, len);
149 }
150
151 /**
152  * glk_put_char_stream:
153  * @str: An output stream.
154  * @ch: A character in Latin-1 encoding.
155  *
156  * Prints one character @ch to the stream @str. It is illegal for @str to be
157  * #NULL, or an input-only stream.
158  */
159 void
160 glk_put_char_stream(strid_t str, unsigned char ch)
161 {
162         g_return_if_fail(str != NULL);
163         g_return_if_fail(str->file_mode != filemode_Read);
164         
165         /* Convert ch to a null-terminated string, call glk_put_string_stream() */
166         gchar *s = g_strndup((gchar *)&ch, 1);
167         glk_put_string_stream(str, s);
168         g_free(s);
169 }
170
171 /* Internal function: change illegal (control) characters in a string to a
172 placeholder character. Must free returned string afterwards. */
173 static gchar *
174 remove_latin1_control_characters(gchar *s)
175 {
176         gchar *retval = g_strdup(s);
177         unsigned char *ptr;
178         for(ptr = (unsigned char *)retval; *ptr != '\0'; ptr++)
179                 if( (*ptr < 32 && *ptr != 10) || (*ptr >= 127 && *ptr <= 159) )
180                         *ptr = '?';
181                         /* Our placeholder character is '?'; other options are possible,
182                         like printing "0x7F" or something */
183         return retval;
184 }
185
186 /* Internal function: convert a Latin-1 string to a UTF-8 string, replacing
187 Latin-1 control characters by a placeholder first. The UTF-8 string must be
188 freed afterwards. Returns NULL on error. */
189 static gchar *
190 convert_latin1_to_utf8(gchar *s)
191 {
192         GError *error = NULL;
193         gchar *utf8;
194         gchar *canonical = remove_latin1_control_characters(s);
195         utf8 = g_convert(canonical, -1, "UTF-8", "ISO-8859-1", NULL, NULL, &error);
196         g_free(canonical);
197         
198         if(utf8 == NULL)
199         {
200                 error_dialog(NULL, error, "Error during latin1->utf8 conversion: ");
201                 return NULL;
202         }
203         
204         return utf8;
205 }
206
207 /* Internal function: write a UTF-8 string to a window's text buffer. */
208 static void
209 write_utf8_to_window(winid_t win, gchar *s)
210 {
211         GtkTextBuffer *buffer = 
212                 gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
213
214         GtkTextIter iter;
215         gtk_text_buffer_get_end_iter(buffer, &iter);
216         gtk_text_buffer_insert(buffer, &iter, s, -1);
217 }
218
219 /**
220  * glk_put_string_stream:
221  * @str: An output stream.
222  * @s: A null-terminated string in Latin-1 encoding.
223  *
224  * Prints @s to the stream @str. It is illegal for @str to be #NULL, or an
225  * input-only stream.
226  */
227 void
228 glk_put_string_stream(strid_t str, char *s)
229 {
230         g_return_if_fail(str != NULL);
231         g_return_if_fail(str->file_mode != filemode_Read);
232
233         switch(str->stream_type)
234         {
235                 case STREAM_TYPE_WINDOW:
236                         /* Each window type has a different way of printing to it */
237                         switch(str->window->window_type)
238                         {
239                                 /* Printing to a these windows' streams does nothing */
240                                 case wintype_Blank:
241                                 case wintype_Pair:
242                                 case wintype_Graphics:
243                                         current_stream->write_count += strlen(s);
244                                         break;
245                                 /* Text buffer window */        
246                                 case wintype_TextBuffer:
247                                 {
248                                         gchar *utf8 = convert_latin1_to_utf8(s);
249                                         write_utf8_to_window(str->window, utf8);
250                                         g_free(utf8);
251                                 }       
252                                         str->write_count += strlen(s);
253                                         break;
254                                 default:
255                                         g_warning("glk_put_string: "
256                                                 "Writing to this kind of window unsupported.");
257                         }
258                         
259                         /* Now write the same buffer to the window's echo stream */
260                         if(str->window->echo_stream != NULL)
261                                 glk_put_string_stream(str->window->echo_stream, s);
262                         
263                         break;
264                 default:
265                         g_warning("glk_put_string: "
266                                 "Writing to this kind of stream unsupported."); 
267         }
268 }
269
270 /**
271  * glk_put_buffer_stream:
272  * @str: An output stream.
273  * @buf: An array of characters in Latin-1 encoding.
274  * @len: Length of @buf.
275  *
276  * Prints @buf to the stream @str. It is illegal for @str to be #NULL, or an
277  * input-only stream.
278  */
279 void
280 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
281 {
282         g_return_if_fail(str != NULL);
283         g_return_if_fail(str->file_mode != filemode_Read);
284         
285         /* Convert buf to a null-terminated string, call glk_put_string_stream() */
286         gchar *s = g_strndup(buf, len);
287         glk_put_string_stream(str, s);
288         g_free(s);
289 }
290
291 /* Internal function: create a stream using the given parameters. */
292 static strid_t
293 stream_new(frefid_t fileref, glui32 fmode, glui32 rock, gboolean unicode)
294 {
295         g_return_val_if_fail(fileref != NULL, NULL);
296         
297         gchar *modestr;
298         gboolean binary = fileref->usage & fileusage_BinaryMode;
299         switch(fmode) 
300         {
301                 case filemode_Read:
302                         if(!g_file_test(fileref->filename, G_FILE_TEST_EXISTS)) {
303                                 g_warning("glk_stream_open_file: Tried to open a file in read "
304                                                   "mode that didn't exist!");
305                                 return NULL;
306                         }
307                         modestr = g_strdup(binary? "rb" : "r");
308                         break;
309                 case filemode_Write:
310                         modestr = g_strdup(binary? "wb" : "w");
311                         break;
312                 case filemode_WriteAppend:
313                         modestr = g_strdup(binary? "ab" : "a");
314                         break;
315                 case filemode_ReadWrite:
316                         modestr = g_strdup(binary? "r+b" : "r+");
317                         break;
318                 default:
319                         g_warning("glk_stream_open_file: Invalid file mode");
320                         return NULL;
321         }
322         
323         FILE *fp = g_fopen(fileref->filename, modestr);
324         g_free(modestr);
325         if(fp == NULL) {
326                 g_warning("glk_stream_open_file: Error opening file");
327                 return NULL;
328         }
329         
330         /* If they opened a file in write mode but didn't specifically get
331         permission to do so, complain if the file already exists */
332         if(fileref->orig_filemode == filemode_Read && fmode != filemode_Read) {
333                 GtkWidget *dialog = gtk_message_dialog_new(NULL, 0,
334                         GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
335                         "File %s already exists. Overwrite?", fileref->filename);
336                 gint response = gtk_dialog_run(GTK_DIALOG(dialog));
337                 gtk_widget_destroy(dialog);
338                 if(response != GTK_RESPONSE_YES) {
339                         fclose(fp);
340                         return NULL;
341                 }
342         }
343         
344         strid_t s = g_new0(struct glk_stream_struct, 1);
345         s->file_mode = fmode;
346         s->stream_type = unicode? STREAM_TYPE_UNICODE_FILE : STREAM_TYPE_FILE;
347         s->file_pointer = fp;
348         s->binary = binary;
349         /* Add it to the global stream list */
350         stream_list = g_list_prepend(stream_list, s);
351         s->stream_list = stream_list;
352
353         return s;
354 }
355
356 /**
357  * glk_stream_open_file:
358  * @fileref: Indicates the file which will be opened.
359  * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
360  * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
361  * @rock: The new stream's rock value.
362  *
363  * Opens a stream which reads to or writes from a disk file. If @fmode is
364  * #filemode_Read, the file must already exist; for the other modes, an empty
365  * file is created if none exists. If @fmode is #filemode_Write, and the file
366  * already exists, it is truncated down to zero length (an empty file). If
367  * @fmode is #filemode_WriteAppend, the file mark is set to the end of the 
368  * file.
369  * 
370  * The file may be written in text or binary mode; this is determined by the
371  * @fileref argument. Similarly, platform-dependent attributes such as file 
372  * type are determined by @fileref.
373  *
374  * When writing in binary mode, Unicode values (characters greater than 255)
375  * cannot be written to the file. If you try, they will be stored as 0x3F ("?")
376  * characters. In text mode, Unicode values are stored in UTF-8.
377  *
378  * Returns: A new stream, or %NULL if the file operation failed.
379  */
380 strid_t
381 glk_stream_open_file(frefid_t fileref, glui32 fmode, glui32 rock)
382 {
383         return stream_new(fileref, fmode, rock, FALSE);
384 }
385
386 /**
387  * glk_stream_open_file_uni:
388  * @fileref: Indicates the file which will be opened.
389  * @fmode: Mode in which the file will be opened. Can be any of #filemode_Read,
390  * #filemode_Write, #filemode_WriteAppend, or #filemode_ReadWrite.
391  * @rock: The new stream's rock value.
392  *
393  * This works just like glk_stream_open_file(), except that in binary mode,
394  * characters are written and read as four-byte (big-endian) values. This
395  * allows you to write any Unicode character.
396  *
397  * In text mode, the file is written and read in UTF-8.
398  *
399  * Returns: A new stream, or %NULL if the file operation failed.
400  */
401 strid_t
402 glk_stream_open_file_uni(frefid_t fileref, glui32 fmode, glui32 rock)
403 {
404         return stream_new(fileref, fmode, rock, TRUE);
405 }
406