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);
52 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
54 if(win->window_stream->hyperlink_mode) {
55 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
56 GtkTextTag *link_tag = win->current_hyperlink->tag;
57 gtk_text_buffer_insert_with_tags(buffer, &iter, win->buffer->str, -1, style_tag, glk_style_tag, link_style_tag, link_tag, NULL);
59 gtk_text_buffer_insert_with_tags(buffer, &iter, win->buffer->str, -1, style_tag, glk_style_tag, NULL);
62 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
64 g_signal_emit_by_name(glk, "text-buffer-output", win->rock, win->buffer->str);
68 case wintype_TextGrid:
70 /* Number of characters to insert */
71 glong length = win->buffer->len;
72 glong chars_left = length;
74 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
76 /* Get cursor position */
78 gtk_text_buffer_get_iter_at_mark(buffer, &start, cursor);
79 /* Spaces available on this line */
80 gint available_space = win->width - gtk_text_iter_get_line_offset(&start);
82 while(chars_left > available_space && !gtk_text_iter_is_end(&start))
84 GtkTextIter end = start;
85 gtk_text_iter_forward_to_line_end(&end);
86 gtk_text_buffer_delete(buffer, &start, &end);
88 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
89 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
90 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
92 if(win->window_stream->hyperlink_mode) {
93 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
94 GtkTextTag *link_tag = win->current_hyperlink->tag;
95 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), available_space, style_tag, glk_style_tag, link_style_tag, link_tag, NULL);
97 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), available_space, style_tag, glk_style_tag, NULL);
100 chars_left -= available_space;
101 gtk_text_iter_forward_line(&start);
102 available_space = win->width;
104 if(!gtk_text_iter_is_end(&start))
106 GtkTextIter end = start;
107 gtk_text_iter_forward_chars(&end, chars_left);
108 gtk_text_buffer_delete(buffer, &start, &end);
110 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
111 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
112 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
114 if(win->window_stream->hyperlink_mode) {
115 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
116 GtkTextTag *link_tag = win->current_hyperlink->tag;
117 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, style_tag, glk_style_tag, link_style_tag, link_tag, NULL);
119 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, style_tag, glk_style_tag, NULL);
123 gtk_text_buffer_move_mark(buffer, cursor, &start);
130 g_string_truncate(win->buffer, 0);
133 /* Internal function: write a Latin-1 buffer with length to a stream. */
135 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
139 case STREAM_TYPE_WINDOW:
140 /* Each window type has a different way of printing to it */
141 switch(str->window->type)
143 /* Printing to these windows' streams does nothing */
146 case wintype_Graphics:
147 str->write_count += len;
150 /* Text grid/buffer windows */
151 case wintype_TextGrid:
152 case wintype_TextBuffer:
154 gchar *utf8 = convert_latin1_to_utf8(buf, len);
156 write_utf8_to_window_buffer(str->window, utf8);
160 str->write_count += len;
163 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
166 /* Now write the same buffer to the window's echo stream */
167 if(str->window->echo_stream != NULL)
168 write_buffer_to_stream(str->window->echo_stream, buf, len);
172 case STREAM_TYPE_MEMORY:
173 if(str->unicode && str->ubuffer)
176 while(str->mark < str->buflen && foo < len)
177 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
179 if(!str->unicode && str->buffer)
181 int copycount = MIN(len, str->buflen - str->mark);
182 memmove(str->buffer + str->mark, buf, copycount);
183 str->mark += copycount;
186 str->write_count += len;
189 case STREAM_TYPE_FILE:
194 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
195 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
198 else /* Regular file */
200 fwrite(buf, sizeof(gchar), len, str->file_pointer);
203 else /* Text mode is the same for Unicode and regular files */
205 gchar *utf8 = convert_latin1_to_utf8(buf, len);
208 g_fprintf(str->file_pointer, "%s", utf8);
213 str->write_count += len;
216 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
220 /* Internal function: write a Unicode buffer with length to a stream. */
222 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
226 case STREAM_TYPE_WINDOW:
227 /* Each window type has a different way of printing to it */
228 switch(str->window->type)
230 /* Printing to these windows' streams does nothing */
233 case wintype_Graphics:
234 str->write_count += len;
237 /* Text grid/buffer windows */
238 case wintype_TextGrid:
239 case wintype_TextBuffer:
241 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
243 write_utf8_to_window_buffer(str->window, utf8);
247 str->write_count += len;
250 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
253 /* Now write the same buffer to the window's echo stream */
254 if(str->window->echo_stream != NULL)
255 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
259 case STREAM_TYPE_MEMORY:
260 if(str->unicode && str->ubuffer)
262 int copycount = MIN(len, str->buflen - str->mark);
263 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
264 str->mark += copycount;
266 if(!str->unicode && str->buffer)
268 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
269 int copycount = MIN(len, str->buflen - str->mark);
270 memmove(str->buffer + str->mark, latin1, copycount);
272 str->mark += copycount;
275 str->write_count += len;
278 case STREAM_TYPE_FILE:
283 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
284 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
287 else /* Regular file */
289 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
290 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
294 else /* Text mode is the same for Unicode and regular files */
296 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
299 g_fprintf(str->file_pointer, "%s", utf8);
304 str->write_count += len;
307 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
312 * glk_put_char_stream:
313 * @str: An output stream.
314 * @ch: A character in Latin-1 encoding.
316 * The same as glk_put_char(), except that you specify a stream @str to print
317 * to, instead of using the current stream. It is illegal for @str to be %NULL,
318 * or an input-only stream.
321 glk_put_char_stream(strid_t str, unsigned char ch)
323 VALID_STREAM(str, return);
324 g_return_if_fail(str->file_mode != filemode_Read);
326 write_buffer_to_stream(str, (gchar *)&ch, 1);
330 * glk_put_char_stream_uni:
331 * @str: An output stream.
332 * @ch: A Unicode code point.
334 * The same as glk_put_char_uni(), except that you specify a stream @str to
335 * print to, instead of using the current stream. It is illegal for @str to be
336 * %NULL, or an input-only stream.
339 glk_put_char_stream_uni(strid_t str, glui32 ch)
341 VALID_STREAM(str, return);
342 g_return_if_fail(str->file_mode != filemode_Read);
344 write_buffer_to_stream_uni(str, &ch, 1);
348 * glk_put_string_stream:
349 * @str: An output stream.
350 * @s: A null-terminated string in Latin-1 encoding.
352 * The same as glk_put_string(), except that you specify a stream @str to print
353 * to, instead of using the current stream. It is illegal for @str to be %NULL,
354 * or an input-only stream.
357 glk_put_string_stream(strid_t str, char *s)
359 VALID_STREAM(str, return);
363 g_return_if_fail(str->file_mode != filemode_Read);
365 write_buffer_to_stream(str, s, strlen(s));
369 * glk_put_string_stream_uni:
370 * @str: An output stream.
371 * @s: A null-terminated array of Unicode code points.
373 * The same as glk_put_string_uni(), except that you specify a stream @str to
374 * print to, instead of using the current stream. It is illegal for @str to be
375 * %NULL, or an input-only stream.
378 glk_put_string_stream_uni(strid_t str, glui32 *s)
380 VALID_STREAM(str, return);
384 g_return_if_fail(str->file_mode != filemode_Read);
386 /* An impromptu strlen() for glui32 arrays */
391 write_buffer_to_stream_uni(str, s, len);
395 * glk_put_buffer_stream:
396 * @str: An output stream.
397 * @buf: An array of characters in Latin-1 encoding.
398 * @len: Length of @buf.
400 * The same as glk_put_buffer(), except that you specify a stream @str to print
401 * to, instead of using the current stream. It is illegal for @str to be %NULL,
402 * or an input-only stream.
405 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
407 VALID_STREAM(str, return);
411 g_return_if_fail(str->file_mode != filemode_Read);
413 write_buffer_to_stream(str, buf, len);
417 * glk_put_buffer_stream_uni:
418 * @str: An output stream.
419 * @buf: An array of Unicode code points.
420 * @len: Length of @buf.
422 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
423 * print to, instead of using the current stream. It is illegal for @str to be
424 * %NULL, or an input-only stream.
427 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
429 VALID_STREAM(str, return);
433 g_return_if_fail(str->file_mode != filemode_Read);
435 write_buffer_to_stream_uni(str, buf, len);
440 **************** READING FUNCTIONS ********************************************
444 /* Internal function: Read one big-endian four-byte character from file fp and
445 return it as a Unicode code point, or -1 on EOF */
447 read_ucs4be_char_from_file(FILE *fp)
449 unsigned char readbuffer[4];
450 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
453 readbuffer[0] << 24 |
454 readbuffer[1] << 16 |
459 /* Internal function: Read one UTF-8 character, which may be more than one byte,
460 from file fp and return it as a Unicode code point, or -1 on EOF */
462 read_utf8_char_from_file(FILE *fp)
464 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
466 gunichar charresult = (gunichar)-2;
467 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
472 readbuffer[foo] = (gchar)ch;
473 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
474 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
477 /* Silently return unknown characters as 0xFFFD, Replacement Character */
478 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
483 /* Internal function: Tell whether this code point is a Unicode newline. The
484 file pointer and eight-bit flag are included in case the newline is a CR
485 (U+000D). If the next character is LF (U+000A) then it also belongs to the
488 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
490 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
493 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
494 read_ucs4be_char_from_file(fp);
496 if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
497 WARNING_S("Seek failed on stream", g_strerror(errno) );
503 /* Internal function: Read one character from a stream. Returns a value which
504 can be returned unchanged by glk_get_char_stream_uni(), but
505 glk_get_char_stream() must replace high values by the placeholder character. */
507 get_char_stream_common(strid_t str)
511 case STREAM_TYPE_MEMORY:
514 if(!str->ubuffer || str->mark >= str->buflen)
516 glui32 ch = str->ubuffer[str->mark++];
522 if(!str->buffer || str->mark >= str->buflen)
524 unsigned char ch = str->buffer[str->mark++];
530 case STREAM_TYPE_FILE:
535 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
541 else /* Regular file */
543 int ch = fgetc(str->file_pointer);
551 else /* Text mode is the same for Unicode and regular files */
553 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
561 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
567 * glk_get_char_stream:
568 * @str: An input stream.
570 * Reads one character from the stream @str. (There is no notion of a
571 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
572 * an output-only stream.
574 * The result will be between 0 and 255. As with all basic text functions, Glk
575 * assumes the Latin-1 encoding. See <link
576 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
577 * of the stream has been reached, the result will be -1.
580 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
581 * returned as negative numbers.
584 * If the stream contains Unicode data — for example, if it was created
585 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
586 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
588 * It is usually more efficient to read several characters at once with
589 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
590 * glk_get_char_stream() several times.
592 * Returns: A character value between 0 and 255, or -1 on end of stream.
595 glk_get_char_stream(strid_t str)
597 VALID_STREAM(str, return -1);
598 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
600 glsi32 ch = get_char_stream_common(str);
601 return (ch > 0xFF)? PLACEHOLDER : ch;
605 * glk_get_char_stream_uni:
606 * @str: An input stream.
608 * Reads one character from the stream @str. The result will be between 0 and
609 * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
611 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
614 glk_get_char_stream_uni(strid_t str)
616 VALID_STREAM(str, return -1);
617 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
619 return get_char_stream_common(str);
623 * glk_get_buffer_stream:
624 * @str: An input stream.
625 * @buf: A buffer with space for at least @len characters.
626 * @len: The number of characters to read.
628 * Reads @len characters from @str, unless the end of stream is reached first.
629 * No terminal null is placed in the buffer.
631 * Returns: The number of characters actually read.
634 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
636 VALID_STREAM(str, return 0);
637 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
638 g_return_val_if_fail(buf != NULL, 0);
642 case STREAM_TYPE_MEMORY:
647 while(copycount < len && str->ubuffer && str->mark < str->buflen)
649 glui32 ch = str->ubuffer[str->mark++];
650 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
655 if(str->buffer) /* if not, copycount stays 0 */
656 copycount = MIN(len, str->buflen - str->mark);
657 memmove(buf, str->buffer + str->mark, copycount);
658 str->mark += copycount;
661 str->read_count += copycount;
664 case STREAM_TYPE_FILE:
667 if(str->unicode) /* Binary file with 4-byte characters */
669 /* Read len characters of 4 bytes each */
670 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
671 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
672 /* If there was an incomplete character */
676 WARNING("Incomplete character in binary Unicode file");
680 for(foo = 0; foo < count; foo += 4)
682 glsi32 ch = readbuffer[foo] << 24
683 | readbuffer[foo + 1] << 16
684 | readbuffer[foo + 2] << 8
685 | readbuffer[foo + 3];
686 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
689 str->read_count += count / 4;
692 else /* Regular binary file */
694 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
695 str->read_count += count;
699 else /* Text mode is the same for Unicode and regular files */
701 /* Do it character-by-character */
703 for(foo = 0; foo < len; foo++)
705 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
709 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
714 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
720 * glk_get_buffer_stream_uni:
721 * @str: An input stream.
722 * @buf: A buffer with space for at least @len Unicode code points.
723 * @len: The number of characters to read.
725 * Reads @len Unicode characters from @str, unless the end of stream is reached
726 * first. No terminal null is placed in the buffer.
728 * Returns: The number of Unicode characters actually read.
731 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
733 VALID_STREAM(str, return 0);
734 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
735 g_return_val_if_fail(buf != NULL, 0);
739 case STREAM_TYPE_MEMORY:
744 if(str->ubuffer) /* if not, copycount stays 0 */
745 copycount = MIN(len, str->buflen - str->mark);
746 memmove(buf, str->ubuffer + str->mark, copycount * 4);
747 str->mark += copycount;
751 while(copycount < len && str->buffer && str->mark < str->buflen)
753 unsigned char ch = str->buffer[str->mark++];
754 buf[copycount++] = ch;
758 str->read_count += copycount;
761 case STREAM_TYPE_FILE:
764 if(str->unicode) /* Binary file with 4-byte characters */
766 /* Read len characters of 4 bytes each */
767 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
768 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
769 /* If there was an incomplete character */
773 WARNING("Incomplete character in binary Unicode file");
777 for(foo = 0; foo < count; foo += 4)
778 buf[foo / 4] = readbuffer[foo] << 24
779 | readbuffer[foo + 1] << 16
780 | readbuffer[foo + 2] << 8
781 | readbuffer[foo + 3];
783 str->read_count += count / 4;
786 else /* Regular binary file */
788 unsigned char *readbuffer = g_new0(unsigned char, len);
789 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
791 for(foo = 0; foo < count; foo++)
792 buf[foo] = readbuffer[foo];
794 str->read_count += count;
798 else /* Text mode is the same for Unicode and regular files */
800 /* Do it character-by-character */
802 for(foo = 0; foo < len; foo++)
804 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
813 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
819 * glk_get_line_stream:
820 * @str: An input stream.
821 * @buf: A buffer with space for at least @len characters.
822 * @len: The number of characters to read, plus one.
824 * Reads characters from @str, until either
826 * <alt>@len - 1</alt>
827 * <mathphrase>@len - 1</mathphrase>
829 * characters have been read or a newline has been read. It then puts a
830 * terminal null (<code>'\0'</code>) aracter on
831 * the end. It returns the number of characters actually read, including the
832 * newline (if there is one) but not including the terminal null.
834 * Returns: The number of characters actually read.
837 glk_get_line_stream(strid_t str, char *buf, glui32 len)
839 VALID_STREAM(str, return 0);
840 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
841 g_return_val_if_fail(buf != NULL, 0);
845 case STREAM_TYPE_MEMORY:
850 /* Do it character-by-character */
851 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
853 glui32 ch = str->ubuffer[str->mark++];
854 /* Check for Unicode newline; slightly different than
856 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
858 buf[copycount++] = '\n';
863 if(str->ubuffer[str->mark] == 0x0A)
864 str->mark++; /* skip past next newline */
865 buf[copycount++] = '\n';
868 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
870 buf[copycount] = '\0';
874 if(str->buffer) /* if not, copycount stays 0 */
875 copycount = MIN(len - 1, str->buflen - str->mark);
876 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
877 if(endptr) /* newline was found */
878 copycount = endptr - buf; /* Real copy count */
879 buf[copycount] = '\0';
880 str->mark += copycount;
883 str->read_count += copycount;
886 case STREAM_TYPE_FILE:
889 if(str->unicode) /* Binary file with 4-byte characters */
891 /* Do it character-by-character */
893 for(foo = 0; foo < len - 1; foo++)
895 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
902 if(is_unicode_newline(ch, str->file_pointer, FALSE))
908 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
913 else /* Regular binary file */
915 if( !fgets(buf, len, str->file_pointer) ) {
920 int nread = strlen(buf);
921 str->read_count += nread;
925 else /* Text mode is the same for Unicode and regular files */
927 /* Do it character-by-character */
929 for(foo = 0; foo < len - 1; foo++)
931 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
938 if(is_unicode_newline(ch, str->file_pointer, TRUE))
944 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
950 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
956 * glk_get_line_stream_uni:
957 * @str: An input stream.
958 * @buf: A buffer with space for at least @len Unicode code points.
959 * @len: The number of characters to read, plus one.
961 * Reads Unicode characters from @str, until either
963 * <alt>@len - 1</alt>
964 * <mathphrase>@len - 1</mathphrase>
966 * Unicode characters have been read or a newline has been read. It then puts a
967 * terminal null (a zero value) on the end.
969 * Returns: The number of characters actually read, including the newline (if
970 * there is one) but not including the terminal null.
973 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
975 VALID_STREAM(str, return 0);
976 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
977 g_return_val_if_fail(buf != NULL, 0);
981 case STREAM_TYPE_MEMORY:
986 /* Do it character-by-character */
987 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
989 glui32 ch = str->ubuffer[str->mark++];
990 /* Check for Unicode newline; slightly different than
992 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
994 buf[copycount++] = '\n';
999 if(str->ubuffer[str->mark] == 0x0A)
1000 str->mark++; /* skip past next newline */
1001 buf[copycount++] = '\n';
1004 buf[copycount++] = ch;
1006 buf[copycount] = '\0';
1010 /* No recourse to memccpy(), so do it character-by-character */
1011 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
1013 gchar ch = str->buffer[str->mark++];
1014 /* Check for newline */
1015 if(ch == '\n') /* Also check for \r and \r\n? */
1017 buf[copycount++] = '\n';
1020 buf[copycount++] = (unsigned char)ch;
1025 str->read_count += copycount;
1028 case STREAM_TYPE_FILE:
1031 if(str->unicode) /* Binary file with 4-byte characters */
1033 /* Do it character-by-character */
1035 for(foo = 0; foo < len - 1; foo++)
1037 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1044 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1046 buf[foo] = ch; /* Preserve newline types??? */
1055 else /* Regular binary file */
1057 gchar *readbuffer = g_new0(gchar, len);
1058 if( !fgets(readbuffer, len, str->file_pointer) ) {
1063 glui32 count = strlen(readbuffer);
1065 for(foo = 0; foo < count + 1; foo++) /* Copy terminator */
1066 buf[foo] = (unsigned char)(readbuffer[foo]);
1067 str->read_count += count;
1071 else /* Text mode is the same for Unicode and regular files */
1073 /* Do it character-by-character */
1075 for(foo = 0; foo < len - 1; foo++)
1077 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1084 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1086 buf[foo] = ch; /* Preserve newline types??? */
1096 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1103 **************** SEEKING FUNCTIONS ********************************************
1108 * glk_stream_get_position:
1109 * @str: A file or memory stream.
1111 * Returns the position of the read/write mark in @str. For memory streams and
1112 * binary file streams, this is exactly the number of characters read or written
1113 * from the beginning of the stream (unless you have moved the mark with
1114 * glk_stream_set_position().) For text file streams, matters are more
1115 * ambiguous, since (for example) writing one byte to a text file may store more
1116 * than one character in the platform's native encoding. You can only be sure
1117 * that the position increases as you read or write to the file.
1119 * Additional complication: for Latin-1 memory and file streams, a character is
1120 * a byte. For Unicode memory and file streams (those created by
1121 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1122 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1126 * If this bothers you, don't use binary Unicode files. I don't think they're
1127 * good for much anyhow.
1130 * Returns: position of the read/write mark in @str.
1133 glk_stream_get_position(strid_t str)
1135 VALID_STREAM(str, return 0);
1139 case STREAM_TYPE_MEMORY:
1141 case STREAM_TYPE_FILE:
1142 return ftell(str->file_pointer);
1144 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1150 * glk_stream_set_position:
1151 * @str: A file or memory stream.
1152 * @pos: The position to set the mark to, relative to @seekmode.
1153 * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1155 * Sets the position of the read/write mark in @str. The position is controlled
1156 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1157 * <code>seekmode_</code> constants below.
1159 * It is illegal to specify a position before the beginning or after the end of
1162 * In binary files, the mark position is exact — it corresponds with the
1163 * number of characters you have read or written. In text files, this mapping
1164 * can vary, because of linefeed conventions or other character-set
1165 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1166 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1167 * the platform's native encoding — after character cookery. Therefore,
1168 * in a text stream, it is safest to use glk_stream_set_position() only to move
1169 * to the beginning or end of a file, or to a position determined by
1170 * glk_stream_get_position().
1172 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1173 * characters are 32-bit words, or four bytes each.
1176 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1178 VALID_STREAM(str, return);
1179 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1180 g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1184 case STREAM_TYPE_MEMORY:
1187 case seekmode_Start: str->mark = pos; break;
1188 case seekmode_Current: str->mark += pos; break;
1189 case seekmode_End: str->mark = str->buflen + pos; break;
1191 g_return_if_reached();
1195 case STREAM_TYPE_FILE:
1200 case seekmode_Start: whence = SEEK_SET; break;
1201 case seekmode_Current: whence = SEEK_CUR; break;
1202 case seekmode_End: whence = SEEK_END; break;
1204 g_return_if_reached();
1207 if(fseek(str->file_pointer, pos, whence) == -1)
1208 WARNING("Seek failed on file stream");
1212 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);