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);
65 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &end);
67 // Player's style overrides
68 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &end);
70 // GLK Program's style overrides
71 gtk_text_buffer_apply_tag(buffer, glk_style_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);
114 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
115 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
116 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
117 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
119 while(chars_left > available_space && !gtk_text_iter_is_end(&insert))
121 GtkTextIter end = insert;
122 gtk_text_iter_forward_to_line_end(&end);
123 gtk_text_buffer_delete(buffer, &insert, &end);
125 start_offset = gtk_text_iter_get_offset(&insert);
126 gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), available_space);
127 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
130 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
132 // Player's style overrides
133 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
135 // GLK Program's style overrides
136 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
138 // Link style overrides
139 if(win->window_stream->hyperlink_mode) {
140 GtkTextTag *link_tag = win->current_hyperlink->tag;
141 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
142 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
145 // GLK Program's style overrides using garglk_set_zcolors()
146 if(win->zcolor != NULL)
147 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
149 // GLK Program's style overrides using garglk_set_reversevideo()
150 if(win->zcolor_reversed != NULL) {
151 gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &insert);
154 chars_left -= available_space;
155 gtk_text_iter_forward_line(&insert);
156 available_space = win->width;
158 if(!gtk_text_iter_is_end(&insert))
160 GtkTextIter end = insert;
161 gtk_text_iter_forward_chars(&end, chars_left);
162 gtk_text_buffer_delete(buffer, &insert, &end);
164 start_offset = gtk_text_iter_get_offset(&insert);
165 gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), -1);
166 gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
169 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
171 // Player's style overrides
172 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
174 // GLK Program's style overrides
175 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
177 // Link style overrides
178 if(win->window_stream->hyperlink_mode) {
179 GtkTextTag *link_tag = win->current_hyperlink->tag;
180 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
181 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
184 // GLK Program's style overrides using garglk_set_zcolors()
185 if(win->zcolor != NULL)
186 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
188 // GLK Program's style overrides using garglk_set_reversevideo()
189 if(win->zcolor_reversed != NULL) {
190 gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &insert);
194 gtk_text_buffer_move_mark(buffer, cursor, &start);
201 g_string_truncate(win->buffer, 0);
204 /* Internal function: write a Latin-1 buffer with length to a stream. */
206 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
210 case STREAM_TYPE_WINDOW:
211 /* Each window type has a different way of printing to it */
212 switch(str->window->type)
214 /* Printing to these windows' streams does nothing */
217 case wintype_Graphics:
218 str->write_count += len;
221 /* Text grid/buffer windows */
222 case wintype_TextGrid:
224 gchar *utf8 = convert_latin1_to_utf8(buf, len);
226 /* Deal with newlines */
229 for(i=0; i<len; i++) {
230 if(utf8[i] == '\n') {
232 write_utf8_to_window_buffer(str->window, line);
233 flush_window_buffer(str->window);
235 /* Move cursor position forward to the next line */
237 GtkTextIter cursor_pos;
238 GtkTextView *textview = GTK_TEXT_VIEW(str->window->widget);
239 GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
240 GtkTextMark *cursor_mark = gtk_text_buffer_get_mark(buffer, "cursor_position");
242 gtk_text_buffer_get_iter_at_mark( buffer, &cursor_pos, cursor_mark);
243 gtk_text_view_forward_display_line(textview, &cursor_pos);
244 gtk_text_view_backward_display_line_start(textview, &cursor_pos);
245 gtk_text_buffer_move_mark(buffer, cursor_mark, &cursor_pos);
248 line = utf8 + (i < len-1 ? (i+1):(len-1));
252 /* No more newlines left. */
253 write_utf8_to_window_buffer(str->window, line);
257 str->write_count += len;
261 case wintype_TextBuffer:
263 gchar *utf8 = convert_latin1_to_utf8(buf, len);
265 write_utf8_to_window_buffer(str->window, utf8);
269 str->write_count += len;
272 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
275 /* Now write the same buffer to the window's echo stream */
276 if(str->window->echo_stream != NULL)
277 write_buffer_to_stream(str->window->echo_stream, buf, len);
281 case STREAM_TYPE_MEMORY:
282 if(str->unicode && str->ubuffer)
285 while(str->mark < str->buflen && foo < len)
286 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
288 if(!str->unicode && str->buffer)
290 int copycount = MIN(len, str->buflen - str->mark);
291 memmove(str->buffer + str->mark, buf, copycount);
292 str->mark += copycount;
295 str->write_count += len;
298 case STREAM_TYPE_FILE:
303 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
304 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
307 else /* Regular file */
309 fwrite(buf, sizeof(gchar), len, str->file_pointer);
312 else /* Text mode is the same for Unicode and regular files */
314 gchar *utf8 = convert_latin1_to_utf8(buf, len);
317 g_fprintf(str->file_pointer, "%s", utf8);
322 str->write_count += len;
325 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
329 /* Internal function: write a Unicode buffer with length to a stream. */
331 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
335 case STREAM_TYPE_WINDOW:
336 /* Each window type has a different way of printing to it */
337 switch(str->window->type)
339 /* Printing to these windows' streams does nothing */
342 case wintype_Graphics:
343 str->write_count += len;
346 /* Text grid/buffer windows */
347 case wintype_TextGrid:
348 case wintype_TextBuffer:
350 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
352 write_utf8_to_window_buffer(str->window, utf8);
356 str->write_count += len;
359 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
362 /* Now write the same buffer to the window's echo stream */
363 if(str->window->echo_stream != NULL)
364 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
368 case STREAM_TYPE_MEMORY:
369 if(str->unicode && str->ubuffer)
371 int copycount = MIN(len, str->buflen - str->mark);
372 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
373 str->mark += copycount;
375 if(!str->unicode && str->buffer)
377 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
378 int copycount = MIN(len, str->buflen - str->mark);
379 memmove(str->buffer + str->mark, latin1, copycount);
381 str->mark += copycount;
384 str->write_count += len;
387 case STREAM_TYPE_FILE:
392 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
393 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
396 else /* Regular file */
398 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
399 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
403 else /* Text mode is the same for Unicode and regular files */
405 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
408 g_fprintf(str->file_pointer, "%s", utf8);
413 str->write_count += len;
416 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
421 * glk_put_char_stream:
422 * @str: An output stream.
423 * @ch: A character in Latin-1 encoding.
425 * The same as glk_put_char(), except that you specify a stream @str to print
426 * to, instead of using the current stream. It is illegal for @str to be %NULL,
427 * or an input-only stream.
430 glk_put_char_stream(strid_t str, unsigned char ch)
432 VALID_STREAM(str, return);
433 g_return_if_fail(str->file_mode != filemode_Read);
435 write_buffer_to_stream(str, (gchar *)&ch, 1);
439 * glk_put_char_stream_uni:
440 * @str: An output stream.
441 * @ch: A Unicode code point.
443 * The same as glk_put_char_uni(), except that you specify a stream @str to
444 * print to, instead of using the current stream. It is illegal for @str to be
445 * %NULL, or an input-only stream.
448 glk_put_char_stream_uni(strid_t str, glui32 ch)
450 VALID_STREAM(str, return);
451 g_return_if_fail(str->file_mode != filemode_Read);
453 write_buffer_to_stream_uni(str, &ch, 1);
457 * glk_put_string_stream:
458 * @str: An output stream.
459 * @s: A null-terminated string in Latin-1 encoding.
461 * The same as glk_put_string(), except that you specify a stream @str to print
462 * to, instead of using the current stream. It is illegal for @str to be %NULL,
463 * or an input-only stream.
466 glk_put_string_stream(strid_t str, char *s)
468 VALID_STREAM(str, return);
472 g_return_if_fail(str->file_mode != filemode_Read);
474 write_buffer_to_stream(str, s, strlen(s));
478 * glk_put_string_stream_uni:
479 * @str: An output stream.
480 * @s: A null-terminated array of Unicode code points.
482 * The same as glk_put_string_uni(), except that you specify a stream @str to
483 * print to, instead of using the current stream. It is illegal for @str to be
484 * %NULL, or an input-only stream.
487 glk_put_string_stream_uni(strid_t str, glui32 *s)
489 VALID_STREAM(str, return);
493 g_return_if_fail(str->file_mode != filemode_Read);
495 /* An impromptu strlen() for glui32 arrays */
500 write_buffer_to_stream_uni(str, s, len);
504 * glk_put_buffer_stream:
505 * @str: An output stream.
506 * @buf: An array of characters in Latin-1 encoding.
507 * @len: Length of @buf.
509 * The same as glk_put_buffer(), except that you specify a stream @str to print
510 * to, instead of using the current stream. It is illegal for @str to be %NULL,
511 * or an input-only stream.
514 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
516 VALID_STREAM(str, return);
520 g_return_if_fail(str->file_mode != filemode_Read);
522 write_buffer_to_stream(str, buf, len);
526 * glk_put_buffer_stream_uni:
527 * @str: An output stream.
528 * @buf: An array of Unicode code points.
529 * @len: Length of @buf.
531 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
532 * print to, instead of using the current stream. It is illegal for @str to be
533 * %NULL, or an input-only stream.
536 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
538 VALID_STREAM(str, return);
542 g_return_if_fail(str->file_mode != filemode_Read);
544 write_buffer_to_stream_uni(str, buf, len);
549 **************** READING FUNCTIONS ********************************************
553 /* Internal function: Read one big-endian four-byte character from file fp and
554 return it as a Unicode code point, or -1 on EOF */
556 read_ucs4be_char_from_file(FILE *fp)
558 unsigned char readbuffer[4];
559 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
562 readbuffer[0] << 24 |
563 readbuffer[1] << 16 |
568 /* Internal function: Read one UTF-8 character, which may be more than one byte,
569 from file fp and return it as a Unicode code point, or -1 on EOF */
571 read_utf8_char_from_file(FILE *fp)
573 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
575 gunichar charresult = (gunichar)-2;
576 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
581 readbuffer[foo] = (gchar)ch;
582 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
583 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
586 /* Silently return unknown characters as 0xFFFD, Replacement Character */
587 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
592 /* Internal function: Tell whether this code point is a Unicode newline. The
593 file pointer and eight-bit flag are included in case the newline is a CR
594 (U+000D). If the next character is LF (U+000A) then it also belongs to the
597 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
599 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
602 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
603 read_ucs4be_char_from_file(fp);
605 if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
606 WARNING_S("Seek failed on stream", g_strerror(errno) );
612 /* Internal function: Read one character from a stream. Returns a value which
613 can be returned unchanged by glk_get_char_stream_uni(), but
614 glk_get_char_stream() must replace high values by the placeholder character. */
616 get_char_stream_common(strid_t str)
620 case STREAM_TYPE_MEMORY:
623 if(!str->ubuffer || str->mark >= str->buflen)
625 glui32 ch = str->ubuffer[str->mark++];
631 if(!str->buffer || str->mark >= str->buflen)
633 unsigned char ch = str->buffer[str->mark++];
639 case STREAM_TYPE_FILE:
644 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
650 else /* Regular file */
652 int ch = fgetc(str->file_pointer);
660 else /* Text mode is the same for Unicode and regular files */
662 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
670 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
676 * glk_get_char_stream:
677 * @str: An input stream.
679 * Reads one character from the stream @str. (There is no notion of a
680 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
681 * an output-only stream.
683 * The result will be between 0 and 255. As with all basic text functions, Glk
684 * assumes the Latin-1 encoding. See <link
685 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
686 * of the stream has been reached, the result will be -1.
689 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
690 * returned as negative numbers.
693 * If the stream contains Unicode data — for example, if it was created
694 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
695 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
697 * It is usually more efficient to read several characters at once with
698 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
699 * glk_get_char_stream() several times.
701 * Returns: A character value between 0 and 255, or -1 on end of stream.
704 glk_get_char_stream(strid_t str)
706 VALID_STREAM(str, return -1);
707 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
709 glsi32 ch = get_char_stream_common(str);
710 return (ch > 0xFF)? PLACEHOLDER : ch;
714 * glk_get_char_stream_uni:
715 * @str: An input stream.
717 * Reads one character from the stream @str. The result will be between 0 and
718 * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
720 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
723 glk_get_char_stream_uni(strid_t str)
725 VALID_STREAM(str, return -1);
726 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
728 return get_char_stream_common(str);
732 * glk_get_buffer_stream:
733 * @str: An input stream.
734 * @buf: A buffer with space for at least @len characters.
735 * @len: The number of characters to read.
737 * Reads @len characters from @str, unless the end of stream is reached first.
738 * No terminal null is placed in the buffer.
740 * Returns: The number of characters actually read.
743 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
745 VALID_STREAM(str, return 0);
746 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
747 g_return_val_if_fail(buf != NULL, 0);
751 case STREAM_TYPE_MEMORY:
756 while(copycount < len && str->ubuffer && str->mark < str->buflen)
758 glui32 ch = str->ubuffer[str->mark++];
759 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
764 if(str->buffer) /* if not, copycount stays 0 */
765 copycount = MIN(len, str->buflen - str->mark);
766 memmove(buf, str->buffer + str->mark, copycount);
767 str->mark += copycount;
770 str->read_count += copycount;
773 case STREAM_TYPE_FILE:
776 if(str->unicode) /* Binary file with 4-byte characters */
778 /* Read len characters of 4 bytes each */
779 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
780 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
781 /* If there was an incomplete character */
785 WARNING("Incomplete character in binary Unicode file");
789 for(foo = 0; foo < count; foo += 4)
791 glsi32 ch = readbuffer[foo] << 24
792 | readbuffer[foo + 1] << 16
793 | readbuffer[foo + 2] << 8
794 | readbuffer[foo + 3];
795 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
798 str->read_count += count / 4;
801 else /* Regular binary file */
803 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
804 str->read_count += count;
808 else /* Text mode is the same for Unicode and regular files */
810 /* Do it character-by-character */
812 for(foo = 0; foo < len; foo++)
814 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
818 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
823 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
829 * glk_get_buffer_stream_uni:
830 * @str: An input stream.
831 * @buf: A buffer with space for at least @len Unicode code points.
832 * @len: The number of characters to read.
834 * Reads @len Unicode characters from @str, unless the end of stream is reached
835 * first. No terminal null is placed in the buffer.
837 * Returns: The number of Unicode characters actually read.
840 glk_get_buffer_stream_uni(strid_t str, glui32 *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 if(str->ubuffer) /* if not, copycount stays 0 */
854 copycount = MIN(len, str->buflen - str->mark);
855 memmove(buf, str->ubuffer + str->mark, copycount * 4);
856 str->mark += copycount;
860 while(copycount < len && str->buffer && str->mark < str->buflen)
862 unsigned char ch = str->buffer[str->mark++];
863 buf[copycount++] = ch;
867 str->read_count += copycount;
870 case STREAM_TYPE_FILE:
873 if(str->unicode) /* Binary file with 4-byte characters */
875 /* Read len characters of 4 bytes each */
876 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
877 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
878 /* If there was an incomplete character */
882 WARNING("Incomplete character in binary Unicode file");
886 for(foo = 0; foo < count; foo += 4)
887 buf[foo / 4] = readbuffer[foo] << 24
888 | readbuffer[foo + 1] << 16
889 | readbuffer[foo + 2] << 8
890 | readbuffer[foo + 3];
892 str->read_count += count / 4;
895 else /* Regular binary file */
897 unsigned char *readbuffer = g_new0(unsigned char, len);
898 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
900 for(foo = 0; foo < count; foo++)
901 buf[foo] = readbuffer[foo];
903 str->read_count += count;
907 else /* Text mode is the same for Unicode and regular files */
909 /* Do it character-by-character */
911 for(foo = 0; foo < len; foo++)
913 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
922 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
928 * glk_get_line_stream:
929 * @str: An input stream.
930 * @buf: A buffer with space for at least @len characters.
931 * @len: The number of characters to read, plus one.
933 * Reads characters from @str, until either
935 * <alt>@len - 1</alt>
936 * <mathphrase>@len - 1</mathphrase>
938 * characters have been read or a newline has been read. It then puts a
939 * terminal null (<code>'\0'</code>) aracter on
940 * the end. It returns the number of characters actually read, including the
941 * newline (if there is one) but not including the terminal null.
943 * Returns: The number of characters actually read.
946 glk_get_line_stream(strid_t str, char *buf, glui32 len)
948 VALID_STREAM(str, return 0);
949 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
950 g_return_val_if_fail(buf != NULL, 0);
954 case STREAM_TYPE_MEMORY:
959 /* Do it character-by-character */
960 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
962 glui32 ch = str->ubuffer[str->mark++];
963 /* Check for Unicode newline; slightly different than
965 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
967 buf[copycount++] = '\n';
972 if(str->ubuffer[str->mark] == 0x0A)
973 str->mark++; /* skip past next newline */
974 buf[copycount++] = '\n';
977 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
979 buf[copycount] = '\0';
983 if(str->buffer) /* if not, copycount stays 0 */
984 copycount = MIN(len - 1, str->buflen - str->mark);
985 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
986 if(endptr) /* newline was found */
987 copycount = endptr - buf; /* Real copy count */
988 buf[copycount] = '\0';
989 str->mark += copycount;
992 str->read_count += copycount;
995 case STREAM_TYPE_FILE:
998 if(str->unicode) /* Binary file with 4-byte characters */
1000 /* Do it character-by-character */
1002 for(foo = 0; foo < len - 1; foo++)
1004 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1011 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1014 buf[foo + 1] = '\0';
1017 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
1022 else /* Regular binary file */
1024 if( !fgets(buf, len, str->file_pointer) ) {
1029 int nread = strlen(buf);
1030 str->read_count += nread;
1034 else /* Text mode is the same for Unicode and regular files */
1036 /* Do it character-by-character */
1038 for(foo = 0; foo < len - 1; foo++)
1040 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1047 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1050 buf[foo + 1] = '\0';
1053 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
1059 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1065 * glk_get_line_stream_uni:
1066 * @str: An input stream.
1067 * @buf: A buffer with space for at least @len Unicode code points.
1068 * @len: The number of characters to read, plus one.
1070 * Reads Unicode characters from @str, until either
1072 * <alt>@len - 1</alt>
1073 * <mathphrase>@len - 1</mathphrase>
1075 * Unicode characters have been read or a newline has been read. It then puts a
1076 * terminal null (a zero value) on the end.
1078 * Returns: The number of characters actually read, including the newline (if
1079 * there is one) but not including the terminal null.
1082 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
1084 VALID_STREAM(str, return 0);
1085 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
1086 g_return_val_if_fail(buf != NULL, 0);
1090 case STREAM_TYPE_MEMORY:
1095 /* Do it character-by-character */
1096 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
1098 glui32 ch = str->ubuffer[str->mark++];
1099 /* Check for Unicode newline; slightly different than
1101 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
1103 buf[copycount++] = '\n';
1108 if(str->ubuffer[str->mark] == 0x0A)
1109 str->mark++; /* skip past next newline */
1110 buf[copycount++] = '\n';
1113 buf[copycount++] = ch;
1115 buf[copycount] = '\0';
1119 /* No recourse to memccpy(), so do it character-by-character */
1120 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
1122 gchar ch = str->buffer[str->mark++];
1123 /* Check for newline */
1124 if(ch == '\n') /* Also check for \r and \r\n? */
1126 buf[copycount++] = '\n';
1129 buf[copycount++] = (unsigned char)ch;
1134 str->read_count += copycount;
1137 case STREAM_TYPE_FILE:
1140 if(str->unicode) /* Binary file with 4-byte characters */
1142 /* Do it character-by-character */
1144 for(foo = 0; foo < len - 1; foo++)
1146 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1153 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1155 buf[foo] = ch; /* Preserve newline types??? */
1164 else /* Regular binary file */
1166 gchar *readbuffer = g_new0(gchar, len);
1167 if( !fgets(readbuffer, len, str->file_pointer) ) {
1172 glui32 count = strlen(readbuffer);
1174 for(foo = 0; foo < count + 1; foo++) /* Copy terminator */
1175 buf[foo] = (unsigned char)(readbuffer[foo]);
1176 str->read_count += count;
1180 else /* Text mode is the same for Unicode and regular files */
1182 /* Do it character-by-character */
1184 for(foo = 0; foo < len - 1; foo++)
1186 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1193 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1195 buf[foo] = ch; /* Preserve newline types??? */
1205 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1212 **************** SEEKING FUNCTIONS ********************************************
1217 * glk_stream_get_position:
1218 * @str: A file or memory stream.
1220 * Returns the position of the read/write mark in @str. For memory streams and
1221 * binary file streams, this is exactly the number of characters read or written
1222 * from the beginning of the stream (unless you have moved the mark with
1223 * glk_stream_set_position().) For text file streams, matters are more
1224 * ambiguous, since (for example) writing one byte to a text file may store more
1225 * than one character in the platform's native encoding. You can only be sure
1226 * that the position increases as you read or write to the file.
1228 * Additional complication: for Latin-1 memory and file streams, a character is
1229 * a byte. For Unicode memory and file streams (those created by
1230 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1231 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1235 * If this bothers you, don't use binary Unicode files. I don't think they're
1236 * good for much anyhow.
1239 * Returns: position of the read/write mark in @str.
1242 glk_stream_get_position(strid_t str)
1244 VALID_STREAM(str, return 0);
1248 case STREAM_TYPE_MEMORY:
1250 case STREAM_TYPE_FILE:
1251 return ftell(str->file_pointer);
1253 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1259 * glk_stream_set_position:
1260 * @str: A file or memory stream.
1261 * @pos: The position to set the mark to, relative to @seekmode.
1262 * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1264 * Sets the position of the read/write mark in @str. The position is controlled
1265 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1266 * <code>seekmode_</code> constants below.
1268 * It is illegal to specify a position before the beginning or after the end of
1271 * In binary files, the mark position is exact — it corresponds with the
1272 * number of characters you have read or written. In text files, this mapping
1273 * can vary, because of linefeed conventions or other character-set
1274 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1275 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1276 * the platform's native encoding — after character cookery. Therefore,
1277 * in a text stream, it is safest to use glk_stream_set_position() only to move
1278 * to the beginning or end of a file, or to a position determined by
1279 * glk_stream_get_position().
1281 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1282 * characters are 32-bit words, or four bytes each.
1285 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1287 VALID_STREAM(str, return);
1288 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1289 g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1293 case STREAM_TYPE_MEMORY:
1296 case seekmode_Start: str->mark = pos; break;
1297 case seekmode_Current: str->mark += pos; break;
1298 case seekmode_End: str->mark = str->buflen + pos; break;
1300 g_return_if_reached();
1304 case STREAM_TYPE_FILE:
1309 case seekmode_Start: whence = SEEK_SET; break;
1310 case seekmode_Current: whence = SEEK_CUR; break;
1311 case seekmode_End: whence = SEEK_END; break;
1313 g_return_if_reached();
1316 if(fseek(str->file_pointer, pos, whence) == -1)
1317 WARNING("Seek failed on file stream");
1321 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);