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);
66 /* Schedule a check for the pager */
67 g_idle_add(pager_check, win);
72 case wintype_TextGrid:
74 /* Number of characters to insert */
75 glong length = win->buffer->len;
76 glong chars_left = length;
78 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
80 /* Get cursor position */
82 gtk_text_buffer_get_iter_at_mark(buffer, &start, cursor);
83 /* Spaces available on this line */
84 gint available_space = win->width - gtk_text_iter_get_line_offset(&start);
86 while(chars_left > available_space && !gtk_text_iter_is_end(&start))
88 GtkTextIter end = start;
89 gtk_text_iter_forward_to_line_end(&end);
90 gtk_text_buffer_delete(buffer, &start, &end);
92 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
93 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
95 if(win->window_stream->hyperlink_mode) {
96 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
97 GtkTextTag *link_tag = win->current_hyperlink->tag;
98 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), available_space, style_tag, link_style_tag, link_tag, NULL);
100 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), available_space, style_tag, NULL);
103 chars_left -= available_space;
104 gtk_text_iter_forward_line(&start);
105 available_space = win->width;
107 if(!gtk_text_iter_is_end(&start))
109 GtkTextIter end = start;
110 gtk_text_iter_forward_chars(&end, chars_left);
111 gtk_text_buffer_delete(buffer, &start, &end);
113 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
114 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
116 if(win->window_stream->hyperlink_mode) {
117 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
118 GtkTextTag *link_tag = win->current_hyperlink->tag;
119 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, style_tag, link_style_tag, link_tag, NULL);
121 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, style_tag, NULL);
125 gtk_text_buffer_move_mark(buffer, cursor, &start);
132 g_string_truncate(win->buffer, 0);
135 /* Internal function: write a Latin-1 buffer with length to a stream. */
137 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
141 case STREAM_TYPE_WINDOW:
142 /* Each window type has a different way of printing to it */
143 switch(str->window->type)
145 /* Printing to these windows' streams does nothing */
148 case wintype_Graphics:
149 str->write_count += len;
152 /* Text grid/buffer windows */
153 case wintype_TextGrid:
154 case wintype_TextBuffer:
156 gchar *utf8 = convert_latin1_to_utf8(buf, len);
158 write_utf8_to_window_buffer(str->window, utf8);
162 str->write_count += len;
165 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
168 /* Now write the same buffer to the window's echo stream */
169 if(str->window->echo_stream != NULL)
170 write_buffer_to_stream(str->window->echo_stream, buf, len);
174 case STREAM_TYPE_MEMORY:
175 if(str->unicode && str->ubuffer)
178 while(str->mark < str->buflen && foo < len)
179 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
181 if(!str->unicode && str->buffer)
183 int copycount = MIN(len, str->buflen - str->mark);
184 memmove(str->buffer + str->mark, buf, copycount);
185 str->mark += copycount;
188 str->write_count += len;
191 case STREAM_TYPE_FILE:
196 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
197 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
200 else /* Regular file */
202 fwrite(buf, sizeof(gchar), len, str->file_pointer);
205 else /* Text mode is the same for Unicode and regular files */
207 gchar *utf8 = convert_latin1_to_utf8(buf, len);
210 g_fprintf(str->file_pointer, "%s", utf8);
215 str->write_count += len;
218 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
222 /* Internal function: write a Unicode buffer with length to a stream. */
224 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
228 case STREAM_TYPE_WINDOW:
229 /* Each window type has a different way of printing to it */
230 switch(str->window->type)
232 /* Printing to these windows' streams does nothing */
235 case wintype_Graphics:
236 str->write_count += len;
239 /* Text grid/buffer windows */
240 case wintype_TextGrid:
241 case wintype_TextBuffer:
243 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
245 write_utf8_to_window_buffer(str->window, utf8);
249 str->write_count += len;
252 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
255 /* Now write the same buffer to the window's echo stream */
256 if(str->window->echo_stream != NULL)
257 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
261 case STREAM_TYPE_MEMORY:
262 if(str->unicode && str->ubuffer)
264 int copycount = MIN(len, str->buflen - str->mark);
265 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
266 str->mark += copycount;
268 if(!str->unicode && str->buffer)
270 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
271 int copycount = MIN(len, str->buflen - str->mark);
272 memmove(str->buffer + str->mark, latin1, copycount);
274 str->mark += copycount;
277 str->write_count += len;
280 case STREAM_TYPE_FILE:
285 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
286 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
289 else /* Regular file */
291 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
292 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
296 else /* Text mode is the same for Unicode and regular files */
298 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
301 g_fprintf(str->file_pointer, "%s", utf8);
306 str->write_count += len;
309 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
314 * glk_put_char_stream:
315 * @str: An output stream.
316 * @ch: A character in Latin-1 encoding.
318 * The same as glk_put_char(), except that you specify a stream @str to print
319 * to, instead of using the current stream. It is illegal for @str to be %NULL,
320 * or an input-only stream.
323 glk_put_char_stream(strid_t str, unsigned char ch)
325 VALID_STREAM(str, return);
326 g_return_if_fail(str->file_mode != filemode_Read);
328 write_buffer_to_stream(str, (gchar *)&ch, 1);
332 * glk_put_char_stream_uni:
333 * @str: An output stream.
334 * @ch: A Unicode code point.
336 * The same as glk_put_char_uni(), except that you specify a stream @str to
337 * print to, instead of using the current stream. It is illegal for @str to be
338 * %NULL, or an input-only stream.
341 glk_put_char_stream_uni(strid_t str, glui32 ch)
343 VALID_STREAM(str, return);
344 g_return_if_fail(str->file_mode != filemode_Read);
346 write_buffer_to_stream_uni(str, &ch, 1);
350 * glk_put_string_stream:
351 * @str: An output stream.
352 * @s: A null-terminated string in Latin-1 encoding.
354 * The same as glk_put_string(), except that you specify a stream @str to print
355 * to, instead of using the current stream. It is illegal for @str to be %NULL,
356 * or an input-only stream.
359 glk_put_string_stream(strid_t str, char *s)
361 VALID_STREAM(str, return);
365 g_return_if_fail(str->file_mode != filemode_Read);
367 write_buffer_to_stream(str, s, strlen(s));
371 * glk_put_string_stream_uni:
372 * @str: An output stream.
373 * @s: A null-terminated array of Unicode code points.
375 * The same as glk_put_string_uni(), except that you specify a stream @str to
376 * print to, instead of using the current stream. It is illegal for @str to be
377 * %NULL, or an input-only stream.
380 glk_put_string_stream_uni(strid_t str, glui32 *s)
382 VALID_STREAM(str, return);
386 g_return_if_fail(str->file_mode != filemode_Read);
388 /* An impromptu strlen() for glui32 arrays */
393 write_buffer_to_stream_uni(str, s, len);
397 * glk_put_buffer_stream:
398 * @str: An output stream.
399 * @buf: An array of characters in Latin-1 encoding.
400 * @len: Length of @buf.
402 * The same as glk_put_buffer(), except that you specify a stream @str to print
403 * to, instead of using the current stream. It is illegal for @str to be %NULL,
404 * or an input-only stream.
407 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
409 VALID_STREAM(str, return);
413 g_return_if_fail(str->file_mode != filemode_Read);
415 write_buffer_to_stream(str, buf, len);
419 * glk_put_buffer_stream_uni:
420 * @str: An output stream.
421 * @buf: An array of Unicode code points.
422 * @len: Length of @buf.
424 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
425 * print to, instead of using the current stream. It is illegal for @str to be
426 * %NULL, or an input-only stream.
429 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
431 VALID_STREAM(str, return);
435 g_return_if_fail(str->file_mode != filemode_Read);
437 write_buffer_to_stream_uni(str, buf, len);
442 **************** READING FUNCTIONS ********************************************
446 /* Internal function: Read one big-endian four-byte character from file fp and
447 return it as a Unicode code point, or -1 on EOF */
449 read_ucs4be_char_from_file(FILE *fp)
451 unsigned char readbuffer[4];
452 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
455 readbuffer[0] << 24 |
456 readbuffer[1] << 16 |
461 /* Internal function: Read one UTF-8 character, which may be more than one byte,
462 from file fp and return it as a Unicode code point, or -1 on EOF */
464 read_utf8_char_from_file(FILE *fp)
466 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
468 gunichar charresult = (gunichar)-2;
469 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
474 readbuffer[foo] = (gchar)ch;
475 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
476 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
479 /* Silently return unknown characters as 0xFFFD, Replacement Character */
480 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
485 /* Internal function: Tell whether this code point is a Unicode newline. The
486 file pointer and eight-bit flag are included in case the newline is a CR
487 (U+000D). If the next character is LF (U+000A) then it also belongs to the
490 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
492 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
495 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
496 read_ucs4be_char_from_file(fp);
498 if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
499 WARNING_S("Seek failed on stream", g_strerror(errno) );
505 /* Internal function: Read one character from a stream. Returns a value which
506 can be returned unchanged by glk_get_char_stream_uni(), but
507 glk_get_char_stream() must replace high values by the placeholder character. */
509 get_char_stream_common(strid_t str)
513 case STREAM_TYPE_MEMORY:
516 if(!str->ubuffer || str->mark >= str->buflen)
518 glui32 ch = str->ubuffer[str->mark++];
524 if(!str->buffer || str->mark >= str->buflen)
526 unsigned char ch = str->buffer[str->mark++];
532 case STREAM_TYPE_FILE:
537 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
543 else /* Regular file */
545 int ch = fgetc(str->file_pointer);
553 else /* Text mode is the same for Unicode and regular files */
555 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
563 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
569 * glk_get_char_stream:
570 * @str: An input stream.
572 * Reads one character from the stream @str. (There is no notion of a
573 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
574 * an output-only stream.
576 * The result will be between 0 and 255. As with all basic text functions, Glk
577 * assumes the Latin-1 encoding. See <link
578 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
579 * of the stream has been reached, the result will be -1.
582 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
583 * returned as negative numbers.
586 * If the stream contains Unicode data — for example, if it was created
587 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
588 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
590 * It is usually more efficient to read several characters at once with
591 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
592 * glk_get_char_stream() several times.
594 * Returns: A character value between 0 and 255, or -1 on end of stream.
597 glk_get_char_stream(strid_t str)
599 VALID_STREAM(str, return -1);
600 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
602 glsi32 ch = get_char_stream_common(str);
603 return (ch > 0xFF)? PLACEHOLDER : ch;
607 * glk_get_char_stream_uni:
608 * @str: An input stream.
610 * Reads one character from the stream @str. The result will be between 0 and
611 * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
613 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
616 glk_get_char_stream_uni(strid_t str)
618 VALID_STREAM(str, return -1);
619 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
621 return get_char_stream_common(str);
625 * glk_get_buffer_stream:
626 * @str: An input stream.
627 * @buf: A buffer with space for at least @len characters.
628 * @len: The number of characters to read.
630 * Reads @len characters from @str, unless the end of stream is reached first.
631 * No terminal null is placed in the buffer.
633 * Returns: The number of characters actually read.
636 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
638 VALID_STREAM(str, return 0);
639 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
640 g_return_val_if_fail(buf != NULL, 0);
644 case STREAM_TYPE_MEMORY:
649 while(copycount < len && str->ubuffer && str->mark < str->buflen)
651 glui32 ch = str->ubuffer[str->mark++];
652 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
657 if(str->buffer) /* if not, copycount stays 0 */
658 copycount = MIN(len, str->buflen - str->mark);
659 memmove(buf, str->buffer + str->mark, copycount);
660 str->mark += copycount;
663 str->read_count += copycount;
666 case STREAM_TYPE_FILE:
669 if(str->unicode) /* Binary file with 4-byte characters */
671 /* Read len characters of 4 bytes each */
672 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
673 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
674 /* If there was an incomplete character */
678 WARNING("Incomplete character in binary Unicode file");
682 for(foo = 0; foo < count; foo += 4)
684 glsi32 ch = readbuffer[foo] << 24
685 | readbuffer[foo + 1] << 16
686 | readbuffer[foo + 2] << 8
687 | readbuffer[foo + 3];
688 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
691 str->read_count += count / 4;
694 else /* Regular binary file */
696 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
697 str->read_count += count;
701 else /* Text mode is the same for Unicode and regular files */
703 /* Do it character-by-character */
705 for(foo = 0; foo < len; foo++)
707 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
711 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
716 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
722 * glk_get_buffer_stream_uni:
723 * @str: An input stream.
724 * @buf: A buffer with space for at least @len Unicode code points.
725 * @len: The number of characters to read.
727 * Reads @len Unicode characters from @str, unless the end of stream is reached
728 * first. No terminal null is placed in the buffer.
730 * Returns: The number of Unicode characters actually read.
733 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
735 VALID_STREAM(str, return 0);
736 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
737 g_return_val_if_fail(buf != NULL, 0);
741 case STREAM_TYPE_MEMORY:
746 if(str->ubuffer) /* if not, copycount stays 0 */
747 copycount = MIN(len, str->buflen - str->mark);
748 memmove(buf, str->ubuffer + str->mark, copycount * 4);
749 str->mark += copycount;
753 while(copycount < len && str->buffer && str->mark < str->buflen)
755 unsigned char ch = str->buffer[str->mark++];
756 buf[copycount++] = ch;
760 str->read_count += copycount;
763 case STREAM_TYPE_FILE:
766 if(str->unicode) /* Binary file with 4-byte characters */
768 /* Read len characters of 4 bytes each */
769 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
770 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
771 /* If there was an incomplete character */
775 WARNING("Incomplete character in binary Unicode file");
779 for(foo = 0; foo < count; foo += 4)
780 buf[foo / 4] = readbuffer[foo] << 24
781 | readbuffer[foo + 1] << 16
782 | readbuffer[foo + 2] << 8
783 | readbuffer[foo + 3];
785 str->read_count += count / 4;
788 else /* Regular binary file */
790 unsigned char *readbuffer = g_new0(unsigned char, len);
791 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
793 for(foo = 0; foo < count; foo++)
794 buf[foo] = readbuffer[foo];
796 str->read_count += count;
800 else /* Text mode is the same for Unicode and regular files */
802 /* Do it character-by-character */
804 for(foo = 0; foo < len; foo++)
806 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
815 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
821 * glk_get_line_stream:
822 * @str: An input stream.
823 * @buf: A buffer with space for at least @len characters.
824 * @len: The number of characters to read, plus one.
826 * Reads characters from @str, until either
828 * <alt>@len - 1</alt>
829 * <mathphrase>@len - 1</mathphrase>
831 * characters have been read or a newline has been read. It then puts a
832 * terminal null (<code>'\0'</code>) aracter on
833 * the end. It returns the number of characters actually read, including the
834 * newline (if there is one) but not including the terminal null.
836 * Returns: The number of characters actually read.
839 glk_get_line_stream(strid_t str, char *buf, glui32 len)
841 VALID_STREAM(str, return 0);
842 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
843 g_return_val_if_fail(buf != NULL, 0);
847 case STREAM_TYPE_MEMORY:
852 /* Do it character-by-character */
853 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
855 glui32 ch = str->ubuffer[str->mark++];
856 /* Check for Unicode newline; slightly different than
858 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
860 buf[copycount++] = '\n';
865 if(str->ubuffer[str->mark] == 0x0A)
866 str->mark++; /* skip past next newline */
867 buf[copycount++] = '\n';
870 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
872 buf[copycount] = '\0';
876 if(str->buffer) /* if not, copycount stays 0 */
877 copycount = MIN(len - 1, str->buflen - str->mark);
878 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
879 if(endptr) /* newline was found */
880 copycount = endptr - buf; /* Real copy count */
881 buf[copycount] = '\0';
882 str->mark += copycount;
885 str->read_count += copycount;
888 case STREAM_TYPE_FILE:
891 if(str->unicode) /* Binary file with 4-byte characters */
893 /* Do it character-by-character */
895 for(foo = 0; foo < len - 1; foo++)
897 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
904 if(is_unicode_newline(ch, str->file_pointer, FALSE))
910 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
915 else /* Regular binary file */
917 if( !fgets(buf, len, str->file_pointer) ) {
922 int nread = strlen(buf);
923 str->read_count += nread;
927 else /* Text mode is the same for Unicode and regular files */
929 /* Do it character-by-character */
931 for(foo = 0; foo < len - 1; foo++)
933 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
940 if(is_unicode_newline(ch, str->file_pointer, TRUE))
946 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
952 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
958 * glk_get_line_stream_uni:
959 * @str: An input stream.
960 * @buf: A buffer with space for at least @len Unicode code points.
961 * @len: The number of characters to read, plus one.
963 * Reads Unicode characters from @str, until either
965 * <alt>@len - 1</alt>
966 * <mathphrase>@len - 1</mathphrase>
968 * Unicode characters have been read or a newline has been read. It then puts a
969 * terminal null (a zero value) on the end.
971 * Returns: The number of characters actually read, including the newline (if
972 * there is one) but not including the terminal null.
975 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
977 VALID_STREAM(str, return 0);
978 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
979 g_return_val_if_fail(buf != NULL, 0);
983 case STREAM_TYPE_MEMORY:
988 /* Do it character-by-character */
989 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
991 glui32 ch = str->ubuffer[str->mark++];
992 /* Check for Unicode newline; slightly different than
994 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
996 buf[copycount++] = '\n';
1001 if(str->ubuffer[str->mark] == 0x0A)
1002 str->mark++; /* skip past next newline */
1003 buf[copycount++] = '\n';
1006 buf[copycount++] = ch;
1008 buf[copycount] = '\0';
1012 /* No recourse to memccpy(), so do it character-by-character */
1013 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
1015 gchar ch = str->buffer[str->mark++];
1016 /* Check for newline */
1017 if(ch == '\n') /* Also check for \r and \r\n? */
1019 buf[copycount++] = '\n';
1022 buf[copycount++] = (unsigned char)ch;
1027 str->read_count += copycount;
1030 case STREAM_TYPE_FILE:
1033 if(str->unicode) /* Binary file with 4-byte characters */
1035 /* Do it character-by-character */
1037 for(foo = 0; foo < len - 1; foo++)
1039 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1046 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1048 buf[foo] = ch; /* Preserve newline types??? */
1057 else /* Regular binary file */
1059 gchar *readbuffer = g_new0(gchar, len);
1060 if( !fgets(readbuffer, len, str->file_pointer) ) {
1065 glui32 count = strlen(readbuffer);
1067 for(foo = 0; foo < count + 1; foo++) /* Copy terminator */
1068 buf[foo] = (unsigned char)(readbuffer[foo]);
1069 str->read_count += count;
1073 else /* Text mode is the same for Unicode and regular files */
1075 /* Do it character-by-character */
1077 for(foo = 0; foo < len - 1; foo++)
1079 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1086 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1088 buf[foo] = ch; /* Preserve newline types??? */
1098 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1105 **************** SEEKING FUNCTIONS ********************************************
1110 * glk_stream_get_position:
1111 * @str: A file or memory stream.
1113 * Returns the position of the read/write mark in @str. For memory streams and
1114 * binary file streams, this is exactly the number of characters read or written
1115 * from the beginning of the stream (unless you have moved the mark with
1116 * glk_stream_set_position().) For text file streams, matters are more
1117 * ambiguous, since (for example) writing one byte to a text file may store more
1118 * than one character in the platform's native encoding. You can only be sure
1119 * that the position increases as you read or write to the file.
1121 * Additional complication: for Latin-1 memory and file streams, a character is
1122 * a byte. For Unicode memory and file streams (those created by
1123 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1124 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1128 * If this bothers you, don't use binary Unicode files. I don't think they're
1129 * good for much anyhow.
1132 * Returns: position of the read/write mark in @str.
1135 glk_stream_get_position(strid_t str)
1137 VALID_STREAM(str, return 0);
1141 case STREAM_TYPE_MEMORY:
1143 case STREAM_TYPE_FILE:
1144 return ftell(str->file_pointer);
1146 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1152 * glk_stream_set_position:
1153 * @str: A file or memory stream.
1154 * @pos: The position to set the mark to, relative to @seekmode.
1155 * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1157 * Sets the position of the read/write mark in @str. The position is controlled
1158 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1159 * <code>seekmode_</code> constants below.
1161 * It is illegal to specify a position before the beginning or after the end of
1164 * In binary files, the mark position is exact — it corresponds with the
1165 * number of characters you have read or written. In text files, this mapping
1166 * can vary, because of linefeed conventions or other character-set
1167 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1168 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1169 * the platform's native encoding — after character cookery. Therefore,
1170 * in a text stream, it is safest to use glk_stream_set_position() only to move
1171 * to the beginning or end of a file, or to a position determined by
1172 * glk_stream_get_position().
1174 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1175 * characters are 32-bit words, or four bytes each.
1178 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1180 VALID_STREAM(str, return);
1181 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1182 g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1186 case STREAM_TYPE_MEMORY:
1189 case seekmode_Start: str->mark = pos; break;
1190 case seekmode_Current: str->mark += pos; break;
1191 case seekmode_End: str->mark = str->buflen + pos; break;
1193 g_return_if_reached();
1197 case STREAM_TYPE_FILE:
1202 case seekmode_Start: whence = SEEK_SET; break;
1203 case seekmode_Current: whence = SEEK_CUR; break;
1204 case seekmode_End: whence = SEEK_END; break;
1206 g_return_if_reached();
1209 if(fseek(str->file_pointer, pos, whence) == -1)
1210 WARNING("Seek failed on file stream");
1214 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);