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)
34 if(win->type != wintype_TextBuffer && win->type != wintype_TextGrid)
37 if(win->buffer->len == 0)
42 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
45 case wintype_TextBuffer:
48 gtk_text_buffer_get_end_iter(buffer, &iter);
49 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, win->buffer->str, -1, win->window_stream->style, NULL);
51 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
53 g_signal_emit_by_name(glk, "text-buffer-output", win->rock, win->buffer->str);
58 case wintype_TextGrid:
60 /* Number of characters to insert */
61 glong length = win->buffer->len;
62 glong chars_left = length;
64 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
66 /* Get cursor position */
68 gtk_text_buffer_get_iter_at_mark(buffer, &start, cursor);
69 /* Spaces available on this line */
70 gint available_space = win->width - gtk_text_iter_get_line_offset(&start);
72 while(chars_left > available_space && !gtk_text_iter_is_end(&start))
74 GtkTextIter end = start;
75 gtk_text_iter_forward_to_line_end(&end);
76 gtk_text_buffer_delete(buffer, &start, &end);
77 gtk_text_buffer_insert_with_tags_by_name(buffer, &start, win->buffer->str + (length - chars_left), available_space, win->window_stream->style, NULL);
78 chars_left -= available_space;
79 gtk_text_iter_forward_line(&start);
80 available_space = win->width;
82 if(!gtk_text_iter_is_end(&start))
84 GtkTextIter end = start;
85 gtk_text_iter_forward_chars(&end, chars_left);
86 gtk_text_buffer_delete(buffer, &start, &end);
87 gtk_text_buffer_insert_with_tags_by_name(buffer, &start, win->buffer->str + (length - chars_left), -1, win->window_stream->style, NULL);
90 gtk_text_buffer_move_mark(buffer, cursor, &start);
97 g_string_truncate(win->buffer, 0);
100 /* Internal function: write a Latin-1 buffer with length to a stream. */
102 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
106 case STREAM_TYPE_WINDOW:
107 /* Each window type has a different way of printing to it */
108 switch(str->window->type)
110 /* Printing to these windows' streams does nothing */
113 case wintype_Graphics:
114 str->write_count += len;
117 /* Text grid/buffer windows */
118 case wintype_TextGrid:
119 case wintype_TextBuffer:
121 gchar *utf8 = convert_latin1_to_utf8(buf, len);
123 write_utf8_to_window_buffer(str->window, utf8);
127 str->write_count += len;
130 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
133 /* Now write the same buffer to the window's echo stream */
134 if(str->window->echo_stream != NULL)
135 write_buffer_to_stream(str->window->echo_stream, buf, len);
139 case STREAM_TYPE_MEMORY:
140 if(str->unicode && str->ubuffer)
143 while(str->mark < str->buflen && foo < len)
144 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
146 if(!str->unicode && str->buffer)
148 int copycount = MIN(len, str->buflen - str->mark);
149 memmove(str->buffer + str->mark, buf, copycount);
150 str->mark += copycount;
153 str->write_count += len;
156 case STREAM_TYPE_FILE:
161 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
162 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
165 else /* Regular file */
167 fwrite(buf, sizeof(gchar), len, str->file_pointer);
170 else /* Text mode is the same for Unicode and regular files */
172 gchar *utf8 = convert_latin1_to_utf8(buf, len);
175 g_fprintf(str->file_pointer, "%s", utf8);
180 str->write_count += len;
183 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
187 /* Internal function: write a Unicode buffer with length to a stream. */
189 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
193 case STREAM_TYPE_WINDOW:
194 /* Each window type has a different way of printing to it */
195 switch(str->window->type)
197 /* Printing to these windows' streams does nothing */
200 case wintype_Graphics:
201 str->write_count += len;
204 /* Text grid/buffer windows */
205 case wintype_TextGrid:
206 case wintype_TextBuffer:
208 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
210 write_utf8_to_window_buffer(str->window, utf8);
214 str->write_count += len;
217 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
220 /* Now write the same buffer to the window's echo stream */
221 if(str->window->echo_stream != NULL)
222 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
226 case STREAM_TYPE_MEMORY:
227 if(str->unicode && str->ubuffer)
229 int copycount = MIN(len, str->buflen - str->mark);
230 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
231 str->mark += copycount;
233 if(!str->unicode && str->buffer)
235 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
236 int copycount = MIN(len, str->buflen - str->mark);
237 memmove(str->buffer + str->mark, latin1, copycount);
239 str->mark += copycount;
242 str->write_count += len;
245 case STREAM_TYPE_FILE:
250 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
251 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
254 else /* Regular file */
256 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
257 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
261 else /* Text mode is the same for Unicode and regular files */
263 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
266 g_fprintf(str->file_pointer, "%s", utf8);
271 str->write_count += len;
274 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
279 * glk_put_char_stream:
280 * @str: An output stream.
281 * @ch: A character in Latin-1 encoding.
283 * The same as glk_put_char(), except that you specify a stream @str to print
284 * to, instead of using the current stream. It is illegal for @str to be %NULL,
285 * or an input-only stream.
288 glk_put_char_stream(strid_t str, unsigned char ch)
290 VALID_STREAM(str, return);
291 g_return_if_fail(str->file_mode != filemode_Read);
293 write_buffer_to_stream(str, (gchar *)&ch, 1);
297 * glk_put_char_stream_uni:
298 * @str: An output stream.
299 * @ch: A Unicode code point.
301 * The same as glk_put_char_uni(), except that you specify a stream @str to
302 * print to, instead of using the current stream. It is illegal for @str to be
303 * %NULL, or an input-only stream.
306 glk_put_char_stream_uni(strid_t str, glui32 ch)
308 VALID_STREAM(str, return);
309 g_return_if_fail(str->file_mode != filemode_Read);
311 write_buffer_to_stream_uni(str, &ch, 1);
315 * glk_put_string_stream:
316 * @str: An output stream.
317 * @s: A null-terminated string in Latin-1 encoding.
319 * The same as glk_put_string(), except that you specify a stream @str to print
320 * to, instead of using the current stream. It is illegal for @str to be %NULL,
321 * or an input-only stream.
324 glk_put_string_stream(strid_t str, char *s)
326 VALID_STREAM(str, return);
330 g_return_if_fail(str->file_mode != filemode_Read);
332 write_buffer_to_stream(str, s, strlen(s));
336 * glk_put_string_stream_uni:
337 * @str: An output stream.
338 * @s: A null-terminated array of Unicode code points.
340 * The same as glk_put_string_uni(), except that you specify a stream @str to
341 * print to, instead of using the current stream. It is illegal for @str to be
342 * %NULL, or an input-only stream.
345 glk_put_string_stream_uni(strid_t str, glui32 *s)
347 VALID_STREAM(str, return);
351 g_return_if_fail(str->file_mode != filemode_Read);
353 /* An impromptu strlen() for glui32 arrays */
358 write_buffer_to_stream_uni(str, s, len);
362 * glk_put_buffer_stream:
363 * @str: An output stream.
364 * @buf: An array of characters in Latin-1 encoding.
365 * @len: Length of @buf.
367 * The same as glk_put_buffer(), except that you specify a stream @str to print
368 * to, instead of using the current stream. It is illegal for @str to be %NULL,
369 * or an input-only stream.
372 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
374 VALID_STREAM(str, return);
378 g_return_if_fail(str->file_mode != filemode_Read);
380 write_buffer_to_stream(str, buf, len);
384 * glk_put_buffer_stream_uni:
385 * @str: An output stream.
386 * @buf: An array of Unicode code points.
387 * @len: Length of @buf.
389 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
390 * print to, instead of using the current stream. It is illegal for @str to be
391 * %NULL, or an input-only stream.
394 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
396 VALID_STREAM(str, return);
400 g_return_if_fail(str->file_mode != filemode_Read);
402 write_buffer_to_stream_uni(str, buf, len);
407 **************** READING FUNCTIONS ********************************************
411 /* Internal function: Read one big-endian four-byte character from file fp and
412 return it as a Unicode code point, or -1 on EOF */
414 read_ucs4be_char_from_file(FILE *fp)
416 unsigned char readbuffer[4];
417 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
420 readbuffer[0] << 24 |
421 readbuffer[1] << 16 |
426 /* Internal function: Read one UTF-8 character, which may be more than one byte,
427 from file fp and return it as a Unicode code point, or -1 on EOF */
429 read_utf8_char_from_file(FILE *fp)
431 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
433 gunichar charresult = (gunichar)-2;
434 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
439 readbuffer[foo] = (gchar)ch;
440 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
441 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
444 /* Silently return unknown characters as 0xFFFD, Replacement Character */
445 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
450 /* Internal function: Tell whether this code point is a Unicode newline. The
451 file pointer and eight-bit flag are included in case the newline is a CR
452 (U+000D). If the next character is LF (U+000A) then it also belongs to the
455 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
457 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
460 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
461 read_ucs4be_char_from_file(fp);
463 if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
464 WARNING_S("Seek failed on stream", g_strerror(errno) );
470 /* Internal function: Read one character from a stream. Returns a value which
471 can be returned unchanged by glk_get_char_stream_uni(), but
472 glk_get_char_stream() must replace high values by the placeholder character. */
474 get_char_stream_common(strid_t str)
478 case STREAM_TYPE_MEMORY:
481 if(!str->ubuffer || str->mark >= str->buflen)
483 glui32 ch = str->ubuffer[str->mark++];
489 if(!str->buffer || str->mark >= str->buflen)
491 unsigned char ch = str->buffer[str->mark++];
497 case STREAM_TYPE_FILE:
502 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
508 else /* Regular file */
510 int ch = fgetc(str->file_pointer);
518 else /* Text mode is the same for Unicode and regular files */
520 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
528 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
534 * glk_get_char_stream:
535 * @str: An input stream.
537 * Reads one character from the stream @str. (There is no notion of a
538 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
539 * an output-only stream.
541 * The result will be between 0 and 255. As with all basic text functions, Glk
542 * assumes the Latin-1 encoding. See <link
543 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
544 * of the stream has been reached, the result will be -1.
547 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
548 * returned as negative numbers.
551 * If the stream contains Unicode data — for example, if it was created
552 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
553 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
555 * It is usually more efficient to read several characters at once with
556 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
557 * glk_get_char_stream() several times.
559 * Returns: A character value between 0 and 255, or -1 on end of stream.
562 glk_get_char_stream(strid_t str)
564 VALID_STREAM(str, return -1);
565 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
567 glsi32 ch = get_char_stream_common(str);
568 return (ch > 0xFF)? PLACEHOLDER : ch;
572 * glk_get_char_stream_uni:
573 * @str: An input stream.
575 * Reads one character from the stream @str. The result will be between 0 and
576 * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
578 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
581 glk_get_char_stream_uni(strid_t str)
583 VALID_STREAM(str, return -1);
584 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
586 return get_char_stream_common(str);
590 * glk_get_buffer_stream:
591 * @str: An input stream.
592 * @buf: A buffer with space for at least @len characters.
593 * @len: The number of characters to read.
595 * Reads @len characters from @str, unless the end of stream is reached first.
596 * No terminal null is placed in the buffer.
598 * Returns: The number of characters actually read.
601 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
603 VALID_STREAM(str, return 0);
604 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
605 g_return_val_if_fail(buf != NULL, 0);
609 case STREAM_TYPE_MEMORY:
614 while(copycount < len && str->ubuffer && str->mark < str->buflen)
616 glui32 ch = str->ubuffer[str->mark++];
617 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
622 if(str->buffer) /* if not, copycount stays 0 */
623 copycount = MIN(len, str->buflen - str->mark);
624 memmove(buf, str->buffer + str->mark, copycount);
625 str->mark += copycount;
628 str->read_count += copycount;
631 case STREAM_TYPE_FILE:
634 if(str->unicode) /* Binary file with 4-byte characters */
636 /* Read len characters of 4 bytes each */
637 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
638 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
639 /* If there was an incomplete character */
643 WARNING("Incomplete character in binary Unicode file");
647 for(foo = 0; foo < count; foo += 4)
649 glsi32 ch = readbuffer[foo] << 24
650 | readbuffer[foo + 1] << 16
651 | readbuffer[foo + 2] << 8
652 | readbuffer[foo + 3];
653 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
656 str->read_count += count / 4;
659 else /* Regular binary file */
661 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
662 str->read_count += count;
666 else /* Text mode is the same for Unicode and regular files */
668 /* Do it character-by-character */
670 for(foo = 0; foo < len; foo++)
672 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
676 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
681 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
687 * glk_get_buffer_stream_uni:
688 * @str: An input stream.
689 * @buf: A buffer with space for at least @len Unicode code points.
690 * @len: The number of characters to read.
692 * Reads @len Unicode characters from @str, unless the end of stream is reached
693 * first. No terminal null is placed in the buffer.
695 * Returns: The number of Unicode characters actually read.
698 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
700 VALID_STREAM(str, return 0);
701 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
702 g_return_val_if_fail(buf != NULL, 0);
706 case STREAM_TYPE_MEMORY:
711 if(str->ubuffer) /* if not, copycount stays 0 */
712 copycount = MIN(len, str->buflen - str->mark);
713 memmove(buf, str->ubuffer + str->mark, copycount * 4);
714 str->mark += copycount;
718 while(copycount < len && str->buffer && str->mark < str->buflen)
720 unsigned char ch = str->buffer[str->mark++];
721 buf[copycount++] = ch;
725 str->read_count += copycount;
728 case STREAM_TYPE_FILE:
731 if(str->unicode) /* Binary file with 4-byte characters */
733 /* Read len characters of 4 bytes each */
734 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
735 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
736 /* If there was an incomplete character */
740 WARNING("Incomplete character in binary Unicode file");
744 for(foo = 0; foo < count; foo += 4)
745 buf[foo / 4] = readbuffer[foo] << 24
746 | readbuffer[foo + 1] << 16
747 | readbuffer[foo + 2] << 8
748 | readbuffer[foo + 3];
750 str->read_count += count / 4;
753 else /* Regular binary file */
755 unsigned char *readbuffer = g_new0(unsigned char, len);
756 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
758 for(foo = 0; foo < count; foo++)
759 buf[foo] = readbuffer[foo];
761 str->read_count += count;
765 else /* Text mode is the same for Unicode and regular files */
767 /* Do it character-by-character */
769 for(foo = 0; foo < len; foo++)
771 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
780 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
786 * glk_get_line_stream:
787 * @str: An input stream.
788 * @buf: A buffer with space for at least @len characters.
789 * @len: The number of characters to read, plus one.
791 * Reads characters from @str, until either
793 * <alt>@len - 1</alt>
794 * <mathphrase>@len - 1</mathphrase>
796 * characters have been read or a newline has been read. It then puts a
797 * terminal null (<code>'\0'</code>) aracter on
798 * the end. It returns the number of characters actually read, including the
799 * newline (if there is one) but not including the terminal null.
801 * Returns: The number of characters actually read.
804 glk_get_line_stream(strid_t str, char *buf, glui32 len)
806 VALID_STREAM(str, return 0);
807 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
808 g_return_val_if_fail(buf != NULL, 0);
812 case STREAM_TYPE_MEMORY:
817 /* Do it character-by-character */
818 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
820 glui32 ch = str->ubuffer[str->mark++];
821 /* Check for Unicode newline; slightly different than
823 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
825 buf[copycount++] = '\n';
830 if(str->ubuffer[str->mark] == 0x0A)
831 str->mark++; /* skip past next newline */
832 buf[copycount++] = '\n';
835 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
837 buf[copycount] = '\0';
841 if(str->buffer) /* if not, copycount stays 0 */
842 copycount = MIN(len - 1, str->buflen - str->mark);
843 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
844 if(endptr) /* newline was found */
845 copycount = endptr - buf; /* Real copy count */
846 buf[copycount] = '\0';
847 str->mark += copycount;
850 str->read_count += copycount;
853 case STREAM_TYPE_FILE:
856 if(str->unicode) /* Binary file with 4-byte characters */
858 /* Do it character-by-character */
860 for(foo = 0; foo < len - 1; foo++)
862 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
869 if(is_unicode_newline(ch, str->file_pointer, FALSE))
875 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
880 else /* Regular binary file */
882 if( !fgets(buf, len, str->file_pointer) ) {
887 int nread = strlen(buf);
888 str->read_count += nread;
892 else /* Text mode is the same for Unicode and regular files */
894 /* Do it character-by-character */
896 for(foo = 0; foo < len - 1; foo++)
898 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
905 if(is_unicode_newline(ch, str->file_pointer, TRUE))
911 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
917 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
923 * glk_get_line_stream_uni:
924 * @str: An input stream.
925 * @buf: A buffer with space for at least @len Unicode code points.
926 * @len: The number of characters to read, plus one.
928 * Reads Unicode characters from @str, until either
930 * <alt>@len - 1</alt>
931 * <mathphrase>@len - 1</mathphrase>
933 * Unicode characters have been read or a newline has been read. It then puts a
934 * terminal null (a zero value) on the end.
936 * Returns: The number of characters actually read, including the newline (if
937 * there is one) but not including the terminal null.
940 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
942 VALID_STREAM(str, return 0);
943 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
944 g_return_val_if_fail(buf != NULL, 0);
948 case STREAM_TYPE_MEMORY:
953 /* Do it character-by-character */
954 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
956 glui32 ch = str->ubuffer[str->mark++];
957 /* Check for Unicode newline; slightly different than
959 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
961 buf[copycount++] = '\n';
966 if(str->ubuffer[str->mark] == 0x0A)
967 str->mark++; /* skip past next newline */
968 buf[copycount++] = '\n';
971 buf[copycount++] = ch;
973 buf[copycount] = '\0';
977 /* No recourse to memccpy(), so do it character-by-character */
978 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
980 gchar ch = str->buffer[str->mark++];
981 /* Check for newline */
982 if(ch == '\n') /* Also check for \r and \r\n? */
984 buf[copycount++] = '\n';
987 buf[copycount++] = (unsigned char)ch;
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))
1013 buf[foo] = ch; /* Preserve newline types??? */
1022 else /* Regular binary file */
1024 gchar *readbuffer = g_new0(gchar, len);
1025 if( !fgets(readbuffer, len, str->file_pointer) ) {
1030 glui32 count = strlen(readbuffer);
1032 for(foo = 0; foo < count + 1; foo++) /* Copy terminator */
1033 buf[foo] = (unsigned char)(readbuffer[foo]);
1034 str->read_count += count;
1038 else /* Text mode is the same for Unicode and regular files */
1040 /* Do it character-by-character */
1042 for(foo = 0; foo < len - 1; foo++)
1044 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1051 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1053 buf[foo] = ch; /* Preserve newline types??? */
1063 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1070 **************** SEEKING FUNCTIONS ********************************************
1075 * glk_stream_get_position:
1076 * @str: A file or memory stream.
1078 * Returns the position of the read/write mark in @str. For memory streams and
1079 * binary file streams, this is exactly the number of characters read or written
1080 * from the beginning of the stream (unless you have moved the mark with
1081 * glk_stream_set_position().) For text file streams, matters are more
1082 * ambiguous, since (for example) writing one byte to a text file may store more
1083 * than one character in the platform's native encoding. You can only be sure
1084 * that the position increases as you read or write to the file.
1086 * Additional complication: for Latin-1 memory and file streams, a character is
1087 * a byte. For Unicode memory and file streams (those created by
1088 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1089 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1093 * If this bothers you, don't use binary Unicode files. I don't think they're
1094 * good for much anyhow.
1097 * Returns: position of the read/write mark in @str.
1100 glk_stream_get_position(strid_t str)
1102 VALID_STREAM(str, return 0);
1106 case STREAM_TYPE_MEMORY:
1108 case STREAM_TYPE_FILE:
1109 return ftell(str->file_pointer);
1111 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1117 * glk_stream_set_position:
1118 * @str: A file or memory stream.
1119 * @pos: The position to set the mark to, relative to @seekmode.
1120 * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1122 * Sets the position of the read/write mark in @str. The position is controlled
1123 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1124 * <code>seekmode_</code> constants below.
1126 * It is illegal to specify a position before the beginning or after the end of
1129 * In binary files, the mark position is exact — it corresponds with the
1130 * number of characters you have read or written. In text files, this mapping
1131 * can vary, because of linefeed conventions or other character-set
1132 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1133 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1134 * the platform's native encoding — after character cookery. Therefore,
1135 * in a text stream, it is safest to use glk_stream_set_position() only to move
1136 * to the beginning or end of a file, or to a position determined by
1137 * glk_stream_get_position().
1139 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1140 * characters are 32-bit words, or four bytes each.
1143 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1145 VALID_STREAM(str, return);
1146 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1147 g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1151 case STREAM_TYPE_MEMORY:
1154 case seekmode_Start: str->mark = pos; break;
1155 case seekmode_Current: str->mark += pos; break;
1156 case seekmode_End: str->mark = str->buflen + pos; break;
1158 g_return_if_reached();
1162 case STREAM_TYPE_FILE:
1167 case seekmode_Start: whence = SEEK_SET; break;
1168 case seekmode_Current: whence = SEEK_CUR; break;
1169 case seekmode_End: whence = SEEK_END; break;
1171 g_return_if_reached();
1174 if(fseek(str->file_pointer, pos, whence) == -1)
1175 WARNING("Seek failed on file stream");
1179 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);