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)
35 g_printf("%s\n", win->buffer->str);
37 if(win->type != wintype_TextBuffer && win->type != wintype_TextGrid)
40 if(win->buffer->len == 0)
45 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
48 case wintype_TextBuffer:
50 GtkTextIter start, end;
51 gtk_text_buffer_get_end_iter(buffer, &end);
54 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
56 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
57 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
58 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
60 start_offset = gtk_text_iter_get_offset(&end);
61 gtk_text_buffer_insert(buffer, &end, win->buffer->str, -1);
62 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
65 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &end);
67 // Player's style overrides
68 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &end);
70 // GLK Program's style overrides
71 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &end);
73 // Link style overrides
74 if(win->window_stream->hyperlink_mode) {
75 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
76 GtkTextTag *link_tag = win->current_hyperlink->tag;
77 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &end);
78 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &end);
81 // GLK Program's style overrides using garglk_set_zcolors()
82 if(win->zcolor != NULL) {
83 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &end);
86 // GLK Program's style overrides using garglk_set_reversevideo()
87 if(win->zcolor_reversed != NULL) {
88 gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &end);
91 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
93 g_signal_emit_by_name(glk, "text-buffer-output", win->rock, win->buffer->str);
97 case wintype_TextGrid:
99 /* Number of characters to insert */
100 glong length = win->buffer->len;
101 glong chars_left = length;
103 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
105 /* Get cursor position */
106 GtkTextIter start, insert;
109 gtk_text_buffer_get_iter_at_mark(buffer, &insert, cursor);
110 /* Spaces available on this line */
111 gint available_space = win->width - gtk_text_iter_get_line_offset(&insert);
113 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
114 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
115 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
116 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
117 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
119 while(chars_left > available_space && !gtk_text_iter_is_end(&insert))
121 GtkTextIter end = insert;
122 gtk_text_iter_forward_to_line_end(&end);
123 gtk_text_buffer_delete(buffer, &insert, &end);
125 start_offset = gtk_text_iter_get_offset(&insert);
126 gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), available_space);
127 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
130 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
132 // Player's style overrides
133 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
135 // GLK Program's style overrides
136 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
138 // Link style overrides
139 if(win->window_stream->hyperlink_mode) {
140 GtkTextTag *link_tag = win->current_hyperlink->tag;
141 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
142 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
145 // GLK Program's style overrides using garglk_set_zcolors()
146 if(win->zcolor != NULL)
147 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
149 // GLK Program's style overrides using garglk_set_reversevideo()
150 if(win->zcolor_reversed != NULL) {
151 gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &insert);
154 chars_left -= available_space;
155 gtk_text_iter_forward_line(&insert);
156 available_space = win->width;
158 if(!gtk_text_iter_is_end(&insert))
160 GtkTextIter end = insert;
161 gtk_text_iter_forward_chars(&end, chars_left);
162 gtk_text_buffer_delete(buffer, &insert, &end);
164 start_offset = gtk_text_iter_get_offset(&insert);
165 gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), -1);
166 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
169 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
171 // Player's style overrides
172 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
174 // GLK Program's style overrides
175 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
177 // Link style overrides
178 if(win->window_stream->hyperlink_mode) {
179 GtkTextTag *link_tag = win->current_hyperlink->tag;
180 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
181 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
184 // GLK Program's style overrides using garglk_set_zcolors()
185 if(win->zcolor != NULL)
186 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
188 // GLK Program's style overrides using garglk_set_reversevideo()
189 if(win->zcolor_reversed != NULL) {
190 gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &insert);
194 gtk_text_buffer_move_mark(buffer, cursor, &start);
201 g_string_truncate(win->buffer, 0);
204 /* Internal function: write a Latin-1 buffer with length to a stream. */
206 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
210 case STREAM_TYPE_WINDOW:
211 /* Each window type has a different way of printing to it */
212 switch(str->window->type)
214 /* Printing to these windows' streams does nothing */
217 case wintype_Graphics:
218 str->write_count += len;
221 /* Text grid/buffer windows */
222 case wintype_TextGrid:
223 case wintype_TextBuffer:
225 gchar *utf8 = convert_latin1_to_utf8(buf, len);
227 write_utf8_to_window_buffer(str->window, utf8);
231 str->write_count += len;
234 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
237 /* Now write the same buffer to the window's echo stream */
238 if(str->window->echo_stream != NULL)
239 write_buffer_to_stream(str->window->echo_stream, buf, len);
243 case STREAM_TYPE_MEMORY:
244 if(str->unicode && str->ubuffer)
247 while(str->mark < str->buflen && foo < len)
248 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
250 if(!str->unicode && str->buffer)
252 int copycount = MIN(len, str->buflen - str->mark);
253 memmove(str->buffer + str->mark, buf, copycount);
254 str->mark += copycount;
257 str->write_count += len;
260 case STREAM_TYPE_FILE:
265 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
266 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
269 else /* Regular file */
271 fwrite(buf, sizeof(gchar), len, str->file_pointer);
274 else /* Text mode is the same for Unicode and regular files */
276 gchar *utf8 = convert_latin1_to_utf8(buf, len);
279 g_fprintf(str->file_pointer, "%s", utf8);
284 str->write_count += len;
287 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
291 /* Internal function: write a Unicode buffer with length to a stream. */
293 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
297 case STREAM_TYPE_WINDOW:
298 /* Each window type has a different way of printing to it */
299 switch(str->window->type)
301 /* Printing to these windows' streams does nothing */
304 case wintype_Graphics:
305 str->write_count += len;
308 /* Text grid/buffer windows */
309 case wintype_TextGrid:
310 case wintype_TextBuffer:
312 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
314 write_utf8_to_window_buffer(str->window, utf8);
318 str->write_count += len;
321 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
324 /* Now write the same buffer to the window's echo stream */
325 if(str->window->echo_stream != NULL)
326 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
330 case STREAM_TYPE_MEMORY:
331 if(str->unicode && str->ubuffer)
333 int copycount = MIN(len, str->buflen - str->mark);
334 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
335 str->mark += copycount;
337 if(!str->unicode && str->buffer)
339 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
340 int copycount = MIN(len, str->buflen - str->mark);
341 memmove(str->buffer + str->mark, latin1, copycount);
343 str->mark += copycount;
346 str->write_count += len;
349 case STREAM_TYPE_FILE:
354 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
355 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
358 else /* Regular file */
360 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
361 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
365 else /* Text mode is the same for Unicode and regular files */
367 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
370 g_fprintf(str->file_pointer, "%s", utf8);
375 str->write_count += len;
378 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
383 * glk_put_char_stream:
384 * @str: An output stream.
385 * @ch: A character in Latin-1 encoding.
387 * The same as glk_put_char(), except that you specify a stream @str to print
388 * to, instead of using the current stream. It is illegal for @str to be %NULL,
389 * or an input-only stream.
392 glk_put_char_stream(strid_t str, unsigned char ch)
394 VALID_STREAM(str, return);
395 g_return_if_fail(str->file_mode != filemode_Read);
397 write_buffer_to_stream(str, (gchar *)&ch, 1);
401 * glk_put_char_stream_uni:
402 * @str: An output stream.
403 * @ch: A Unicode code point.
405 * The same as glk_put_char_uni(), except that you specify a stream @str to
406 * print to, instead of using the current stream. It is illegal for @str to be
407 * %NULL, or an input-only stream.
410 glk_put_char_stream_uni(strid_t str, glui32 ch)
412 VALID_STREAM(str, return);
413 g_return_if_fail(str->file_mode != filemode_Read);
415 write_buffer_to_stream_uni(str, &ch, 1);
419 * glk_put_string_stream:
420 * @str: An output stream.
421 * @s: A null-terminated string in Latin-1 encoding.
423 * The same as glk_put_string(), except that you specify a stream @str to print
424 * to, instead of using the current stream. It is illegal for @str to be %NULL,
425 * or an input-only stream.
428 glk_put_string_stream(strid_t str, char *s)
430 VALID_STREAM(str, return);
434 g_return_if_fail(str->file_mode != filemode_Read);
436 write_buffer_to_stream(str, s, strlen(s));
440 * glk_put_string_stream_uni:
441 * @str: An output stream.
442 * @s: A null-terminated array of Unicode code points.
444 * The same as glk_put_string_uni(), except that you specify a stream @str to
445 * print to, instead of using the current stream. It is illegal for @str to be
446 * %NULL, or an input-only stream.
449 glk_put_string_stream_uni(strid_t str, glui32 *s)
451 VALID_STREAM(str, return);
455 g_return_if_fail(str->file_mode != filemode_Read);
457 /* An impromptu strlen() for glui32 arrays */
462 write_buffer_to_stream_uni(str, s, len);
466 * glk_put_buffer_stream:
467 * @str: An output stream.
468 * @buf: An array of characters in Latin-1 encoding.
469 * @len: Length of @buf.
471 * The same as glk_put_buffer(), except that you specify a stream @str to print
472 * to, instead of using the current stream. It is illegal for @str to be %NULL,
473 * or an input-only stream.
476 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
478 VALID_STREAM(str, return);
482 g_return_if_fail(str->file_mode != filemode_Read);
484 write_buffer_to_stream(str, buf, len);
488 * glk_put_buffer_stream_uni:
489 * @str: An output stream.
490 * @buf: An array of Unicode code points.
491 * @len: Length of @buf.
493 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
494 * print to, instead of using the current stream. It is illegal for @str to be
495 * %NULL, or an input-only stream.
498 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
500 VALID_STREAM(str, return);
504 g_return_if_fail(str->file_mode != filemode_Read);
506 write_buffer_to_stream_uni(str, buf, len);
511 **************** READING FUNCTIONS ********************************************
515 /* Internal function: Read one big-endian four-byte character from file fp and
516 return it as a Unicode code point, or -1 on EOF */
518 read_ucs4be_char_from_file(FILE *fp)
520 unsigned char readbuffer[4];
521 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
524 readbuffer[0] << 24 |
525 readbuffer[1] << 16 |
530 /* Internal function: Read one UTF-8 character, which may be more than one byte,
531 from file fp and return it as a Unicode code point, or -1 on EOF */
533 read_utf8_char_from_file(FILE *fp)
535 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
537 gunichar charresult = (gunichar)-2;
538 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
543 readbuffer[foo] = (gchar)ch;
544 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
545 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
548 /* Silently return unknown characters as 0xFFFD, Replacement Character */
549 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
554 /* Internal function: Tell whether this code point is a Unicode newline. The
555 file pointer and eight-bit flag are included in case the newline is a CR
556 (U+000D). If the next character is LF (U+000A) then it also belongs to the
559 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
561 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
564 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
565 read_ucs4be_char_from_file(fp);
567 if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
568 WARNING_S("Seek failed on stream", g_strerror(errno) );
574 /* Internal function: Read one character from a stream. Returns a value which
575 can be returned unchanged by glk_get_char_stream_uni(), but
576 glk_get_char_stream() must replace high values by the placeholder character. */
578 get_char_stream_common(strid_t str)
582 case STREAM_TYPE_MEMORY:
585 if(!str->ubuffer || str->mark >= str->buflen)
587 glui32 ch = str->ubuffer[str->mark++];
593 if(!str->buffer || str->mark >= str->buflen)
595 unsigned char ch = str->buffer[str->mark++];
601 case STREAM_TYPE_FILE:
606 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
612 else /* Regular file */
614 int ch = fgetc(str->file_pointer);
622 else /* Text mode is the same for Unicode and regular files */
624 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
632 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
638 * glk_get_char_stream:
639 * @str: An input stream.
641 * Reads one character from the stream @str. (There is no notion of a
642 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
643 * an output-only stream.
645 * The result will be between 0 and 255. As with all basic text functions, Glk
646 * assumes the Latin-1 encoding. See <link
647 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
648 * of the stream has been reached, the result will be -1.
651 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
652 * returned as negative numbers.
655 * If the stream contains Unicode data — for example, if it was created
656 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
657 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
659 * It is usually more efficient to read several characters at once with
660 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
661 * glk_get_char_stream() several times.
663 * Returns: A character value between 0 and 255, or -1 on end of stream.
666 glk_get_char_stream(strid_t str)
668 VALID_STREAM(str, return -1);
669 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
671 glsi32 ch = get_char_stream_common(str);
672 return (ch > 0xFF)? PLACEHOLDER : ch;
676 * glk_get_char_stream_uni:
677 * @str: An input stream.
679 * Reads one character from the stream @str. The result will be between 0 and
680 * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
682 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
685 glk_get_char_stream_uni(strid_t str)
687 VALID_STREAM(str, return -1);
688 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
690 return get_char_stream_common(str);
694 * glk_get_buffer_stream:
695 * @str: An input stream.
696 * @buf: A buffer with space for at least @len characters.
697 * @len: The number of characters to read.
699 * Reads @len characters from @str, unless the end of stream is reached first.
700 * No terminal null is placed in the buffer.
702 * Returns: The number of characters actually read.
705 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
707 VALID_STREAM(str, return 0);
708 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
709 g_return_val_if_fail(buf != NULL, 0);
713 case STREAM_TYPE_MEMORY:
718 while(copycount < len && str->ubuffer && str->mark < str->buflen)
720 glui32 ch = str->ubuffer[str->mark++];
721 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
726 if(str->buffer) /* if not, copycount stays 0 */
727 copycount = MIN(len, str->buflen - str->mark);
728 memmove(buf, str->buffer + str->mark, copycount);
729 str->mark += copycount;
732 str->read_count += copycount;
735 case STREAM_TYPE_FILE:
738 if(str->unicode) /* Binary file with 4-byte characters */
740 /* Read len characters of 4 bytes each */
741 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
742 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
743 /* If there was an incomplete character */
747 WARNING("Incomplete character in binary Unicode file");
751 for(foo = 0; foo < count; foo += 4)
753 glsi32 ch = readbuffer[foo] << 24
754 | readbuffer[foo + 1] << 16
755 | readbuffer[foo + 2] << 8
756 | readbuffer[foo + 3];
757 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
760 str->read_count += count / 4;
763 else /* Regular binary file */
765 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
766 str->read_count += count;
770 else /* Text mode is the same for Unicode and regular files */
772 /* Do it character-by-character */
774 for(foo = 0; foo < len; foo++)
776 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
780 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
785 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
791 * glk_get_buffer_stream_uni:
792 * @str: An input stream.
793 * @buf: A buffer with space for at least @len Unicode code points.
794 * @len: The number of characters to read.
796 * Reads @len Unicode characters from @str, unless the end of stream is reached
797 * first. No terminal null is placed in the buffer.
799 * Returns: The number of Unicode characters actually read.
802 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
804 VALID_STREAM(str, return 0);
805 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
806 g_return_val_if_fail(buf != NULL, 0);
810 case STREAM_TYPE_MEMORY:
815 if(str->ubuffer) /* if not, copycount stays 0 */
816 copycount = MIN(len, str->buflen - str->mark);
817 memmove(buf, str->ubuffer + str->mark, copycount * 4);
818 str->mark += copycount;
822 while(copycount < len && str->buffer && str->mark < str->buflen)
824 unsigned char ch = str->buffer[str->mark++];
825 buf[copycount++] = ch;
829 str->read_count += copycount;
832 case STREAM_TYPE_FILE:
835 if(str->unicode) /* Binary file with 4-byte characters */
837 /* Read len characters of 4 bytes each */
838 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
839 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
840 /* If there was an incomplete character */
844 WARNING("Incomplete character in binary Unicode file");
848 for(foo = 0; foo < count; foo += 4)
849 buf[foo / 4] = readbuffer[foo] << 24
850 | readbuffer[foo + 1] << 16
851 | readbuffer[foo + 2] << 8
852 | readbuffer[foo + 3];
854 str->read_count += count / 4;
857 else /* Regular binary file */
859 unsigned char *readbuffer = g_new0(unsigned char, len);
860 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
862 for(foo = 0; foo < count; foo++)
863 buf[foo] = readbuffer[foo];
865 str->read_count += count;
869 else /* Text mode is the same for Unicode and regular files */
871 /* Do it character-by-character */
873 for(foo = 0; foo < len; foo++)
875 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
884 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
890 * glk_get_line_stream:
891 * @str: An input stream.
892 * @buf: A buffer with space for at least @len characters.
893 * @len: The number of characters to read, plus one.
895 * Reads characters from @str, until either
897 * <alt>@len - 1</alt>
898 * <mathphrase>@len - 1</mathphrase>
900 * characters have been read or a newline has been read. It then puts a
901 * terminal null (<code>'\0'</code>) aracter on
902 * the end. It returns the number of characters actually read, including the
903 * newline (if there is one) but not including the terminal null.
905 * Returns: The number of characters actually read.
908 glk_get_line_stream(strid_t str, char *buf, glui32 len)
910 VALID_STREAM(str, return 0);
911 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
912 g_return_val_if_fail(buf != NULL, 0);
916 case STREAM_TYPE_MEMORY:
921 /* Do it character-by-character */
922 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
924 glui32 ch = str->ubuffer[str->mark++];
925 /* Check for Unicode newline; slightly different than
927 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
929 buf[copycount++] = '\n';
934 if(str->ubuffer[str->mark] == 0x0A)
935 str->mark++; /* skip past next newline */
936 buf[copycount++] = '\n';
939 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
941 buf[copycount] = '\0';
945 if(str->buffer) /* if not, copycount stays 0 */
946 copycount = MIN(len - 1, str->buflen - str->mark);
947 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
948 if(endptr) /* newline was found */
949 copycount = endptr - buf; /* Real copy count */
950 buf[copycount] = '\0';
951 str->mark += copycount;
954 str->read_count += copycount;
957 case STREAM_TYPE_FILE:
960 if(str->unicode) /* Binary file with 4-byte characters */
962 /* Do it character-by-character */
964 for(foo = 0; foo < len - 1; foo++)
966 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
973 if(is_unicode_newline(ch, str->file_pointer, FALSE))
979 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
984 else /* Regular binary file */
986 if( !fgets(buf, len, str->file_pointer) ) {
991 int nread = strlen(buf);
992 str->read_count += nread;
996 else /* Text mode is the same for Unicode and regular files */
998 /* Do it character-by-character */
1000 for(foo = 0; foo < len - 1; foo++)
1002 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1009 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1012 buf[foo + 1] = '\0';
1015 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
1021 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1027 * glk_get_line_stream_uni:
1028 * @str: An input stream.
1029 * @buf: A buffer with space for at least @len Unicode code points.
1030 * @len: The number of characters to read, plus one.
1032 * Reads Unicode characters from @str, until either
1034 * <alt>@len - 1</alt>
1035 * <mathphrase>@len - 1</mathphrase>
1037 * Unicode characters have been read or a newline has been read. It then puts a
1038 * terminal null (a zero value) on the end.
1040 * Returns: The number of characters actually read, including the newline (if
1041 * there is one) but not including the terminal null.
1044 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
1046 VALID_STREAM(str, return 0);
1047 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
1048 g_return_val_if_fail(buf != NULL, 0);
1052 case STREAM_TYPE_MEMORY:
1057 /* Do it character-by-character */
1058 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
1060 glui32 ch = str->ubuffer[str->mark++];
1061 /* Check for Unicode newline; slightly different than
1063 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
1065 buf[copycount++] = '\n';
1070 if(str->ubuffer[str->mark] == 0x0A)
1071 str->mark++; /* skip past next newline */
1072 buf[copycount++] = '\n';
1075 buf[copycount++] = ch;
1077 buf[copycount] = '\0';
1081 /* No recourse to memccpy(), so do it character-by-character */
1082 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
1084 gchar ch = str->buffer[str->mark++];
1085 /* Check for newline */
1086 if(ch == '\n') /* Also check for \r and \r\n? */
1088 buf[copycount++] = '\n';
1091 buf[copycount++] = (unsigned char)ch;
1096 str->read_count += copycount;
1099 case STREAM_TYPE_FILE:
1102 if(str->unicode) /* Binary file with 4-byte characters */
1104 /* Do it character-by-character */
1106 for(foo = 0; foo < len - 1; foo++)
1108 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1115 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1117 buf[foo] = ch; /* Preserve newline types??? */
1126 else /* Regular binary file */
1128 gchar *readbuffer = g_new0(gchar, len);
1129 if( !fgets(readbuffer, len, str->file_pointer) ) {
1134 glui32 count = strlen(readbuffer);
1136 for(foo = 0; foo < count + 1; foo++) /* Copy terminator */
1137 buf[foo] = (unsigned char)(readbuffer[foo]);
1138 str->read_count += count;
1142 else /* Text mode is the same for Unicode and regular files */
1144 /* Do it character-by-character */
1146 for(foo = 0; foo < len - 1; foo++)
1148 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1155 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1157 buf[foo] = ch; /* Preserve newline types??? */
1167 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1174 **************** SEEKING FUNCTIONS ********************************************
1179 * glk_stream_get_position:
1180 * @str: A file or memory stream.
1182 * Returns the position of the read/write mark in @str. For memory streams and
1183 * binary file streams, this is exactly the number of characters read or written
1184 * from the beginning of the stream (unless you have moved the mark with
1185 * glk_stream_set_position().) For text file streams, matters are more
1186 * ambiguous, since (for example) writing one byte to a text file may store more
1187 * than one character in the platform's native encoding. You can only be sure
1188 * that the position increases as you read or write to the file.
1190 * Additional complication: for Latin-1 memory and file streams, a character is
1191 * a byte. For Unicode memory and file streams (those created by
1192 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1193 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1197 * If this bothers you, don't use binary Unicode files. I don't think they're
1198 * good for much anyhow.
1201 * Returns: position of the read/write mark in @str.
1204 glk_stream_get_position(strid_t str)
1206 VALID_STREAM(str, return 0);
1210 case STREAM_TYPE_MEMORY:
1212 case STREAM_TYPE_FILE:
1213 return ftell(str->file_pointer);
1215 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1221 * glk_stream_set_position:
1222 * @str: A file or memory stream.
1223 * @pos: The position to set the mark to, relative to @seekmode.
1224 * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1226 * Sets the position of the read/write mark in @str. The position is controlled
1227 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1228 * <code>seekmode_</code> constants below.
1230 * It is illegal to specify a position before the beginning or after the end of
1233 * In binary files, the mark position is exact — it corresponds with the
1234 * number of characters you have read or written. In text files, this mapping
1235 * can vary, because of linefeed conventions or other character-set
1236 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1237 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1238 * the platform's native encoding — after character cookery. Therefore,
1239 * in a text stream, it is safest to use glk_stream_set_position() only to move
1240 * to the beginning or end of a file, or to a position determined by
1241 * glk_stream_get_position().
1243 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1244 * characters are 32-bit words, or four bytes each.
1247 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1249 VALID_STREAM(str, return);
1250 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1251 g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1255 case STREAM_TYPE_MEMORY:
1258 case seekmode_Start: str->mark = pos; break;
1259 case seekmode_Current: str->mark += pos; break;
1260 case seekmode_End: str->mark = str->buflen + pos; break;
1262 g_return_if_reached();
1266 case STREAM_TYPE_FILE:
1271 case seekmode_Start: whence = SEEK_SET; break;
1272 case seekmode_Current: whence = SEEK_CUR; break;
1273 case seekmode_End: whence = SEEK_END; break;
1275 g_return_if_reached();
1278 if(fseek(str->file_pointer, pos, whence) == -1)
1279 WARNING("Seek failed on file stream");
1283 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);