8 #include <glib/gstdio.h>
12 **************** WRITING FUNCTIONS ********************************************
16 /* Internal function: write a UTF-8 string to a text grid window's text buffer. */
18 write_utf8_to_grid(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 grid window with line input pending.");
26 /* Number of characters to insert */
27 glong length = g_utf8_strlen(s, -1);
28 glong chars_left = length;
32 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
33 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
35 /* Get cursor position */
37 gtk_text_buffer_get_iter_at_mark(buffer, &start, cursor);
38 /* Spaces available on this line */
39 gint available_space = win->width - gtk_text_iter_get_line_offset(&start);
41 while(chars_left > available_space && !gtk_text_iter_is_end(&start))
43 GtkTextIter end = start;
44 gtk_text_iter_forward_to_line_end(&end);
45 gtk_text_buffer_delete(buffer, &start, &end);
46 gtk_text_buffer_insert(buffer, &start, s + (length - chars_left), available_space);
47 chars_left -= available_space;
48 gtk_text_iter_forward_line(&start);
49 available_space = win->width;
51 if(!gtk_text_iter_is_end(&start))
53 GtkTextIter end = start;
54 gtk_text_iter_forward_chars(&end, chars_left);
55 gtk_text_buffer_delete(buffer, &start, &end);
56 gtk_text_buffer_insert(buffer, &start, s + (length - chars_left), -1);
59 gtk_text_buffer_move_mark(buffer, cursor, &start);
64 /* Internal function: write a UTF-8 string to a text buffer window's text buffer. */
66 write_utf8_to_window(winid_t win, gchar *s)
68 if(win->input_request_type == INPUT_REQUEST_LINE || win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
70 ILLEGAL("Tried to print to a text buffer window with line input pending.");
76 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
79 gtk_text_buffer_get_end_iter(buffer, &iter);
80 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, s, -1, win->window_stream->style, NULL);
85 /* Internal function: write a Latin-1 buffer with length to a stream. */
87 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
91 case STREAM_TYPE_WINDOW:
92 /* Each window type has a different way of printing to it */
93 switch(str->window->type)
95 /* Printing to these windows' streams does nothing */
98 case wintype_Graphics:
99 str->write_count += len;
102 /* Text grid window */
103 case wintype_TextGrid:
105 gchar *utf8 = convert_latin1_to_utf8(buf, len);
108 /* FIXME: What to do if string contains \n? Split the input string at newlines and write each string separately? */
109 write_utf8_to_grid(str->window, utf8);
113 str->write_count += len;
116 /* Text buffer window */
117 case wintype_TextBuffer:
119 gchar *utf8 = convert_latin1_to_utf8(buf, len);
122 write_utf8_to_window(str->window, utf8);
126 str->write_count += len;
129 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
132 /* Now write the same buffer to the window's echo stream */
133 if(str->window->echo_stream != NULL)
134 write_buffer_to_stream(str->window->echo_stream, buf, len);
138 case STREAM_TYPE_MEMORY:
139 if(str->unicode && str->ubuffer)
142 while(str->mark < str->buflen && foo < len)
143 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
145 if(!str->unicode && str->buffer)
147 int copycount = MIN(len, str->buflen - str->mark);
148 memmove(str->buffer + str->mark, buf, copycount);
149 str->mark += copycount;
152 str->write_count += len;
155 case STREAM_TYPE_FILE:
160 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
161 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
164 else /* Regular file */
166 fwrite(buf, sizeof(gchar), len, str->file_pointer);
169 else /* Text mode is the same for Unicode and regular files */
171 gchar *utf8 = convert_latin1_to_utf8(buf, len);
174 g_fprintf(str->file_pointer, "%s", utf8);
179 str->write_count += len;
182 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
186 /* Internal function: write a Unicode buffer with length to a stream. */
188 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
192 case STREAM_TYPE_WINDOW:
193 /* Each window type has a different way of printing to it */
194 switch(str->window->type)
196 /* Printing to these windows' streams does nothing */
199 case wintype_Graphics:
200 str->write_count += len;
203 /* Text grid window */
204 case wintype_TextGrid:
206 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
209 /* FIXME: What to do if string contains \n? Split the input string at newlines and write each string separately? */
210 write_utf8_to_grid(str->window, utf8);
214 str->write_count += len;
217 /* Text buffer window */
218 case wintype_TextBuffer:
220 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
223 write_utf8_to_window(str->window, utf8);
227 str->write_count += len;
230 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
233 /* Now write the same buffer to the window's echo stream */
234 if(str->window->echo_stream != NULL)
235 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
239 case STREAM_TYPE_MEMORY:
240 if(str->unicode && str->ubuffer)
242 int copycount = MIN(len, str->buflen - str->mark);
243 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
244 str->mark += copycount;
246 if(!str->unicode && str->buffer)
248 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
249 int copycount = MIN(len, str->buflen - str->mark);
250 memmove(str->buffer + str->mark, latin1, copycount);
252 str->mark += copycount;
255 str->write_count += len;
258 case STREAM_TYPE_FILE:
263 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
264 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
267 else /* Regular file */
269 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
270 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
274 else /* Text mode is the same for Unicode and regular files */
276 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
279 g_fprintf(str->file_pointer, "%s", utf8);
284 str->write_count += len;
287 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
292 * glk_put_char_stream:
293 * @str: An output stream.
294 * @ch: A character in Latin-1 encoding.
296 * The same as glk_put_char(), except that you specify a stream @str to print
297 * to, instead of using the current stream. It is illegal for @str to be %NULL,
298 * or an input-only stream.
301 glk_put_char_stream(strid_t str, unsigned char ch)
303 VALID_STREAM(str, return);
304 g_return_if_fail(str->file_mode != filemode_Read);
306 write_buffer_to_stream(str, (gchar *)&ch, 1);
310 * glk_put_char_stream_uni:
311 * @str: An output stream.
312 * @ch: A Unicode code point.
314 * The same as glk_put_char_uni(), except that you specify a stream @str to
315 * print to, instead of using the current stream. It is illegal for @str to be
316 * %NULL, or an input-only stream.
319 glk_put_char_stream_uni(strid_t str, glui32 ch)
321 VALID_STREAM(str, return);
322 g_return_if_fail(str->file_mode != filemode_Read);
324 write_buffer_to_stream_uni(str, &ch, 1);
328 * glk_put_string_stream:
329 * @str: An output stream.
330 * @s: A null-terminated string in Latin-1 encoding.
332 * The same as glk_put_string(), except that you specify a stream @str to print
333 * to, instead of using the current stream. It is illegal for @str to be %NULL,
334 * or an input-only stream.
337 glk_put_string_stream(strid_t str, char *s)
339 VALID_STREAM(str, return);
340 g_return_if_fail(str->file_mode != filemode_Read);
342 write_buffer_to_stream(str, s, strlen(s));
346 * glk_put_string_stream_uni:
347 * @str: An output stream.
348 * @s: A null-terminated array of Unicode code points.
350 * The same as glk_put_string_uni(), except that you specify a stream @str to
351 * print to, instead of using the current stream. It is illegal for @str to be
352 * %NULL, or an input-only stream.
355 glk_put_string_stream_uni(strid_t str, glui32 *s)
357 VALID_STREAM(str, return);
358 g_return_if_fail(str->file_mode != filemode_Read);
360 /* An impromptu strlen() for glui32 arrays */
365 write_buffer_to_stream_uni(str, s, len);
369 * glk_put_buffer_stream:
370 * @str: An output stream.
371 * @buf: An array of characters in Latin-1 encoding.
372 * @len: Length of @buf.
374 * The same as glk_put_buffer(), except that you specify a stream @str to print
375 * to, instead of using the current stream. It is illegal for @str to be %NULL,
376 * or an input-only stream.
379 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
381 VALID_STREAM(str, return);
382 g_return_if_fail(str->file_mode != filemode_Read);
384 write_buffer_to_stream(str, buf, len);
388 * glk_put_buffer_stream_uni:
389 * @str: An output stream.
390 * @buf: An array of Unicode code points.
391 * @len: Length of @buf.
393 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
394 * print to, instead of using the current stream. It is illegal for @str to be
395 * %NULL, or an input-only stream.
398 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
400 VALID_STREAM(str, return);
401 g_return_if_fail(str->file_mode != filemode_Read);
403 write_buffer_to_stream_uni(str, buf, len);
408 **************** READING FUNCTIONS ********************************************
412 /* Internal function: Read one big-endian four-byte character from file fp and
413 return it as a Unicode code point, or -1 on EOF */
415 read_ucs4be_char_from_file(FILE *fp)
417 unsigned char readbuffer[4];
418 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
421 readbuffer[0] << 24 |
422 readbuffer[1] << 16 |
427 /* Internal function: Read one UTF-8 character, which may be more than one byte,
428 from file fp and return it as a Unicode code point, or -1 on EOF */
430 read_utf8_char_from_file(FILE *fp)
432 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
434 gunichar charresult = (gunichar)-2;
435 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
440 readbuffer[foo] = (gchar)ch;
441 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
442 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
445 /* Silently return unknown characters as 0xFFFD, Replacement Character */
446 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
451 /* Internal function: Tell whether this code point is a Unicode newline. The
452 file pointer and eight-bit flag are included in case the newline is a CR
453 (U+000D). If the next character is LF (U+000A) then it also belongs to the
456 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
458 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
461 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
462 read_ucs4be_char_from_file(fp);
464 if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
465 WARNING_S("Seek failed on stream", g_strerror(errno) );
471 /* Internal function: Read one character from a stream. Returns a value which
472 can be returned unchanged by glk_get_char_stream_uni(), but
473 glk_get_char_stream() must replace high values by the placeholder character. */
475 get_char_stream_common(strid_t str)
479 case STREAM_TYPE_MEMORY:
482 if(!str->ubuffer || str->mark >= str->buflen)
484 glui32 ch = str->ubuffer[str->mark++];
490 if(!str->buffer || str->mark >= str->buflen)
492 unsigned char ch = str->buffer[str->mark++];
498 case STREAM_TYPE_FILE:
503 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
509 else /* Regular file */
511 int ch = fgetc(str->file_pointer);
519 else /* Text mode is the same for Unicode and regular files */
521 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
529 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
535 * glk_get_char_stream:
536 * @str: An input stream.
538 * Reads one character from the stream @str. (There is no notion of a
539 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
540 * an output-only stream.
542 * The result will be between 0 and 255. As with all basic text functions, Glk
543 * assumes the Latin-1 encoding. See <link
544 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
545 * of the stream has been reached, the result will be -1.
548 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
549 * returned as negative numbers.
552 * If the stream contains Unicode data — for example, if it was created
553 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
554 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
556 * It is usually more efficient to read several characters at once with
557 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
558 * glk_get_char_stream() several times.
560 * Returns: A character value between 0 and 255, or -1 on end of stream.
563 glk_get_char_stream(strid_t str)
565 VALID_STREAM(str, return -1);
566 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
568 glsi32 ch = get_char_stream_common(str);
569 return (ch > 0xFF)? PLACEHOLDER : ch;
573 * glk_get_char_stream_uni:
574 * @str: An input stream.
576 * Reads one character from the stream @str. The result will be between 0 and
577 * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
579 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
582 glk_get_char_stream_uni(strid_t str)
584 VALID_STREAM(str, return -1);
585 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
587 return get_char_stream_common(str);
591 * glk_get_buffer_stream:
592 * @str: An input stream.
593 * @buf: A buffer with space for at least @len characters.
594 * @len: The number of characters to read.
596 * Reads @len characters from @str, unless the end of stream is reached first.
597 * No terminal null is placed in the buffer.
599 * Returns: The number of characters actually read.
602 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
604 VALID_STREAM(str, return 0);
605 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
606 g_return_val_if_fail(buf != NULL, 0);
610 case STREAM_TYPE_MEMORY:
615 while(copycount < len && str->ubuffer && str->mark < str->buflen)
617 glui32 ch = str->ubuffer[str->mark++];
618 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
623 if(str->buffer) /* if not, copycount stays 0 */
624 copycount = MIN(len, str->buflen - str->mark);
625 memmove(buf, str->buffer + str->mark, copycount);
626 str->mark += copycount;
629 str->read_count += copycount;
632 case STREAM_TYPE_FILE:
635 if(str->unicode) /* Binary file with 4-byte characters */
637 /* Read len characters of 4 bytes each */
638 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
639 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
640 /* If there was an incomplete character */
644 WARNING("Incomplete character in binary Unicode file");
648 for(foo = 0; foo < count; foo += 4)
650 glsi32 ch = readbuffer[foo] << 24
651 | readbuffer[foo + 1] << 16
652 | readbuffer[foo + 2] << 8
653 | readbuffer[foo + 3];
654 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
657 str->read_count += count / 4;
660 else /* Regular binary file */
662 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
663 str->read_count += count;
667 else /* Text mode is the same for Unicode and regular files */
669 /* Do it character-by-character */
671 for(foo = 0; foo < len; foo++)
673 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
677 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
682 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
688 * glk_get_buffer_stream_uni:
689 * @str: An input stream.
690 * @buf: A buffer with space for at least @len Unicode code points.
691 * @len: The number of characters to read.
693 * Reads @len Unicode characters from @str, unless the end of stream is reached
694 * first. No terminal null is placed in the buffer.
696 * Returns: The number of Unicode characters actually read.
699 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
701 VALID_STREAM(str, return 0);
702 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
703 g_return_val_if_fail(buf != NULL, 0);
707 case STREAM_TYPE_MEMORY:
712 if(str->ubuffer) /* if not, copycount stays 0 */
713 copycount = MIN(len, str->buflen - str->mark);
714 memmove(buf, str->ubuffer + str->mark, copycount * 4);
715 str->mark += copycount;
719 while(copycount < len && str->buffer && str->mark < str->buflen)
721 unsigned char ch = str->buffer[str->mark++];
722 buf[copycount++] = ch;
726 str->read_count += copycount;
729 case STREAM_TYPE_FILE:
732 if(str->unicode) /* Binary file with 4-byte characters */
734 /* Read len characters of 4 bytes each */
735 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
736 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
737 /* If there was an incomplete character */
741 WARNING("Incomplete character in binary Unicode file");
745 for(foo = 0; foo < count; foo += 4)
746 buf[foo / 4] = readbuffer[foo] << 24
747 | readbuffer[foo + 1] << 16
748 | readbuffer[foo + 2] << 8
749 | readbuffer[foo + 3];
751 str->read_count += count / 4;
754 else /* Regular binary file */
756 unsigned char *readbuffer = g_new0(unsigned char, len);
757 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
759 for(foo = 0; foo < count; foo++)
760 buf[foo] = readbuffer[foo];
762 str->read_count += count;
766 else /* Text mode is the same for Unicode and regular files */
768 /* Do it character-by-character */
770 for(foo = 0; foo < len; foo++)
772 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
781 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
787 * glk_get_line_stream:
788 * @str: An input stream.
789 * @buf: A buffer with space for at least @len characters.
790 * @len: The number of characters to read, plus one.
792 * Reads characters from @str, until either
794 * <alt>@len - 1</alt>
795 * <mathphrase>@len - 1</mathphrase>
797 * characters have been read or a newline has been read. It then puts a
798 * terminal null (<code>'\0'</code>) aracter on
799 * the end. It returns the number of characters actually read, including the
800 * newline (if there is one) but not including the terminal null.
802 * Returns: The number of characters actually read.
805 glk_get_line_stream(strid_t str, char *buf, glui32 len)
807 VALID_STREAM(str, return 0);
808 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
809 g_return_val_if_fail(buf != NULL, 0);
813 case STREAM_TYPE_MEMORY:
818 /* Do it character-by-character */
819 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
821 glui32 ch = str->ubuffer[str->mark++];
822 /* Check for Unicode newline; slightly different than
824 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
826 buf[copycount++] = '\n';
831 if(str->ubuffer[str->mark] == 0x0A)
832 str->mark++; /* skip past next newline */
833 buf[copycount++] = '\n';
836 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
838 buf[copycount] = '\0';
842 if(str->buffer) /* if not, copycount stays 0 */
843 copycount = MIN(len - 1, str->buflen - str->mark);
844 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
845 if(endptr) /* newline was found */
846 copycount = endptr - buf; /* Real copy count */
847 buf[copycount] = '\0';
848 str->mark += copycount;
851 str->read_count += copycount;
854 case STREAM_TYPE_FILE:
857 if(str->unicode) /* Binary file with 4-byte characters */
859 /* Do it character-by-character */
861 for(foo = 0; foo < len - 1; foo++)
863 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
870 if(is_unicode_newline(ch, str->file_pointer, FALSE))
876 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
881 else /* Regular binary file */
883 fgets(buf, len, str->file_pointer);
884 str->read_count += strlen(buf);
888 else /* Text mode is the same for Unicode and regular files */
890 /* Do it character-by-character */
892 for(foo = 0; foo < len - 1; foo++)
894 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
901 if(is_unicode_newline(ch, str->file_pointer, TRUE))
907 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
913 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
919 * glk_get_line_stream_uni:
920 * @str: An input stream.
921 * @buf: A buffer with space for at least @len Unicode code points.
922 * @len: The number of characters to read, plus one.
924 * Reads Unicode characters from @str, until either
926 * <alt>@len - 1</alt>
927 * <mathphrase>@len - 1</mathphrase>
929 * Unicode characters have been read or a newline has been read. It then puts a
930 * terminal null (a zero value) on the end.
932 * Returns: The number of characters actually read, including the newline (if
933 * there is one) but not including the terminal null.
936 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
938 VALID_STREAM(str, return 0);
939 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
940 g_return_val_if_fail(buf != NULL, 0);
944 case STREAM_TYPE_MEMORY:
949 /* Do it character-by-character */
950 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
952 glui32 ch = str->ubuffer[str->mark++];
953 /* Check for Unicode newline; slightly different than
955 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
957 buf[copycount++] = '\n';
962 if(str->ubuffer[str->mark] == 0x0A)
963 str->mark++; /* skip past next newline */
964 buf[copycount++] = '\n';
967 buf[copycount++] = ch;
969 buf[copycount] = '\0';
973 /* No recourse to memccpy(), so do it character-by-character */
974 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
976 gchar ch = str->buffer[str->mark++];
977 /* Check for newline */
978 if(ch == '\n') /* Also check for \r and \r\n? */
980 buf[copycount++] = '\n';
983 buf[copycount++] = (unsigned char)ch;
988 str->read_count += copycount;
991 case STREAM_TYPE_FILE:
994 if(str->unicode) /* Binary file with 4-byte characters */
996 /* Do it character-by-character */
998 for(foo = 0; foo < len - 1; foo++)
1000 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1007 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1009 buf[foo] = ch; /* Preserve newline types??? */
1018 else /* Regular binary file */
1020 gchar *readbuffer = g_new0(gchar, len);
1021 fgets(readbuffer, len, str->file_pointer);
1022 glui32 count = strlen(readbuffer) + 1; /* Copy terminator */
1024 for(foo = 0; foo < count; foo++)
1025 buf[foo] = (unsigned char)(readbuffer[foo]);
1026 str->read_count += count;
1030 else /* Text mode is the same for Unicode and regular files */
1032 /* Do it character-by-character */
1034 for(foo = 0; foo < len - 1; foo++)
1036 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1043 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1045 buf[foo] = ch; /* Preserve newline types??? */
1055 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1062 **************** SEEKING FUNCTIONS ********************************************
1067 * glk_stream_get_position:
1068 * @str: A file or memory stream.
1070 * Returns the position of the read/write mark in @str. For memory streams and
1071 * binary file streams, this is exactly the number of characters read or written
1072 * from the beginning of the stream (unless you have moved the mark with
1073 * glk_stream_set_position().) For text file streams, matters are more
1074 * ambiguous, since (for example) writing one byte to a text file may store more
1075 * than one character in the platform's native encoding. You can only be sure
1076 * that the position increases as you read or write to the file.
1078 * Additional complication: for Latin-1 memory and file streams, a character is
1079 * a byte. For Unicode memory and file streams (those created by
1080 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1081 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1085 * If this bothers you, don't use binary Unicode files. I don't think they're
1086 * good for much anyhow.
1089 * Returns: position of the read/write mark in @str.
1092 glk_stream_get_position(strid_t str)
1094 VALID_STREAM(str, return 0);
1098 case STREAM_TYPE_MEMORY:
1100 case STREAM_TYPE_FILE:
1101 return ftell(str->file_pointer);
1103 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1109 * glk_stream_set_position:
1110 * @str: A file or memory stream.
1111 * @pos: The position to set the mark to, relative to @seekmode.
1112 * @seekmode: One of #seekmode_Start, #seekmode_Current, or #seekmode_End.
1114 * Sets the position of the read/write mark in @str. The position is controlled
1115 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1116 * <code>seekmode_</code> constants below.
1118 * It is illegal to specify a position before the beginning or after the end of
1121 * In binary files, the mark position is exact — it corresponds with the
1122 * number of characters you have read or written. In text files, this mapping
1123 * can vary, because of linefeed conventions or other character-set
1124 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1125 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1126 * the platform's native encoding — after character cookery. Therefore,
1127 * in a text stream, it is safest to use glk_stream_set_position() only to move
1128 * to the beginning or end of a file, or to a position determined by
1129 * glk_stream_get_position().
1131 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1132 * characters are 32-bit words, or four bytes each.
1135 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1137 VALID_STREAM(str, return);
1138 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1139 g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1143 case STREAM_TYPE_MEMORY:
1146 case seekmode_Start: str->mark = pos; break;
1147 case seekmode_Current: str->mark += pos; break;
1148 case seekmode_End: str->mark = str->buflen + pos; break;
1150 g_return_if_reached();
1154 case STREAM_TYPE_FILE:
1159 case seekmode_Start: whence = SEEK_SET; break;
1160 case seekmode_Current: whence = SEEK_CUR; break;
1161 case seekmode_End: whence = SEEK_END; break;
1163 g_return_if_reached();
1166 if(fseek(str->file_pointer, pos, whence) == -1)
1167 WARNING("Seek failed on file stream");
1171 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);