8 #include <glib/gstdio.h>
12 **************** WRITING FUNCTIONS ********************************************
16 /* Internal function: write a UTF-8 string to a text buffer window's text buffer. */
18 write_utf8_to_window_buffer(winid_t win, gchar *s)
20 if(win->input_request_type == INPUT_REQUEST_LINE || win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
22 ILLEGAL("Tried to print to a text buffer window with line input pending.");
26 // Write to the buffer
27 g_string_append(win->buffer, s);
30 /* Internal function: flush a window's text buffer to the screen. */
32 flush_window_buffer(winid_t win)
35 g_printf("%s\n", win->buffer->str);
37 if(win->type != wintype_TextBuffer && win->type != wintype_TextGrid)
40 if(win->buffer->len == 0)
45 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
48 case wintype_TextBuffer:
50 GtkTextIter start, end;
51 gtk_text_buffer_get_end_iter(buffer, &end);
54 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
56 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
57 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
58 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
60 start_offset = gtk_text_iter_get_offset(&end);
61 gtk_text_buffer_insert(buffer, &end, win->buffer->str, -1);
62 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
64 // Player's style overrides
65 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &end);
67 // GLK Program's style overrides
68 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &end);
71 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &end);
73 // Link style overrides
74 if(win->window_stream->hyperlink_mode) {
75 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
76 GtkTextTag *link_tag = win->current_hyperlink->tag;
77 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &end);
78 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &end);
81 // GLK Program's style overrides using garglk_set_zcolors()
82 if(win->zcolor != NULL) {
83 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &end);
86 // GLK Program's style overrides using garglk_set_reversevideo()
87 if(win->zcolor_reversed != NULL) {
88 gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &end);
91 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
93 g_signal_emit_by_name(glk, "text-buffer-output", win->rock, win->buffer->str);
97 case wintype_TextGrid:
99 /* Number of characters to insert */
100 glong length = win->buffer->len;
101 glong chars_left = length;
103 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
105 /* Get cursor position */
106 GtkTextIter start, insert;
109 gtk_text_buffer_get_iter_at_mark(buffer, &insert, cursor);
110 /* Spaces available on this line */
111 gint available_space = win->width - gtk_text_iter_get_line_offset(&insert);
113 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
115 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
116 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
117 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
118 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
120 while(chars_left > available_space && !gtk_text_iter_is_end(&insert))
122 GtkTextIter end = insert;
123 gtk_text_iter_forward_to_line_end(&end);
124 gtk_text_buffer_delete(buffer, &insert, &end);
126 start_offset = gtk_text_iter_get_offset(&insert);
127 gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), available_space);
128 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
131 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
133 // Player's style overrides
134 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
136 // GLK Program's style overrides
137 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
139 // Link style overrides
140 if(win->window_stream->hyperlink_mode) {
141 GtkTextTag *link_tag = win->current_hyperlink->tag;
142 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
143 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
146 // GLK Program's style overrides using garglk_set_zcolors()
147 if(win->zcolor != NULL)
148 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
150 // GLK Program's style overrides using garglk_set_reversevideo()
151 if(win->zcolor_reversed != NULL) {
152 gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &insert);
155 chars_left -= available_space;
156 gtk_text_iter_forward_line(&insert);
157 available_space = win->width;
159 if(!gtk_text_iter_is_end(&insert))
161 GtkTextIter end = insert;
162 gtk_text_iter_forward_chars(&end, chars_left);
163 gtk_text_buffer_delete(buffer, &insert, &end);
165 start_offset = gtk_text_iter_get_offset(&insert);
166 gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), -1);
167 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
170 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
172 // Player's style overrides
173 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
175 // GLK Program's style overrides
176 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
178 // Link style overrides
179 if(win->window_stream->hyperlink_mode) {
180 GtkTextTag *link_tag = win->current_hyperlink->tag;
181 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
182 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
185 // GLK Program's style overrides using garglk_set_zcolors()
186 if(win->zcolor != NULL)
187 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
189 // GLK Program's style overrides using garglk_set_reversevideo()
190 if(win->zcolor_reversed != NULL) {
191 gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &insert);
195 gtk_text_buffer_move_mark(buffer, cursor, &start);
202 g_string_truncate(win->buffer, 0);
205 /* Internal function: write a Latin-1 buffer with length to a stream. */
207 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
211 case STREAM_TYPE_WINDOW:
212 /* Each window type has a different way of printing to it */
213 switch(str->window->type)
215 /* Printing to these windows' streams does nothing */
218 case wintype_Graphics:
219 str->write_count += len;
222 /* Text grid/buffer windows */
223 case wintype_TextGrid:
225 gchar *utf8 = convert_latin1_to_utf8(buf, len);
227 /* Deal with newlines */
230 for(i=0; i<len; i++) {
231 if(utf8[i] == '\n') {
233 write_utf8_to_window_buffer(str->window, line);
234 flush_window_buffer(str->window);
236 /* Move cursor position forward to the next line */
238 GtkTextIter cursor_pos;
239 GtkTextView *textview = GTK_TEXT_VIEW(str->window->widget);
240 GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
241 GtkTextMark *cursor_mark = gtk_text_buffer_get_mark(buffer, "cursor_position");
243 gtk_text_buffer_get_iter_at_mark( buffer, &cursor_pos, cursor_mark);
244 gtk_text_view_forward_display_line(textview, &cursor_pos);
245 gtk_text_view_backward_display_line_start(textview, &cursor_pos);
246 gtk_text_buffer_move_mark(buffer, cursor_mark, &cursor_pos);
249 line = utf8 + (i < len-1 ? (i+1):(len-1));
253 /* No more newlines left. */
254 write_utf8_to_window_buffer(str->window, line);
258 str->write_count += len;
262 case wintype_TextBuffer:
264 gchar *utf8 = convert_latin1_to_utf8(buf, len);
266 write_utf8_to_window_buffer(str->window, utf8);
270 str->write_count += len;
273 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
276 /* Now write the same buffer to the window's echo stream */
277 if(str->window->echo_stream != NULL)
278 write_buffer_to_stream(str->window->echo_stream, buf, len);
282 case STREAM_TYPE_MEMORY:
283 if(str->unicode && str->ubuffer)
286 while(str->mark < str->buflen && foo < len)
287 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
289 if(!str->unicode && str->buffer)
291 int copycount = MIN(len, str->buflen - str->mark);
292 memmove(str->buffer + str->mark, buf, copycount);
293 str->mark += copycount;
296 str->write_count += len;
299 case STREAM_TYPE_FILE:
304 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
305 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
308 else /* Regular file */
310 fwrite(buf, sizeof(gchar), len, str->file_pointer);
313 else /* Text mode is the same for Unicode and regular files */
315 gchar *utf8 = convert_latin1_to_utf8(buf, len);
318 g_fprintf(str->file_pointer, "%s", utf8);
323 str->write_count += len;
326 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
330 /* Internal function: write a Unicode buffer with length to a stream. */
332 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
336 case STREAM_TYPE_WINDOW:
337 /* Each window type has a different way of printing to it */
338 switch(str->window->type)
340 /* Printing to these windows' streams does nothing */
343 case wintype_Graphics:
344 str->write_count += len;
347 /* Text grid/buffer windows */
348 case wintype_TextGrid:
349 case wintype_TextBuffer:
351 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
353 write_utf8_to_window_buffer(str->window, utf8);
357 str->write_count += len;
360 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
363 /* Now write the same buffer to the window's echo stream */
364 if(str->window->echo_stream != NULL)
365 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
369 case STREAM_TYPE_MEMORY:
370 if(str->unicode && str->ubuffer)
372 int copycount = MIN(len, str->buflen - str->mark);
373 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
374 str->mark += copycount;
376 if(!str->unicode && str->buffer)
378 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
379 int copycount = MIN(len, str->buflen - str->mark);
380 memmove(str->buffer + str->mark, latin1, copycount);
382 str->mark += copycount;
385 str->write_count += len;
388 case STREAM_TYPE_FILE:
393 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
394 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
397 else /* Regular file */
399 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
400 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
404 else /* Text mode is the same for Unicode and regular files */
406 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
409 g_fprintf(str->file_pointer, "%s", utf8);
414 str->write_count += len;
417 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
422 * glk_put_char_stream:
423 * @str: An output stream.
424 * @ch: A character in Latin-1 encoding.
426 * The same as glk_put_char(), except that you specify a stream @str to print
427 * to, instead of using the current stream. It is illegal for @str to be %NULL,
428 * or an input-only stream.
431 glk_put_char_stream(strid_t str, unsigned char ch)
433 VALID_STREAM(str, return);
434 g_return_if_fail(str->file_mode != filemode_Read);
436 write_buffer_to_stream(str, (gchar *)&ch, 1);
440 * glk_put_char_stream_uni:
441 * @str: An output stream.
442 * @ch: A Unicode code point.
444 * The same as glk_put_char_uni(), except that you specify a stream @str to
445 * print to, instead of using the current stream. It is illegal for @str to be
446 * %NULL, or an input-only stream.
449 glk_put_char_stream_uni(strid_t str, glui32 ch)
451 VALID_STREAM(str, return);
452 g_return_if_fail(str->file_mode != filemode_Read);
454 write_buffer_to_stream_uni(str, &ch, 1);
458 * glk_put_string_stream:
459 * @str: An output stream.
460 * @s: A null-terminated string in Latin-1 encoding.
462 * The same as glk_put_string(), except that you specify a stream @str to print
463 * to, instead of using the current stream. It is illegal for @str to be %NULL,
464 * or an input-only stream.
467 glk_put_string_stream(strid_t str, char *s)
469 VALID_STREAM(str, return);
473 g_return_if_fail(str->file_mode != filemode_Read);
475 write_buffer_to_stream(str, s, strlen(s));
479 * glk_put_string_stream_uni:
480 * @str: An output stream.
481 * @s: A null-terminated array of Unicode code points.
483 * The same as glk_put_string_uni(), except that you specify a stream @str to
484 * print to, instead of using the current stream. It is illegal for @str to be
485 * %NULL, or an input-only stream.
488 glk_put_string_stream_uni(strid_t str, glui32 *s)
490 VALID_STREAM(str, return);
494 g_return_if_fail(str->file_mode != filemode_Read);
496 /* An impromptu strlen() for glui32 arrays */
501 write_buffer_to_stream_uni(str, s, len);
505 * glk_put_buffer_stream:
506 * @str: An output stream.
507 * @buf: An array of characters in Latin-1 encoding.
508 * @len: Length of @buf.
510 * The same as glk_put_buffer(), except that you specify a stream @str to print
511 * to, instead of using the current stream. It is illegal for @str to be %NULL,
512 * or an input-only stream.
515 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
517 VALID_STREAM(str, return);
521 g_return_if_fail(str->file_mode != filemode_Read);
523 write_buffer_to_stream(str, buf, len);
527 * glk_put_buffer_stream_uni:
528 * @str: An output stream.
529 * @buf: An array of Unicode code points.
530 * @len: Length of @buf.
532 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
533 * print to, instead of using the current stream. It is illegal for @str to be
534 * %NULL, or an input-only stream.
537 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
539 VALID_STREAM(str, return);
543 g_return_if_fail(str->file_mode != filemode_Read);
545 write_buffer_to_stream_uni(str, buf, len);
550 **************** READING FUNCTIONS ********************************************
554 /* Internal function: Read one big-endian four-byte character from file fp and
555 return it as a Unicode code point, or -1 on EOF */
557 read_ucs4be_char_from_file(FILE *fp)
559 unsigned char readbuffer[4];
560 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
563 readbuffer[0] << 24 |
564 readbuffer[1] << 16 |
569 /* Internal function: Read one UTF-8 character, which may be more than one byte,
570 from file fp and return it as a Unicode code point, or -1 on EOF */
572 read_utf8_char_from_file(FILE *fp)
574 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
576 gunichar charresult = (gunichar)-2;
577 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
582 readbuffer[foo] = (gchar)ch;
583 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
584 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
587 /* Silently return unknown characters as 0xFFFD, Replacement Character */
588 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
593 /* Internal function: Tell whether this code point is a Unicode newline. The
594 file pointer and eight-bit flag are included in case the newline is a CR
595 (U+000D). If the next character is LF (U+000A) then it also belongs to the
598 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
600 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
603 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
604 read_ucs4be_char_from_file(fp);
606 if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
607 WARNING_S("Seek failed on stream", g_strerror(errno) );
613 /* Internal function: Read one character from a stream. Returns a value which
614 can be returned unchanged by glk_get_char_stream_uni(), but
615 glk_get_char_stream() must replace high values by the placeholder character. */
617 get_char_stream_common(strid_t str)
621 case STREAM_TYPE_MEMORY:
624 if(!str->ubuffer || str->mark >= str->buflen)
626 glui32 ch = str->ubuffer[str->mark++];
632 if(!str->buffer || str->mark >= str->buflen)
634 unsigned char ch = str->buffer[str->mark++];
640 case STREAM_TYPE_FILE:
645 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
651 else /* Regular file */
653 int ch = fgetc(str->file_pointer);
661 else /* Text mode is the same for Unicode and regular files */
663 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
671 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
677 * glk_get_char_stream:
678 * @str: An input stream.
680 * Reads one character from the stream @str. (There is no notion of a
681 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
682 * an output-only stream.
684 * The result will be between 0 and 255. As with all basic text functions, Glk
685 * assumes the Latin-1 encoding. See <link
686 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
687 * of the stream has been reached, the result will be -1.
690 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
691 * returned as negative numbers.
694 * If the stream contains Unicode data — for example, if it was created
695 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
696 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
698 * It is usually more efficient to read several characters at once with
699 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
700 * glk_get_char_stream() several times.
702 * Returns: A character value between 0 and 255, or -1 on end of stream.
705 glk_get_char_stream(strid_t str)
707 VALID_STREAM(str, return -1);
708 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
710 glsi32 ch = get_char_stream_common(str);
711 return (ch > 0xFF)? PLACEHOLDER : ch;
715 * glk_get_char_stream_uni:
716 * @str: An input stream.
718 * Reads one character from the stream @str. If the end of the stream has been
719 * reached, the result will be -1.
721 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
724 glk_get_char_stream_uni(strid_t str)
726 VALID_STREAM(str, return -1);
727 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
729 return get_char_stream_common(str);
733 * glk_get_buffer_stream:
734 * @str: An input stream.
735 * @buf: A buffer with space for at least @len characters.
736 * @len: The number of characters to read.
738 * Reads @len characters from @str, unless the end of stream is reached first.
739 * No terminal null is placed in the buffer.
741 * Returns: The number of characters actually read.
744 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
746 VALID_STREAM(str, return 0);
747 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
748 g_return_val_if_fail(buf != NULL, 0);
752 case STREAM_TYPE_MEMORY:
757 while(copycount < len && str->ubuffer && str->mark < str->buflen)
759 glui32 ch = str->ubuffer[str->mark++];
760 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
765 if(str->buffer) /* if not, copycount stays 0 */
766 copycount = MIN(len, str->buflen - str->mark);
767 memmove(buf, str->buffer + str->mark, copycount);
768 str->mark += copycount;
771 str->read_count += copycount;
774 case STREAM_TYPE_FILE:
777 if(str->unicode) /* Binary file with 4-byte characters */
779 /* Read len characters of 4 bytes each */
780 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
781 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
782 /* If there was an incomplete character */
786 WARNING("Incomplete character in binary Unicode file");
790 for(foo = 0; foo < count; foo += 4)
792 glsi32 ch = readbuffer[foo] << 24
793 | readbuffer[foo + 1] << 16
794 | readbuffer[foo + 2] << 8
795 | readbuffer[foo + 3];
796 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
799 str->read_count += count / 4;
802 else /* Regular binary file */
804 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
805 str->read_count += count;
809 else /* Text mode is the same for Unicode and regular files */
811 /* Do it character-by-character */
813 for(foo = 0; foo < len; foo++)
815 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
819 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
824 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
830 * glk_get_buffer_stream_uni:
831 * @str: An input stream.
832 * @buf: A buffer with space for at least @len Unicode code points.
833 * @len: The number of characters to read.
835 * Reads @len Unicode characters from @str, unless the end of stream is reached
836 * first. No terminal null is placed in the buffer.
838 * Returns: The number of Unicode characters actually read.
841 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
843 VALID_STREAM(str, return 0);
844 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
845 g_return_val_if_fail(buf != NULL, 0);
849 case STREAM_TYPE_MEMORY:
854 if(str->ubuffer) /* if not, copycount stays 0 */
855 copycount = MIN(len, str->buflen - str->mark);
856 memmove(buf, str->ubuffer + str->mark, copycount * 4);
857 str->mark += copycount;
861 while(copycount < len && str->buffer && str->mark < str->buflen)
863 unsigned char ch = str->buffer[str->mark++];
864 buf[copycount++] = ch;
868 str->read_count += copycount;
871 case STREAM_TYPE_FILE:
874 if(str->unicode) /* Binary file with 4-byte characters */
876 /* Read len characters of 4 bytes each */
877 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
878 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
879 /* If there was an incomplete character */
883 WARNING("Incomplete character in binary Unicode file");
887 for(foo = 0; foo < count; foo += 4)
888 buf[foo / 4] = readbuffer[foo] << 24
889 | readbuffer[foo + 1] << 16
890 | readbuffer[foo + 2] << 8
891 | readbuffer[foo + 3];
893 str->read_count += count / 4;
896 else /* Regular binary file */
898 unsigned char *readbuffer = g_new0(unsigned char, len);
899 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
901 for(foo = 0; foo < count; foo++)
902 buf[foo] = readbuffer[foo];
904 str->read_count += count;
908 else /* Text mode is the same for Unicode and regular files */
910 /* Do it character-by-character */
912 for(foo = 0; foo < len; foo++)
914 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
923 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
929 * glk_get_line_stream:
930 * @str: An input stream.
931 * @buf: A buffer with space for at least @len characters.
932 * @len: The number of characters to read, plus one.
934 * Reads characters from @str, until either
936 * <alt>@len - 1</alt>
937 * <mathphrase>@len - 1</mathphrase>
939 * characters have been read or a newline has been read. It then puts a
940 * terminal null (<code>'\0'</code>) aracter on
941 * the end. It returns the number of characters actually read, including the
942 * newline (if there is one) but not including the terminal null.
944 * Returns: The number of characters actually read.
947 glk_get_line_stream(strid_t str, char *buf, glui32 len)
949 VALID_STREAM(str, return 0);
950 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
951 g_return_val_if_fail(buf != NULL, 0);
955 case STREAM_TYPE_MEMORY:
960 /* Do it character-by-character */
961 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
963 glui32 ch = str->ubuffer[str->mark++];
964 /* Check for Unicode newline; slightly different than
966 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
968 buf[copycount++] = '\n';
973 if(str->ubuffer[str->mark] == 0x0A)
974 str->mark++; /* skip past next newline */
975 buf[copycount++] = '\n';
978 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
980 buf[copycount] = '\0';
984 if(str->buffer) /* if not, copycount stays 0 */
985 copycount = MIN(len - 1, str->buflen - str->mark);
986 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
987 if(endptr) /* newline was found */
988 copycount = endptr - buf; /* Real copy count */
989 buf[copycount] = '\0';
990 str->mark += copycount;
993 str->read_count += copycount;
996 case STREAM_TYPE_FILE:
999 if(str->unicode) /* Binary file with 4-byte characters */
1001 /* Do it character-by-character */
1003 for(foo = 0; foo < len - 1; foo++)
1005 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1012 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1015 buf[foo + 1] = '\0';
1018 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
1023 else /* Regular binary file */
1025 if( !fgets(buf, len, str->file_pointer) ) {
1030 int nread = strlen(buf);
1031 str->read_count += nread;
1035 else /* Text mode is the same for Unicode and regular files */
1037 /* Do it character-by-character */
1039 for(foo = 0; foo < len - 1; foo++)
1041 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1048 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1051 buf[foo + 1] = '\0';
1054 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
1060 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1066 * glk_get_line_stream_uni:
1067 * @str: An input stream.
1068 * @buf: A buffer with space for at least @len Unicode code points.
1069 * @len: The number of characters to read, plus one.
1071 * Reads Unicode characters from @str, until either
1073 * <alt>@len - 1</alt>
1074 * <mathphrase>@len - 1</mathphrase>
1076 * Unicode characters have been read or a newline has been read. It then puts a
1077 * terminal null (a zero value) on the end.
1079 * Returns: The number of characters actually read, including the newline (if
1080 * there is one) but not including the terminal null.
1083 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
1085 VALID_STREAM(str, return 0);
1086 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
1087 g_return_val_if_fail(buf != NULL, 0);
1091 case STREAM_TYPE_MEMORY:
1096 /* Do it character-by-character */
1097 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
1099 glui32 ch = str->ubuffer[str->mark++];
1100 /* Check for Unicode newline; slightly different than
1102 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
1104 buf[copycount++] = '\n';
1109 if(str->ubuffer[str->mark] == 0x0A)
1110 str->mark++; /* skip past next newline */
1111 buf[copycount++] = '\n';
1114 buf[copycount++] = ch;
1116 buf[copycount] = '\0';
1120 /* No recourse to memccpy(), so do it character-by-character */
1121 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
1123 gchar ch = str->buffer[str->mark++];
1124 /* Check for newline */
1125 if(ch == '\n') /* Also check for \r and \r\n? */
1127 buf[copycount++] = '\n';
1130 buf[copycount++] = (unsigned char)ch;
1135 str->read_count += copycount;
1138 case STREAM_TYPE_FILE:
1141 if(str->unicode) /* Binary file with 4-byte characters */
1143 /* Do it character-by-character */
1145 for(foo = 0; foo < len - 1; foo++)
1147 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1154 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1156 buf[foo] = ch; /* Preserve newline types??? */
1165 else /* Regular binary file */
1167 gchar *readbuffer = g_new0(gchar, len);
1168 if( !fgets(readbuffer, len, str->file_pointer) ) {
1173 glui32 count = strlen(readbuffer);
1175 for(foo = 0; foo < count + 1; foo++) /* Copy terminator */
1176 buf[foo] = (unsigned char)(readbuffer[foo]);
1177 str->read_count += count;
1181 else /* Text mode is the same for Unicode and regular files */
1183 /* Do it character-by-character */
1185 for(foo = 0; foo < len - 1; foo++)
1187 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1194 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1196 buf[foo] = ch; /* Preserve newline types??? */
1206 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1213 **************** SEEKING FUNCTIONS ********************************************
1218 * glk_stream_get_position:
1219 * @str: A file or memory stream.
1221 * Returns the position of the read/write mark in @str. For memory streams and
1222 * binary file streams, this is exactly the number of characters read or written
1223 * from the beginning of the stream (unless you have moved the mark with
1224 * glk_stream_set_position().) For text file streams, matters are more
1225 * ambiguous, since (for example) writing one byte to a text file may store more
1226 * than one character in the platform's native encoding. You can only be sure
1227 * that the position increases as you read or write to the file.
1229 * Additional complication: for Latin-1 memory and file streams, a character is
1230 * a byte. For Unicode memory and file streams (those created by
1231 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1232 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1236 * If this bothers you, don't use binary Unicode files. I don't think they're
1237 * good for much anyhow.
1240 * glk_stream_get_position() on a window stream will always return zero.
1243 * It might make more sense to return the number of characters written to the
1244 * window, but existing libraries do not support this and it's not really
1245 * worth adding the feature.
1248 * Returns: position of the read/write mark in @str.
1251 glk_stream_get_position(strid_t str)
1253 VALID_STREAM(str, return 0);
1257 case STREAM_TYPE_MEMORY:
1259 case STREAM_TYPE_FILE:
1260 return ftell(str->file_pointer);
1261 case STREAM_TYPE_WINDOW:
1264 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1270 * glk_stream_set_position:
1271 * @str: A file or memory stream.
1272 * @pos: The position to set the mark to, relative to @seekmode.
1273 * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1275 * Sets the position of the read/write mark in @str. The position is controlled
1276 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1277 * <code>seekmode_</code> constants below.
1279 * It is illegal to specify a position before the beginning or after the end of
1282 * In binary files, the mark position is exact — it corresponds with the
1283 * number of characters you have read or written. In text files, this mapping
1284 * can vary, because of linefeed conventions or other character-set
1285 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1286 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1287 * the platform's native encoding — after character cookery. Therefore,
1288 * in a text stream, it is safest to use glk_stream_set_position() only to move
1289 * to the beginning or end of a file, or to a position determined by
1290 * glk_stream_get_position().
1292 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1293 * characters are 32-bit words, or four bytes each.
1295 * A window stream doesn't have a movable mark, so calling
1296 * glk_stream_set_position() has no effect.
1299 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1301 VALID_STREAM(str, return);
1302 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1303 g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1307 case STREAM_TYPE_MEMORY:
1310 case seekmode_Start: str->mark = pos; break;
1311 case seekmode_Current: str->mark += pos; break;
1312 case seekmode_End: str->mark = str->buflen + pos; break;
1314 g_return_if_reached();
1318 case STREAM_TYPE_FILE:
1323 case seekmode_Start: whence = SEEK_SET; break;
1324 case seekmode_Current: whence = SEEK_CUR; break;
1325 case seekmode_End: whence = SEEK_END; break;
1327 g_return_if_reached();
1330 if(fseek(str->file_pointer, pos, whence) == -1)
1331 WARNING("Seek failed on file stream");
1334 case STREAM_TYPE_WINDOW:
1335 break; /* Quietly do nothing */
1337 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);