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 *default_tag = gtk_text_tag_table_lookup(tags, "default");
52 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
53 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
55 if(win->window_stream->hyperlink_mode) {
56 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
57 GtkTextTag *link_tag = win->current_hyperlink->tag;
58 gtk_text_buffer_insert_with_tags(buffer, &iter, win->buffer->str, -1, default_tag, style_tag, glk_style_tag, link_style_tag, link_tag, NULL);
60 gtk_text_buffer_insert_with_tags(buffer, &iter, win->buffer->str, -1, default_tag, style_tag, glk_style_tag, NULL);
63 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
65 g_signal_emit_by_name(glk, "text-buffer-output", win->rock, win->buffer->str);
69 case wintype_TextGrid:
71 /* Number of characters to insert */
72 glong length = win->buffer->len;
73 glong chars_left = length;
75 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
77 /* Get cursor position */
79 gtk_text_buffer_get_iter_at_mark(buffer, &start, cursor);
80 /* Spaces available on this line */
81 gint available_space = win->width - gtk_text_iter_get_line_offset(&start);
83 while(chars_left > available_space && !gtk_text_iter_is_end(&start))
85 GtkTextIter end = start;
86 gtk_text_iter_forward_to_line_end(&end);
87 gtk_text_buffer_delete(buffer, &start, &end);
89 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
90 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
91 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
92 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
94 if(win->window_stream->hyperlink_mode) {
95 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
96 GtkTextTag *link_tag = win->current_hyperlink->tag;
97 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), available_space, default_tag, style_tag, glk_style_tag, link_style_tag, link_tag, NULL);
99 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), available_space, default_tag, style_tag, glk_style_tag, NULL);
102 chars_left -= available_space;
103 gtk_text_iter_forward_line(&start);
104 available_space = win->width;
106 if(!gtk_text_iter_is_end(&start))
108 GtkTextIter end = start;
109 gtk_text_iter_forward_chars(&end, chars_left);
110 gtk_text_buffer_delete(buffer, &start, &end);
112 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
113 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
114 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
115 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
117 if(win->window_stream->hyperlink_mode) {
118 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
119 GtkTextTag *link_tag = win->current_hyperlink->tag;
120 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);
122 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, default_tag, style_tag, glk_style_tag, NULL);
126 gtk_text_buffer_move_mark(buffer, cursor, &start);
133 g_string_truncate(win->buffer, 0);
136 /* Internal function: write a Latin-1 buffer with length to a stream. */
138 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
142 case STREAM_TYPE_WINDOW:
143 /* Each window type has a different way of printing to it */
144 switch(str->window->type)
146 /* Printing to these windows' streams does nothing */
149 case wintype_Graphics:
150 str->write_count += len;
153 /* Text grid/buffer windows */
154 case wintype_TextGrid:
155 case wintype_TextBuffer:
157 gchar *utf8 = convert_latin1_to_utf8(buf, len);
159 write_utf8_to_window_buffer(str->window, utf8);
163 str->write_count += len;
166 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
169 /* Now write the same buffer to the window's echo stream */
170 if(str->window->echo_stream != NULL)
171 write_buffer_to_stream(str->window->echo_stream, buf, len);
175 case STREAM_TYPE_MEMORY:
176 if(str->unicode && str->ubuffer)
179 while(str->mark < str->buflen && foo < len)
180 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
182 if(!str->unicode && str->buffer)
184 int copycount = MIN(len, str->buflen - str->mark);
185 memmove(str->buffer + str->mark, buf, copycount);
186 str->mark += copycount;
189 str->write_count += len;
192 case STREAM_TYPE_FILE:
197 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
198 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
201 else /* Regular file */
203 fwrite(buf, sizeof(gchar), len, str->file_pointer);
206 else /* Text mode is the same for Unicode and regular files */
208 gchar *utf8 = convert_latin1_to_utf8(buf, len);
211 g_fprintf(str->file_pointer, "%s", utf8);
216 str->write_count += len;
219 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
223 /* Internal function: write a Unicode buffer with length to a stream. */
225 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
229 case STREAM_TYPE_WINDOW:
230 /* Each window type has a different way of printing to it */
231 switch(str->window->type)
233 /* Printing to these windows' streams does nothing */
236 case wintype_Graphics:
237 str->write_count += len;
240 /* Text grid/buffer windows */
241 case wintype_TextGrid:
242 case wintype_TextBuffer:
244 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
246 write_utf8_to_window_buffer(str->window, utf8);
250 str->write_count += len;
253 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
256 /* Now write the same buffer to the window's echo stream */
257 if(str->window->echo_stream != NULL)
258 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
262 case STREAM_TYPE_MEMORY:
263 if(str->unicode && str->ubuffer)
265 int copycount = MIN(len, str->buflen - str->mark);
266 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
267 str->mark += copycount;
269 if(!str->unicode && str->buffer)
271 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
272 int copycount = MIN(len, str->buflen - str->mark);
273 memmove(str->buffer + str->mark, latin1, copycount);
275 str->mark += copycount;
278 str->write_count += len;
281 case STREAM_TYPE_FILE:
286 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
287 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
290 else /* Regular file */
292 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
293 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
297 else /* Text mode is the same for Unicode and regular files */
299 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
302 g_fprintf(str->file_pointer, "%s", utf8);
307 str->write_count += len;
310 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
315 * glk_put_char_stream:
316 * @str: An output stream.
317 * @ch: A character in Latin-1 encoding.
319 * The same as glk_put_char(), except that you specify a stream @str to print
320 * to, instead of using the current stream. It is illegal for @str to be %NULL,
321 * or an input-only stream.
324 glk_put_char_stream(strid_t str, unsigned char ch)
326 VALID_STREAM(str, return);
327 g_return_if_fail(str->file_mode != filemode_Read);
329 write_buffer_to_stream(str, (gchar *)&ch, 1);
333 * glk_put_char_stream_uni:
334 * @str: An output stream.
335 * @ch: A Unicode code point.
337 * The same as glk_put_char_uni(), except that you specify a stream @str to
338 * print to, instead of using the current stream. It is illegal for @str to be
339 * %NULL, or an input-only stream.
342 glk_put_char_stream_uni(strid_t str, glui32 ch)
344 VALID_STREAM(str, return);
345 g_return_if_fail(str->file_mode != filemode_Read);
347 write_buffer_to_stream_uni(str, &ch, 1);
351 * glk_put_string_stream:
352 * @str: An output stream.
353 * @s: A null-terminated string in Latin-1 encoding.
355 * The same as glk_put_string(), except that you specify a stream @str to print
356 * to, instead of using the current stream. It is illegal for @str to be %NULL,
357 * or an input-only stream.
360 glk_put_string_stream(strid_t str, char *s)
362 VALID_STREAM(str, return);
366 g_return_if_fail(str->file_mode != filemode_Read);
368 write_buffer_to_stream(str, s, strlen(s));
372 * glk_put_string_stream_uni:
373 * @str: An output stream.
374 * @s: A null-terminated array of Unicode code points.
376 * The same as glk_put_string_uni(), except that you specify a stream @str to
377 * print to, instead of using the current stream. It is illegal for @str to be
378 * %NULL, or an input-only stream.
381 glk_put_string_stream_uni(strid_t str, glui32 *s)
383 VALID_STREAM(str, return);
387 g_return_if_fail(str->file_mode != filemode_Read);
389 /* An impromptu strlen() for glui32 arrays */
394 write_buffer_to_stream_uni(str, s, len);
398 * glk_put_buffer_stream:
399 * @str: An output stream.
400 * @buf: An array of characters in Latin-1 encoding.
401 * @len: Length of @buf.
403 * The same as glk_put_buffer(), except that you specify a stream @str to print
404 * to, instead of using the current stream. It is illegal for @str to be %NULL,
405 * or an input-only stream.
408 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
410 VALID_STREAM(str, return);
414 g_return_if_fail(str->file_mode != filemode_Read);
416 write_buffer_to_stream(str, buf, len);
420 * glk_put_buffer_stream_uni:
421 * @str: An output stream.
422 * @buf: An array of Unicode code points.
423 * @len: Length of @buf.
425 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
426 * print to, instead of using the current stream. It is illegal for @str to be
427 * %NULL, or an input-only stream.
430 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
432 VALID_STREAM(str, return);
436 g_return_if_fail(str->file_mode != filemode_Read);
438 write_buffer_to_stream_uni(str, buf, len);
443 **************** READING FUNCTIONS ********************************************
447 /* Internal function: Read one big-endian four-byte character from file fp and
448 return it as a Unicode code point, or -1 on EOF */
450 read_ucs4be_char_from_file(FILE *fp)
452 unsigned char readbuffer[4];
453 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
456 readbuffer[0] << 24 |
457 readbuffer[1] << 16 |
462 /* Internal function: Read one UTF-8 character, which may be more than one byte,
463 from file fp and return it as a Unicode code point, or -1 on EOF */
465 read_utf8_char_from_file(FILE *fp)
467 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
469 gunichar charresult = (gunichar)-2;
470 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
475 readbuffer[foo] = (gchar)ch;
476 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
477 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
480 /* Silently return unknown characters as 0xFFFD, Replacement Character */
481 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
486 /* Internal function: Tell whether this code point is a Unicode newline. The
487 file pointer and eight-bit flag are included in case the newline is a CR
488 (U+000D). If the next character is LF (U+000A) then it also belongs to the
491 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
493 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
496 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
497 read_ucs4be_char_from_file(fp);
499 if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
500 WARNING_S("Seek failed on stream", g_strerror(errno) );
506 /* Internal function: Read one character from a stream. Returns a value which
507 can be returned unchanged by glk_get_char_stream_uni(), but
508 glk_get_char_stream() must replace high values by the placeholder character. */
510 get_char_stream_common(strid_t str)
514 case STREAM_TYPE_MEMORY:
517 if(!str->ubuffer || str->mark >= str->buflen)
519 glui32 ch = str->ubuffer[str->mark++];
525 if(!str->buffer || str->mark >= str->buflen)
527 unsigned char ch = str->buffer[str->mark++];
533 case STREAM_TYPE_FILE:
538 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
544 else /* Regular file */
546 int ch = fgetc(str->file_pointer);
554 else /* Text mode is the same for Unicode and regular files */
556 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
564 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
570 * glk_get_char_stream:
571 * @str: An input stream.
573 * Reads one character from the stream @str. (There is no notion of a
574 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
575 * an output-only stream.
577 * The result will be between 0 and 255. As with all basic text functions, Glk
578 * assumes the Latin-1 encoding. See <link
579 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
580 * of the stream has been reached, the result will be -1.
583 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
584 * returned as negative numbers.
587 * If the stream contains Unicode data — for example, if it was created
588 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
589 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
591 * It is usually more efficient to read several characters at once with
592 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
593 * glk_get_char_stream() several times.
595 * Returns: A character value between 0 and 255, or -1 on end of stream.
598 glk_get_char_stream(strid_t str)
600 VALID_STREAM(str, return -1);
601 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
603 glsi32 ch = get_char_stream_common(str);
604 return (ch > 0xFF)? PLACEHOLDER : ch;
608 * glk_get_char_stream_uni:
609 * @str: An input stream.
611 * Reads one character from the stream @str. The result will be between 0 and
612 * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
614 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
617 glk_get_char_stream_uni(strid_t str)
619 VALID_STREAM(str, return -1);
620 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
622 return get_char_stream_common(str);
626 * glk_get_buffer_stream:
627 * @str: An input stream.
628 * @buf: A buffer with space for at least @len characters.
629 * @len: The number of characters to read.
631 * Reads @len characters from @str, unless the end of stream is reached first.
632 * No terminal null is placed in the buffer.
634 * Returns: The number of characters actually read.
637 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
639 VALID_STREAM(str, return 0);
640 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
641 g_return_val_if_fail(buf != NULL, 0);
645 case STREAM_TYPE_MEMORY:
650 while(copycount < len && str->ubuffer && str->mark < str->buflen)
652 glui32 ch = str->ubuffer[str->mark++];
653 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
658 if(str->buffer) /* if not, copycount stays 0 */
659 copycount = MIN(len, str->buflen - str->mark);
660 memmove(buf, str->buffer + str->mark, copycount);
661 str->mark += copycount;
664 str->read_count += copycount;
667 case STREAM_TYPE_FILE:
670 if(str->unicode) /* Binary file with 4-byte characters */
672 /* Read len characters of 4 bytes each */
673 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
674 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
675 /* If there was an incomplete character */
679 WARNING("Incomplete character in binary Unicode file");
683 for(foo = 0; foo < count; foo += 4)
685 glsi32 ch = readbuffer[foo] << 24
686 | readbuffer[foo + 1] << 16
687 | readbuffer[foo + 2] << 8
688 | readbuffer[foo + 3];
689 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
692 str->read_count += count / 4;
695 else /* Regular binary file */
697 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
698 str->read_count += count;
702 else /* Text mode is the same for Unicode and regular files */
704 /* Do it character-by-character */
706 for(foo = 0; foo < len; foo++)
708 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
712 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
717 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
723 * glk_get_buffer_stream_uni:
724 * @str: An input stream.
725 * @buf: A buffer with space for at least @len Unicode code points.
726 * @len: The number of characters to read.
728 * Reads @len Unicode characters from @str, unless the end of stream is reached
729 * first. No terminal null is placed in the buffer.
731 * Returns: The number of Unicode characters actually read.
734 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
736 VALID_STREAM(str, return 0);
737 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
738 g_return_val_if_fail(buf != NULL, 0);
742 case STREAM_TYPE_MEMORY:
747 if(str->ubuffer) /* if not, copycount stays 0 */
748 copycount = MIN(len, str->buflen - str->mark);
749 memmove(buf, str->ubuffer + str->mark, copycount * 4);
750 str->mark += copycount;
754 while(copycount < len && str->buffer && str->mark < str->buflen)
756 unsigned char ch = str->buffer[str->mark++];
757 buf[copycount++] = ch;
761 str->read_count += copycount;
764 case STREAM_TYPE_FILE:
767 if(str->unicode) /* Binary file with 4-byte characters */
769 /* Read len characters of 4 bytes each */
770 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
771 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
772 /* If there was an incomplete character */
776 WARNING("Incomplete character in binary Unicode file");
780 for(foo = 0; foo < count; foo += 4)
781 buf[foo / 4] = readbuffer[foo] << 24
782 | readbuffer[foo + 1] << 16
783 | readbuffer[foo + 2] << 8
784 | readbuffer[foo + 3];
786 str->read_count += count / 4;
789 else /* Regular binary file */
791 unsigned char *readbuffer = g_new0(unsigned char, len);
792 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
794 for(foo = 0; foo < count; foo++)
795 buf[foo] = readbuffer[foo];
797 str->read_count += count;
801 else /* Text mode is the same for Unicode and regular files */
803 /* Do it character-by-character */
805 for(foo = 0; foo < len; foo++)
807 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
816 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
822 * glk_get_line_stream:
823 * @str: An input stream.
824 * @buf: A buffer with space for at least @len characters.
825 * @len: The number of characters to read, plus one.
827 * Reads characters from @str, until either
829 * <alt>@len - 1</alt>
830 * <mathphrase>@len - 1</mathphrase>
832 * characters have been read or a newline has been read. It then puts a
833 * terminal null (<code>'\0'</code>) aracter on
834 * the end. It returns the number of characters actually read, including the
835 * newline (if there is one) but not including the terminal null.
837 * Returns: The number of characters actually read.
840 glk_get_line_stream(strid_t str, char *buf, glui32 len)
842 VALID_STREAM(str, return 0);
843 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
844 g_return_val_if_fail(buf != NULL, 0);
848 case STREAM_TYPE_MEMORY:
853 /* Do it character-by-character */
854 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
856 glui32 ch = str->ubuffer[str->mark++];
857 /* Check for Unicode newline; slightly different than
859 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
861 buf[copycount++] = '\n';
866 if(str->ubuffer[str->mark] == 0x0A)
867 str->mark++; /* skip past next newline */
868 buf[copycount++] = '\n';
871 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
873 buf[copycount] = '\0';
877 if(str->buffer) /* if not, copycount stays 0 */
878 copycount = MIN(len - 1, str->buflen - str->mark);
879 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
880 if(endptr) /* newline was found */
881 copycount = endptr - buf; /* Real copy count */
882 buf[copycount] = '\0';
883 str->mark += copycount;
886 str->read_count += copycount;
889 case STREAM_TYPE_FILE:
892 if(str->unicode) /* Binary file with 4-byte characters */
894 /* Do it character-by-character */
896 for(foo = 0; foo < len - 1; foo++)
898 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
905 if(is_unicode_newline(ch, str->file_pointer, FALSE))
911 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
916 else /* Regular binary file */
918 if( !fgets(buf, len, str->file_pointer) ) {
923 int nread = strlen(buf);
924 str->read_count += nread;
928 else /* Text mode is the same for Unicode and regular files */
930 /* Do it character-by-character */
932 for(foo = 0; foo < len - 1; foo++)
934 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
941 if(is_unicode_newline(ch, str->file_pointer, TRUE))
947 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
953 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
959 * glk_get_line_stream_uni:
960 * @str: An input stream.
961 * @buf: A buffer with space for at least @len Unicode code points.
962 * @len: The number of characters to read, plus one.
964 * Reads Unicode characters from @str, until either
966 * <alt>@len - 1</alt>
967 * <mathphrase>@len - 1</mathphrase>
969 * Unicode characters have been read or a newline has been read. It then puts a
970 * terminal null (a zero value) on the end.
972 * Returns: The number of characters actually read, including the newline (if
973 * there is one) but not including the terminal null.
976 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
978 VALID_STREAM(str, return 0);
979 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
980 g_return_val_if_fail(buf != NULL, 0);
984 case STREAM_TYPE_MEMORY:
989 /* Do it character-by-character */
990 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
992 glui32 ch = str->ubuffer[str->mark++];
993 /* Check for Unicode newline; slightly different than
995 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
997 buf[copycount++] = '\n';
1002 if(str->ubuffer[str->mark] == 0x0A)
1003 str->mark++; /* skip past next newline */
1004 buf[copycount++] = '\n';
1007 buf[copycount++] = ch;
1009 buf[copycount] = '\0';
1013 /* No recourse to memccpy(), so do it character-by-character */
1014 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
1016 gchar ch = str->buffer[str->mark++];
1017 /* Check for newline */
1018 if(ch == '\n') /* Also check for \r and \r\n? */
1020 buf[copycount++] = '\n';
1023 buf[copycount++] = (unsigned char)ch;
1028 str->read_count += copycount;
1031 case STREAM_TYPE_FILE:
1034 if(str->unicode) /* Binary file with 4-byte characters */
1036 /* Do it character-by-character */
1038 for(foo = 0; foo < len - 1; foo++)
1040 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1047 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1049 buf[foo] = ch; /* Preserve newline types??? */
1058 else /* Regular binary file */
1060 gchar *readbuffer = g_new0(gchar, len);
1061 if( !fgets(readbuffer, len, str->file_pointer) ) {
1066 glui32 count = strlen(readbuffer);
1068 for(foo = 0; foo < count + 1; foo++) /* Copy terminator */
1069 buf[foo] = (unsigned char)(readbuffer[foo]);
1070 str->read_count += count;
1074 else /* Text mode is the same for Unicode and regular files */
1076 /* Do it character-by-character */
1078 for(foo = 0; foo < len - 1; foo++)
1080 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1087 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1089 buf[foo] = ch; /* Preserve newline types??? */
1099 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1106 **************** SEEKING FUNCTIONS ********************************************
1111 * glk_stream_get_position:
1112 * @str: A file or memory stream.
1114 * Returns the position of the read/write mark in @str. For memory streams and
1115 * binary file streams, this is exactly the number of characters read or written
1116 * from the beginning of the stream (unless you have moved the mark with
1117 * glk_stream_set_position().) For text file streams, matters are more
1118 * ambiguous, since (for example) writing one byte to a text file may store more
1119 * than one character in the platform's native encoding. You can only be sure
1120 * that the position increases as you read or write to the file.
1122 * Additional complication: for Latin-1 memory and file streams, a character is
1123 * a byte. For Unicode memory and file streams (those created by
1124 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1125 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1129 * If this bothers you, don't use binary Unicode files. I don't think they're
1130 * good for much anyhow.
1133 * Returns: position of the read/write mark in @str.
1136 glk_stream_get_position(strid_t str)
1138 VALID_STREAM(str, return 0);
1142 case STREAM_TYPE_MEMORY:
1144 case STREAM_TYPE_FILE:
1145 return ftell(str->file_pointer);
1147 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1153 * glk_stream_set_position:
1154 * @str: A file or memory stream.
1155 * @pos: The position to set the mark to, relative to @seekmode.
1156 * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1158 * Sets the position of the read/write mark in @str. The position is controlled
1159 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1160 * <code>seekmode_</code> constants below.
1162 * It is illegal to specify a position before the beginning or after the end of
1165 * In binary files, the mark position is exact — it corresponds with the
1166 * number of characters you have read or written. In text files, this mapping
1167 * can vary, because of linefeed conventions or other character-set
1168 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1169 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1170 * the platform's native encoding — after character cookery. Therefore,
1171 * in a text stream, it is safest to use glk_stream_set_position() only to move
1172 * to the beginning or end of a file, or to a position determined by
1173 * glk_stream_get_position().
1175 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1176 * characters are 32-bit words, or four bytes each.
1179 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1181 VALID_STREAM(str, return);
1182 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1183 g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1187 case STREAM_TYPE_MEMORY:
1190 case seekmode_Start: str->mark = pos; break;
1191 case seekmode_Current: str->mark += pos; break;
1192 case seekmode_End: str->mark = str->buflen + pos; break;
1194 g_return_if_reached();
1198 case STREAM_TYPE_FILE:
1203 case seekmode_Start: whence = SEEK_SET; break;
1204 case seekmode_Current: whence = SEEK_CUR; break;
1205 case seekmode_End: whence = SEEK_END; break;
1207 g_return_if_reached();
1210 if(fseek(str->file_pointer, pos, whence) == -1)
1211 WARNING("Seek failed on file stream");
1215 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);