9 #include <glib/gstdio.h>
13 **************** WRITING FUNCTIONS ********************************************
17 /* Internal function: write a UTF-8 string to a text buffer window's text buffer. */
19 write_utf8_to_window_buffer(winid_t win, gchar *s)
21 if(win->input_request_type == INPUT_REQUEST_LINE || win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
23 ILLEGAL("Tried to print to a text buffer window with line input pending.");
27 // Write to the buffer
28 g_string_append(win->buffer, s);
31 /* Internal function: flush a window's text buffer to the screen. */
33 flush_window_buffer(winid_t win)
35 if(win->type != wintype_TextBuffer && win->type != wintype_TextGrid)
38 if(win->buffer->len == 0)
43 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
46 case wintype_TextBuffer:
49 gtk_text_buffer_get_end_iter(buffer, &iter);
51 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
52 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->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, link_style_tag, link_tag, NULL);
59 gtk_text_buffer_insert_with_tags(buffer, &iter, win->buffer->str, -1, 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);
91 if(win->window_stream->hyperlink_mode) {
92 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
93 GtkTextTag *link_tag = win->current_hyperlink->tag;
94 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), available_space, style_tag, link_style_tag, link_tag, NULL);
96 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), available_space, style_tag, NULL);
99 chars_left -= available_space;
100 gtk_text_iter_forward_line(&start);
101 available_space = win->width;
103 if(!gtk_text_iter_is_end(&start))
105 GtkTextIter end = start;
106 gtk_text_iter_forward_chars(&end, chars_left);
107 gtk_text_buffer_delete(buffer, &start, &end);
109 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
110 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
112 if(win->window_stream->hyperlink_mode) {
113 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
114 GtkTextTag *link_tag = win->current_hyperlink->tag;
115 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, style_tag, link_style_tag, link_tag, NULL);
117 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, style_tag, NULL);
121 gtk_text_buffer_move_mark(buffer, cursor, &start);
128 g_string_truncate(win->buffer, 0);
131 /* Internal function: write a Latin-1 buffer with length to a stream. */
133 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
137 case STREAM_TYPE_WINDOW:
138 /* Each window type has a different way of printing to it */
139 switch(str->window->type)
141 /* Printing to these windows' streams does nothing */
144 case wintype_Graphics:
145 str->write_count += len;
148 /* Text grid/buffer windows */
149 case wintype_TextGrid:
150 case wintype_TextBuffer:
152 gchar *utf8 = convert_latin1_to_utf8(buf, len);
154 write_utf8_to_window_buffer(str->window, utf8);
158 str->write_count += len;
161 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
164 /* Now write the same buffer to the window's echo stream */
165 if(str->window->echo_stream != NULL)
166 write_buffer_to_stream(str->window->echo_stream, buf, len);
170 case STREAM_TYPE_MEMORY:
171 if(str->unicode && str->ubuffer)
174 while(str->mark < str->buflen && foo < len)
175 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
177 if(!str->unicode && str->buffer)
179 int copycount = MIN(len, str->buflen - str->mark);
180 memmove(str->buffer + str->mark, buf, copycount);
181 str->mark += copycount;
184 str->write_count += len;
187 case STREAM_TYPE_FILE:
192 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
193 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
196 else /* Regular file */
198 fwrite(buf, sizeof(gchar), len, str->file_pointer);
201 else /* Text mode is the same for Unicode and regular files */
203 gchar *utf8 = convert_latin1_to_utf8(buf, len);
206 g_fprintf(str->file_pointer, "%s", utf8);
211 str->write_count += len;
214 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
218 /* Internal function: write a Unicode buffer with length to a stream. */
220 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
224 case STREAM_TYPE_WINDOW:
225 /* Each window type has a different way of printing to it */
226 switch(str->window->type)
228 /* Printing to these windows' streams does nothing */
231 case wintype_Graphics:
232 str->write_count += len;
235 /* Text grid/buffer windows */
236 case wintype_TextGrid:
237 case wintype_TextBuffer:
239 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
241 write_utf8_to_window_buffer(str->window, utf8);
245 str->write_count += len;
248 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
251 /* Now write the same buffer to the window's echo stream */
252 if(str->window->echo_stream != NULL)
253 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
257 case STREAM_TYPE_MEMORY:
258 if(str->unicode && str->ubuffer)
260 int copycount = MIN(len, str->buflen - str->mark);
261 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
262 str->mark += copycount;
264 if(!str->unicode && str->buffer)
266 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
267 int copycount = MIN(len, str->buflen - str->mark);
268 memmove(str->buffer + str->mark, latin1, copycount);
270 str->mark += copycount;
273 str->write_count += len;
276 case STREAM_TYPE_FILE:
281 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
282 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
285 else /* Regular file */
287 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
288 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
292 else /* Text mode is the same for Unicode and regular files */
294 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
297 g_fprintf(str->file_pointer, "%s", utf8);
302 str->write_count += len;
305 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
310 * glk_put_char_stream:
311 * @str: An output stream.
312 * @ch: A character in Latin-1 encoding.
314 * The same as glk_put_char(), except that you specify a stream @str to print
315 * to, instead of using the current stream. It is illegal for @str to be %NULL,
316 * or an input-only stream.
319 glk_put_char_stream(strid_t str, unsigned char ch)
321 VALID_STREAM(str, return);
322 g_return_if_fail(str->file_mode != filemode_Read);
324 write_buffer_to_stream(str, (gchar *)&ch, 1);
328 * glk_put_char_stream_uni:
329 * @str: An output stream.
330 * @ch: A Unicode code point.
332 * The same as glk_put_char_uni(), except that you specify a stream @str to
333 * print to, instead of using the current stream. It is illegal for @str to be
334 * %NULL, or an input-only stream.
337 glk_put_char_stream_uni(strid_t str, glui32 ch)
339 VALID_STREAM(str, return);
340 g_return_if_fail(str->file_mode != filemode_Read);
342 write_buffer_to_stream_uni(str, &ch, 1);
346 * glk_put_string_stream:
347 * @str: An output stream.
348 * @s: A null-terminated string in Latin-1 encoding.
350 * The same as glk_put_string(), except that you specify a stream @str to print
351 * to, instead of using the current stream. It is illegal for @str to be %NULL,
352 * or an input-only stream.
355 glk_put_string_stream(strid_t str, char *s)
357 VALID_STREAM(str, return);
361 g_return_if_fail(str->file_mode != filemode_Read);
363 write_buffer_to_stream(str, s, strlen(s));
367 * glk_put_string_stream_uni:
368 * @str: An output stream.
369 * @s: A null-terminated array of Unicode code points.
371 * The same as glk_put_string_uni(), except that you specify a stream @str to
372 * print to, instead of using the current stream. It is illegal for @str to be
373 * %NULL, or an input-only stream.
376 glk_put_string_stream_uni(strid_t str, glui32 *s)
378 VALID_STREAM(str, return);
382 g_return_if_fail(str->file_mode != filemode_Read);
384 /* An impromptu strlen() for glui32 arrays */
389 write_buffer_to_stream_uni(str, s, len);
393 * glk_put_buffer_stream:
394 * @str: An output stream.
395 * @buf: An array of characters in Latin-1 encoding.
396 * @len: Length of @buf.
398 * The same as glk_put_buffer(), except that you specify a stream @str to print
399 * to, instead of using the current stream. It is illegal for @str to be %NULL,
400 * or an input-only stream.
403 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
405 VALID_STREAM(str, return);
409 g_return_if_fail(str->file_mode != filemode_Read);
411 write_buffer_to_stream(str, buf, len);
415 * glk_put_buffer_stream_uni:
416 * @str: An output stream.
417 * @buf: An array of Unicode code points.
418 * @len: Length of @buf.
420 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
421 * print to, instead of using the current stream. It is illegal for @str to be
422 * %NULL, or an input-only stream.
425 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
427 VALID_STREAM(str, return);
431 g_return_if_fail(str->file_mode != filemode_Read);
433 write_buffer_to_stream_uni(str, buf, len);
438 **************** READING FUNCTIONS ********************************************
442 /* Internal function: Read one big-endian four-byte character from file fp and
443 return it as a Unicode code point, or -1 on EOF */
445 read_ucs4be_char_from_file(FILE *fp)
447 unsigned char readbuffer[4];
448 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
451 readbuffer[0] << 24 |
452 readbuffer[1] << 16 |
457 /* Internal function: Read one UTF-8 character, which may be more than one byte,
458 from file fp and return it as a Unicode code point, or -1 on EOF */
460 read_utf8_char_from_file(FILE *fp)
462 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
464 gunichar charresult = (gunichar)-2;
465 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
470 readbuffer[foo] = (gchar)ch;
471 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
472 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
475 /* Silently return unknown characters as 0xFFFD, Replacement Character */
476 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
481 /* Internal function: Tell whether this code point is a Unicode newline. The
482 file pointer and eight-bit flag are included in case the newline is a CR
483 (U+000D). If the next character is LF (U+000A) then it also belongs to the
486 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
488 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
491 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
492 read_ucs4be_char_from_file(fp);
494 if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
495 WARNING_S("Seek failed on stream", g_strerror(errno) );
501 /* Internal function: Read one character from a stream. Returns a value which
502 can be returned unchanged by glk_get_char_stream_uni(), but
503 glk_get_char_stream() must replace high values by the placeholder character. */
505 get_char_stream_common(strid_t str)
509 case STREAM_TYPE_MEMORY:
512 if(!str->ubuffer || str->mark >= str->buflen)
514 glui32 ch = str->ubuffer[str->mark++];
520 if(!str->buffer || str->mark >= str->buflen)
522 unsigned char ch = str->buffer[str->mark++];
528 case STREAM_TYPE_FILE:
533 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
539 else /* Regular file */
541 int ch = fgetc(str->file_pointer);
549 else /* Text mode is the same for Unicode and regular files */
551 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
559 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
565 * glk_get_char_stream:
566 * @str: An input stream.
568 * Reads one character from the stream @str. (There is no notion of a
569 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
570 * an output-only stream.
572 * The result will be between 0 and 255. As with all basic text functions, Glk
573 * assumes the Latin-1 encoding. See <link
574 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
575 * of the stream has been reached, the result will be -1.
578 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
579 * returned as negative numbers.
582 * If the stream contains Unicode data — for example, if it was created
583 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
584 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
586 * It is usually more efficient to read several characters at once with
587 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
588 * glk_get_char_stream() several times.
590 * Returns: A character value between 0 and 255, or -1 on end of stream.
593 glk_get_char_stream(strid_t str)
595 VALID_STREAM(str, return -1);
596 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
598 glsi32 ch = get_char_stream_common(str);
599 return (ch > 0xFF)? PLACEHOLDER : ch;
603 * glk_get_char_stream_uni:
604 * @str: An input stream.
606 * Reads one character from the stream @str. The result will be between 0 and
607 * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
609 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
612 glk_get_char_stream_uni(strid_t str)
614 VALID_STREAM(str, return -1);
615 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
617 return get_char_stream_common(str);
621 * glk_get_buffer_stream:
622 * @str: An input stream.
623 * @buf: A buffer with space for at least @len characters.
624 * @len: The number of characters to read.
626 * Reads @len characters from @str, unless the end of stream is reached first.
627 * No terminal null is placed in the buffer.
629 * Returns: The number of characters actually read.
632 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
634 VALID_STREAM(str, return 0);
635 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
636 g_return_val_if_fail(buf != NULL, 0);
640 case STREAM_TYPE_MEMORY:
645 while(copycount < len && str->ubuffer && str->mark < str->buflen)
647 glui32 ch = str->ubuffer[str->mark++];
648 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
653 if(str->buffer) /* if not, copycount stays 0 */
654 copycount = MIN(len, str->buflen - str->mark);
655 memmove(buf, str->buffer + str->mark, copycount);
656 str->mark += copycount;
659 str->read_count += copycount;
662 case STREAM_TYPE_FILE:
665 if(str->unicode) /* Binary file with 4-byte characters */
667 /* Read len characters of 4 bytes each */
668 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
669 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
670 /* If there was an incomplete character */
674 WARNING("Incomplete character in binary Unicode file");
678 for(foo = 0; foo < count; foo += 4)
680 glsi32 ch = readbuffer[foo] << 24
681 | readbuffer[foo + 1] << 16
682 | readbuffer[foo + 2] << 8
683 | readbuffer[foo + 3];
684 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
687 str->read_count += count / 4;
690 else /* Regular binary file */
692 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
693 str->read_count += count;
697 else /* Text mode is the same for Unicode and regular files */
699 /* Do it character-by-character */
701 for(foo = 0; foo < len; foo++)
703 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
707 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
712 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
718 * glk_get_buffer_stream_uni:
719 * @str: An input stream.
720 * @buf: A buffer with space for at least @len Unicode code points.
721 * @len: The number of characters to read.
723 * Reads @len Unicode characters from @str, unless the end of stream is reached
724 * first. No terminal null is placed in the buffer.
726 * Returns: The number of Unicode characters actually read.
729 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
731 VALID_STREAM(str, return 0);
732 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
733 g_return_val_if_fail(buf != NULL, 0);
737 case STREAM_TYPE_MEMORY:
742 if(str->ubuffer) /* if not, copycount stays 0 */
743 copycount = MIN(len, str->buflen - str->mark);
744 memmove(buf, str->ubuffer + str->mark, copycount * 4);
745 str->mark += copycount;
749 while(copycount < len && str->buffer && str->mark < str->buflen)
751 unsigned char ch = str->buffer[str->mark++];
752 buf[copycount++] = ch;
756 str->read_count += copycount;
759 case STREAM_TYPE_FILE:
762 if(str->unicode) /* Binary file with 4-byte characters */
764 /* Read len characters of 4 bytes each */
765 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
766 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
767 /* If there was an incomplete character */
771 WARNING("Incomplete character in binary Unicode file");
775 for(foo = 0; foo < count; foo += 4)
776 buf[foo / 4] = readbuffer[foo] << 24
777 | readbuffer[foo + 1] << 16
778 | readbuffer[foo + 2] << 8
779 | readbuffer[foo + 3];
781 str->read_count += count / 4;
784 else /* Regular binary file */
786 unsigned char *readbuffer = g_new0(unsigned char, len);
787 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
789 for(foo = 0; foo < count; foo++)
790 buf[foo] = readbuffer[foo];
792 str->read_count += count;
796 else /* Text mode is the same for Unicode and regular files */
798 /* Do it character-by-character */
800 for(foo = 0; foo < len; foo++)
802 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
811 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
817 * glk_get_line_stream:
818 * @str: An input stream.
819 * @buf: A buffer with space for at least @len characters.
820 * @len: The number of characters to read, plus one.
822 * Reads characters from @str, until either
824 * <alt>@len - 1</alt>
825 * <mathphrase>@len - 1</mathphrase>
827 * characters have been read or a newline has been read. It then puts a
828 * terminal null (<code>'\0'</code>) aracter on
829 * the end. It returns the number of characters actually read, including the
830 * newline (if there is one) but not including the terminal null.
832 * Returns: The number of characters actually read.
835 glk_get_line_stream(strid_t str, char *buf, glui32 len)
837 VALID_STREAM(str, return 0);
838 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
839 g_return_val_if_fail(buf != NULL, 0);
843 case STREAM_TYPE_MEMORY:
848 /* Do it character-by-character */
849 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
851 glui32 ch = str->ubuffer[str->mark++];
852 /* Check for Unicode newline; slightly different than
854 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
856 buf[copycount++] = '\n';
861 if(str->ubuffer[str->mark] == 0x0A)
862 str->mark++; /* skip past next newline */
863 buf[copycount++] = '\n';
866 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
868 buf[copycount] = '\0';
872 if(str->buffer) /* if not, copycount stays 0 */
873 copycount = MIN(len - 1, str->buflen - str->mark);
874 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
875 if(endptr) /* newline was found */
876 copycount = endptr - buf; /* Real copy count */
877 buf[copycount] = '\0';
878 str->mark += copycount;
881 str->read_count += copycount;
884 case STREAM_TYPE_FILE:
887 if(str->unicode) /* Binary file with 4-byte characters */
889 /* Do it character-by-character */
891 for(foo = 0; foo < len - 1; foo++)
893 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
900 if(is_unicode_newline(ch, str->file_pointer, FALSE))
906 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
911 else /* Regular binary file */
913 if( !fgets(buf, len, str->file_pointer) ) {
918 int nread = strlen(buf);
919 str->read_count += nread;
923 else /* Text mode is the same for Unicode and regular files */
925 /* Do it character-by-character */
927 for(foo = 0; foo < len - 1; foo++)
929 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
936 if(is_unicode_newline(ch, str->file_pointer, TRUE))
942 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
948 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
954 * glk_get_line_stream_uni:
955 * @str: An input stream.
956 * @buf: A buffer with space for at least @len Unicode code points.
957 * @len: The number of characters to read, plus one.
959 * Reads Unicode characters from @str, until either
961 * <alt>@len - 1</alt>
962 * <mathphrase>@len - 1</mathphrase>
964 * Unicode characters have been read or a newline has been read. It then puts a
965 * terminal null (a zero value) on the end.
967 * Returns: The number of characters actually read, including the newline (if
968 * there is one) but not including the terminal null.
971 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
973 VALID_STREAM(str, return 0);
974 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
975 g_return_val_if_fail(buf != NULL, 0);
979 case STREAM_TYPE_MEMORY:
984 /* Do it character-by-character */
985 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
987 glui32 ch = str->ubuffer[str->mark++];
988 /* Check for Unicode newline; slightly different than
990 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
992 buf[copycount++] = '\n';
997 if(str->ubuffer[str->mark] == 0x0A)
998 str->mark++; /* skip past next newline */
999 buf[copycount++] = '\n';
1002 buf[copycount++] = ch;
1004 buf[copycount] = '\0';
1008 /* No recourse to memccpy(), so do it character-by-character */
1009 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
1011 gchar ch = str->buffer[str->mark++];
1012 /* Check for newline */
1013 if(ch == '\n') /* Also check for \r and \r\n? */
1015 buf[copycount++] = '\n';
1018 buf[copycount++] = (unsigned char)ch;
1023 str->read_count += copycount;
1026 case STREAM_TYPE_FILE:
1029 if(str->unicode) /* Binary file with 4-byte characters */
1031 /* Do it character-by-character */
1033 for(foo = 0; foo < len - 1; foo++)
1035 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1042 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1044 buf[foo] = ch; /* Preserve newline types??? */
1053 else /* Regular binary file */
1055 gchar *readbuffer = g_new0(gchar, len);
1056 if( !fgets(readbuffer, len, str->file_pointer) ) {
1061 glui32 count = strlen(readbuffer);
1063 for(foo = 0; foo < count + 1; foo++) /* Copy terminator */
1064 buf[foo] = (unsigned char)(readbuffer[foo]);
1065 str->read_count += count;
1069 else /* Text mode is the same for Unicode and regular files */
1071 /* Do it character-by-character */
1073 for(foo = 0; foo < len - 1; foo++)
1075 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1082 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1084 buf[foo] = ch; /* Preserve newline types??? */
1094 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1101 **************** SEEKING FUNCTIONS ********************************************
1106 * glk_stream_get_position:
1107 * @str: A file or memory stream.
1109 * Returns the position of the read/write mark in @str. For memory streams and
1110 * binary file streams, this is exactly the number of characters read or written
1111 * from the beginning of the stream (unless you have moved the mark with
1112 * glk_stream_set_position().) For text file streams, matters are more
1113 * ambiguous, since (for example) writing one byte to a text file may store more
1114 * than one character in the platform's native encoding. You can only be sure
1115 * that the position increases as you read or write to the file.
1117 * Additional complication: for Latin-1 memory and file streams, a character is
1118 * a byte. For Unicode memory and file streams (those created by
1119 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1120 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1124 * If this bothers you, don't use binary Unicode files. I don't think they're
1125 * good for much anyhow.
1128 * Returns: position of the read/write mark in @str.
1131 glk_stream_get_position(strid_t str)
1133 VALID_STREAM(str, return 0);
1137 case STREAM_TYPE_MEMORY:
1139 case STREAM_TYPE_FILE:
1140 return ftell(str->file_pointer);
1142 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1148 * glk_stream_set_position:
1149 * @str: A file or memory stream.
1150 * @pos: The position to set the mark to, relative to @seekmode.
1151 * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1153 * Sets the position of the read/write mark in @str. The position is controlled
1154 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1155 * <code>seekmode_</code> constants below.
1157 * It is illegal to specify a position before the beginning or after the end of
1160 * In binary files, the mark position is exact — it corresponds with the
1161 * number of characters you have read or written. In text files, this mapping
1162 * can vary, because of linefeed conventions or other character-set
1163 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1164 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1165 * the platform's native encoding — after character cookery. Therefore,
1166 * in a text stream, it is safest to use glk_stream_set_position() only to move
1167 * to the beginning or end of a file, or to a position determined by
1168 * glk_stream_get_position().
1170 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1171 * characters are 32-bit words, or four bytes each.
1174 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1176 VALID_STREAM(str, return);
1177 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1178 g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1182 case STREAM_TYPE_MEMORY:
1185 case seekmode_Start: str->mark = pos; break;
1186 case seekmode_Current: str->mark += pos; break;
1187 case seekmode_End: str->mark = str->buflen + pos; break;
1189 g_return_if_reached();
1193 case STREAM_TYPE_FILE:
1198 case seekmode_Start: whence = SEEK_SET; break;
1199 case seekmode_Current: whence = SEEK_CUR; break;
1200 case seekmode_End: whence = SEEK_END; break;
1202 g_return_if_reached();
1205 if(fseek(str->file_pointer, pos, whence) == -1)
1206 WARNING("Seek failed on file stream");
1210 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);