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, &start);
50 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
52 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
53 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
54 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
56 gtk_text_buffer_insert(buffer, &start, win->buffer->str, -1);
57 gtk_text_buffer_get_end_iter(buffer, &end);
60 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &end);
62 // Player's style overrides
63 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &end);
65 // GLK Program's style overrides
66 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &end);
68 // Link style overrides
69 if(win->window_stream->hyperlink_mode) {
70 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
71 GtkTextTag *link_tag = win->current_hyperlink->tag;
72 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &end);
73 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &end);
76 // GLK Program's style overrides using garglk_set_zcolors()
77 if(win->zcolor != NULL)
78 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &end);
80 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
82 g_signal_emit_by_name(glk, "text-buffer-output", win->rock, win->buffer->str);
86 case wintype_TextGrid:
88 /* Number of characters to insert */
89 glong length = win->buffer->len;
90 glong chars_left = length;
92 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
94 /* Get cursor position */
96 gtk_text_buffer_get_iter_at_mark(buffer, &start, cursor);
97 /* Spaces available on this line */
98 gint available_space = win->width - gtk_text_iter_get_line_offset(&start);
100 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
101 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
102 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
103 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
104 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
106 while(chars_left > available_space && !gtk_text_iter_is_end(&start))
108 GtkTextIter end = start;
109 gtk_text_iter_forward_to_line_end(&end);
110 gtk_text_buffer_delete(buffer, &start, &end);
112 gtk_text_buffer_insert(buffer, &start, win->buffer->str + (length - chars_left), available_space);
115 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &end);
117 // Player's style overrides
118 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &end);
120 // GLK Program's style overrides
121 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &end);
123 // Link style overrides
124 if(win->window_stream->hyperlink_mode) {
125 GtkTextTag *link_tag = win->current_hyperlink->tag;
126 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &end);
127 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &end);
130 // GLK Program's style overrides using garglk_set_zcolors()
131 if(win->zcolor != NULL)
132 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &end);
134 chars_left -= available_space;
135 gtk_text_iter_forward_line(&start);
136 available_space = win->width;
138 if(!gtk_text_iter_is_end(&start))
140 GtkTextIter end = start;
141 gtk_text_iter_forward_chars(&end, chars_left);
142 gtk_text_buffer_delete(buffer, &start, &end);
144 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
145 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
146 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
147 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
149 if(win->window_stream->hyperlink_mode) {
150 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
151 GtkTextTag *link_tag = win->current_hyperlink->tag;
152 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, default_tag, style_tag, glk_style_tag, link_style_tag, link_tag, NULL);
154 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, default_tag, style_tag, glk_style_tag, NULL);
158 gtk_text_buffer_move_mark(buffer, cursor, &start);
165 g_string_truncate(win->buffer, 0);
168 /* Internal function: write a Latin-1 buffer with length to a stream. */
170 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
174 case STREAM_TYPE_WINDOW:
175 /* Each window type has a different way of printing to it */
176 switch(str->window->type)
178 /* Printing to these windows' streams does nothing */
181 case wintype_Graphics:
182 str->write_count += len;
185 /* Text grid/buffer windows */
186 case wintype_TextGrid:
187 case wintype_TextBuffer:
189 gchar *utf8 = convert_latin1_to_utf8(buf, len);
191 write_utf8_to_window_buffer(str->window, utf8);
195 str->write_count += len;
198 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
201 /* Now write the same buffer to the window's echo stream */
202 if(str->window->echo_stream != NULL)
203 write_buffer_to_stream(str->window->echo_stream, buf, len);
207 case STREAM_TYPE_MEMORY:
208 if(str->unicode && str->ubuffer)
211 while(str->mark < str->buflen && foo < len)
212 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
214 if(!str->unicode && str->buffer)
216 int copycount = MIN(len, str->buflen - str->mark);
217 memmove(str->buffer + str->mark, buf, copycount);
218 str->mark += copycount;
221 str->write_count += len;
224 case STREAM_TYPE_FILE:
229 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
230 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
233 else /* Regular file */
235 fwrite(buf, sizeof(gchar), len, str->file_pointer);
238 else /* Text mode is the same for Unicode and regular files */
240 gchar *utf8 = convert_latin1_to_utf8(buf, len);
243 g_fprintf(str->file_pointer, "%s", utf8);
248 str->write_count += len;
251 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
255 /* Internal function: write a Unicode buffer with length to a stream. */
257 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
261 case STREAM_TYPE_WINDOW:
262 /* Each window type has a different way of printing to it */
263 switch(str->window->type)
265 /* Printing to these windows' streams does nothing */
268 case wintype_Graphics:
269 str->write_count += len;
272 /* Text grid/buffer windows */
273 case wintype_TextGrid:
274 case wintype_TextBuffer:
276 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
278 write_utf8_to_window_buffer(str->window, utf8);
282 str->write_count += len;
285 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
288 /* Now write the same buffer to the window's echo stream */
289 if(str->window->echo_stream != NULL)
290 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
294 case STREAM_TYPE_MEMORY:
295 if(str->unicode && str->ubuffer)
297 int copycount = MIN(len, str->buflen - str->mark);
298 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
299 str->mark += copycount;
301 if(!str->unicode && str->buffer)
303 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
304 int copycount = MIN(len, str->buflen - str->mark);
305 memmove(str->buffer + str->mark, latin1, copycount);
307 str->mark += copycount;
310 str->write_count += len;
313 case STREAM_TYPE_FILE:
318 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
319 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
322 else /* Regular file */
324 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
325 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
329 else /* Text mode is the same for Unicode and regular files */
331 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
334 g_fprintf(str->file_pointer, "%s", utf8);
339 str->write_count += len;
342 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
347 * glk_put_char_stream:
348 * @str: An output stream.
349 * @ch: A character in Latin-1 encoding.
351 * The same as glk_put_char(), except that you specify a stream @str to print
352 * to, instead of using the current stream. It is illegal for @str to be %NULL,
353 * or an input-only stream.
356 glk_put_char_stream(strid_t str, unsigned char ch)
358 VALID_STREAM(str, return);
359 g_return_if_fail(str->file_mode != filemode_Read);
361 write_buffer_to_stream(str, (gchar *)&ch, 1);
365 * glk_put_char_stream_uni:
366 * @str: An output stream.
367 * @ch: A Unicode code point.
369 * The same as glk_put_char_uni(), except that you specify a stream @str to
370 * print to, instead of using the current stream. It is illegal for @str to be
371 * %NULL, or an input-only stream.
374 glk_put_char_stream_uni(strid_t str, glui32 ch)
376 VALID_STREAM(str, return);
377 g_return_if_fail(str->file_mode != filemode_Read);
379 write_buffer_to_stream_uni(str, &ch, 1);
383 * glk_put_string_stream:
384 * @str: An output stream.
385 * @s: A null-terminated string in Latin-1 encoding.
387 * The same as glk_put_string(), 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_string_stream(strid_t str, char *s)
394 VALID_STREAM(str, return);
398 g_return_if_fail(str->file_mode != filemode_Read);
400 write_buffer_to_stream(str, s, strlen(s));
404 * glk_put_string_stream_uni:
405 * @str: An output stream.
406 * @s: A null-terminated array of Unicode code points.
408 * The same as glk_put_string_uni(), except that you specify a stream @str to
409 * print to, instead of using the current stream. It is illegal for @str to be
410 * %NULL, or an input-only stream.
413 glk_put_string_stream_uni(strid_t str, glui32 *s)
415 VALID_STREAM(str, return);
419 g_return_if_fail(str->file_mode != filemode_Read);
421 /* An impromptu strlen() for glui32 arrays */
426 write_buffer_to_stream_uni(str, s, len);
430 * glk_put_buffer_stream:
431 * @str: An output stream.
432 * @buf: An array of characters in Latin-1 encoding.
433 * @len: Length of @buf.
435 * The same as glk_put_buffer(), except that you specify a stream @str to print
436 * to, instead of using the current stream. It is illegal for @str to be %NULL,
437 * or an input-only stream.
440 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
442 VALID_STREAM(str, return);
446 g_return_if_fail(str->file_mode != filemode_Read);
448 write_buffer_to_stream(str, buf, len);
452 * glk_put_buffer_stream_uni:
453 * @str: An output stream.
454 * @buf: An array of Unicode code points.
455 * @len: Length of @buf.
457 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
458 * print to, instead of using the current stream. It is illegal for @str to be
459 * %NULL, or an input-only stream.
462 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
464 VALID_STREAM(str, return);
468 g_return_if_fail(str->file_mode != filemode_Read);
470 write_buffer_to_stream_uni(str, buf, len);
475 **************** READING FUNCTIONS ********************************************
479 /* Internal function: Read one big-endian four-byte character from file fp and
480 return it as a Unicode code point, or -1 on EOF */
482 read_ucs4be_char_from_file(FILE *fp)
484 unsigned char readbuffer[4];
485 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
488 readbuffer[0] << 24 |
489 readbuffer[1] << 16 |
494 /* Internal function: Read one UTF-8 character, which may be more than one byte,
495 from file fp and return it as a Unicode code point, or -1 on EOF */
497 read_utf8_char_from_file(FILE *fp)
499 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
501 gunichar charresult = (gunichar)-2;
502 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
507 readbuffer[foo] = (gchar)ch;
508 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
509 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
512 /* Silently return unknown characters as 0xFFFD, Replacement Character */
513 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
518 /* Internal function: Tell whether this code point is a Unicode newline. The
519 file pointer and eight-bit flag are included in case the newline is a CR
520 (U+000D). If the next character is LF (U+000A) then it also belongs to the
523 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
525 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
528 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
529 read_ucs4be_char_from_file(fp);
531 if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
532 WARNING_S("Seek failed on stream", g_strerror(errno) );
538 /* Internal function: Read one character from a stream. Returns a value which
539 can be returned unchanged by glk_get_char_stream_uni(), but
540 glk_get_char_stream() must replace high values by the placeholder character. */
542 get_char_stream_common(strid_t str)
546 case STREAM_TYPE_MEMORY:
549 if(!str->ubuffer || str->mark >= str->buflen)
551 glui32 ch = str->ubuffer[str->mark++];
557 if(!str->buffer || str->mark >= str->buflen)
559 unsigned char ch = str->buffer[str->mark++];
565 case STREAM_TYPE_FILE:
570 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
576 else /* Regular file */
578 int ch = fgetc(str->file_pointer);
586 else /* Text mode is the same for Unicode and regular files */
588 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
596 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
602 * glk_get_char_stream:
603 * @str: An input stream.
605 * Reads one character from the stream @str. (There is no notion of a
606 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
607 * an output-only stream.
609 * The result will be between 0 and 255. As with all basic text functions, Glk
610 * assumes the Latin-1 encoding. See <link
611 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
612 * of the stream has been reached, the result will be -1.
615 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
616 * returned as negative numbers.
619 * If the stream contains Unicode data — for example, if it was created
620 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
621 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
623 * It is usually more efficient to read several characters at once with
624 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
625 * glk_get_char_stream() several times.
627 * Returns: A character value between 0 and 255, or -1 on end of stream.
630 glk_get_char_stream(strid_t str)
632 VALID_STREAM(str, return -1);
633 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
635 glsi32 ch = get_char_stream_common(str);
636 return (ch > 0xFF)? PLACEHOLDER : ch;
640 * glk_get_char_stream_uni:
641 * @str: An input stream.
643 * Reads one character from the stream @str. The result will be between 0 and
644 * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
646 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
649 glk_get_char_stream_uni(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 return get_char_stream_common(str);
658 * glk_get_buffer_stream:
659 * @str: An input stream.
660 * @buf: A buffer with space for at least @len characters.
661 * @len: The number of characters to read.
663 * Reads @len characters from @str, unless the end of stream is reached first.
664 * No terminal null is placed in the buffer.
666 * Returns: The number of characters actually read.
669 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
671 VALID_STREAM(str, return 0);
672 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
673 g_return_val_if_fail(buf != NULL, 0);
677 case STREAM_TYPE_MEMORY:
682 while(copycount < len && str->ubuffer && str->mark < str->buflen)
684 glui32 ch = str->ubuffer[str->mark++];
685 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
690 if(str->buffer) /* if not, copycount stays 0 */
691 copycount = MIN(len, str->buflen - str->mark);
692 memmove(buf, str->buffer + str->mark, copycount);
693 str->mark += copycount;
696 str->read_count += copycount;
699 case STREAM_TYPE_FILE:
702 if(str->unicode) /* Binary file with 4-byte characters */
704 /* Read len characters of 4 bytes each */
705 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
706 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
707 /* If there was an incomplete character */
711 WARNING("Incomplete character in binary Unicode file");
715 for(foo = 0; foo < count; foo += 4)
717 glsi32 ch = readbuffer[foo] << 24
718 | readbuffer[foo + 1] << 16
719 | readbuffer[foo + 2] << 8
720 | readbuffer[foo + 3];
721 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
724 str->read_count += count / 4;
727 else /* Regular binary file */
729 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
730 str->read_count += count;
734 else /* Text mode is the same for Unicode and regular files */
736 /* Do it character-by-character */
738 for(foo = 0; foo < len; foo++)
740 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
744 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
749 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
755 * glk_get_buffer_stream_uni:
756 * @str: An input stream.
757 * @buf: A buffer with space for at least @len Unicode code points.
758 * @len: The number of characters to read.
760 * Reads @len Unicode characters from @str, unless the end of stream is reached
761 * first. No terminal null is placed in the buffer.
763 * Returns: The number of Unicode characters actually read.
766 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
768 VALID_STREAM(str, return 0);
769 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
770 g_return_val_if_fail(buf != NULL, 0);
774 case STREAM_TYPE_MEMORY:
779 if(str->ubuffer) /* if not, copycount stays 0 */
780 copycount = MIN(len, str->buflen - str->mark);
781 memmove(buf, str->ubuffer + str->mark, copycount * 4);
782 str->mark += copycount;
786 while(copycount < len && str->buffer && str->mark < str->buflen)
788 unsigned char ch = str->buffer[str->mark++];
789 buf[copycount++] = ch;
793 str->read_count += copycount;
796 case STREAM_TYPE_FILE:
799 if(str->unicode) /* Binary file with 4-byte characters */
801 /* Read len characters of 4 bytes each */
802 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
803 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
804 /* If there was an incomplete character */
808 WARNING("Incomplete character in binary Unicode file");
812 for(foo = 0; foo < count; foo += 4)
813 buf[foo / 4] = readbuffer[foo] << 24
814 | readbuffer[foo + 1] << 16
815 | readbuffer[foo + 2] << 8
816 | readbuffer[foo + 3];
818 str->read_count += count / 4;
821 else /* Regular binary file */
823 unsigned char *readbuffer = g_new0(unsigned char, len);
824 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
826 for(foo = 0; foo < count; foo++)
827 buf[foo] = readbuffer[foo];
829 str->read_count += count;
833 else /* Text mode is the same for Unicode and regular files */
835 /* Do it character-by-character */
837 for(foo = 0; foo < len; foo++)
839 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
848 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
854 * glk_get_line_stream:
855 * @str: An input stream.
856 * @buf: A buffer with space for at least @len characters.
857 * @len: The number of characters to read, plus one.
859 * Reads characters from @str, until either
861 * <alt>@len - 1</alt>
862 * <mathphrase>@len - 1</mathphrase>
864 * characters have been read or a newline has been read. It then puts a
865 * terminal null (<code>'\0'</code>) aracter on
866 * the end. It returns the number of characters actually read, including the
867 * newline (if there is one) but not including the terminal null.
869 * Returns: The number of characters actually read.
872 glk_get_line_stream(strid_t str, char *buf, glui32 len)
874 VALID_STREAM(str, return 0);
875 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
876 g_return_val_if_fail(buf != NULL, 0);
880 case STREAM_TYPE_MEMORY:
885 /* Do it character-by-character */
886 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
888 glui32 ch = str->ubuffer[str->mark++];
889 /* Check for Unicode newline; slightly different than
891 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
893 buf[copycount++] = '\n';
898 if(str->ubuffer[str->mark] == 0x0A)
899 str->mark++; /* skip past next newline */
900 buf[copycount++] = '\n';
903 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
905 buf[copycount] = '\0';
909 if(str->buffer) /* if not, copycount stays 0 */
910 copycount = MIN(len - 1, str->buflen - str->mark);
911 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
912 if(endptr) /* newline was found */
913 copycount = endptr - buf; /* Real copy count */
914 buf[copycount] = '\0';
915 str->mark += copycount;
918 str->read_count += copycount;
921 case STREAM_TYPE_FILE:
924 if(str->unicode) /* Binary file with 4-byte characters */
926 /* Do it character-by-character */
928 for(foo = 0; foo < len - 1; foo++)
930 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
937 if(is_unicode_newline(ch, str->file_pointer, FALSE))
943 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
948 else /* Regular binary file */
950 if( !fgets(buf, len, str->file_pointer) ) {
955 int nread = strlen(buf);
956 str->read_count += nread;
960 else /* Text mode is the same for Unicode and regular files */
962 /* Do it character-by-character */
964 for(foo = 0; foo < len - 1; foo++)
966 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
973 if(is_unicode_newline(ch, str->file_pointer, TRUE))
979 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
985 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
991 * glk_get_line_stream_uni:
992 * @str: An input stream.
993 * @buf: A buffer with space for at least @len Unicode code points.
994 * @len: The number of characters to read, plus one.
996 * Reads Unicode characters from @str, until either
998 * <alt>@len - 1</alt>
999 * <mathphrase>@len - 1</mathphrase>
1001 * Unicode characters have been read or a newline has been read. It then puts a
1002 * terminal null (a zero value) on the end.
1004 * Returns: The number of characters actually read, including the newline (if
1005 * there is one) but not including the terminal null.
1008 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
1010 VALID_STREAM(str, return 0);
1011 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
1012 g_return_val_if_fail(buf != NULL, 0);
1016 case STREAM_TYPE_MEMORY:
1021 /* Do it character-by-character */
1022 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
1024 glui32 ch = str->ubuffer[str->mark++];
1025 /* Check for Unicode newline; slightly different than
1027 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
1029 buf[copycount++] = '\n';
1034 if(str->ubuffer[str->mark] == 0x0A)
1035 str->mark++; /* skip past next newline */
1036 buf[copycount++] = '\n';
1039 buf[copycount++] = ch;
1041 buf[copycount] = '\0';
1045 /* No recourse to memccpy(), so do it character-by-character */
1046 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
1048 gchar ch = str->buffer[str->mark++];
1049 /* Check for newline */
1050 if(ch == '\n') /* Also check for \r and \r\n? */
1052 buf[copycount++] = '\n';
1055 buf[copycount++] = (unsigned char)ch;
1060 str->read_count += copycount;
1063 case STREAM_TYPE_FILE:
1066 if(str->unicode) /* Binary file with 4-byte characters */
1068 /* Do it character-by-character */
1070 for(foo = 0; foo < len - 1; foo++)
1072 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1079 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1081 buf[foo] = ch; /* Preserve newline types??? */
1090 else /* Regular binary file */
1092 gchar *readbuffer = g_new0(gchar, len);
1093 if( !fgets(readbuffer, len, str->file_pointer) ) {
1098 glui32 count = strlen(readbuffer);
1100 for(foo = 0; foo < count + 1; foo++) /* Copy terminator */
1101 buf[foo] = (unsigned char)(readbuffer[foo]);
1102 str->read_count += count;
1106 else /* Text mode is the same for Unicode and regular files */
1108 /* Do it character-by-character */
1110 for(foo = 0; foo < len - 1; foo++)
1112 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1119 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1121 buf[foo] = ch; /* Preserve newline types??? */
1131 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1138 **************** SEEKING FUNCTIONS ********************************************
1143 * glk_stream_get_position:
1144 * @str: A file or memory stream.
1146 * Returns the position of the read/write mark in @str. For memory streams and
1147 * binary file streams, this is exactly the number of characters read or written
1148 * from the beginning of the stream (unless you have moved the mark with
1149 * glk_stream_set_position().) For text file streams, matters are more
1150 * ambiguous, since (for example) writing one byte to a text file may store more
1151 * than one character in the platform's native encoding. You can only be sure
1152 * that the position increases as you read or write to the file.
1154 * Additional complication: for Latin-1 memory and file streams, a character is
1155 * a byte. For Unicode memory and file streams (those created by
1156 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1157 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1161 * If this bothers you, don't use binary Unicode files. I don't think they're
1162 * good for much anyhow.
1165 * Returns: position of the read/write mark in @str.
1168 glk_stream_get_position(strid_t str)
1170 VALID_STREAM(str, return 0);
1174 case STREAM_TYPE_MEMORY:
1176 case STREAM_TYPE_FILE:
1177 return ftell(str->file_pointer);
1179 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1185 * glk_stream_set_position:
1186 * @str: A file or memory stream.
1187 * @pos: The position to set the mark to, relative to @seekmode.
1188 * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1190 * Sets the position of the read/write mark in @str. The position is controlled
1191 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1192 * <code>seekmode_</code> constants below.
1194 * It is illegal to specify a position before the beginning or after the end of
1197 * In binary files, the mark position is exact — it corresponds with the
1198 * number of characters you have read or written. In text files, this mapping
1199 * can vary, because of linefeed conventions or other character-set
1200 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1201 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1202 * the platform's native encoding — after character cookery. Therefore,
1203 * in a text stream, it is safest to use glk_stream_set_position() only to move
1204 * to the beginning or end of a file, or to a position determined by
1205 * glk_stream_get_position().
1207 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1208 * characters are 32-bit words, or four bytes each.
1211 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1213 VALID_STREAM(str, return);
1214 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1215 g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1219 case STREAM_TYPE_MEMORY:
1222 case seekmode_Start: str->mark = pos; break;
1223 case seekmode_Current: str->mark += pos; break;
1224 case seekmode_End: str->mark = str->buflen + pos; break;
1226 g_return_if_reached();
1230 case STREAM_TYPE_FILE:
1235 case seekmode_Start: whence = SEEK_SET; break;
1236 case seekmode_Current: whence = SEEK_CUR; break;
1237 case seekmode_End: whence = SEEK_END; break;
1239 g_return_if_reached();
1242 if(fseek(str->file_pointer, pos, whence) == -1)
1243 WARNING("Seek failed on file stream");
1247 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);