8 #include <glib/gstdio.h>
10 /* Internal function: ensure that an fseek() is called on a file pointer in
11 between reading and writing operations, and vice versa. This will only come up
12 for ReadWrite or WriteAppend files. */
14 ensure_file_operation(strid_t str, glui32 op)
16 if(str->lastop != 0 && str->lastop != op)
18 long pos = ftell(str->file_pointer);
20 WARNING_S("ftell() failed", g_strerror(errno));
21 if(fseek(str->file_pointer, pos, SEEK_SET) != 0)
22 WARNING_S("fseek() failed", g_strerror(errno));
24 str->lastop = op; /* Not 0, because we are about to do the operation anyway */
29 **************** WRITING FUNCTIONS ********************************************
33 /* Internal function: write a UTF-8 string to a text buffer window's text buffer. */
35 write_utf8_to_window_buffer(winid_t win, gchar *s)
37 if(win->input_request_type == INPUT_REQUEST_LINE || win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
39 ILLEGAL("Tried to print to a text buffer window with line input pending.");
43 // Write to the buffer
44 g_string_append(win->buffer, s);
47 /* Internal function: flush a window's text buffer to the screen. */
49 flush_window_buffer(winid_t win)
52 g_printf("%s\n", win->buffer->str);
54 if(win->type != wintype_TextBuffer && win->type != wintype_TextGrid)
57 if(win->buffer->len == 0)
62 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
65 case wintype_TextBuffer:
67 GtkTextIter start, end;
68 gtk_text_buffer_get_end_iter(buffer, &end);
71 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
73 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
74 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
75 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
77 start_offset = gtk_text_iter_get_offset(&end);
78 gtk_text_buffer_insert(buffer, &end, win->buffer->str, -1);
79 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
81 // Player's style overrides
82 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &end);
84 // GLK Program's style overrides
85 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &end);
88 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &end);
90 // Link style overrides
91 if(win->window_stream->hyperlink_mode) {
92 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
93 GtkTextTag *link_tag = win->current_hyperlink->tag;
94 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &end);
95 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &end);
98 // GLK Program's style overrides using garglk_set_zcolors()
99 if(win->zcolor != NULL) {
100 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &end);
103 // GLK Program's style overrides using garglk_set_reversevideo()
104 if(win->zcolor_reversed != NULL) {
105 gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &end);
108 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
110 g_signal_emit_by_name(glk, "text-buffer-output", win->rock, win->buffer->str);
114 case wintype_TextGrid:
116 /* Number of characters to insert */
117 glong length = win->buffer->len;
118 glong chars_left = length;
120 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
122 /* Get cursor position */
123 GtkTextIter start, insert;
126 gtk_text_buffer_get_iter_at_mark(buffer, &insert, cursor);
127 /* Spaces available on this line */
128 gint available_space = win->width - gtk_text_iter_get_line_offset(&insert);
130 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
132 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
133 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
134 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
135 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
137 while(chars_left > available_space && !gtk_text_iter_is_end(&insert))
139 GtkTextIter end = insert;
140 gtk_text_iter_forward_to_line_end(&end);
141 gtk_text_buffer_delete(buffer, &insert, &end);
143 start_offset = gtk_text_iter_get_offset(&insert);
144 gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), available_space);
145 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
148 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
150 // Player's style overrides
151 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
153 // GLK Program's style overrides
154 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
156 // Link style overrides
157 if(win->window_stream->hyperlink_mode) {
158 GtkTextTag *link_tag = win->current_hyperlink->tag;
159 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
160 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
163 // GLK Program's style overrides using garglk_set_zcolors()
164 if(win->zcolor != NULL)
165 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
167 // GLK Program's style overrides using garglk_set_reversevideo()
168 if(win->zcolor_reversed != NULL) {
169 gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &insert);
172 chars_left -= available_space;
173 gtk_text_iter_forward_line(&insert);
174 available_space = win->width;
176 if(!gtk_text_iter_is_end(&insert))
178 GtkTextIter end = insert;
179 gtk_text_iter_forward_chars(&end, chars_left);
180 gtk_text_buffer_delete(buffer, &insert, &end);
182 start_offset = gtk_text_iter_get_offset(&insert);
183 gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), -1);
184 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
187 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
189 // Player's style overrides
190 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
192 // GLK Program's style overrides
193 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
195 // Link style overrides
196 if(win->window_stream->hyperlink_mode) {
197 GtkTextTag *link_tag = win->current_hyperlink->tag;
198 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
199 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
202 // GLK Program's style overrides using garglk_set_zcolors()
203 if(win->zcolor != NULL)
204 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
206 // GLK Program's style overrides using garglk_set_reversevideo()
207 if(win->zcolor_reversed != NULL) {
208 gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &insert);
212 gtk_text_buffer_move_mark(buffer, cursor, &start);
219 g_string_truncate(win->buffer, 0);
222 /* Internal function: write a Latin-1 buffer with length to a stream. */
224 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
228 case STREAM_TYPE_WINDOW:
229 /* Each window type has a different way of printing to it */
230 switch(str->window->type)
232 /* Printing to these windows' streams does nothing */
235 case wintype_Graphics:
236 str->write_count += len;
239 /* Text grid/buffer windows */
240 case wintype_TextGrid:
242 gchar *utf8 = convert_latin1_to_utf8(buf, len);
244 /* Deal with newlines */
247 for(i=0; i<len; i++) {
248 if(utf8[i] == '\n') {
250 write_utf8_to_window_buffer(str->window, line);
251 flush_window_buffer(str->window);
253 /* Move cursor position forward to the next line */
255 GtkTextIter cursor_pos;
256 GtkTextView *textview = GTK_TEXT_VIEW(str->window->widget);
257 GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
258 GtkTextMark *cursor_mark = gtk_text_buffer_get_mark(buffer, "cursor_position");
260 gtk_text_buffer_get_iter_at_mark( buffer, &cursor_pos, cursor_mark);
261 gtk_text_view_forward_display_line(textview, &cursor_pos);
262 gtk_text_view_backward_display_line_start(textview, &cursor_pos);
263 gtk_text_buffer_move_mark(buffer, cursor_mark, &cursor_pos);
266 line = utf8 + (i < len-1 ? (i+1):(len-1));
270 /* No more newlines left. */
271 write_utf8_to_window_buffer(str->window, line);
275 str->write_count += len;
279 case wintype_TextBuffer:
281 gchar *utf8 = convert_latin1_to_utf8(buf, len);
283 write_utf8_to_window_buffer(str->window, utf8);
287 str->write_count += len;
290 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
293 /* Now write the same buffer to the window's echo stream */
294 if(str->window->echo_stream != NULL)
295 write_buffer_to_stream(str->window->echo_stream, buf, len);
299 case STREAM_TYPE_MEMORY:
300 if(str->unicode && str->ubuffer)
303 while(str->mark < str->buflen && foo < len)
304 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
306 if(!str->unicode && str->buffer)
308 int copycount = MIN(len, str->buflen - str->mark);
309 memmove(str->buffer + str->mark, buf, copycount);
310 str->mark += copycount;
313 /* Move the EOF marker if we wrote past it */
314 if(str->mark > str->endmark)
315 str->endmark = str->mark;
317 str->write_count += len;
320 case STREAM_TYPE_FILE:
325 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
326 ensure_file_operation(str, filemode_Write);
327 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
330 else /* Regular file */
332 ensure_file_operation(str, filemode_Write);
333 fwrite(buf, sizeof(gchar), len, str->file_pointer);
336 else /* Text mode is the same for Unicode and regular files */
338 gchar *utf8 = convert_latin1_to_utf8(buf, len);
341 ensure_file_operation(str, filemode_Write);
342 g_fprintf(str->file_pointer, "%s", utf8);
347 str->write_count += len;
350 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
354 /* Internal function: write a Unicode buffer with length to a stream. */
356 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
360 case STREAM_TYPE_WINDOW:
361 /* Each window type has a different way of printing to it */
362 switch(str->window->type)
364 /* Printing to these windows' streams does nothing */
367 case wintype_Graphics:
368 str->write_count += len;
371 /* Text grid/buffer windows */
372 case wintype_TextGrid:
373 case wintype_TextBuffer:
375 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
377 write_utf8_to_window_buffer(str->window, utf8);
381 str->write_count += len;
384 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
387 /* Now write the same buffer to the window's echo stream */
388 if(str->window->echo_stream != NULL)
389 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
393 case STREAM_TYPE_MEMORY:
394 if(str->unicode && str->ubuffer)
396 int copycount = MIN(len, str->buflen - str->mark);
397 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
398 str->mark += copycount;
400 if(!str->unicode && str->buffer)
402 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
403 int copycount = MIN(len, str->buflen - str->mark);
404 memmove(str->buffer + str->mark, latin1, copycount);
406 str->mark += copycount;
409 /* Move the EOF marker if we wrote past it */
410 if(str->mark > str->endmark)
411 str->endmark = str->mark;
413 str->write_count += len;
416 case STREAM_TYPE_FILE:
421 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
422 ensure_file_operation(str, filemode_Write);
423 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
426 else /* Regular file */
428 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
429 ensure_file_operation(str, filemode_Write);
430 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
434 else /* Text mode is the same for Unicode and regular files */
436 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
439 ensure_file_operation(str, filemode_Write);
440 g_fprintf(str->file_pointer, "%s", utf8);
445 str->write_count += len;
448 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
453 * glk_put_char_stream:
454 * @str: An output stream.
455 * @ch: A character in Latin-1 encoding.
457 * The same as glk_put_char(), except that you specify a stream @str to print
458 * to, instead of using the current stream. It is illegal for @str to be %NULL,
459 * or an input-only stream.
462 glk_put_char_stream(strid_t str, unsigned char ch)
464 VALID_STREAM(str, return);
465 g_return_if_fail(str->file_mode != filemode_Read);
467 write_buffer_to_stream(str, (gchar *)&ch, 1);
471 * glk_put_char_stream_uni:
472 * @str: An output stream.
473 * @ch: A Unicode code point.
475 * The same as glk_put_char_uni(), except that you specify a stream @str to
476 * print to, instead of using the current stream. It is illegal for @str to be
477 * %NULL, or an input-only stream.
480 glk_put_char_stream_uni(strid_t str, glui32 ch)
482 VALID_STREAM(str, return);
483 g_return_if_fail(str->file_mode != filemode_Read);
485 write_buffer_to_stream_uni(str, &ch, 1);
489 * glk_put_string_stream:
490 * @str: An output stream.
491 * @s: A null-terminated string in Latin-1 encoding.
493 * The same as glk_put_string(), except that you specify a stream @str to print
494 * to, instead of using the current stream. It is illegal for @str to be %NULL,
495 * or an input-only stream.
498 glk_put_string_stream(strid_t str, char *s)
500 VALID_STREAM(str, return);
504 g_return_if_fail(str->file_mode != filemode_Read);
506 write_buffer_to_stream(str, s, strlen(s));
510 * glk_put_string_stream_uni:
511 * @str: An output stream.
512 * @s: A null-terminated array of Unicode code points.
514 * The same as glk_put_string_uni(), except that you specify a stream @str to
515 * print to, instead of using the current stream. It is illegal for @str to be
516 * %NULL, or an input-only stream.
519 glk_put_string_stream_uni(strid_t str, glui32 *s)
521 VALID_STREAM(str, return);
525 g_return_if_fail(str->file_mode != filemode_Read);
527 /* An impromptu strlen() for glui32 arrays */
532 write_buffer_to_stream_uni(str, s, len);
536 * glk_put_buffer_stream:
537 * @str: An output stream.
538 * @buf: An array of characters in Latin-1 encoding.
539 * @len: Length of @buf.
541 * The same as glk_put_buffer(), except that you specify a stream @str to print
542 * to, instead of using the current stream. It is illegal for @str to be %NULL,
543 * or an input-only stream.
546 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
548 VALID_STREAM(str, return);
552 g_return_if_fail(str->file_mode != filemode_Read);
554 write_buffer_to_stream(str, buf, len);
558 * glk_put_buffer_stream_uni:
559 * @str: An output stream.
560 * @buf: An array of Unicode code points.
561 * @len: Length of @buf.
563 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
564 * print to, instead of using the current stream. It is illegal for @str to be
565 * %NULL, or an input-only stream.
568 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
570 VALID_STREAM(str, return);
574 g_return_if_fail(str->file_mode != filemode_Read);
576 write_buffer_to_stream_uni(str, buf, len);
581 **************** READING FUNCTIONS ********************************************
585 /* Internal function: Read one big-endian four-byte character from file fp and
586 return it as a Unicode code point, or -1 on EOF */
588 read_ucs4be_char_from_file(strid_t str)
590 unsigned char readbuffer[4];
591 ensure_file_operation(str, filemode_Read);
592 if(fread(readbuffer, sizeof(unsigned char), 4, str->file_pointer) < 4)
595 readbuffer[0] << 24 |
596 readbuffer[1] << 16 |
601 /* Internal function: Read one UTF-8 character, which may be more than one byte,
602 from file fp and return it as a Unicode code point, or -1 on EOF */
604 read_utf8_char_from_file(strid_t str)
606 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
608 gunichar charresult = (gunichar)-2;
609 ensure_file_operation(str, filemode_Read);
610 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
612 int ch = fgetc(str->file_pointer);
615 readbuffer[foo] = (gchar)ch;
616 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
617 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
620 /* Silently return unknown characters as 0xFFFD, Replacement Character */
621 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
626 /* Internal function: Tell whether this code point is a Unicode newline. The
627 file pointer and eight-bit flag are included in case the newline is a CR
628 (U+000D). If the next character is LF (U+000A) then it also belongs to the
631 is_unicode_newline(glsi32 ch, strid_t str, gboolean utf8)
633 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
636 glsi32 ch2 = utf8? read_utf8_char_from_file(str) :
637 read_ucs4be_char_from_file(str);
639 if(fseek(str->file_pointer, utf8? -1 : -4, SEEK_CUR) == -1);
640 WARNING_S("Seek failed on stream", g_strerror(errno) );
641 str->lastop = 0; /* can read or write after a seek */
648 /* Internal function: Read one character from a stream. Returns a value which
649 can be returned unchanged by glk_get_char_stream_uni(), but
650 glk_get_char_stream() must replace high values by the placeholder character. */
652 get_char_stream_common(strid_t str)
656 case STREAM_TYPE_MEMORY:
659 if(!str->ubuffer || str->mark >= str->buflen)
661 glui32 ch = str->ubuffer[str->mark++];
667 if(!str->buffer || str->mark >= str->buflen)
669 unsigned char ch = str->buffer[str->mark++];
675 case STREAM_TYPE_FILE:
680 glsi32 ch = read_ucs4be_char_from_file(str);
686 else /* Regular file */
688 ensure_file_operation(str, filemode_Read);
689 int ch = fgetc(str->file_pointer);
697 else /* Text mode is the same for Unicode and regular files */
699 glsi32 ch = read_utf8_char_from_file(str);
707 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
713 * glk_get_char_stream:
714 * @str: An input stream.
716 * Reads one character from the stream @str. (There is no notion of a
717 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
718 * an output-only stream.
720 * The result will be between 0 and 255. As with all basic text functions, Glk
721 * assumes the Latin-1 encoding. See <link
722 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
723 * of the stream has been reached, the result will be -1.
726 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
727 * returned as negative numbers.
730 * If the stream contains Unicode data — for example, if it was created
731 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
732 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
734 * It is usually more efficient to read several characters at once with
735 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
736 * glk_get_char_stream() several times.
738 * Returns: A character value between 0 and 255, or -1 on end of stream.
741 glk_get_char_stream(strid_t str)
743 VALID_STREAM(str, return -1);
744 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
746 glsi32 ch = get_char_stream_common(str);
747 return (ch > 0xFF)? PLACEHOLDER : ch;
751 * glk_get_char_stream_uni:
752 * @str: An input stream.
754 * Reads one character from the stream @str. If the end of the stream has been
755 * reached, the result will be -1.
757 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
760 glk_get_char_stream_uni(strid_t str)
762 VALID_STREAM(str, return -1);
763 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
765 return get_char_stream_common(str);
769 * glk_get_buffer_stream:
770 * @str: An input stream.
771 * @buf: A buffer with space for at least @len characters.
772 * @len: The number of characters to read.
774 * Reads @len characters from @str, unless the end of stream is reached first.
775 * No terminal null is placed in the buffer.
777 * Returns: The number of characters actually read.
780 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
782 VALID_STREAM(str, return 0);
783 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
784 g_return_val_if_fail(buf != NULL, 0);
788 case STREAM_TYPE_MEMORY:
793 while(copycount < len && str->ubuffer && str->mark < str->buflen)
795 glui32 ch = str->ubuffer[str->mark++];
796 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
801 if(str->buffer) /* if not, copycount stays 0 */
802 copycount = MIN(len, str->buflen - str->mark);
803 memmove(buf, str->buffer + str->mark, copycount);
804 str->mark += copycount;
807 str->read_count += copycount;
810 case STREAM_TYPE_FILE:
813 if(str->unicode) /* Binary file with 4-byte characters */
815 /* Read len characters of 4 bytes each */
816 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
817 ensure_file_operation(str, filemode_Read);
818 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
819 /* If there was an incomplete character */
823 WARNING("Incomplete character in binary Unicode file");
827 for(foo = 0; foo < count; foo += 4)
829 glsi32 ch = readbuffer[foo] << 24
830 | readbuffer[foo + 1] << 16
831 | readbuffer[foo + 2] << 8
832 | readbuffer[foo + 3];
833 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
836 str->read_count += count / 4;
839 else /* Regular binary file */
841 ensure_file_operation(str, filemode_Read);
842 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
843 str->read_count += count;
847 else /* Text mode is the same for Unicode and regular files */
849 /* Do it character-by-character */
851 for(foo = 0; foo < len; foo++)
853 glsi32 ch = read_utf8_char_from_file(str);
857 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
862 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
868 * glk_get_buffer_stream_uni:
869 * @str: An input stream.
870 * @buf: A buffer with space for at least @len Unicode code points.
871 * @len: The number of characters to read.
873 * Reads @len Unicode characters from @str, unless the end of stream is reached
874 * first. No terminal null is placed in the buffer.
876 * Returns: The number of Unicode characters actually read.
879 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
881 VALID_STREAM(str, return 0);
882 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
883 g_return_val_if_fail(buf != NULL, 0);
887 case STREAM_TYPE_MEMORY:
892 if(str->ubuffer) /* if not, copycount stays 0 */
893 copycount = MIN(len, str->buflen - str->mark);
894 memmove(buf, str->ubuffer + str->mark, copycount * 4);
895 str->mark += copycount;
899 while(copycount < len && str->buffer && str->mark < str->buflen)
901 unsigned char ch = str->buffer[str->mark++];
902 buf[copycount++] = ch;
906 str->read_count += copycount;
909 case STREAM_TYPE_FILE:
912 if(str->unicode) /* Binary file with 4-byte characters */
914 /* Read len characters of 4 bytes each */
915 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
916 ensure_file_operation(str, filemode_Read);
917 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
918 /* If there was an incomplete character */
922 WARNING("Incomplete character in binary Unicode file");
926 for(foo = 0; foo < count; foo += 4)
927 buf[foo / 4] = readbuffer[foo] << 24
928 | readbuffer[foo + 1] << 16
929 | readbuffer[foo + 2] << 8
930 | readbuffer[foo + 3];
932 str->read_count += count / 4;
935 else /* Regular binary file */
937 unsigned char *readbuffer = g_new0(unsigned char, len);
938 ensure_file_operation(str, filemode_Read);
939 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
941 for(foo = 0; foo < count; foo++)
942 buf[foo] = readbuffer[foo];
944 str->read_count += count;
948 else /* Text mode is the same for Unicode and regular files */
950 /* Do it character-by-character */
952 for(foo = 0; foo < len; foo++)
954 glsi32 ch = read_utf8_char_from_file(str);
963 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
969 * glk_get_line_stream:
970 * @str: An input stream.
971 * @buf: A buffer with space for at least @len characters.
972 * @len: The number of characters to read, plus one.
974 * Reads characters from @str, until either
976 * <alt>@len - 1</alt>
977 * <mathphrase>@len - 1</mathphrase>
979 * characters have been read or a newline has been read. It then puts a
980 * terminal null (<code>'\0'</code>) character on
981 * the end. It returns the number of characters actually read, including the
982 * newline (if there is one) but not including the terminal null.
984 * Returns: The number of characters actually read.
987 glk_get_line_stream(strid_t str, char *buf, glui32 len)
989 VALID_STREAM(str, return 0);
990 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
991 g_return_val_if_fail(buf != NULL, 0);
995 case STREAM_TYPE_MEMORY:
1000 /* Do it character-by-character */
1001 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
1003 glui32 ch = str->ubuffer[str->mark++];
1004 /* Check for Unicode newline; slightly different than
1006 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
1008 buf[copycount++] = '\n';
1013 if(str->ubuffer[str->mark] == 0x0A)
1014 str->mark++; /* skip past next newline */
1015 buf[copycount++] = '\n';
1018 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
1020 buf[copycount] = '\0';
1024 if(str->buffer) /* if not, copycount stays 0 */
1025 copycount = MIN(len - 1, str->buflen - str->mark);
1026 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
1027 if(endptr) /* newline was found */
1028 copycount = endptr - buf; /* Real copy count */
1029 buf[copycount] = '\0';
1030 str->mark += copycount;
1033 str->read_count += copycount;
1036 case STREAM_TYPE_FILE:
1039 if(str->unicode) /* Binary file with 4-byte characters */
1041 /* Do it character-by-character */
1043 for(copycount = 0; copycount < len - 1; copycount++)
1045 glsi32 ch = read_ucs4be_char_from_file(str);
1048 buf[copycount] = '\0';
1052 if(is_unicode_newline(ch, str, FALSE))
1054 buf[copycount++] = '\n';
1055 buf[copycount] = '\0';
1058 buf[copycount] = (ch > 0xFF)? '?' : (char)ch;
1063 else /* Regular binary file */
1065 ensure_file_operation(str, filemode_Read);
1066 if( !fgets(buf, len, str->file_pointer) ) {
1071 int nread = strlen(buf);
1072 str->read_count += nread;
1076 else /* Text mode is the same for Unicode and regular files */
1078 /* Do it character-by-character */
1080 for(foo = 0; foo < len - 1; foo++)
1082 glsi32 ch = read_utf8_char_from_file(str);
1089 if(is_unicode_newline(ch, str, TRUE))
1092 buf[foo + 1] = '\0';
1095 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
1101 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1107 * glk_get_line_stream_uni:
1108 * @str: An input stream.
1109 * @buf: A buffer with space for at least @len Unicode code points.
1110 * @len: The number of characters to read, plus one.
1112 * Reads Unicode characters from @str, until either
1114 * <alt>@len - 1</alt>
1115 * <mathphrase>@len - 1</mathphrase>
1117 * Unicode characters have been read or a newline has been read. It then puts a
1118 * terminal null (a zero value) on the end.
1120 * Returns: The number of characters actually read, including the newline (if
1121 * there is one) but not including the terminal null.
1124 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
1126 VALID_STREAM(str, return 0);
1127 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
1128 g_return_val_if_fail(buf != NULL, 0);
1132 case STREAM_TYPE_MEMORY:
1137 /* Do it character-by-character */
1138 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
1140 glui32 ch = str->ubuffer[str->mark++];
1141 /* Check for Unicode newline; slightly different than
1143 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
1145 buf[copycount++] = '\n';
1150 if(str->ubuffer[str->mark] == 0x0A)
1151 str->mark++; /* skip past next newline */
1152 buf[copycount++] = '\n';
1155 buf[copycount++] = ch;
1157 buf[copycount] = '\0';
1161 /* No recourse to memccpy(), so do it character-by-character */
1162 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
1164 gchar ch = str->buffer[str->mark++];
1165 /* Check for newline */
1166 if(ch == '\n') /* Also check for \r and \r\n? */
1168 buf[copycount++] = '\n';
1171 buf[copycount++] = (unsigned char)ch;
1176 str->read_count += copycount;
1179 case STREAM_TYPE_FILE:
1182 if(str->unicode) /* Binary file with 4-byte characters */
1184 /* Do it character-by-character */
1186 for(copycount = 0; copycount < len - 1; copycount++)
1188 glsi32 ch = read_ucs4be_char_from_file(str);
1195 if(is_unicode_newline(ch, str, FALSE))
1197 buf[copycount++] = ch; /* Preserve newline types??? */
1201 buf[copycount] = ch;
1206 else /* Regular binary file */
1208 gchar *readbuffer = g_new0(gchar, len);
1209 ensure_file_operation(str, filemode_Read);
1210 if( !fgets(readbuffer, len, str->file_pointer) ) {
1215 glui32 count = strlen(readbuffer);
1217 for(foo = 0; foo < count + 1; foo++) /* Copy terminator */
1218 buf[foo] = (unsigned char)(readbuffer[foo]);
1219 str->read_count += count;
1223 else /* Text mode is the same for Unicode and regular files */
1225 /* Do it character-by-character */
1227 for(foo = 0; foo < len - 1; foo++)
1229 glsi32 ch = read_utf8_char_from_file(str);
1236 if(is_unicode_newline(ch, str, TRUE))
1238 buf[foo] = ch; /* Preserve newline types??? */
1248 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1255 **************** SEEKING FUNCTIONS ********************************************
1260 * glk_stream_get_position:
1261 * @str: A file or memory stream.
1263 * Returns the position of the read/write mark in @str. For memory streams and
1264 * binary file streams, this is exactly the number of characters read or written
1265 * from the beginning of the stream (unless you have moved the mark with
1266 * glk_stream_set_position().) For text file streams, matters are more
1267 * ambiguous, since (for example) writing one byte to a text file may store more
1268 * than one character in the platform's native encoding. You can only be sure
1269 * that the position increases as you read or write to the file.
1271 * Additional complication: for Latin-1 memory and file streams, a character is
1272 * a byte. For Unicode memory and file streams (those created by
1273 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1274 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1278 * If this bothers you, don't use binary Unicode files. I don't think they're
1279 * good for much anyhow.
1282 * glk_stream_get_position() on a window stream will always return zero.
1285 * It might make more sense to return the number of characters written to the
1286 * window, but existing libraries do not support this and it's not really
1287 * worth adding the feature.
1290 * Returns: position of the read/write mark in @str.
1293 glk_stream_get_position(strid_t str)
1295 VALID_STREAM(str, return 0);
1299 case STREAM_TYPE_MEMORY:
1301 case STREAM_TYPE_FILE:
1302 return ftell(str->file_pointer);
1303 case STREAM_TYPE_WINDOW:
1306 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1312 * glk_stream_set_position:
1313 * @str: A file or memory stream.
1314 * @pos: The position to set the mark to, relative to @seekmode.
1315 * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1317 * Sets the position of the read/write mark in @str. The position is controlled
1318 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1319 * <code>seekmode_</code> constants below.
1321 * It is illegal to specify a position before the beginning or after the end of
1324 * In binary files, the mark position is exact — it corresponds with the
1325 * number of characters you have read or written. In text files, this mapping
1326 * can vary, because of linefeed conventions or other character-set
1327 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1328 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1329 * the platform's native encoding — after character cookery. Therefore,
1330 * in a text stream, it is safest to use glk_stream_set_position() only to move
1331 * to the beginning or end of a file, or to a position determined by
1332 * glk_stream_get_position().
1334 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1335 * characters are 32-bit words, or four bytes each.
1337 * A window stream doesn't have a movable mark, so calling
1338 * glk_stream_set_position() has no effect.
1341 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1343 VALID_STREAM(str, return);
1344 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1345 g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1349 case STREAM_TYPE_MEMORY:
1352 case seekmode_Start: str->mark = pos; break;
1353 case seekmode_Current: str->mark += pos; break;
1354 case seekmode_End: str->mark = str->endmark + pos; break;
1356 g_return_if_reached();
1360 case STREAM_TYPE_FILE:
1365 case seekmode_Start: whence = SEEK_SET; break;
1366 case seekmode_Current: whence = SEEK_CUR; break;
1367 case seekmode_End: whence = SEEK_END; break;
1369 g_return_if_reached();
1372 if(fseek(str->file_pointer, pos, whence) == -1)
1373 WARNING("Seek failed on file stream");
1374 str->lastop = 0; /* Either reading or writing is legal after fseek() */
1377 case STREAM_TYPE_WINDOW:
1378 break; /* Quietly do nothing */
1380 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);