fixed glk_request_mouse_event to only generate one mouse event. When
[projects/chimara/chimara.git] / libchimara / strio.c
1 #include "charset.h"
2 #include "magic.h"
3 #include "stream.h"
4 #include <errno.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <glib.h>
8 #include <glib/gstdio.h>
9
10 /*
11  *
12  **************** WRITING FUNCTIONS ********************************************
13  *
14  */
15
16 /* Internal function: write a UTF-8 string to a text buffer window's text buffer. */
17 static void
18 write_utf8_to_window_buffer(winid_t win, gchar *s)
19 {
20         if(win->input_request_type == INPUT_REQUEST_LINE || win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
21         {
22                 ILLEGAL("Tried to print to a text buffer window with line input pending.");
23                 return;
24         }
25
26         // Write to the buffer  
27         g_string_append(win->buffer, s);
28 }
29         
30 /* Internal function: flush a window's text buffer to the screen. */
31 void
32 flush_window_buffer(winid_t win)
33 {
34 #ifdef DEBUG_STYLES
35         g_printf("%s\n", win->buffer->str);
36 #endif
37         if(win->type != wintype_TextBuffer && win->type != wintype_TextGrid)
38                 return;
39
40         if(win->buffer->len == 0)
41                 return;
42
43         gdk_threads_enter();
44
45         GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
46
47         switch(win->type) {
48         case wintype_TextBuffer:
49         {
50                 GtkTextIter start, end;
51                 gtk_text_buffer_get_end_iter(buffer, &end);
52                 gint start_offset;
53
54                 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
55
56                 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
57                 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
58                 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
59
60                 start_offset = gtk_text_iter_get_offset(&end);
61                 gtk_text_buffer_insert(buffer, &end, win->buffer->str, -1);
62                 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
63
64                 // Default style
65                 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &end);
66
67                 // Player's style overrides
68                 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &end);
69
70                 // GLK Program's style overrides
71                 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &end);
72
73                 // Link style overrides
74                 if(win->window_stream->hyperlink_mode) {
75                         GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
76                         GtkTextTag *link_tag = win->current_hyperlink->tag;
77                         gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &end);
78                         gtk_text_buffer_apply_tag(buffer, link_tag, &start, &end);
79                 }
80
81                 // GLK Program's style overrides using garglk_set_zcolors()
82                 if(win->zcolor != NULL) {
83                         gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &end);
84                 }
85
86                 // GLK Program's style overrides using garglk_set_reversevideo()
87                 if(win->zcolor_reversed != NULL) {
88                         gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &end);
89                 }
90
91                 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
92                 g_assert(glk);
93                 g_signal_emit_by_name(glk, "text-buffer-output", win->rock, win->buffer->str);
94         }
95                 break;
96
97         case wintype_TextGrid:
98         {
99                 /* Number of characters to insert */
100                 glong length = win->buffer->len;
101                 glong chars_left = length;
102                 
103                 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
104                 
105                 /* Get cursor position */
106                 GtkTextIter start, insert;
107                 gint start_offset;
108
109                 gtk_text_buffer_get_iter_at_mark(buffer, &insert, cursor);
110                 /* Spaces available on this line */
111                 gint available_space = win->width - gtk_text_iter_get_line_offset(&insert);
112                 
113                 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
114                 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
115                 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
116                 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
117                 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
118
119                 while(chars_left > available_space && !gtk_text_iter_is_end(&insert))
120                 {
121                         GtkTextIter end = insert;
122                         gtk_text_iter_forward_to_line_end(&end);
123                         gtk_text_buffer_delete(buffer, &insert, &end);
124
125                         start_offset = gtk_text_iter_get_offset(&insert);
126                         gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), available_space);
127                         gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
128
129                         // Default style
130                         gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
131
132                         // Player's style overrides
133                         gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
134
135                         // GLK Program's style overrides
136                         gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
137
138                         // Link style overrides
139                         if(win->window_stream->hyperlink_mode) {
140                                 GtkTextTag *link_tag = win->current_hyperlink->tag;
141                                 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
142                                 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
143                         }
144
145                         // GLK Program's style overrides using garglk_set_zcolors()
146                         if(win->zcolor != NULL)
147                                 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
148
149                         // GLK Program's style overrides using garglk_set_reversevideo()
150                         if(win->zcolor_reversed != NULL) {
151                                 gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &insert);
152                         }
153
154                         chars_left -= available_space;
155                         gtk_text_iter_forward_line(&insert);
156                         available_space = win->width;
157                 }
158                 if(!gtk_text_iter_is_end(&insert))
159                 {
160                         GtkTextIter end = insert;
161                         gtk_text_iter_forward_chars(&end, chars_left);
162                         gtk_text_buffer_delete(buffer, &insert, &end);
163
164                         start_offset = gtk_text_iter_get_offset(&insert);
165                         gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), -1);
166                         gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
167
168                         // Default style
169                         gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
170
171                         // Player's style overrides
172                         gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
173
174                         // GLK Program's style overrides
175                         gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
176
177                         // Link style overrides
178                         if(win->window_stream->hyperlink_mode) {
179                                 GtkTextTag *link_tag = win->current_hyperlink->tag;
180                                 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
181                                 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
182                         }
183
184                         // GLK Program's style overrides using garglk_set_zcolors()
185                         if(win->zcolor != NULL)
186                                 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
187
188                         // GLK Program's style overrides using garglk_set_reversevideo()
189                         if(win->zcolor_reversed != NULL) {
190                                 gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &insert);
191                         }
192                 }
193                 
194                 gtk_text_buffer_move_mark(buffer, cursor, &start);
195         }
196                 break;
197         }
198
199         gdk_threads_leave();
200
201         g_string_truncate(win->buffer, 0);
202 }
203
204 /* Internal function: write a Latin-1 buffer with length to a stream. */
205 static void
206 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
207 {
208         switch(str->type)
209         {
210                 case STREAM_TYPE_WINDOW:
211                         /* Each window type has a different way of printing to it */
212                         switch(str->window->type)
213                         {
214                                 /* Printing to these windows' streams does nothing */
215                                 case wintype_Blank:
216                                 case wintype_Pair:
217                                 case wintype_Graphics:
218                                         str->write_count += len;
219                                         break;
220                                         
221                             /* Text grid/buffer windows */
222                             case wintype_TextGrid:
223                                 {
224                                 gchar *utf8 = convert_latin1_to_utf8(buf, len);
225                                 if(utf8 != NULL) {
226                                                 /* Deal with newlines */
227                                                 int i;
228                                                 gchar *line = utf8;
229                                                 for(i=0; i<len; i++) {
230                                                         if(utf8[i] == '\n') {
231                                                                 utf8[i] = '\0';
232                                                                 write_utf8_to_window_buffer(str->window, line);
233                                                                 flush_window_buffer(str->window);
234
235                                                                 /* Move cursor position forward to the next line */
236                                                                 gdk_threads_enter();
237                                                                 GtkTextIter cursor_pos;
238                                                                 GtkTextView *textview = GTK_TEXT_VIEW(str->window->widget);
239                                                                 GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
240                                                                 GtkTextMark *cursor_mark = gtk_text_buffer_get_mark(buffer, "cursor_position");
241
242                                                             gtk_text_buffer_get_iter_at_mark( buffer, &cursor_pos, cursor_mark);
243                                                                 gtk_text_view_forward_display_line(textview, &cursor_pos);
244                                                                 gtk_text_view_backward_display_line_start(textview, &cursor_pos);
245                                                                 gtk_text_buffer_move_mark(buffer, cursor_mark, &cursor_pos);
246                                                                 gdk_threads_leave();
247
248                                                                 line = utf8 + (i < len-1 ? (i+1):(len-1));
249                                                         }
250                                                 }
251                                                                 
252                                                 /* No more newlines left. */
253                                                 write_utf8_to_window_buffer(str->window, line);
254                                                 g_free(utf8);
255                                         }
256
257                                         str->write_count += len;
258                                 }
259                                         break;
260
261                                 case wintype_TextBuffer:
262                             {
263                                 gchar *utf8 = convert_latin1_to_utf8(buf, len);
264                                 if(utf8 != NULL) {
265                                                 write_utf8_to_window_buffer(str->window, utf8);
266                                                 g_free(utf8);
267                                         }
268                                 }       
269                                         str->write_count += len;
270                                         break;
271                                 default:
272                                         ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
273                         }
274                         
275                         /* Now write the same buffer to the window's echo stream */
276                         if(str->window->echo_stream != NULL)
277                                 write_buffer_to_stream(str->window->echo_stream, buf, len);
278                         
279                         break;
280                         
281                 case STREAM_TYPE_MEMORY:
282                         if(str->unicode && str->ubuffer)
283                         {
284                                 int foo = 0;
285                                 while(str->mark < str->buflen && foo < len)
286                                         str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
287                         }
288                         if(!str->unicode && str->buffer)
289                         {
290                                 int copycount = MIN(len, str->buflen - str->mark);
291                                 memmove(str->buffer + str->mark, buf, copycount);
292                                 str->mark += copycount;
293                         }
294
295                         str->write_count += len;
296                         break;
297                         
298                 case STREAM_TYPE_FILE:
299                         if(str->binary) 
300                         {
301                                 if(str->unicode) 
302                                 {
303                                         gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
304                                         fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
305                                         g_free(writebuffer);
306                                 } 
307                                 else /* Regular file */
308                                 {
309                                         fwrite(buf, sizeof(gchar), len, str->file_pointer);
310                                 }
311                         }
312                         else /* Text mode is the same for Unicode and regular files */
313                         {
314                                 gchar *utf8 = convert_latin1_to_utf8(buf, len);
315                                 if(utf8 != NULL)
316                                 {
317                                         g_fprintf(str->file_pointer, "%s", utf8);
318                                         g_free(utf8);
319                                 }
320                         }
321                         
322                         str->write_count += len;
323                         break;
324                 default:
325                         ILLEGAL_PARAM("Unknown stream type: %u", str->type);
326         }
327 }
328
329 /* Internal function: write a Unicode buffer with length to a stream. */
330 static void
331 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
332 {
333         switch(str->type)
334         {
335                 case STREAM_TYPE_WINDOW:
336                         /* Each window type has a different way of printing to it */
337                         switch(str->window->type)
338                         {
339                                 /* Printing to these windows' streams does nothing */
340                                 case wintype_Blank:
341                                 case wintype_Pair:
342                                 case wintype_Graphics:
343                                         str->write_count += len;
344                                         break;
345                                         
346                             /* Text grid/buffer windows */
347                             case wintype_TextGrid:
348                             case wintype_TextBuffer:
349                             {
350                                 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
351                                 if(utf8 != NULL) {
352                                                 write_utf8_to_window_buffer(str->window, utf8);
353                                                 g_free(utf8);
354                                         }
355                                 }       
356                                         str->write_count += len;
357                                         break;
358                                 default:
359                                         ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
360                         }
361                         
362                         /* Now write the same buffer to the window's echo stream */
363                         if(str->window->echo_stream != NULL)
364                                 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
365                         
366                         break;
367                         
368                 case STREAM_TYPE_MEMORY:
369                         if(str->unicode && str->ubuffer)
370                         {
371                                 int copycount = MIN(len, str->buflen - str->mark);
372                                 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
373                                 str->mark += copycount;
374                         }
375                         if(!str->unicode && str->buffer)
376                         {
377                                 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
378                                 int copycount = MIN(len, str->buflen - str->mark);
379                                 memmove(str->buffer + str->mark, latin1, copycount);
380                                 g_free(latin1);
381                                 str->mark += copycount;
382                         }
383
384                         str->write_count += len;
385                         break;
386                         
387                 case STREAM_TYPE_FILE:
388                         if(str->binary) 
389                         {
390                                 if(str->unicode) 
391                                 {
392                                         gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
393                                         fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
394                                         g_free(writebuffer);
395                                 } 
396                                 else /* Regular file */
397                                 {
398                                         gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
399                                         fwrite(latin1, sizeof(gchar), len, str->file_pointer);
400                                         g_free(latin1);
401                                 }
402                         }
403                         else /* Text mode is the same for Unicode and regular files */
404                         {
405                                 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
406                                 if(utf8 != NULL) 
407                                 {
408                                         g_fprintf(str->file_pointer, "%s", utf8);
409                                         g_free(utf8);
410                                 }
411                         }
412                         
413                         str->write_count += len;
414                         break;
415                 default:
416                         ILLEGAL_PARAM("Unknown stream type: %u", str->type);
417         }
418 }
419
420 /**
421  * glk_put_char_stream:
422  * @str: An output stream.
423  * @ch: A character in Latin-1 encoding.
424  *
425  * The same as glk_put_char(), except that you specify a stream @str to print 
426  * to, instead of using the current stream. It is illegal for @str to be %NULL,
427  * or an input-only stream.
428  */
429 void
430 glk_put_char_stream(strid_t str, unsigned char ch)
431 {
432         VALID_STREAM(str, return);
433         g_return_if_fail(str->file_mode != filemode_Read);
434         
435         write_buffer_to_stream(str, (gchar *)&ch, 1);
436 }
437
438 /**
439  * glk_put_char_stream_uni:
440  * @str: An output stream.
441  * @ch: A Unicode code point.
442  *
443  * The same as glk_put_char_uni(), except that you specify a stream @str to
444  * print to, instead of using the current stream. It is illegal for @str to be 
445  * %NULL, or an input-only stream.
446  */
447 void
448 glk_put_char_stream_uni(strid_t str, glui32 ch)
449 {
450         VALID_STREAM(str, return);
451         g_return_if_fail(str->file_mode != filemode_Read);
452         
453         write_buffer_to_stream_uni(str, &ch, 1);
454 }
455
456 /**
457  * glk_put_string_stream:
458  * @str: An output stream.
459  * @s: A null-terminated string in Latin-1 encoding.
460  *
461  * The same as glk_put_string(), except that you specify a stream @str to print 
462  * to, instead of using the current stream. It is illegal for @str to be %NULL,
463  * or an input-only stream.
464  */
465 void
466 glk_put_string_stream(strid_t str, char *s)
467 {
468         VALID_STREAM(str, return);
469         if(*s == 0)
470                 return;
471
472         g_return_if_fail(str->file_mode != filemode_Read);
473
474         write_buffer_to_stream(str, s, strlen(s));
475 }
476
477 /**
478  * glk_put_string_stream_uni:
479  * @str: An output stream.
480  * @s: A null-terminated array of Unicode code points.
481  *
482  * The same as glk_put_string_uni(), except that you specify a stream @str to
483  * print to, instead of using the current stream. It is illegal for @str to be 
484  * %NULL, or an input-only stream.
485  */
486 void
487 glk_put_string_stream_uni(strid_t str, glui32 *s)
488 {
489         VALID_STREAM(str, return);
490         if(*s == 0)
491                 return;
492
493         g_return_if_fail(str->file_mode != filemode_Read);
494         
495         /* An impromptu strlen() for glui32 arrays */
496         glong len = 0;
497         glui32 *ptr = s;
498         while(*ptr++)
499                 len++;
500         write_buffer_to_stream_uni(str, s, len);
501 }
502
503 /**
504  * glk_put_buffer_stream:
505  * @str: An output stream.
506  * @buf: An array of characters in Latin-1 encoding.
507  * @len: Length of @buf.
508  *
509  * The same as glk_put_buffer(), except that you specify a stream @str to print 
510  * to, instead of using the current stream. It is illegal for @str to be %NULL,
511  * or an input-only stream.
512  */
513 void
514 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
515 {
516         VALID_STREAM(str, return);
517         if(len == 0)
518                 return;
519
520         g_return_if_fail(str->file_mode != filemode_Read);
521         
522         write_buffer_to_stream(str, buf, len);
523 }
524
525 /**
526  * glk_put_buffer_stream_uni:
527  * @str: An output stream.
528  * @buf: An array of Unicode code points.
529  * @len: Length of @buf.
530  *
531  * The same as glk_put_buffer_uni(), except that you specify a stream @str to
532  * print to, instead of using the current stream. It is illegal for @str to be 
533  * %NULL, or an input-only stream.
534  */
535 void
536 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
537 {
538         VALID_STREAM(str, return);
539         if(len == 0)
540                 return;
541
542         g_return_if_fail(str->file_mode != filemode_Read);
543         
544         write_buffer_to_stream_uni(str, buf, len);
545 }
546
547 /*
548  *
549  **************** READING FUNCTIONS ********************************************
550  *
551  */
552
553 /* Internal function: Read one big-endian four-byte character from file fp and
554 return it as a Unicode code point, or -1 on EOF */
555 static glsi32
556 read_ucs4be_char_from_file(FILE *fp)
557 {
558         unsigned char readbuffer[4];
559         if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
560                 return -1; /* EOF */
561         return
562                 readbuffer[0] << 24 | 
563                 readbuffer[1] << 16 | 
564                 readbuffer[2] << 8  | 
565                 readbuffer[3];
566 }
567
568 /* Internal function: Read one UTF-8 character, which may be more than one byte,
569 from file fp and return it as a Unicode code point, or -1 on EOF */
570 static glsi32
571 read_utf8_char_from_file(FILE *fp)
572 {
573         gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
574         int foo;
575         gunichar charresult = (gunichar)-2;
576         for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++) 
577         {
578                 int ch = fgetc(fp);
579                 if(ch == EOF)
580                         return -1;
581                 readbuffer[foo] = (gchar)ch;
582                 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
583                 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
584                 point otherwise */
585         }
586         /* Silently return unknown characters as 0xFFFD, Replacement Character */
587         if(charresult == (gunichar)-1 || charresult == (gunichar)-2) 
588                 return 0xFFFD;
589         return charresult;
590 }
591
592 /* Internal function: Tell whether this code point is a Unicode newline. The
593 file pointer and eight-bit flag are included in case the newline is a CR 
594 (U+000D). If the next character is LF (U+000A) then it also belongs to the
595 newline. */
596 static gboolean
597 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
598 {
599         if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
600                 return TRUE;
601         if(ch == 0x0D) {
602                 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) : 
603                         read_ucs4be_char_from_file(fp);
604                 if(ch2 != 0x0A)
605                         if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
606                                 WARNING_S("Seek failed on stream", g_strerror(errno) );
607                 return TRUE;
608         }
609         return FALSE;
610 }
611
612 /* Internal function: Read one character from a stream. Returns a value which
613  can be returned unchanged by glk_get_char_stream_uni(), but 
614  glk_get_char_stream() must replace high values by the placeholder character. */
615 static glsi32
616 get_char_stream_common(strid_t str)
617 {
618         switch(str->type)
619         {
620                 case STREAM_TYPE_MEMORY:
621                         if(str->unicode)
622                         {
623                                 if(!str->ubuffer || str->mark >= str->buflen)
624                                         return -1;
625                                 glui32 ch = str->ubuffer[str->mark++];
626                                 str->read_count++;
627                                 return ch;
628                         }
629                         else
630                         {
631                                 if(!str->buffer || str->mark >= str->buflen)
632                                         return -1;
633                                 unsigned char ch = str->buffer[str->mark++];
634                                 str->read_count++;
635                                 return ch;
636                         }
637                         break;
638                         
639                 case STREAM_TYPE_FILE:
640                         if(str->binary) 
641                         {
642                                 if(str->unicode) 
643                                 {
644                                         glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
645                                         if(ch == -1)
646                                                 return -1;
647                                         str->read_count++;
648                                         return ch;
649                                 }
650                                 else /* Regular file */
651                                 {
652                                         int ch = fgetc(str->file_pointer);
653                                         if(ch == EOF)
654                                                 return -1;
655                                         
656                                         str->read_count++;
657                                         return ch;
658                                 }
659                         }
660                         else /* Text mode is the same for Unicode and regular files */
661                         {
662                                 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
663                                 if(ch == -1)
664                                         return -1;
665                                         
666                                 str->read_count++;
667                                 return ch;
668                         }
669                 default:
670                         ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
671                         return -1;
672         }
673 }
674
675 /**
676  * glk_get_char_stream:
677  * @str: An input stream.
678  *
679  * Reads one character from the stream @str. (There is no notion of a
680  * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
681  * an output-only stream.
682  *
683  * The result will be between 0 and 255. As with all basic text functions, Glk
684  * assumes the Latin-1 encoding. See <link 
685  * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
686  * of the stream has been reached, the result will be -1. 
687  *
688  * <note><para>
689  *   Note that high-bit characters (128..255) are <emphasis>not</emphasis>
690  *   returned as negative numbers.
691  * </para></note>
692  *
693  * If the stream contains Unicode data &mdash; for example, if it was created
694  * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() &mdash; then
695  * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
696  *
697  * It is usually more efficient to read several characters at once with
698  * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
699  * glk_get_char_stream() several times.
700  *
701  * Returns: A character value between 0 and 255, or -1 on end of stream.
702  */
703 glsi32
704 glk_get_char_stream(strid_t str)
705 {
706         VALID_STREAM(str, return -1);
707         g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
708         
709         glsi32 ch = get_char_stream_common(str);
710         return (ch > 0xFF)? PLACEHOLDER : ch;
711 }
712
713 /**
714  * glk_get_char_stream_uni:
715  * @str: An input stream.
716  *
717  * Reads one character from the stream @str. If the end of the stream has been
718  * reached, the result will be -1.
719  *
720  * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
721  */
722 glsi32
723 glk_get_char_stream_uni(strid_t str)
724 {
725         VALID_STREAM(str, return -1);
726         g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
727         
728         return get_char_stream_common(str);
729 }
730
731 /**
732  * glk_get_buffer_stream:
733  * @str: An input stream.
734  * @buf: A buffer with space for at least @len characters.
735  * @len: The number of characters to read.
736  *
737  * Reads @len characters from @str, unless the end of stream is reached first.
738  * No terminal null is placed in the buffer.
739  *
740  * Returns: The number of characters actually read.
741  */
742 glui32
743 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
744 {
745         VALID_STREAM(str, return 0);
746         g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
747         g_return_val_if_fail(buf != NULL, 0);
748         
749         switch(str->type)
750         {
751                 case STREAM_TYPE_MEMORY:
752                 {
753                         int copycount = 0;
754                         if(str->unicode)
755                         {
756                                 while(copycount < len && str->ubuffer && str->mark < str->buflen) 
757                                 {
758                                         glui32 ch = str->ubuffer[str->mark++];
759                                         buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
760                                 }
761                         }
762                         else
763                         {
764                                 if(str->buffer) /* if not, copycount stays 0 */
765                                         copycount = MIN(len, str->buflen - str->mark);
766                                 memmove(buf, str->buffer + str->mark, copycount);
767                                 str->mark += copycount;
768                         }
769
770                         str->read_count += copycount;           
771                         return copycount;
772                 }       
773                 case STREAM_TYPE_FILE:
774                         if(str->binary) 
775                         {
776                                 if(str->unicode) /* Binary file with 4-byte characters */
777                                 {
778                                         /* Read len characters of 4 bytes each */
779                                         unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
780                                         size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
781                                         /* If there was an incomplete character */
782                                         if(count % 4 != 0) 
783                                         {
784                                                 count -= count % 4;
785                                                 WARNING("Incomplete character in binary Unicode file");
786                                         }
787                                         
788                                         int foo;
789                                         for(foo = 0; foo < count; foo += 4)
790                                         {
791                                                 glsi32 ch = readbuffer[foo] << 24
792                                                         | readbuffer[foo + 1] << 16
793                                                         | readbuffer[foo + 2] << 8
794                                                         | readbuffer[foo + 3];
795                                                 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
796                                         }
797                                         g_free(readbuffer);
798                                         str->read_count += count / 4;
799                                         return count / 4;
800                                 }
801                                 else /* Regular binary file */
802                                 {
803                                         size_t count = fread(buf, sizeof(char), len, str->file_pointer);
804                                         str->read_count += count;
805                                         return count;
806                                 }
807                         }
808                         else /* Text mode is the same for Unicode and regular files */
809                         {
810                                 /* Do it character-by-character */
811                                 int foo;
812                                 for(foo = 0; foo < len; foo++)
813                                 {
814                                         glsi32 ch = read_utf8_char_from_file(str->file_pointer);
815                                         if(ch == -1)
816                                                 break;
817                                         str->read_count++;
818                                         buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
819                                 }
820                                 return foo;
821                         }
822                 default:
823                         ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
824                         return 0;
825         }
826 }
827
828 /**
829  * glk_get_buffer_stream_uni:
830  * @str: An input stream.
831  * @buf: A buffer with space for at least @len Unicode code points.
832  * @len: The number of characters to read.
833  *
834  * Reads @len Unicode characters from @str, unless the end of stream is reached 
835  * first. No terminal null is placed in the buffer.
836  *
837  * Returns: The number of Unicode characters actually read.
838  */
839 glui32
840 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
841 {
842         VALID_STREAM(str, return 0);
843         g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
844         g_return_val_if_fail(buf != NULL, 0);
845         
846         switch(str->type)
847         {
848                 case STREAM_TYPE_MEMORY:
849                 {
850                         int copycount = 0;
851                         if(str->unicode)
852                         {
853                                 if(str->ubuffer) /* if not, copycount stays 0 */
854                                         copycount = MIN(len, str->buflen - str->mark);
855                                 memmove(buf, str->ubuffer + str->mark, copycount * 4);
856                                 str->mark += copycount;
857                         }
858                         else
859                         {
860                                 while(copycount < len && str->buffer && str->mark < str->buflen)
861                                 {
862                                         unsigned char ch = str->buffer[str->mark++];
863                                         buf[copycount++] = ch;
864                                 }
865                         }
866
867                         str->read_count += copycount;           
868                         return copycount;
869                 }       
870                 case STREAM_TYPE_FILE:
871                         if(str->binary) 
872                         {
873                                 if(str->unicode) /* Binary file with 4-byte characters */
874                                 {
875                                         /* Read len characters of 4 bytes each */
876                                         unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
877                                         size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
878                                         /* If there was an incomplete character */
879                                         if(count % 4 != 0) 
880                                         {
881                                                 count -= count % 4;
882                                                 WARNING("Incomplete character in binary Unicode file");
883                                         }
884                                         
885                                         int foo;
886                                         for(foo = 0; foo < count; foo += 4)
887                                                 buf[foo / 4] = readbuffer[foo] << 24
888                                                         | readbuffer[foo + 1] << 16
889                                                         | readbuffer[foo + 2] << 8
890                                                         | readbuffer[foo + 3];
891                                         g_free(readbuffer);
892                                         str->read_count += count / 4;
893                                         return count / 4;
894                                 }
895                                 else /* Regular binary file */
896                                 {
897                                         unsigned char *readbuffer = g_new0(unsigned char, len);
898                                         size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
899                                         int foo;
900                                         for(foo = 0; foo < count; foo++)
901                                                 buf[foo] = readbuffer[foo];
902                                         g_free(readbuffer);
903                                         str->read_count += count;
904                                         return count;
905                                 }
906                         }
907                         else /* Text mode is the same for Unicode and regular files */
908                         {
909                                 /* Do it character-by-character */
910                                 int foo;
911                                 for(foo = 0; foo < len; foo++)
912                                 {
913                                         glsi32 ch = read_utf8_char_from_file(str->file_pointer);
914                                         if(ch == -1)
915                                                 break;
916                                         str->read_count++;
917                                         buf[foo] = ch;
918                                 }
919                                 return foo;
920                         }
921                 default:
922                         ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
923                         return 0;
924         }
925 }
926
927 /**
928  * glk_get_line_stream:
929  * @str: An input stream.
930  * @buf: A buffer with space for at least @len characters.
931  * @len: The number of characters to read, plus one.
932  *
933  * Reads characters from @str, until either 
934  * <inlineequation>
935  *   <alt>@len - 1</alt>
936  *   <mathphrase>@len - 1</mathphrase>
937  * </inlineequation>
938  * characters have been read or a newline has been read. It then puts a
939  * terminal null (<code>'\0'</code>) aracter on
940  * the end. It returns the number of characters actually read, including the
941  * newline (if there is one) but not including the terminal null.
942  *
943  * Returns: The number of characters actually read.
944  */
945 glui32
946 glk_get_line_stream(strid_t str, char *buf, glui32 len)
947 {
948         VALID_STREAM(str, return 0);
949         g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
950         g_return_val_if_fail(buf != NULL, 0);
951
952         switch(str->type)
953         {
954                 case STREAM_TYPE_MEMORY:
955                 {
956                         int copycount = 0;
957                         if(str->unicode)
958                         {
959                                 /* Do it character-by-character */
960                                 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen) 
961                                 {
962                                         glui32 ch = str->ubuffer[str->mark++];
963                                         /* Check for Unicode newline; slightly different than
964                                         in file streams */
965                                         if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
966                                         {
967                                                 buf[copycount++] = '\n';
968                                                 break;
969                                         }
970                                         if(ch == 0x0D)
971                                         {
972                                                 if(str->ubuffer[str->mark] == 0x0A)
973                                                         str->mark++; /* skip past next newline */
974                                                 buf[copycount++] = '\n';
975                                                 break;
976                                         }
977                                         buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
978                                 }
979                                 buf[copycount] = '\0';
980                         }
981                         else
982                         {
983                                 if(str->buffer) /* if not, copycount stays 0 */
984                                         copycount = MIN(len - 1, str->buflen - str->mark);
985                                 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
986                                 if(endptr) /* newline was found */
987                                         copycount = endptr - buf; /* Real copy count */
988                                 buf[copycount] = '\0';
989                                 str->mark += copycount;
990                         }
991                         
992                         str->read_count += copycount;
993                         return copycount;
994                 }       
995                 case STREAM_TYPE_FILE:
996                         if(str->binary) 
997                         {
998                                 if(str->unicode) /* Binary file with 4-byte characters */
999                                 {
1000                                         /* Do it character-by-character */
1001                                         int foo;
1002                                         for(foo = 0; foo < len - 1; foo++)
1003                                         {
1004                                                 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1005                                                 if(ch == -1) 
1006                                                 {
1007                                                         buf[foo] = '\0';
1008                                                         return foo - 1;
1009                                                 }
1010                                                 str->read_count++;
1011                                                 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1012                                                 {
1013                                                         buf[foo] = '\n';
1014                                                         buf[foo + 1] = '\0';
1015                                                         return foo;
1016                                                 }
1017                                                 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
1018                                         }
1019                                         buf[len] = '\0';
1020                                         return foo;
1021                                 }
1022                                 else /* Regular binary file */
1023                                 {
1024                                         if( !fgets(buf, len, str->file_pointer) ) {
1025                                                 *buf = 0;
1026                                                 return 0;
1027                                         }
1028
1029                                         int nread = strlen(buf);
1030                                         str->read_count += nread;
1031                                         return nread;
1032                                 }
1033                         }
1034                         else /* Text mode is the same for Unicode and regular files */
1035                         {
1036                                 /* Do it character-by-character */
1037                                 int foo;
1038                                 for(foo = 0; foo < len - 1; foo++)
1039                                 {
1040                                         glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1041                                         if(ch == -1)
1042                                         {
1043                                                 buf[foo] = '\0';
1044                                                 return foo - 1;
1045                                         }
1046                                         str->read_count++;
1047                                         if(is_unicode_newline(ch, str->file_pointer, TRUE))
1048                                         {
1049                                                 buf[foo] = '\n';
1050                                                 buf[foo + 1] = '\0';
1051                                                 return foo;
1052                                         }
1053                                         buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
1054                                 }
1055                                 buf[len] = '\0';
1056                                 return foo;
1057                         }
1058                 default:
1059                         ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1060                         return 0;
1061         }
1062 }
1063
1064 /**
1065  * glk_get_line_stream_uni:
1066  * @str: An input stream.
1067  * @buf: A buffer with space for at least @len Unicode code points.
1068  * @len: The number of characters to read, plus one.
1069  *
1070  * Reads Unicode characters from @str, until either 
1071  * <inlineequation>
1072  *   <alt>@len - 1</alt>
1073  *   <mathphrase>@len - 1</mathphrase>
1074  * </inlineequation> 
1075  * Unicode characters have been read or a newline has been read. It then puts a
1076  * terminal null (a zero value) on the end.
1077  *
1078  * Returns: The number of characters actually read, including the newline (if
1079  * there is one) but not including the terminal null.
1080  */
1081 glui32
1082 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
1083 {
1084         VALID_STREAM(str, return 0);
1085         g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
1086         g_return_val_if_fail(buf != NULL, 0);
1087
1088         switch(str->type)
1089         {
1090                 case STREAM_TYPE_MEMORY:
1091                 {
1092                         int copycount = 0;
1093                         if(str->unicode)
1094                         {
1095                                 /* Do it character-by-character */
1096                                 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen) 
1097                                 {
1098                                         glui32 ch = str->ubuffer[str->mark++];
1099                                         /* Check for Unicode newline; slightly different than
1100                                         in file streams */
1101                                         if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
1102                                         {
1103                                                 buf[copycount++] = '\n';
1104                                                 break;
1105                                         }
1106                                         if(ch == 0x0D)
1107                                         {
1108                                                 if(str->ubuffer[str->mark] == 0x0A)
1109                                                         str->mark++; /* skip past next newline */
1110                                                 buf[copycount++] = '\n';
1111                                                 break;
1112                                         }
1113                                         buf[copycount++] = ch;
1114                                 }
1115                                 buf[copycount] = '\0';
1116                         }
1117                         else
1118                         {
1119                                 /* No recourse to memccpy(), so do it character-by-character */
1120                                 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
1121                                 {
1122                                         gchar ch = str->buffer[str->mark++];
1123                                         /* Check for newline */
1124                                         if(ch == '\n') /* Also check for \r and \r\n? */
1125                                         {
1126                                                 buf[copycount++] = '\n';
1127                                                 break;
1128                                         }
1129                                         buf[copycount++] = (unsigned char)ch;
1130                                 }
1131                                 buf[copycount] = 0;
1132                         }
1133                         
1134                         str->read_count += copycount;
1135                         return copycount;
1136                 }       
1137                 case STREAM_TYPE_FILE:
1138                         if(str->binary) 
1139                         {
1140                                 if(str->unicode) /* Binary file with 4-byte characters */
1141                                 {
1142                                         /* Do it character-by-character */
1143                                         int foo;
1144                                         for(foo = 0; foo < len - 1; foo++)
1145                                         {
1146                                                 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1147                                                 if(ch == -1) 
1148                                                 {
1149                                                         buf[foo] = 0;
1150                                                         return foo - 1;
1151                                                 }
1152                                                 str->read_count++;
1153                                                 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1154                                                 {
1155                                                         buf[foo] = ch; /* Preserve newline types??? */
1156                                                         buf[foo + 1] = 0;
1157                                                         return foo;
1158                                                 }
1159                                                 buf[foo] = ch;
1160                                         }
1161                                         buf[len] = 0;
1162                                         return foo;
1163                                 }
1164                                 else /* Regular binary file */
1165                                 {
1166                                         gchar *readbuffer = g_new0(gchar, len);
1167                                         if( !fgets(readbuffer, len, str->file_pointer) ) {
1168                                                 *buf = 0;
1169                                                 return 0;
1170                                         }
1171
1172                                         glui32 count = strlen(readbuffer);
1173                                         int foo;
1174                                         for(foo = 0; foo < count + 1; foo++) /* Copy terminator */
1175                                                 buf[foo] = (unsigned char)(readbuffer[foo]);
1176                                         str->read_count += count;
1177                                         return count;
1178                                 }
1179                         }
1180                         else /* Text mode is the same for Unicode and regular files */
1181                         {
1182                                 /* Do it character-by-character */
1183                                 int foo;
1184                                 for(foo = 0; foo < len - 1; foo++)
1185                                 {
1186                                         glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1187                                         if(ch == -1)
1188                                         {
1189                                                 buf[foo] = 0;
1190                                                 return foo - 1;
1191                                         }
1192                                         str->read_count++;
1193                                         if(is_unicode_newline(ch, str->file_pointer, TRUE))
1194                                         {
1195                                                 buf[foo] = ch; /* Preserve newline types??? */
1196                                                 buf[foo + 1] = 0;
1197                                                 return foo;
1198                                         }
1199                                         buf[foo] = ch;
1200                                 }
1201                                 buf[len] = 0;
1202                                 return foo;
1203                         }
1204                 default:
1205                         ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1206                         return 0;
1207         }
1208 }
1209
1210 /*
1211  *
1212  **************** SEEKING FUNCTIONS ********************************************
1213  *
1214  */
1215
1216 /**
1217  * glk_stream_get_position:
1218  * @str: A file or memory stream.
1219  *
1220  * Returns the position of the read/write mark in @str. For memory streams and
1221  * binary file streams, this is exactly the number of characters read or written
1222  * from the beginning of the stream (unless you have moved the mark with
1223  * glk_stream_set_position().) For text file streams, matters are more 
1224  * ambiguous, since (for example) writing one byte to a text file may store more
1225  * than one character in the platform's native encoding. You can only be sure
1226  * that the position increases as you read or write to the file.
1227  *
1228  * Additional complication: for Latin-1 memory and file streams, a character is
1229  * a byte. For Unicode memory and file streams (those created by
1230  * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1231  * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1232  * bytes.
1233  *
1234  * <note><para>
1235  *   If this bothers you, don't use binary Unicode files. I don't think they're
1236  *   good for much anyhow.
1237  * </para></note>
1238  *
1239  * glk_stream_get_position() on a window stream will always return zero.
1240  *
1241  * <note><para>
1242  *   It might make more sense to return the number of characters written to the
1243  *   window, but existing libraries do not support this and it's not really
1244  *   worth adding the feature.
1245  * </para></note>
1246  *
1247  * Returns: position of the read/write mark in @str.
1248  */
1249 glui32
1250 glk_stream_get_position(strid_t str)
1251 {
1252         VALID_STREAM(str, return 0);
1253         
1254         switch(str->type)
1255         {
1256                 case STREAM_TYPE_MEMORY:
1257                         return str->mark;
1258                 case STREAM_TYPE_FILE:
1259                         return ftell(str->file_pointer);
1260                 case STREAM_TYPE_WINDOW:
1261                         return 0;
1262                 default:
1263                         ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1264                         return 0;
1265         }
1266 }
1267
1268 /**
1269  * glk_stream_set_position:
1270  * @str: A file or memory stream.
1271  * @pos: The position to set the mark to, relative to @seekmode.
1272  * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1273  *
1274  * Sets the position of the read/write mark in @str. The position is controlled
1275  * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1276  * <code>seekmode_</code> constants below.
1277  *
1278  * It is illegal to specify a position before the beginning or after the end of
1279  * the file.
1280  *
1281  * In binary files, the mark position is exact &mdash; it corresponds with the
1282  * number of characters you have read or written. In text files, this mapping 
1283  * can vary, because of linefeed conventions or other character-set 
1284  * approximations. See <link linkend="chimara-Streams">Streams</link>.
1285  * glk_stream_set_position() and glk_stream_get_position() measure positions in
1286  * the platform's native encoding &mdash; after character cookery. Therefore,
1287  * in a text stream, it is safest to use glk_stream_set_position() only to move
1288  * to the beginning or end of a file, or to a position determined by
1289  * glk_stream_get_position().
1290  *
1291  * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1292  * characters are 32-bit words, or four bytes each.
1293  *
1294  * A window stream doesn't have a movable mark, so calling
1295  * glk_stream_set_position() has no effect.
1296  */
1297 void
1298 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1299 {
1300         VALID_STREAM(str, return);
1301         g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1302         g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1303         
1304         switch(str->type)
1305         {
1306                 case STREAM_TYPE_MEMORY:
1307                         switch(seekmode)
1308                         {
1309                                 case seekmode_Start:   str->mark = pos;  break;
1310                                 case seekmode_Current: str->mark += pos; break;
1311                                 case seekmode_End:     str->mark = str->buflen + pos; break;
1312                                 default:
1313                                         g_return_if_reached();
1314                                         return;
1315                         }
1316                         break;
1317                 case STREAM_TYPE_FILE:
1318                 {
1319                         int whence;
1320                         switch(seekmode)
1321                         {
1322                                 case seekmode_Start:   whence = SEEK_SET; break;
1323                                 case seekmode_Current: whence = SEEK_CUR; break;
1324                                 case seekmode_End:     whence = SEEK_END; break;
1325                                 default:
1326                                         g_return_if_reached();
1327                                         return;
1328                         }
1329                         if(fseek(str->file_pointer, pos, whence) == -1)
1330                                 WARNING("Seek failed on file stream");
1331                         break;
1332                 }
1333                 case STREAM_TYPE_WINDOW:
1334                         break; /* Quietly do nothing */
1335                 default:
1336                         ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1337                         return;
1338         }
1339 }
1340