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