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);
84 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
86 g_signal_emit_by_name(glk, "text-buffer-output", win->rock, win->buffer->str);
90 case wintype_TextGrid:
92 /* Number of characters to insert */
93 glong length = win->buffer->len;
94 glong chars_left = length;
96 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
98 /* Get cursor position */
99 GtkTextIter start, insert;
102 gtk_text_buffer_get_iter_at_mark(buffer, &insert, cursor);
103 /* Spaces available on this line */
104 gint available_space = win->width - gtk_text_iter_get_line_offset(&insert);
106 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
107 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
108 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
109 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
110 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
112 while(chars_left > available_space && !gtk_text_iter_is_end(&insert))
114 GtkTextIter end = insert;
115 gtk_text_iter_forward_to_line_end(&end);
116 gtk_text_buffer_delete(buffer, &insert, &end);
118 start_offset = gtk_text_iter_get_offset(&insert);
119 gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), available_space);
120 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
123 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
125 // Player's style overrides
126 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
128 // GLK Program's style overrides
129 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
131 // Link style overrides
132 if(win->window_stream->hyperlink_mode) {
133 GtkTextTag *link_tag = win->current_hyperlink->tag;
134 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
135 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
138 // GLK Program's style overrides using garglk_set_zcolors()
139 if(win->zcolor != NULL)
140 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
142 chars_left -= available_space;
143 gtk_text_iter_forward_line(&insert);
144 available_space = win->width;
146 if(!gtk_text_iter_is_end(&insert))
148 GtkTextIter end = insert;
149 gtk_text_iter_forward_chars(&end, chars_left);
150 gtk_text_buffer_delete(buffer, &insert, &end);
152 start_offset = gtk_text_iter_get_offset(&insert);
153 gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), -1);
154 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
157 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
159 // Player's style overrides
160 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
162 // GLK Program's style overrides
163 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
165 // Link style overrides
166 if(win->window_stream->hyperlink_mode) {
167 GtkTextTag *link_tag = win->current_hyperlink->tag;
168 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
169 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
172 // GLK Program's style overrides using garglk_set_zcolors()
173 if(win->zcolor != NULL)
174 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
177 gtk_text_buffer_move_mark(buffer, cursor, &start);
184 g_string_truncate(win->buffer, 0);
187 /* Internal function: write a Latin-1 buffer with length to a stream. */
189 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
193 case STREAM_TYPE_WINDOW:
194 /* Each window type has a different way of printing to it */
195 switch(str->window->type)
197 /* Printing to these windows' streams does nothing */
200 case wintype_Graphics:
201 str->write_count += len;
204 /* Text grid/buffer windows */
205 case wintype_TextGrid:
206 case wintype_TextBuffer:
208 gchar *utf8 = convert_latin1_to_utf8(buf, len);
210 write_utf8_to_window_buffer(str->window, utf8);
214 str->write_count += len;
217 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
220 /* Now write the same buffer to the window's echo stream */
221 if(str->window->echo_stream != NULL)
222 write_buffer_to_stream(str->window->echo_stream, buf, len);
226 case STREAM_TYPE_MEMORY:
227 if(str->unicode && str->ubuffer)
230 while(str->mark < str->buflen && foo < len)
231 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
233 if(!str->unicode && str->buffer)
235 int copycount = MIN(len, str->buflen - str->mark);
236 memmove(str->buffer + str->mark, buf, copycount);
237 str->mark += copycount;
240 str->write_count += len;
243 case STREAM_TYPE_FILE:
248 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
249 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
252 else /* Regular file */
254 fwrite(buf, sizeof(gchar), len, str->file_pointer);
257 else /* Text mode is the same for Unicode and regular files */
259 gchar *utf8 = convert_latin1_to_utf8(buf, len);
262 g_fprintf(str->file_pointer, "%s", utf8);
267 str->write_count += len;
270 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
274 /* Internal function: write a Unicode buffer with length to a stream. */
276 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
280 case STREAM_TYPE_WINDOW:
281 /* Each window type has a different way of printing to it */
282 switch(str->window->type)
284 /* Printing to these windows' streams does nothing */
287 case wintype_Graphics:
288 str->write_count += len;
291 /* Text grid/buffer windows */
292 case wintype_TextGrid:
293 case wintype_TextBuffer:
295 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
297 write_utf8_to_window_buffer(str->window, utf8);
301 str->write_count += len;
304 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
307 /* Now write the same buffer to the window's echo stream */
308 if(str->window->echo_stream != NULL)
309 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
313 case STREAM_TYPE_MEMORY:
314 if(str->unicode && str->ubuffer)
316 int copycount = MIN(len, str->buflen - str->mark);
317 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
318 str->mark += copycount;
320 if(!str->unicode && str->buffer)
322 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
323 int copycount = MIN(len, str->buflen - str->mark);
324 memmove(str->buffer + str->mark, latin1, copycount);
326 str->mark += copycount;
329 str->write_count += len;
332 case STREAM_TYPE_FILE:
337 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
338 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
341 else /* Regular file */
343 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
344 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
348 else /* Text mode is the same for Unicode and regular files */
350 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
353 g_fprintf(str->file_pointer, "%s", utf8);
358 str->write_count += len;
361 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
366 * glk_put_char_stream:
367 * @str: An output stream.
368 * @ch: A character in Latin-1 encoding.
370 * The same as glk_put_char(), except that you specify a stream @str to print
371 * to, instead of using the current stream. It is illegal for @str to be %NULL,
372 * or an input-only stream.
375 glk_put_char_stream(strid_t str, unsigned char ch)
377 VALID_STREAM(str, return);
378 g_return_if_fail(str->file_mode != filemode_Read);
380 write_buffer_to_stream(str, (gchar *)&ch, 1);
384 * glk_put_char_stream_uni:
385 * @str: An output stream.
386 * @ch: A Unicode code point.
388 * The same as glk_put_char_uni(), except that you specify a stream @str to
389 * print to, instead of using the current stream. It is illegal for @str to be
390 * %NULL, or an input-only stream.
393 glk_put_char_stream_uni(strid_t str, glui32 ch)
395 VALID_STREAM(str, return);
396 g_return_if_fail(str->file_mode != filemode_Read);
398 write_buffer_to_stream_uni(str, &ch, 1);
402 * glk_put_string_stream:
403 * @str: An output stream.
404 * @s: A null-terminated string in Latin-1 encoding.
406 * The same as glk_put_string(), except that you specify a stream @str to print
407 * to, instead of using the current stream. It is illegal for @str to be %NULL,
408 * or an input-only stream.
411 glk_put_string_stream(strid_t str, char *s)
413 VALID_STREAM(str, return);
417 g_return_if_fail(str->file_mode != filemode_Read);
419 write_buffer_to_stream(str, s, strlen(s));
423 * glk_put_string_stream_uni:
424 * @str: An output stream.
425 * @s: A null-terminated array of Unicode code points.
427 * The same as glk_put_string_uni(), except that you specify a stream @str to
428 * print to, instead of using the current stream. It is illegal for @str to be
429 * %NULL, or an input-only stream.
432 glk_put_string_stream_uni(strid_t str, glui32 *s)
434 VALID_STREAM(str, return);
438 g_return_if_fail(str->file_mode != filemode_Read);
440 /* An impromptu strlen() for glui32 arrays */
445 write_buffer_to_stream_uni(str, s, len);
449 * glk_put_buffer_stream:
450 * @str: An output stream.
451 * @buf: An array of characters in Latin-1 encoding.
452 * @len: Length of @buf.
454 * The same as glk_put_buffer(), except that you specify a stream @str to print
455 * to, instead of using the current stream. It is illegal for @str to be %NULL,
456 * or an input-only stream.
459 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
461 VALID_STREAM(str, return);
465 g_return_if_fail(str->file_mode != filemode_Read);
467 write_buffer_to_stream(str, buf, len);
471 * glk_put_buffer_stream_uni:
472 * @str: An output stream.
473 * @buf: An array of Unicode code points.
474 * @len: Length of @buf.
476 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
477 * print to, instead of using the current stream. It is illegal for @str to be
478 * %NULL, or an input-only stream.
481 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
483 VALID_STREAM(str, return);
487 g_return_if_fail(str->file_mode != filemode_Read);
489 write_buffer_to_stream_uni(str, buf, len);
494 **************** READING FUNCTIONS ********************************************
498 /* Internal function: Read one big-endian four-byte character from file fp and
499 return it as a Unicode code point, or -1 on EOF */
501 read_ucs4be_char_from_file(FILE *fp)
503 unsigned char readbuffer[4];
504 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
507 readbuffer[0] << 24 |
508 readbuffer[1] << 16 |
513 /* Internal function: Read one UTF-8 character, which may be more than one byte,
514 from file fp and return it as a Unicode code point, or -1 on EOF */
516 read_utf8_char_from_file(FILE *fp)
518 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
520 gunichar charresult = (gunichar)-2;
521 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
526 readbuffer[foo] = (gchar)ch;
527 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
528 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
531 /* Silently return unknown characters as 0xFFFD, Replacement Character */
532 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
537 /* Internal function: Tell whether this code point is a Unicode newline. The
538 file pointer and eight-bit flag are included in case the newline is a CR
539 (U+000D). If the next character is LF (U+000A) then it also belongs to the
542 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
544 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
547 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
548 read_ucs4be_char_from_file(fp);
550 if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
551 WARNING_S("Seek failed on stream", g_strerror(errno) );
557 /* Internal function: Read one character from a stream. Returns a value which
558 can be returned unchanged by glk_get_char_stream_uni(), but
559 glk_get_char_stream() must replace high values by the placeholder character. */
561 get_char_stream_common(strid_t str)
565 case STREAM_TYPE_MEMORY:
568 if(!str->ubuffer || str->mark >= str->buflen)
570 glui32 ch = str->ubuffer[str->mark++];
576 if(!str->buffer || str->mark >= str->buflen)
578 unsigned char ch = str->buffer[str->mark++];
584 case STREAM_TYPE_FILE:
589 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
595 else /* Regular file */
597 int ch = fgetc(str->file_pointer);
605 else /* Text mode is the same for Unicode and regular files */
607 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
615 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
621 * glk_get_char_stream:
622 * @str: An input stream.
624 * Reads one character from the stream @str. (There is no notion of a
625 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
626 * an output-only stream.
628 * The result will be between 0 and 255. As with all basic text functions, Glk
629 * assumes the Latin-1 encoding. See <link
630 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
631 * of the stream has been reached, the result will be -1.
634 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
635 * returned as negative numbers.
638 * If the stream contains Unicode data — for example, if it was created
639 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
640 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
642 * It is usually more efficient to read several characters at once with
643 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
644 * glk_get_char_stream() several times.
646 * Returns: A character value between 0 and 255, or -1 on end of stream.
649 glk_get_char_stream(strid_t str)
651 VALID_STREAM(str, return -1);
652 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
654 glsi32 ch = get_char_stream_common(str);
655 return (ch > 0xFF)? PLACEHOLDER : ch;
659 * glk_get_char_stream_uni:
660 * @str: An input stream.
662 * Reads one character from the stream @str. The result will be between 0 and
663 * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
665 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
668 glk_get_char_stream_uni(strid_t str)
670 VALID_STREAM(str, return -1);
671 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
673 return get_char_stream_common(str);
677 * glk_get_buffer_stream:
678 * @str: An input stream.
679 * @buf: A buffer with space for at least @len characters.
680 * @len: The number of characters to read.
682 * Reads @len characters from @str, unless the end of stream is reached first.
683 * No terminal null is placed in the buffer.
685 * Returns: The number of characters actually read.
688 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
690 VALID_STREAM(str, return 0);
691 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
692 g_return_val_if_fail(buf != NULL, 0);
696 case STREAM_TYPE_MEMORY:
701 while(copycount < len && str->ubuffer && str->mark < str->buflen)
703 glui32 ch = str->ubuffer[str->mark++];
704 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
709 if(str->buffer) /* if not, copycount stays 0 */
710 copycount = MIN(len, str->buflen - str->mark);
711 memmove(buf, str->buffer + str->mark, copycount);
712 str->mark += copycount;
715 str->read_count += copycount;
718 case STREAM_TYPE_FILE:
721 if(str->unicode) /* Binary file with 4-byte characters */
723 /* Read len characters of 4 bytes each */
724 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
725 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
726 /* If there was an incomplete character */
730 WARNING("Incomplete character in binary Unicode file");
734 for(foo = 0; foo < count; foo += 4)
736 glsi32 ch = readbuffer[foo] << 24
737 | readbuffer[foo + 1] << 16
738 | readbuffer[foo + 2] << 8
739 | readbuffer[foo + 3];
740 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
743 str->read_count += count / 4;
746 else /* Regular binary file */
748 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
749 str->read_count += count;
753 else /* Text mode is the same for Unicode and regular files */
755 /* Do it character-by-character */
757 for(foo = 0; foo < len; foo++)
759 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
763 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
768 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
774 * glk_get_buffer_stream_uni:
775 * @str: An input stream.
776 * @buf: A buffer with space for at least @len Unicode code points.
777 * @len: The number of characters to read.
779 * Reads @len Unicode characters from @str, unless the end of stream is reached
780 * first. No terminal null is placed in the buffer.
782 * Returns: The number of Unicode characters actually read.
785 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
787 VALID_STREAM(str, return 0);
788 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
789 g_return_val_if_fail(buf != NULL, 0);
793 case STREAM_TYPE_MEMORY:
798 if(str->ubuffer) /* if not, copycount stays 0 */
799 copycount = MIN(len, str->buflen - str->mark);
800 memmove(buf, str->ubuffer + str->mark, copycount * 4);
801 str->mark += copycount;
805 while(copycount < len && str->buffer && str->mark < str->buflen)
807 unsigned char ch = str->buffer[str->mark++];
808 buf[copycount++] = ch;
812 str->read_count += copycount;
815 case STREAM_TYPE_FILE:
818 if(str->unicode) /* Binary file with 4-byte characters */
820 /* Read len characters of 4 bytes each */
821 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
822 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
823 /* If there was an incomplete character */
827 WARNING("Incomplete character in binary Unicode file");
831 for(foo = 0; foo < count; foo += 4)
832 buf[foo / 4] = readbuffer[foo] << 24
833 | readbuffer[foo + 1] << 16
834 | readbuffer[foo + 2] << 8
835 | readbuffer[foo + 3];
837 str->read_count += count / 4;
840 else /* Regular binary file */
842 unsigned char *readbuffer = g_new0(unsigned char, len);
843 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
845 for(foo = 0; foo < count; foo++)
846 buf[foo] = readbuffer[foo];
848 str->read_count += count;
852 else /* Text mode is the same for Unicode and regular files */
854 /* Do it character-by-character */
856 for(foo = 0; foo < len; foo++)
858 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
867 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
873 * glk_get_line_stream:
874 * @str: An input stream.
875 * @buf: A buffer with space for at least @len characters.
876 * @len: The number of characters to read, plus one.
878 * Reads characters from @str, until either
880 * <alt>@len - 1</alt>
881 * <mathphrase>@len - 1</mathphrase>
883 * characters have been read or a newline has been read. It then puts a
884 * terminal null (<code>'\0'</code>) aracter on
885 * the end. It returns the number of characters actually read, including the
886 * newline (if there is one) but not including the terminal null.
888 * Returns: The number of characters actually read.
891 glk_get_line_stream(strid_t str, char *buf, glui32 len)
893 VALID_STREAM(str, return 0);
894 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
895 g_return_val_if_fail(buf != NULL, 0);
899 case STREAM_TYPE_MEMORY:
904 /* Do it character-by-character */
905 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
907 glui32 ch = str->ubuffer[str->mark++];
908 /* Check for Unicode newline; slightly different than
910 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
912 buf[copycount++] = '\n';
917 if(str->ubuffer[str->mark] == 0x0A)
918 str->mark++; /* skip past next newline */
919 buf[copycount++] = '\n';
922 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
924 buf[copycount] = '\0';
928 if(str->buffer) /* if not, copycount stays 0 */
929 copycount = MIN(len - 1, str->buflen - str->mark);
930 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
931 if(endptr) /* newline was found */
932 copycount = endptr - buf; /* Real copy count */
933 buf[copycount] = '\0';
934 str->mark += copycount;
937 str->read_count += copycount;
940 case STREAM_TYPE_FILE:
943 if(str->unicode) /* Binary file with 4-byte characters */
945 /* Do it character-by-character */
947 for(foo = 0; foo < len - 1; foo++)
949 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
956 if(is_unicode_newline(ch, str->file_pointer, FALSE))
962 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
967 else /* Regular binary file */
969 if( !fgets(buf, len, str->file_pointer) ) {
974 int nread = strlen(buf);
975 str->read_count += nread;
979 else /* Text mode is the same for Unicode and regular files */
981 /* Do it character-by-character */
983 for(foo = 0; foo < len - 1; foo++)
985 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
992 if(is_unicode_newline(ch, str->file_pointer, TRUE))
998 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
1004 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1010 * glk_get_line_stream_uni:
1011 * @str: An input stream.
1012 * @buf: A buffer with space for at least @len Unicode code points.
1013 * @len: The number of characters to read, plus one.
1015 * Reads Unicode characters from @str, until either
1017 * <alt>@len - 1</alt>
1018 * <mathphrase>@len - 1</mathphrase>
1020 * Unicode characters have been read or a newline has been read. It then puts a
1021 * terminal null (a zero value) on the end.
1023 * Returns: The number of characters actually read, including the newline (if
1024 * there is one) but not including the terminal null.
1027 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
1029 VALID_STREAM(str, return 0);
1030 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
1031 g_return_val_if_fail(buf != NULL, 0);
1035 case STREAM_TYPE_MEMORY:
1040 /* Do it character-by-character */
1041 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
1043 glui32 ch = str->ubuffer[str->mark++];
1044 /* Check for Unicode newline; slightly different than
1046 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
1048 buf[copycount++] = '\n';
1053 if(str->ubuffer[str->mark] == 0x0A)
1054 str->mark++; /* skip past next newline */
1055 buf[copycount++] = '\n';
1058 buf[copycount++] = ch;
1060 buf[copycount] = '\0';
1064 /* No recourse to memccpy(), so do it character-by-character */
1065 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
1067 gchar ch = str->buffer[str->mark++];
1068 /* Check for newline */
1069 if(ch == '\n') /* Also check for \r and \r\n? */
1071 buf[copycount++] = '\n';
1074 buf[copycount++] = (unsigned char)ch;
1079 str->read_count += copycount;
1082 case STREAM_TYPE_FILE:
1085 if(str->unicode) /* Binary file with 4-byte characters */
1087 /* Do it character-by-character */
1089 for(foo = 0; foo < len - 1; foo++)
1091 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1098 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1100 buf[foo] = ch; /* Preserve newline types??? */
1109 else /* Regular binary file */
1111 gchar *readbuffer = g_new0(gchar, len);
1112 if( !fgets(readbuffer, len, str->file_pointer) ) {
1117 glui32 count = strlen(readbuffer);
1119 for(foo = 0; foo < count + 1; foo++) /* Copy terminator */
1120 buf[foo] = (unsigned char)(readbuffer[foo]);
1121 str->read_count += count;
1125 else /* Text mode is the same for Unicode and regular files */
1127 /* Do it character-by-character */
1129 for(foo = 0; foo < len - 1; foo++)
1131 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1138 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1140 buf[foo] = ch; /* Preserve newline types??? */
1150 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1157 **************** SEEKING FUNCTIONS ********************************************
1162 * glk_stream_get_position:
1163 * @str: A file or memory stream.
1165 * Returns the position of the read/write mark in @str. For memory streams and
1166 * binary file streams, this is exactly the number of characters read or written
1167 * from the beginning of the stream (unless you have moved the mark with
1168 * glk_stream_set_position().) For text file streams, matters are more
1169 * ambiguous, since (for example) writing one byte to a text file may store more
1170 * than one character in the platform's native encoding. You can only be sure
1171 * that the position increases as you read or write to the file.
1173 * Additional complication: for Latin-1 memory and file streams, a character is
1174 * a byte. For Unicode memory and file streams (those created by
1175 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1176 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1180 * If this bothers you, don't use binary Unicode files. I don't think they're
1181 * good for much anyhow.
1184 * Returns: position of the read/write mark in @str.
1187 glk_stream_get_position(strid_t str)
1189 VALID_STREAM(str, return 0);
1193 case STREAM_TYPE_MEMORY:
1195 case STREAM_TYPE_FILE:
1196 return ftell(str->file_pointer);
1198 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1204 * glk_stream_set_position:
1205 * @str: A file or memory stream.
1206 * @pos: The position to set the mark to, relative to @seekmode.
1207 * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1209 * Sets the position of the read/write mark in @str. The position is controlled
1210 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1211 * <code>seekmode_</code> constants below.
1213 * It is illegal to specify a position before the beginning or after the end of
1216 * In binary files, the mark position is exact — it corresponds with the
1217 * number of characters you have read or written. In text files, this mapping
1218 * can vary, because of linefeed conventions or other character-set
1219 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1220 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1221 * the platform's native encoding — after character cookery. Therefore,
1222 * in a text stream, it is safest to use glk_stream_set_position() only to move
1223 * to the beginning or end of a file, or to a position determined by
1224 * glk_stream_get_position().
1226 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1227 * characters are 32-bit words, or four bytes each.
1230 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1232 VALID_STREAM(str, return);
1233 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1234 g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1238 case STREAM_TYPE_MEMORY:
1241 case seekmode_Start: str->mark = pos; break;
1242 case seekmode_Current: str->mark += pos; break;
1243 case seekmode_End: str->mark = str->buflen + pos; break;
1245 g_return_if_reached();
1249 case STREAM_TYPE_FILE:
1254 case seekmode_Start: whence = SEEK_SET; break;
1255 case seekmode_Current: whence = SEEK_CUR; break;
1256 case seekmode_End: whence = SEEK_END; break;
1258 g_return_if_reached();
1261 if(fseek(str->file_pointer, pos, whence) == -1)
1262 WARNING("Seek failed on file stream");
1266 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);