Commentaar toegevoegd aan code en tevens Gtk-Doc comments voor alle
[rodin/chimara.git] / src / stream.c
1 #include "stream.h"
2
3 /* Global current stream */
4 static strid_t current_stream = NULL;
5 /* List of streams currently in existence */
6 static GList *stream_list = NULL;
7
8 /* Internal function: create a window stream to go with window. */
9 strid_t
10 window_stream_new(winid_t window)
11 {
12         /* Create stream and connect it to window */
13         strid_t s = g_new0(struct glk_stream_struct, 1);
14         s->file_mode = filemode_Write;
15         s->stream_type = STREAM_TYPE_WINDOW;
16         s->window = window;
17         /* Add it to the global stream list */
18         stream_list = g_list_prepend(stream_list, s);
19         s->stream_list = stream_list;
20
21         return s;
22 }
23
24 /**
25  * glk_stream_set_current:
26  * @str: An output stream, or NULL.
27  *
28  * Sets the current stream to @str, or to nothing if @str is #NULL.
29  */
30 void
31 glk_stream_set_current(strid_t str)
32 {
33         if(str != NULL && str->file_mode != filemode_Write)
34         {
35                 g_warning("glk_stream_set_current: "
36                         "Cannot set current stream to non output stream");
37                 return;
38         }
39
40         current_stream = str;
41 }
42
43 /**
44  * glk_put_string:
45  * @s: A null-terminated string in Latin-1 encoding.
46  *
47  * Prints @s to the current stream.
48  */
49 void
50 glk_put_string(char *s)
51 {
52         GError *error = NULL;
53         gchar *utf8;
54
55         switch(current_stream->stream_type)
56         {
57                 case STREAM_TYPE_WINDOW:
58                         utf8 = g_convert(s, -1, "UTF-8", "ISO-8859-1", NULL, NULL, &error);
59
60                         if(utf8 == NULL)
61                         {
62                                 g_warning("glk_put_string: "
63                                         "Error during latin1->utf8 conversion: %s", 
64                                         error->message);
65                                 g_error_free(error);
66                                 return;
67                         }
68
69                         GtkTextBuffer *buffer = gtk_text_view_get_buffer( 
70                                 GTK_TEXT_VIEW(current_stream->window->widget) );
71
72                         GtkTextIter iter;
73                         gtk_text_buffer_get_end_iter(buffer, &iter);
74
75                         gtk_text_buffer_insert(buffer, &iter, utf8, -1);
76
77                         g_free(utf8);
78                         break;
79                 default:
80                         g_warning("glk_put_string: "
81                                 "Writing to this kind of stream unsupported."); 
82         }
83 }