8 #include <glib/gstdio.h>
12 **************** WRITING FUNCTIONS ********************************************
16 /* Internal function: write a UTF-8 string to a text buffer window's text buffer. */
18 write_utf8_to_window_buffer(winid_t win, gchar *s)
20 if(win->input_request_type == INPUT_REQUEST_LINE || win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
22 ILLEGAL("Tried to print to a text buffer window with line input pending.");
26 // Write to the buffer
27 g_string_append(win->buffer, s);
30 /* Internal function: flush a window's text buffer to the screen. */
32 flush_window_buffer(winid_t win)
34 if(win->type != wintype_TextBuffer && win->type != wintype_TextGrid)
37 if(win->buffer->len == 0)
42 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
45 case wintype_TextBuffer:
47 GtkTextIter start, end;
48 gtk_text_buffer_get_end_iter(buffer, &end);
51 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
53 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
54 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
55 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
57 start_offset = gtk_text_iter_get_offset(&end);
58 gtk_text_buffer_insert(buffer, &end, win->buffer->str, -1);
59 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
62 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &end);
64 // Player's style overrides
65 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &end);
67 // GLK Program's style overrides
68 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &end);
70 // Link style overrides
71 if(win->window_stream->hyperlink_mode) {
72 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
73 GtkTextTag *link_tag = win->current_hyperlink->tag;
74 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &end);
75 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &end);
78 // GLK Program's style overrides using garglk_set_zcolors()
79 if(win->zcolor != NULL)
80 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &end);
82 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
84 g_signal_emit_by_name(glk, "text-buffer-output", win->rock, win->buffer->str);
88 case wintype_TextGrid:
90 /* Number of characters to insert */
91 glong length = win->buffer->len;
92 glong chars_left = length;
94 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
96 /* Get cursor position */
97 GtkTextIter start, insert;
100 gtk_text_buffer_get_iter_at_mark(buffer, &insert, cursor);
101 /* Spaces available on this line */
102 gint available_space = win->width - gtk_text_iter_get_line_offset(&insert);
104 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
105 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
106 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
107 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
108 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
110 while(chars_left > available_space && !gtk_text_iter_is_end(&start))
112 GtkTextIter end = start;
113 gtk_text_iter_forward_to_line_end(&end);
114 gtk_text_buffer_delete(buffer, &insert, &end);
116 start_offset = gtk_text_iter_get_offset(&insert);
117 gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), available_space);
118 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
121 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
123 // Player's style overrides
124 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
126 // GLK Program's style overrides
127 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
129 // Link style overrides
130 if(win->window_stream->hyperlink_mode) {
131 GtkTextTag *link_tag = win->current_hyperlink->tag;
132 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
133 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
136 // GLK Program's style overrides using garglk_set_zcolors()
138 if(win->zcolor != NULL)
139 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
143 chars_left -= available_space;
144 gtk_text_iter_forward_line(&insert);
145 available_space = win->width;
147 if(!gtk_text_iter_is_end(&insert))
149 GtkTextIter end = insert;
150 gtk_text_iter_forward_chars(&end, chars_left);
151 gtk_text_buffer_delete(buffer, &insert, &end);
153 start_offset = gtk_text_iter_get_offset(&insert);
154 gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), -1);
155 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
158 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
160 // Player's style overrides
161 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
163 // GLK Program's style overrides
164 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
166 // Link style overrides
167 if(win->window_stream->hyperlink_mode) {
168 GtkTextTag *link_tag = win->current_hyperlink->tag;
169 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
170 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
173 // GLK Program's style overrides using garglk_set_zcolors()
175 if(win->zcolor != NULL)
176 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
180 gtk_text_buffer_move_mark(buffer, cursor, &start);
187 g_string_truncate(win->buffer, 0);
190 /* Internal function: write a Latin-1 buffer with length to a stream. */
192 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
196 case STREAM_TYPE_WINDOW:
197 /* Each window type has a different way of printing to it */
198 switch(str->window->type)
200 /* Printing to these windows' streams does nothing */
203 case wintype_Graphics:
204 str->write_count += len;
207 /* Text grid/buffer windows */
208 case wintype_TextGrid:
209 case wintype_TextBuffer:
211 gchar *utf8 = convert_latin1_to_utf8(buf, len);
213 write_utf8_to_window_buffer(str->window, utf8);
217 str->write_count += len;
220 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
223 /* Now write the same buffer to the window's echo stream */
224 if(str->window->echo_stream != NULL)
225 write_buffer_to_stream(str->window->echo_stream, buf, len);
229 case STREAM_TYPE_MEMORY:
230 if(str->unicode && str->ubuffer)
233 while(str->mark < str->buflen && foo < len)
234 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
236 if(!str->unicode && str->buffer)
238 int copycount = MIN(len, str->buflen - str->mark);
239 memmove(str->buffer + str->mark, buf, copycount);
240 str->mark += copycount;
243 str->write_count += len;
246 case STREAM_TYPE_FILE:
251 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
252 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
255 else /* Regular file */
257 fwrite(buf, sizeof(gchar), len, str->file_pointer);
260 else /* Text mode is the same for Unicode and regular files */
262 gchar *utf8 = convert_latin1_to_utf8(buf, len);
265 g_fprintf(str->file_pointer, "%s", utf8);
270 str->write_count += len;
273 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
277 /* Internal function: write a Unicode buffer with length to a stream. */
279 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
283 case STREAM_TYPE_WINDOW:
284 /* Each window type has a different way of printing to it */
285 switch(str->window->type)
287 /* Printing to these windows' streams does nothing */
290 case wintype_Graphics:
291 str->write_count += len;
294 /* Text grid/buffer windows */
295 case wintype_TextGrid:
296 case wintype_TextBuffer:
298 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
300 write_utf8_to_window_buffer(str->window, utf8);
304 str->write_count += len;
307 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
310 /* Now write the same buffer to the window's echo stream */
311 if(str->window->echo_stream != NULL)
312 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
316 case STREAM_TYPE_MEMORY:
317 if(str->unicode && str->ubuffer)
319 int copycount = MIN(len, str->buflen - str->mark);
320 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
321 str->mark += copycount;
323 if(!str->unicode && str->buffer)
325 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
326 int copycount = MIN(len, str->buflen - str->mark);
327 memmove(str->buffer + str->mark, latin1, copycount);
329 str->mark += copycount;
332 str->write_count += len;
335 case STREAM_TYPE_FILE:
340 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
341 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
344 else /* Regular file */
346 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
347 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
351 else /* Text mode is the same for Unicode and regular files */
353 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
356 g_fprintf(str->file_pointer, "%s", utf8);
361 str->write_count += len;
364 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
369 * glk_put_char_stream:
370 * @str: An output stream.
371 * @ch: A character in Latin-1 encoding.
373 * The same as glk_put_char(), except that you specify a stream @str to print
374 * to, instead of using the current stream. It is illegal for @str to be %NULL,
375 * or an input-only stream.
378 glk_put_char_stream(strid_t str, unsigned char ch)
380 VALID_STREAM(str, return);
381 g_return_if_fail(str->file_mode != filemode_Read);
383 write_buffer_to_stream(str, (gchar *)&ch, 1);
387 * glk_put_char_stream_uni:
388 * @str: An output stream.
389 * @ch: A Unicode code point.
391 * The same as glk_put_char_uni(), except that you specify a stream @str to
392 * print to, instead of using the current stream. It is illegal for @str to be
393 * %NULL, or an input-only stream.
396 glk_put_char_stream_uni(strid_t str, glui32 ch)
398 VALID_STREAM(str, return);
399 g_return_if_fail(str->file_mode != filemode_Read);
401 write_buffer_to_stream_uni(str, &ch, 1);
405 * glk_put_string_stream:
406 * @str: An output stream.
407 * @s: A null-terminated string in Latin-1 encoding.
409 * The same as glk_put_string(), except that you specify a stream @str to print
410 * to, instead of using the current stream. It is illegal for @str to be %NULL,
411 * or an input-only stream.
414 glk_put_string_stream(strid_t str, char *s)
416 VALID_STREAM(str, return);
420 g_return_if_fail(str->file_mode != filemode_Read);
422 write_buffer_to_stream(str, s, strlen(s));
426 * glk_put_string_stream_uni:
427 * @str: An output stream.
428 * @s: A null-terminated array of Unicode code points.
430 * The same as glk_put_string_uni(), except that you specify a stream @str to
431 * print to, instead of using the current stream. It is illegal for @str to be
432 * %NULL, or an input-only stream.
435 glk_put_string_stream_uni(strid_t str, glui32 *s)
437 VALID_STREAM(str, return);
441 g_return_if_fail(str->file_mode != filemode_Read);
443 /* An impromptu strlen() for glui32 arrays */
448 write_buffer_to_stream_uni(str, s, len);
452 * glk_put_buffer_stream:
453 * @str: An output stream.
454 * @buf: An array of characters in Latin-1 encoding.
455 * @len: Length of @buf.
457 * The same as glk_put_buffer(), 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_buffer_stream(strid_t str, char *buf, glui32 len)
464 VALID_STREAM(str, return);
468 g_return_if_fail(str->file_mode != filemode_Read);
470 write_buffer_to_stream(str, buf, len);
474 * glk_put_buffer_stream_uni:
475 * @str: An output stream.
476 * @buf: An array of Unicode code points.
477 * @len: Length of @buf.
479 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
480 * print to, instead of using the current stream. It is illegal for @str to be
481 * %NULL, or an input-only stream.
484 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
486 VALID_STREAM(str, return);
490 g_return_if_fail(str->file_mode != filemode_Read);
492 write_buffer_to_stream_uni(str, buf, len);
497 **************** READING FUNCTIONS ********************************************
501 /* Internal function: Read one big-endian four-byte character from file fp and
502 return it as a Unicode code point, or -1 on EOF */
504 read_ucs4be_char_from_file(FILE *fp)
506 unsigned char readbuffer[4];
507 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
510 readbuffer[0] << 24 |
511 readbuffer[1] << 16 |
516 /* Internal function: Read one UTF-8 character, which may be more than one byte,
517 from file fp and return it as a Unicode code point, or -1 on EOF */
519 read_utf8_char_from_file(FILE *fp)
521 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
523 gunichar charresult = (gunichar)-2;
524 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
529 readbuffer[foo] = (gchar)ch;
530 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
531 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
534 /* Silently return unknown characters as 0xFFFD, Replacement Character */
535 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
540 /* Internal function: Tell whether this code point is a Unicode newline. The
541 file pointer and eight-bit flag are included in case the newline is a CR
542 (U+000D). If the next character is LF (U+000A) then it also belongs to the
545 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
547 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
550 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
551 read_ucs4be_char_from_file(fp);
553 if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
554 WARNING_S("Seek failed on stream", g_strerror(errno) );
560 /* Internal function: Read one character from a stream. Returns a value which
561 can be returned unchanged by glk_get_char_stream_uni(), but
562 glk_get_char_stream() must replace high values by the placeholder character. */
564 get_char_stream_common(strid_t str)
568 case STREAM_TYPE_MEMORY:
571 if(!str->ubuffer || str->mark >= str->buflen)
573 glui32 ch = str->ubuffer[str->mark++];
579 if(!str->buffer || str->mark >= str->buflen)
581 unsigned char ch = str->buffer[str->mark++];
587 case STREAM_TYPE_FILE:
592 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
598 else /* Regular file */
600 int ch = fgetc(str->file_pointer);
608 else /* Text mode is the same for Unicode and regular files */
610 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
618 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
624 * glk_get_char_stream:
625 * @str: An input stream.
627 * Reads one character from the stream @str. (There is no notion of a
628 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
629 * an output-only stream.
631 * The result will be between 0 and 255. As with all basic text functions, Glk
632 * assumes the Latin-1 encoding. See <link
633 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
634 * of the stream has been reached, the result will be -1.
637 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
638 * returned as negative numbers.
641 * If the stream contains Unicode data — for example, if it was created
642 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
643 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
645 * It is usually more efficient to read several characters at once with
646 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
647 * glk_get_char_stream() several times.
649 * Returns: A character value between 0 and 255, or -1 on end of stream.
652 glk_get_char_stream(strid_t str)
654 VALID_STREAM(str, return -1);
655 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
657 glsi32 ch = get_char_stream_common(str);
658 return (ch > 0xFF)? PLACEHOLDER : ch;
662 * glk_get_char_stream_uni:
663 * @str: An input stream.
665 * Reads one character from the stream @str. The result will be between 0 and
666 * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
668 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
671 glk_get_char_stream_uni(strid_t str)
673 VALID_STREAM(str, return -1);
674 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
676 return get_char_stream_common(str);
680 * glk_get_buffer_stream:
681 * @str: An input stream.
682 * @buf: A buffer with space for at least @len characters.
683 * @len: The number of characters to read.
685 * Reads @len characters from @str, unless the end of stream is reached first.
686 * No terminal null is placed in the buffer.
688 * Returns: The number of characters actually read.
691 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
693 VALID_STREAM(str, return 0);
694 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
695 g_return_val_if_fail(buf != NULL, 0);
699 case STREAM_TYPE_MEMORY:
704 while(copycount < len && str->ubuffer && str->mark < str->buflen)
706 glui32 ch = str->ubuffer[str->mark++];
707 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
712 if(str->buffer) /* if not, copycount stays 0 */
713 copycount = MIN(len, str->buflen - str->mark);
714 memmove(buf, str->buffer + str->mark, copycount);
715 str->mark += copycount;
718 str->read_count += copycount;
721 case STREAM_TYPE_FILE:
724 if(str->unicode) /* Binary file with 4-byte characters */
726 /* Read len characters of 4 bytes each */
727 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
728 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
729 /* If there was an incomplete character */
733 WARNING("Incomplete character in binary Unicode file");
737 for(foo = 0; foo < count; foo += 4)
739 glsi32 ch = readbuffer[foo] << 24
740 | readbuffer[foo + 1] << 16
741 | readbuffer[foo + 2] << 8
742 | readbuffer[foo + 3];
743 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
746 str->read_count += count / 4;
749 else /* Regular binary file */
751 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
752 str->read_count += count;
756 else /* Text mode is the same for Unicode and regular files */
758 /* Do it character-by-character */
760 for(foo = 0; foo < len; foo++)
762 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
766 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
771 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
777 * glk_get_buffer_stream_uni:
778 * @str: An input stream.
779 * @buf: A buffer with space for at least @len Unicode code points.
780 * @len: The number of characters to read.
782 * Reads @len Unicode characters from @str, unless the end of stream is reached
783 * first. No terminal null is placed in the buffer.
785 * Returns: The number of Unicode characters actually read.
788 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
790 VALID_STREAM(str, return 0);
791 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
792 g_return_val_if_fail(buf != NULL, 0);
796 case STREAM_TYPE_MEMORY:
801 if(str->ubuffer) /* if not, copycount stays 0 */
802 copycount = MIN(len, str->buflen - str->mark);
803 memmove(buf, str->ubuffer + str->mark, copycount * 4);
804 str->mark += copycount;
808 while(copycount < len && str->buffer && str->mark < str->buflen)
810 unsigned char ch = str->buffer[str->mark++];
811 buf[copycount++] = ch;
815 str->read_count += copycount;
818 case STREAM_TYPE_FILE:
821 if(str->unicode) /* Binary file with 4-byte characters */
823 /* Read len characters of 4 bytes each */
824 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
825 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
826 /* If there was an incomplete character */
830 WARNING("Incomplete character in binary Unicode file");
834 for(foo = 0; foo < count; foo += 4)
835 buf[foo / 4] = readbuffer[foo] << 24
836 | readbuffer[foo + 1] << 16
837 | readbuffer[foo + 2] << 8
838 | readbuffer[foo + 3];
840 str->read_count += count / 4;
843 else /* Regular binary file */
845 unsigned char *readbuffer = g_new0(unsigned char, len);
846 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
848 for(foo = 0; foo < count; foo++)
849 buf[foo] = readbuffer[foo];
851 str->read_count += count;
855 else /* Text mode is the same for Unicode and regular files */
857 /* Do it character-by-character */
859 for(foo = 0; foo < len; foo++)
861 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
870 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
876 * glk_get_line_stream:
877 * @str: An input stream.
878 * @buf: A buffer with space for at least @len characters.
879 * @len: The number of characters to read, plus one.
881 * Reads characters from @str, until either
883 * <alt>@len - 1</alt>
884 * <mathphrase>@len - 1</mathphrase>
886 * characters have been read or a newline has been read. It then puts a
887 * terminal null (<code>'\0'</code>) aracter on
888 * the end. It returns the number of characters actually read, including the
889 * newline (if there is one) but not including the terminal null.
891 * Returns: The number of characters actually read.
894 glk_get_line_stream(strid_t str, char *buf, glui32 len)
896 VALID_STREAM(str, return 0);
897 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
898 g_return_val_if_fail(buf != NULL, 0);
902 case STREAM_TYPE_MEMORY:
907 /* Do it character-by-character */
908 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
910 glui32 ch = str->ubuffer[str->mark++];
911 /* Check for Unicode newline; slightly different than
913 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
915 buf[copycount++] = '\n';
920 if(str->ubuffer[str->mark] == 0x0A)
921 str->mark++; /* skip past next newline */
922 buf[copycount++] = '\n';
925 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
927 buf[copycount] = '\0';
931 if(str->buffer) /* if not, copycount stays 0 */
932 copycount = MIN(len - 1, str->buflen - str->mark);
933 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
934 if(endptr) /* newline was found */
935 copycount = endptr - buf; /* Real copy count */
936 buf[copycount] = '\0';
937 str->mark += copycount;
940 str->read_count += copycount;
943 case STREAM_TYPE_FILE:
946 if(str->unicode) /* Binary file with 4-byte characters */
948 /* Do it character-by-character */
950 for(foo = 0; foo < len - 1; foo++)
952 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
959 if(is_unicode_newline(ch, str->file_pointer, FALSE))
965 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
970 else /* Regular binary file */
972 if( !fgets(buf, len, str->file_pointer) ) {
977 int nread = strlen(buf);
978 str->read_count += nread;
982 else /* Text mode is the same for Unicode and regular files */
984 /* Do it character-by-character */
986 for(foo = 0; foo < len - 1; foo++)
988 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
995 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1001 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
1007 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1013 * glk_get_line_stream_uni:
1014 * @str: An input stream.
1015 * @buf: A buffer with space for at least @len Unicode code points.
1016 * @len: The number of characters to read, plus one.
1018 * Reads Unicode characters from @str, until either
1020 * <alt>@len - 1</alt>
1021 * <mathphrase>@len - 1</mathphrase>
1023 * Unicode characters have been read or a newline has been read. It then puts a
1024 * terminal null (a zero value) on the end.
1026 * Returns: The number of characters actually read, including the newline (if
1027 * there is one) but not including the terminal null.
1030 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
1032 VALID_STREAM(str, return 0);
1033 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
1034 g_return_val_if_fail(buf != NULL, 0);
1038 case STREAM_TYPE_MEMORY:
1043 /* Do it character-by-character */
1044 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
1046 glui32 ch = str->ubuffer[str->mark++];
1047 /* Check for Unicode newline; slightly different than
1049 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
1051 buf[copycount++] = '\n';
1056 if(str->ubuffer[str->mark] == 0x0A)
1057 str->mark++; /* skip past next newline */
1058 buf[copycount++] = '\n';
1061 buf[copycount++] = ch;
1063 buf[copycount] = '\0';
1067 /* No recourse to memccpy(), so do it character-by-character */
1068 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
1070 gchar ch = str->buffer[str->mark++];
1071 /* Check for newline */
1072 if(ch == '\n') /* Also check for \r and \r\n? */
1074 buf[copycount++] = '\n';
1077 buf[copycount++] = (unsigned char)ch;
1082 str->read_count += copycount;
1085 case STREAM_TYPE_FILE:
1088 if(str->unicode) /* Binary file with 4-byte characters */
1090 /* Do it character-by-character */
1092 for(foo = 0; foo < len - 1; foo++)
1094 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1101 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1103 buf[foo] = ch; /* Preserve newline types??? */
1112 else /* Regular binary file */
1114 gchar *readbuffer = g_new0(gchar, len);
1115 if( !fgets(readbuffer, len, str->file_pointer) ) {
1120 glui32 count = strlen(readbuffer);
1122 for(foo = 0; foo < count + 1; foo++) /* Copy terminator */
1123 buf[foo] = (unsigned char)(readbuffer[foo]);
1124 str->read_count += count;
1128 else /* Text mode is the same for Unicode and regular files */
1130 /* Do it character-by-character */
1132 for(foo = 0; foo < len - 1; foo++)
1134 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1141 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1143 buf[foo] = ch; /* Preserve newline types??? */
1153 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1160 **************** SEEKING FUNCTIONS ********************************************
1165 * glk_stream_get_position:
1166 * @str: A file or memory stream.
1168 * Returns the position of the read/write mark in @str. For memory streams and
1169 * binary file streams, this is exactly the number of characters read or written
1170 * from the beginning of the stream (unless you have moved the mark with
1171 * glk_stream_set_position().) For text file streams, matters are more
1172 * ambiguous, since (for example) writing one byte to a text file may store more
1173 * than one character in the platform's native encoding. You can only be sure
1174 * that the position increases as you read or write to the file.
1176 * Additional complication: for Latin-1 memory and file streams, a character is
1177 * a byte. For Unicode memory and file streams (those created by
1178 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1179 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1183 * If this bothers you, don't use binary Unicode files. I don't think they're
1184 * good for much anyhow.
1187 * Returns: position of the read/write mark in @str.
1190 glk_stream_get_position(strid_t str)
1192 VALID_STREAM(str, return 0);
1196 case STREAM_TYPE_MEMORY:
1198 case STREAM_TYPE_FILE:
1199 return ftell(str->file_pointer);
1201 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1207 * glk_stream_set_position:
1208 * @str: A file or memory stream.
1209 * @pos: The position to set the mark to, relative to @seekmode.
1210 * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1212 * Sets the position of the read/write mark in @str. The position is controlled
1213 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1214 * <code>seekmode_</code> constants below.
1216 * It is illegal to specify a position before the beginning or after the end of
1219 * In binary files, the mark position is exact — it corresponds with the
1220 * number of characters you have read or written. In text files, this mapping
1221 * can vary, because of linefeed conventions or other character-set
1222 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1223 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1224 * the platform's native encoding — after character cookery. Therefore,
1225 * in a text stream, it is safest to use glk_stream_set_position() only to move
1226 * to the beginning or end of a file, or to a position determined by
1227 * glk_stream_get_position().
1229 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1230 * characters are 32-bit words, or four bytes each.
1233 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1235 VALID_STREAM(str, return);
1236 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1237 g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1241 case STREAM_TYPE_MEMORY:
1244 case seekmode_Start: str->mark = pos; break;
1245 case seekmode_Current: str->mark += pos; break;
1246 case seekmode_End: str->mark = str->buflen + pos; break;
1248 g_return_if_reached();
1252 case STREAM_TYPE_FILE:
1257 case seekmode_Start: whence = SEEK_SET; break;
1258 case seekmode_Current: whence = SEEK_CUR; break;
1259 case seekmode_End: whence = SEEK_END; break;
1261 g_return_if_reached();
1264 if(fseek(str->file_pointer, pos, whence) == -1)
1265 WARNING("Seek failed on file stream");
1269 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);