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:
48 gtk_text_buffer_get_end_iter(buffer, &iter);
50 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
51 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
53 if(win->window_stream->hyperlink_mode) {
54 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
55 GtkTextTag *link_tag = win->current_hyperlink->tag;
56 gtk_text_buffer_insert_with_tags(buffer, &iter, win->buffer->str, -1, style_tag, link_style_tag, link_tag, NULL);
58 gtk_text_buffer_insert_with_tags(buffer, &iter, win->buffer->str, -1, style_tag, NULL);
61 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
63 g_signal_emit_by_name(glk, "text-buffer-output", win->rock, win->buffer->str);
67 case wintype_TextGrid:
69 /* Number of characters to insert */
70 glong length = win->buffer->len;
71 glong chars_left = length;
73 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
75 /* Get cursor position */
77 gtk_text_buffer_get_iter_at_mark(buffer, &start, cursor);
78 /* Spaces available on this line */
79 gint available_space = win->width - gtk_text_iter_get_line_offset(&start);
81 while(chars_left > available_space && !gtk_text_iter_is_end(&start))
83 GtkTextIter end = start;
84 gtk_text_iter_forward_to_line_end(&end);
85 gtk_text_buffer_delete(buffer, &start, &end);
87 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
88 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
90 if(win->window_stream->hyperlink_mode) {
91 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
92 GtkTextTag *link_tag = win->current_hyperlink->tag;
93 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), available_space, style_tag, link_style_tag, link_tag, NULL);
95 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), available_space, style_tag, NULL);
98 chars_left -= available_space;
99 gtk_text_iter_forward_line(&start);
100 available_space = win->width;
102 if(!gtk_text_iter_is_end(&start))
104 GtkTextIter end = start;
105 gtk_text_iter_forward_chars(&end, chars_left);
106 gtk_text_buffer_delete(buffer, &start, &end);
108 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
109 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
111 if(win->window_stream->hyperlink_mode) {
112 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
113 GtkTextTag *link_tag = win->current_hyperlink->tag;
114 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, style_tag, link_style_tag, link_tag, NULL);
116 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, style_tag, NULL);
120 gtk_text_buffer_move_mark(buffer, cursor, &start);
127 g_string_truncate(win->buffer, 0);
130 /* Internal function: write a Latin-1 buffer with length to a stream. */
132 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
136 case STREAM_TYPE_WINDOW:
137 /* Each window type has a different way of printing to it */
138 switch(str->window->type)
140 /* Printing to these windows' streams does nothing */
143 case wintype_Graphics:
144 str->write_count += len;
147 /* Text grid/buffer windows */
148 case wintype_TextGrid:
149 case wintype_TextBuffer:
151 gchar *utf8 = convert_latin1_to_utf8(buf, len);
153 write_utf8_to_window_buffer(str->window, utf8);
157 str->write_count += len;
160 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
163 /* Now write the same buffer to the window's echo stream */
164 if(str->window->echo_stream != NULL)
165 write_buffer_to_stream(str->window->echo_stream, buf, len);
169 case STREAM_TYPE_MEMORY:
170 if(str->unicode && str->ubuffer)
173 while(str->mark < str->buflen && foo < len)
174 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
176 if(!str->unicode && str->buffer)
178 int copycount = MIN(len, str->buflen - str->mark);
179 memmove(str->buffer + str->mark, buf, copycount);
180 str->mark += copycount;
183 str->write_count += len;
186 case STREAM_TYPE_FILE:
191 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
192 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
195 else /* Regular file */
197 fwrite(buf, sizeof(gchar), len, str->file_pointer);
200 else /* Text mode is the same for Unicode and regular files */
202 gchar *utf8 = convert_latin1_to_utf8(buf, len);
205 g_fprintf(str->file_pointer, "%s", utf8);
210 str->write_count += len;
213 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
217 /* Internal function: write a Unicode buffer with length to a stream. */
219 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
223 case STREAM_TYPE_WINDOW:
224 /* Each window type has a different way of printing to it */
225 switch(str->window->type)
227 /* Printing to these windows' streams does nothing */
230 case wintype_Graphics:
231 str->write_count += len;
234 /* Text grid/buffer windows */
235 case wintype_TextGrid:
236 case wintype_TextBuffer:
238 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
240 write_utf8_to_window_buffer(str->window, utf8);
244 str->write_count += len;
247 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
250 /* Now write the same buffer to the window's echo stream */
251 if(str->window->echo_stream != NULL)
252 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
256 case STREAM_TYPE_MEMORY:
257 if(str->unicode && str->ubuffer)
259 int copycount = MIN(len, str->buflen - str->mark);
260 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
261 str->mark += copycount;
263 if(!str->unicode && str->buffer)
265 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
266 int copycount = MIN(len, str->buflen - str->mark);
267 memmove(str->buffer + str->mark, latin1, copycount);
269 str->mark += copycount;
272 str->write_count += len;
275 case STREAM_TYPE_FILE:
280 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
281 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
284 else /* Regular file */
286 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
287 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
291 else /* Text mode is the same for Unicode and regular files */
293 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
296 g_fprintf(str->file_pointer, "%s", utf8);
301 str->write_count += len;
304 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
309 * glk_put_char_stream:
310 * @str: An output stream.
311 * @ch: A character in Latin-1 encoding.
313 * The same as glk_put_char(), except that you specify a stream @str to print
314 * to, instead of using the current stream. It is illegal for @str to be %NULL,
315 * or an input-only stream.
318 glk_put_char_stream(strid_t str, unsigned char ch)
320 VALID_STREAM(str, return);
321 g_return_if_fail(str->file_mode != filemode_Read);
323 write_buffer_to_stream(str, (gchar *)&ch, 1);
327 * glk_put_char_stream_uni:
328 * @str: An output stream.
329 * @ch: A Unicode code point.
331 * The same as glk_put_char_uni(), except that you specify a stream @str to
332 * print to, instead of using the current stream. It is illegal for @str to be
333 * %NULL, or an input-only stream.
336 glk_put_char_stream_uni(strid_t str, glui32 ch)
338 VALID_STREAM(str, return);
339 g_return_if_fail(str->file_mode != filemode_Read);
341 write_buffer_to_stream_uni(str, &ch, 1);
345 * glk_put_string_stream:
346 * @str: An output stream.
347 * @s: A null-terminated string in Latin-1 encoding.
349 * The same as glk_put_string(), except that you specify a stream @str to print
350 * to, instead of using the current stream. It is illegal for @str to be %NULL,
351 * or an input-only stream.
354 glk_put_string_stream(strid_t str, char *s)
356 VALID_STREAM(str, return);
360 g_return_if_fail(str->file_mode != filemode_Read);
362 write_buffer_to_stream(str, s, strlen(s));
366 * glk_put_string_stream_uni:
367 * @str: An output stream.
368 * @s: A null-terminated array of Unicode code points.
370 * The same as glk_put_string_uni(), except that you specify a stream @str to
371 * print to, instead of using the current stream. It is illegal for @str to be
372 * %NULL, or an input-only stream.
375 glk_put_string_stream_uni(strid_t str, glui32 *s)
377 VALID_STREAM(str, return);
381 g_return_if_fail(str->file_mode != filemode_Read);
383 /* An impromptu strlen() for glui32 arrays */
388 write_buffer_to_stream_uni(str, s, len);
392 * glk_put_buffer_stream:
393 * @str: An output stream.
394 * @buf: An array of characters in Latin-1 encoding.
395 * @len: Length of @buf.
397 * The same as glk_put_buffer(), except that you specify a stream @str to print
398 * to, instead of using the current stream. It is illegal for @str to be %NULL,
399 * or an input-only stream.
402 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
404 VALID_STREAM(str, return);
408 g_return_if_fail(str->file_mode != filemode_Read);
410 write_buffer_to_stream(str, buf, len);
414 * glk_put_buffer_stream_uni:
415 * @str: An output stream.
416 * @buf: An array of Unicode code points.
417 * @len: Length of @buf.
419 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
420 * print to, instead of using the current stream. It is illegal for @str to be
421 * %NULL, or an input-only stream.
424 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
426 VALID_STREAM(str, return);
430 g_return_if_fail(str->file_mode != filemode_Read);
432 write_buffer_to_stream_uni(str, buf, len);
437 **************** READING FUNCTIONS ********************************************
441 /* Internal function: Read one big-endian four-byte character from file fp and
442 return it as a Unicode code point, or -1 on EOF */
444 read_ucs4be_char_from_file(FILE *fp)
446 unsigned char readbuffer[4];
447 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
450 readbuffer[0] << 24 |
451 readbuffer[1] << 16 |
456 /* Internal function: Read one UTF-8 character, which may be more than one byte,
457 from file fp and return it as a Unicode code point, or -1 on EOF */
459 read_utf8_char_from_file(FILE *fp)
461 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
463 gunichar charresult = (gunichar)-2;
464 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
469 readbuffer[foo] = (gchar)ch;
470 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
471 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
474 /* Silently return unknown characters as 0xFFFD, Replacement Character */
475 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
480 /* Internal function: Tell whether this code point is a Unicode newline. The
481 file pointer and eight-bit flag are included in case the newline is a CR
482 (U+000D). If the next character is LF (U+000A) then it also belongs to the
485 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
487 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
490 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
491 read_ucs4be_char_from_file(fp);
493 if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
494 WARNING_S("Seek failed on stream", g_strerror(errno) );
500 /* Internal function: Read one character from a stream. Returns a value which
501 can be returned unchanged by glk_get_char_stream_uni(), but
502 glk_get_char_stream() must replace high values by the placeholder character. */
504 get_char_stream_common(strid_t str)
508 case STREAM_TYPE_MEMORY:
511 if(!str->ubuffer || str->mark >= str->buflen)
513 glui32 ch = str->ubuffer[str->mark++];
519 if(!str->buffer || str->mark >= str->buflen)
521 unsigned char ch = str->buffer[str->mark++];
527 case STREAM_TYPE_FILE:
532 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
538 else /* Regular file */
540 int ch = fgetc(str->file_pointer);
548 else /* Text mode is the same for Unicode and regular files */
550 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
558 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
564 * glk_get_char_stream:
565 * @str: An input stream.
567 * Reads one character from the stream @str. (There is no notion of a
568 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
569 * an output-only stream.
571 * The result will be between 0 and 255. As with all basic text functions, Glk
572 * assumes the Latin-1 encoding. See <link
573 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
574 * of the stream has been reached, the result will be -1.
577 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
578 * returned as negative numbers.
581 * If the stream contains Unicode data — for example, if it was created
582 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
583 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
585 * It is usually more efficient to read several characters at once with
586 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
587 * glk_get_char_stream() several times.
589 * Returns: A character value between 0 and 255, or -1 on end of stream.
592 glk_get_char_stream(strid_t str)
594 VALID_STREAM(str, return -1);
595 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
597 glsi32 ch = get_char_stream_common(str);
598 return (ch > 0xFF)? PLACEHOLDER : ch;
602 * glk_get_char_stream_uni:
603 * @str: An input stream.
605 * Reads one character from the stream @str. The result will be between 0 and
606 * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
608 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
611 glk_get_char_stream_uni(strid_t str)
613 VALID_STREAM(str, return -1);
614 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
616 return get_char_stream_common(str);
620 * glk_get_buffer_stream:
621 * @str: An input stream.
622 * @buf: A buffer with space for at least @len characters.
623 * @len: The number of characters to read.
625 * Reads @len characters from @str, unless the end of stream is reached first.
626 * No terminal null is placed in the buffer.
628 * Returns: The number of characters actually read.
631 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
633 VALID_STREAM(str, return 0);
634 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
635 g_return_val_if_fail(buf != NULL, 0);
639 case STREAM_TYPE_MEMORY:
644 while(copycount < len && str->ubuffer && str->mark < str->buflen)
646 glui32 ch = str->ubuffer[str->mark++];
647 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
652 if(str->buffer) /* if not, copycount stays 0 */
653 copycount = MIN(len, str->buflen - str->mark);
654 memmove(buf, str->buffer + str->mark, copycount);
655 str->mark += copycount;
658 str->read_count += copycount;
661 case STREAM_TYPE_FILE:
664 if(str->unicode) /* Binary file with 4-byte characters */
666 /* Read len characters of 4 bytes each */
667 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
668 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
669 /* If there was an incomplete character */
673 WARNING("Incomplete character in binary Unicode file");
677 for(foo = 0; foo < count; foo += 4)
679 glsi32 ch = readbuffer[foo] << 24
680 | readbuffer[foo + 1] << 16
681 | readbuffer[foo + 2] << 8
682 | readbuffer[foo + 3];
683 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
686 str->read_count += count / 4;
689 else /* Regular binary file */
691 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
692 str->read_count += count;
696 else /* Text mode is the same for Unicode and regular files */
698 /* Do it character-by-character */
700 for(foo = 0; foo < len; foo++)
702 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
706 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
711 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
717 * glk_get_buffer_stream_uni:
718 * @str: An input stream.
719 * @buf: A buffer with space for at least @len Unicode code points.
720 * @len: The number of characters to read.
722 * Reads @len Unicode characters from @str, unless the end of stream is reached
723 * first. No terminal null is placed in the buffer.
725 * Returns: The number of Unicode characters actually read.
728 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
730 VALID_STREAM(str, return 0);
731 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
732 g_return_val_if_fail(buf != NULL, 0);
736 case STREAM_TYPE_MEMORY:
741 if(str->ubuffer) /* if not, copycount stays 0 */
742 copycount = MIN(len, str->buflen - str->mark);
743 memmove(buf, str->ubuffer + str->mark, copycount * 4);
744 str->mark += copycount;
748 while(copycount < len && str->buffer && str->mark < str->buflen)
750 unsigned char ch = str->buffer[str->mark++];
751 buf[copycount++] = ch;
755 str->read_count += copycount;
758 case STREAM_TYPE_FILE:
761 if(str->unicode) /* Binary file with 4-byte characters */
763 /* Read len characters of 4 bytes each */
764 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
765 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
766 /* If there was an incomplete character */
770 WARNING("Incomplete character in binary Unicode file");
774 for(foo = 0; foo < count; foo += 4)
775 buf[foo / 4] = readbuffer[foo] << 24
776 | readbuffer[foo + 1] << 16
777 | readbuffer[foo + 2] << 8
778 | readbuffer[foo + 3];
780 str->read_count += count / 4;
783 else /* Regular binary file */
785 unsigned char *readbuffer = g_new0(unsigned char, len);
786 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
788 for(foo = 0; foo < count; foo++)
789 buf[foo] = readbuffer[foo];
791 str->read_count += count;
795 else /* Text mode is the same for Unicode and regular files */
797 /* Do it character-by-character */
799 for(foo = 0; foo < len; foo++)
801 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
810 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
816 * glk_get_line_stream:
817 * @str: An input stream.
818 * @buf: A buffer with space for at least @len characters.
819 * @len: The number of characters to read, plus one.
821 * Reads characters from @str, until either
823 * <alt>@len - 1</alt>
824 * <mathphrase>@len - 1</mathphrase>
826 * characters have been read or a newline has been read. It then puts a
827 * terminal null (<code>'\0'</code>) aracter on
828 * the end. It returns the number of characters actually read, including the
829 * newline (if there is one) but not including the terminal null.
831 * Returns: The number of characters actually read.
834 glk_get_line_stream(strid_t str, char *buf, glui32 len)
836 VALID_STREAM(str, return 0);
837 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
838 g_return_val_if_fail(buf != NULL, 0);
842 case STREAM_TYPE_MEMORY:
847 /* Do it character-by-character */
848 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
850 glui32 ch = str->ubuffer[str->mark++];
851 /* Check for Unicode newline; slightly different than
853 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
855 buf[copycount++] = '\n';
860 if(str->ubuffer[str->mark] == 0x0A)
861 str->mark++; /* skip past next newline */
862 buf[copycount++] = '\n';
865 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
867 buf[copycount] = '\0';
871 if(str->buffer) /* if not, copycount stays 0 */
872 copycount = MIN(len - 1, str->buflen - str->mark);
873 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
874 if(endptr) /* newline was found */
875 copycount = endptr - buf; /* Real copy count */
876 buf[copycount] = '\0';
877 str->mark += copycount;
880 str->read_count += copycount;
883 case STREAM_TYPE_FILE:
886 if(str->unicode) /* Binary file with 4-byte characters */
888 /* Do it character-by-character */
890 for(foo = 0; foo < len - 1; foo++)
892 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
899 if(is_unicode_newline(ch, str->file_pointer, FALSE))
905 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
910 else /* Regular binary file */
912 if( !fgets(buf, len, str->file_pointer) ) {
917 int nread = strlen(buf);
918 str->read_count += nread;
922 else /* Text mode is the same for Unicode and regular files */
924 /* Do it character-by-character */
926 for(foo = 0; foo < len - 1; foo++)
928 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
935 if(is_unicode_newline(ch, str->file_pointer, TRUE))
941 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
947 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
953 * glk_get_line_stream_uni:
954 * @str: An input stream.
955 * @buf: A buffer with space for at least @len Unicode code points.
956 * @len: The number of characters to read, plus one.
958 * Reads Unicode characters from @str, until either
960 * <alt>@len - 1</alt>
961 * <mathphrase>@len - 1</mathphrase>
963 * Unicode characters have been read or a newline has been read. It then puts a
964 * terminal null (a zero value) on the end.
966 * Returns: The number of characters actually read, including the newline (if
967 * there is one) but not including the terminal null.
970 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
972 VALID_STREAM(str, return 0);
973 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
974 g_return_val_if_fail(buf != NULL, 0);
978 case STREAM_TYPE_MEMORY:
983 /* Do it character-by-character */
984 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
986 glui32 ch = str->ubuffer[str->mark++];
987 /* Check for Unicode newline; slightly different than
989 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
991 buf[copycount++] = '\n';
996 if(str->ubuffer[str->mark] == 0x0A)
997 str->mark++; /* skip past next newline */
998 buf[copycount++] = '\n';
1001 buf[copycount++] = ch;
1003 buf[copycount] = '\0';
1007 /* No recourse to memccpy(), so do it character-by-character */
1008 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
1010 gchar ch = str->buffer[str->mark++];
1011 /* Check for newline */
1012 if(ch == '\n') /* Also check for \r and \r\n? */
1014 buf[copycount++] = '\n';
1017 buf[copycount++] = (unsigned char)ch;
1022 str->read_count += copycount;
1025 case STREAM_TYPE_FILE:
1028 if(str->unicode) /* Binary file with 4-byte characters */
1030 /* Do it character-by-character */
1032 for(foo = 0; foo < len - 1; foo++)
1034 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1041 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1043 buf[foo] = ch; /* Preserve newline types??? */
1052 else /* Regular binary file */
1054 gchar *readbuffer = g_new0(gchar, len);
1055 if( !fgets(readbuffer, len, str->file_pointer) ) {
1060 glui32 count = strlen(readbuffer);
1062 for(foo = 0; foo < count + 1; foo++) /* Copy terminator */
1063 buf[foo] = (unsigned char)(readbuffer[foo]);
1064 str->read_count += count;
1068 else /* Text mode is the same for Unicode and regular files */
1070 /* Do it character-by-character */
1072 for(foo = 0; foo < len - 1; foo++)
1074 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1081 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1083 buf[foo] = ch; /* Preserve newline types??? */
1093 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1100 **************** SEEKING FUNCTIONS ********************************************
1105 * glk_stream_get_position:
1106 * @str: A file or memory stream.
1108 * Returns the position of the read/write mark in @str. For memory streams and
1109 * binary file streams, this is exactly the number of characters read or written
1110 * from the beginning of the stream (unless you have moved the mark with
1111 * glk_stream_set_position().) For text file streams, matters are more
1112 * ambiguous, since (for example) writing one byte to a text file may store more
1113 * than one character in the platform's native encoding. You can only be sure
1114 * that the position increases as you read or write to the file.
1116 * Additional complication: for Latin-1 memory and file streams, a character is
1117 * a byte. For Unicode memory and file streams (those created by
1118 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1119 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1123 * If this bothers you, don't use binary Unicode files. I don't think they're
1124 * good for much anyhow.
1127 * Returns: position of the read/write mark in @str.
1130 glk_stream_get_position(strid_t str)
1132 VALID_STREAM(str, return 0);
1136 case STREAM_TYPE_MEMORY:
1138 case STREAM_TYPE_FILE:
1139 return ftell(str->file_pointer);
1141 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1147 * glk_stream_set_position:
1148 * @str: A file or memory stream.
1149 * @pos: The position to set the mark to, relative to @seekmode.
1150 * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1152 * Sets the position of the read/write mark in @str. The position is controlled
1153 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1154 * <code>seekmode_</code> constants below.
1156 * It is illegal to specify a position before the beginning or after the end of
1159 * In binary files, the mark position is exact — it corresponds with the
1160 * number of characters you have read or written. In text files, this mapping
1161 * can vary, because of linefeed conventions or other character-set
1162 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1163 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1164 * the platform's native encoding — after character cookery. Therefore,
1165 * in a text stream, it is safest to use glk_stream_set_position() only to move
1166 * to the beginning or end of a file, or to a position determined by
1167 * glk_stream_get_position().
1169 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1170 * characters are 32-bit words, or four bytes each.
1173 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1175 VALID_STREAM(str, return);
1176 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1177 g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1181 case STREAM_TYPE_MEMORY:
1184 case seekmode_Start: str->mark = pos; break;
1185 case seekmode_Current: str->mark += pos; break;
1186 case seekmode_End: str->mark = str->buflen + pos; break;
1188 g_return_if_reached();
1192 case STREAM_TYPE_FILE:
1197 case seekmode_Start: whence = SEEK_SET; break;
1198 case seekmode_Current: whence = SEEK_CUR; break;
1199 case seekmode_End: whence = SEEK_END; break;
1201 g_return_if_reached();
1204 if(fseek(str->file_pointer, pos, whence) == -1)
1205 WARNING("Seek failed on file stream");
1209 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);