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_with_tags_by_name(buffer, &start, s + (length - chars_left), available_space, win->window_stream->style, NULL);
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_with_tags_by_name(buffer, &start, s + (length - chars_left), -1, win->window_stream->style, NULL);
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);
84 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
86 g_signal_emit_by_name(glk, "text-buffer-output", win->rock, s);
89 /* Internal function: write a Latin-1 buffer with length to a stream. */
91 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
95 case STREAM_TYPE_WINDOW:
96 /* Each window type has a different way of printing to it */
97 switch(str->window->type)
99 /* Printing to these windows' streams does nothing */
102 case wintype_Graphics:
103 str->write_count += len;
106 /* Text grid window */
107 case wintype_TextGrid:
109 gchar *utf8 = convert_latin1_to_utf8(buf, len);
112 /* FIXME: What to do if string contains \n? Split the input string at newlines and write each string separately? */
113 write_utf8_to_grid(str->window, utf8);
117 str->write_count += len;
120 /* Text buffer window */
121 case wintype_TextBuffer:
123 gchar *utf8 = convert_latin1_to_utf8(buf, len);
126 write_utf8_to_window(str->window, utf8);
130 str->write_count += len;
133 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
136 /* Now write the same buffer to the window's echo stream */
137 if(str->window->echo_stream != NULL)
138 write_buffer_to_stream(str->window->echo_stream, buf, len);
142 case STREAM_TYPE_MEMORY:
143 if(str->unicode && str->ubuffer)
146 while(str->mark < str->buflen && foo < len)
147 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
149 if(!str->unicode && str->buffer)
151 int copycount = MIN(len, str->buflen - str->mark);
152 memmove(str->buffer + str->mark, buf, copycount);
153 str->mark += copycount;
156 str->write_count += len;
159 case STREAM_TYPE_FILE:
164 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
165 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
168 else /* Regular file */
170 fwrite(buf, sizeof(gchar), len, str->file_pointer);
173 else /* Text mode is the same for Unicode and regular files */
175 gchar *utf8 = convert_latin1_to_utf8(buf, len);
178 g_fprintf(str->file_pointer, "%s", utf8);
183 str->write_count += len;
186 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
190 /* Internal function: write a Unicode buffer with length to a stream. */
192 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
196 case STREAM_TYPE_WINDOW:
197 /* Each window type has a different way of printing to it */
198 switch(str->window->type)
200 /* Printing to these windows' streams does nothing */
203 case wintype_Graphics:
204 str->write_count += len;
207 /* Text grid window */
208 case wintype_TextGrid:
210 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
213 /* FIXME: What to do if string contains \n? Split the input string at newlines and write each string separately? */
214 write_utf8_to_grid(str->window, utf8);
218 str->write_count += len;
221 /* Text buffer window */
222 case wintype_TextBuffer:
224 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
227 write_utf8_to_window(str->window, utf8);
231 str->write_count += len;
234 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
237 /* Now write the same buffer to the window's echo stream */
238 if(str->window->echo_stream != NULL)
239 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
243 case STREAM_TYPE_MEMORY:
244 if(str->unicode && str->ubuffer)
246 int copycount = MIN(len, str->buflen - str->mark);
247 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
248 str->mark += copycount;
250 if(!str->unicode && str->buffer)
252 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
253 int copycount = MIN(len, str->buflen - str->mark);
254 memmove(str->buffer + str->mark, latin1, copycount);
256 str->mark += copycount;
259 str->write_count += len;
262 case STREAM_TYPE_FILE:
267 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
268 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
271 else /* Regular file */
273 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
274 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
278 else /* Text mode is the same for Unicode and regular files */
280 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
283 g_fprintf(str->file_pointer, "%s", utf8);
288 str->write_count += len;
291 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
296 * glk_put_char_stream:
297 * @str: An output stream.
298 * @ch: A character in Latin-1 encoding.
300 * The same as glk_put_char(), except that you specify a stream @str to print
301 * to, instead of using the current stream. It is illegal for @str to be %NULL,
302 * or an input-only stream.
305 glk_put_char_stream(strid_t str, unsigned char ch)
307 VALID_STREAM(str, return);
308 g_return_if_fail(str->file_mode != filemode_Read);
310 write_buffer_to_stream(str, (gchar *)&ch, 1);
314 * glk_put_char_stream_uni:
315 * @str: An output stream.
316 * @ch: A Unicode code point.
318 * The same as glk_put_char_uni(), except that you specify a stream @str to
319 * print to, instead of using the current stream. It is illegal for @str to be
320 * %NULL, or an input-only stream.
323 glk_put_char_stream_uni(strid_t str, glui32 ch)
325 VALID_STREAM(str, return);
326 g_return_if_fail(str->file_mode != filemode_Read);
328 write_buffer_to_stream_uni(str, &ch, 1);
332 * glk_put_string_stream:
333 * @str: An output stream.
334 * @s: A null-terminated string in Latin-1 encoding.
336 * The same as glk_put_string(), except that you specify a stream @str to print
337 * to, instead of using the current stream. It is illegal for @str to be %NULL,
338 * or an input-only stream.
341 glk_put_string_stream(strid_t str, char *s)
343 VALID_STREAM(str, return);
344 g_return_if_fail(str->file_mode != filemode_Read);
346 write_buffer_to_stream(str, s, strlen(s));
350 * glk_put_string_stream_uni:
351 * @str: An output stream.
352 * @s: A null-terminated array of Unicode code points.
354 * The same as glk_put_string_uni(), except that you specify a stream @str to
355 * print to, instead of using the current stream. It is illegal for @str to be
356 * %NULL, or an input-only stream.
359 glk_put_string_stream_uni(strid_t str, glui32 *s)
361 VALID_STREAM(str, return);
362 g_return_if_fail(str->file_mode != filemode_Read);
364 /* An impromptu strlen() for glui32 arrays */
369 write_buffer_to_stream_uni(str, s, len);
373 * glk_put_buffer_stream:
374 * @str: An output stream.
375 * @buf: An array of characters in Latin-1 encoding.
376 * @len: Length of @buf.
378 * The same as glk_put_buffer(), except that you specify a stream @str to print
379 * to, instead of using the current stream. It is illegal for @str to be %NULL,
380 * or an input-only stream.
383 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
385 VALID_STREAM(str, return);
386 g_return_if_fail(str->file_mode != filemode_Read);
388 write_buffer_to_stream(str, buf, len);
392 * glk_put_buffer_stream_uni:
393 * @str: An output stream.
394 * @buf: An array of Unicode code points.
395 * @len: Length of @buf.
397 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
398 * print to, instead of using the current stream. It is illegal for @str to be
399 * %NULL, or an input-only stream.
402 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
404 VALID_STREAM(str, return);
405 g_return_if_fail(str->file_mode != filemode_Read);
407 write_buffer_to_stream_uni(str, buf, len);
412 **************** READING FUNCTIONS ********************************************
416 /* Internal function: Read one big-endian four-byte character from file fp and
417 return it as a Unicode code point, or -1 on EOF */
419 read_ucs4be_char_from_file(FILE *fp)
421 unsigned char readbuffer[4];
422 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
425 readbuffer[0] << 24 |
426 readbuffer[1] << 16 |
431 /* Internal function: Read one UTF-8 character, which may be more than one byte,
432 from file fp and return it as a Unicode code point, or -1 on EOF */
434 read_utf8_char_from_file(FILE *fp)
436 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
438 gunichar charresult = (gunichar)-2;
439 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
444 readbuffer[foo] = (gchar)ch;
445 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
446 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
449 /* Silently return unknown characters as 0xFFFD, Replacement Character */
450 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
455 /* Internal function: Tell whether this code point is a Unicode newline. The
456 file pointer and eight-bit flag are included in case the newline is a CR
457 (U+000D). If the next character is LF (U+000A) then it also belongs to the
460 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
462 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
465 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
466 read_ucs4be_char_from_file(fp);
468 if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
469 WARNING_S("Seek failed on stream", g_strerror(errno) );
475 /* Internal function: Read one character from a stream. Returns a value which
476 can be returned unchanged by glk_get_char_stream_uni(), but
477 glk_get_char_stream() must replace high values by the placeholder character. */
479 get_char_stream_common(strid_t str)
483 case STREAM_TYPE_MEMORY:
486 if(!str->ubuffer || str->mark >= str->buflen)
488 glui32 ch = str->ubuffer[str->mark++];
494 if(!str->buffer || str->mark >= str->buflen)
496 unsigned char ch = str->buffer[str->mark++];
502 case STREAM_TYPE_FILE:
507 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
513 else /* Regular file */
515 int ch = fgetc(str->file_pointer);
523 else /* Text mode is the same for Unicode and regular files */
525 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
533 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
539 * glk_get_char_stream:
540 * @str: An input stream.
542 * Reads one character from the stream @str. (There is no notion of a
543 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
544 * an output-only stream.
546 * The result will be between 0 and 255. As with all basic text functions, Glk
547 * assumes the Latin-1 encoding. See <link
548 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
549 * of the stream has been reached, the result will be -1.
552 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
553 * returned as negative numbers.
556 * If the stream contains Unicode data — for example, if it was created
557 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
558 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
560 * It is usually more efficient to read several characters at once with
561 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
562 * glk_get_char_stream() several times.
564 * Returns: A character value between 0 and 255, or -1 on end of stream.
567 glk_get_char_stream(strid_t str)
569 VALID_STREAM(str, return -1);
570 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
572 glsi32 ch = get_char_stream_common(str);
573 return (ch > 0xFF)? PLACEHOLDER : ch;
577 * glk_get_char_stream_uni:
578 * @str: An input stream.
580 * Reads one character from the stream @str. The result will be between 0 and
581 * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
583 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
586 glk_get_char_stream_uni(strid_t str)
588 VALID_STREAM(str, return -1);
589 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
591 return get_char_stream_common(str);
595 * glk_get_buffer_stream:
596 * @str: An input stream.
597 * @buf: A buffer with space for at least @len characters.
598 * @len: The number of characters to read.
600 * Reads @len characters from @str, unless the end of stream is reached first.
601 * No terminal null is placed in the buffer.
603 * Returns: The number of characters actually read.
606 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
608 VALID_STREAM(str, return 0);
609 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
610 g_return_val_if_fail(buf != NULL, 0);
614 case STREAM_TYPE_MEMORY:
619 while(copycount < len && str->ubuffer && str->mark < str->buflen)
621 glui32 ch = str->ubuffer[str->mark++];
622 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
627 if(str->buffer) /* if not, copycount stays 0 */
628 copycount = MIN(len, str->buflen - str->mark);
629 memmove(buf, str->buffer + str->mark, copycount);
630 str->mark += copycount;
633 str->read_count += copycount;
636 case STREAM_TYPE_FILE:
639 if(str->unicode) /* Binary file with 4-byte characters */
641 /* Read len characters of 4 bytes each */
642 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
643 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
644 /* If there was an incomplete character */
648 WARNING("Incomplete character in binary Unicode file");
652 for(foo = 0; foo < count; foo += 4)
654 glsi32 ch = readbuffer[foo] << 24
655 | readbuffer[foo + 1] << 16
656 | readbuffer[foo + 2] << 8
657 | readbuffer[foo + 3];
658 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
661 str->read_count += count / 4;
664 else /* Regular binary file */
666 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
667 str->read_count += count;
671 else /* Text mode is the same for Unicode and regular files */
673 /* Do it character-by-character */
675 for(foo = 0; foo < len; foo++)
677 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
681 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
686 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
692 * glk_get_buffer_stream_uni:
693 * @str: An input stream.
694 * @buf: A buffer with space for at least @len Unicode code points.
695 * @len: The number of characters to read.
697 * Reads @len Unicode characters from @str, unless the end of stream is reached
698 * first. No terminal null is placed in the buffer.
700 * Returns: The number of Unicode characters actually read.
703 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
705 VALID_STREAM(str, return 0);
706 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
707 g_return_val_if_fail(buf != NULL, 0);
711 case STREAM_TYPE_MEMORY:
716 if(str->ubuffer) /* if not, copycount stays 0 */
717 copycount = MIN(len, str->buflen - str->mark);
718 memmove(buf, str->ubuffer + str->mark, copycount * 4);
719 str->mark += copycount;
723 while(copycount < len && str->buffer && str->mark < str->buflen)
725 unsigned char ch = str->buffer[str->mark++];
726 buf[copycount++] = ch;
730 str->read_count += copycount;
733 case STREAM_TYPE_FILE:
736 if(str->unicode) /* Binary file with 4-byte characters */
738 /* Read len characters of 4 bytes each */
739 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
740 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
741 /* If there was an incomplete character */
745 WARNING("Incomplete character in binary Unicode file");
749 for(foo = 0; foo < count; foo += 4)
750 buf[foo / 4] = readbuffer[foo] << 24
751 | readbuffer[foo + 1] << 16
752 | readbuffer[foo + 2] << 8
753 | readbuffer[foo + 3];
755 str->read_count += count / 4;
758 else /* Regular binary file */
760 unsigned char *readbuffer = g_new0(unsigned char, len);
761 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
763 for(foo = 0; foo < count; foo++)
764 buf[foo] = readbuffer[foo];
766 str->read_count += count;
770 else /* Text mode is the same for Unicode and regular files */
772 /* Do it character-by-character */
774 for(foo = 0; foo < len; foo++)
776 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
785 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
791 * glk_get_line_stream:
792 * @str: An input stream.
793 * @buf: A buffer with space for at least @len characters.
794 * @len: The number of characters to read, plus one.
796 * Reads characters from @str, until either
798 * <alt>@len - 1</alt>
799 * <mathphrase>@len - 1</mathphrase>
801 * characters have been read or a newline has been read. It then puts a
802 * terminal null (<code>'\0'</code>) aracter on
803 * the end. It returns the number of characters actually read, including the
804 * newline (if there is one) but not including the terminal null.
806 * Returns: The number of characters actually read.
809 glk_get_line_stream(strid_t str, char *buf, glui32 len)
811 VALID_STREAM(str, return 0);
812 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
813 g_return_val_if_fail(buf != NULL, 0);
817 case STREAM_TYPE_MEMORY:
822 /* Do it character-by-character */
823 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
825 glui32 ch = str->ubuffer[str->mark++];
826 /* Check for Unicode newline; slightly different than
828 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
830 buf[copycount++] = '\n';
835 if(str->ubuffer[str->mark] == 0x0A)
836 str->mark++; /* skip past next newline */
837 buf[copycount++] = '\n';
840 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
842 buf[copycount] = '\0';
846 if(str->buffer) /* if not, copycount stays 0 */
847 copycount = MIN(len - 1, str->buflen - str->mark);
848 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
849 if(endptr) /* newline was found */
850 copycount = endptr - buf; /* Real copy count */
851 buf[copycount] = '\0';
852 str->mark += copycount;
855 str->read_count += copycount;
858 case STREAM_TYPE_FILE:
861 if(str->unicode) /* Binary file with 4-byte characters */
863 /* Do it character-by-character */
865 for(foo = 0; foo < len - 1; foo++)
867 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
874 if(is_unicode_newline(ch, str->file_pointer, FALSE))
880 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
885 else /* Regular binary file */
887 fgets(buf, len, str->file_pointer);
888 str->read_count += strlen(buf);
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 fgets(readbuffer, len, str->file_pointer);
1026 glui32 count = strlen(readbuffer) + 1; /* Copy terminator */
1028 for(foo = 0; foo < count; foo++)
1029 buf[foo] = (unsigned char)(readbuffer[foo]);
1030 str->read_count += count;
1034 else /* Text mode is the same for Unicode and regular files */
1036 /* Do it character-by-character */
1038 for(foo = 0; foo < len - 1; foo++)
1040 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1047 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1049 buf[foo] = ch; /* Preserve newline types??? */
1059 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1066 **************** SEEKING FUNCTIONS ********************************************
1071 * glk_stream_get_position:
1072 * @str: A file or memory stream.
1074 * Returns the position of the read/write mark in @str. For memory streams and
1075 * binary file streams, this is exactly the number of characters read or written
1076 * from the beginning of the stream (unless you have moved the mark with
1077 * glk_stream_set_position().) For text file streams, matters are more
1078 * ambiguous, since (for example) writing one byte to a text file may store more
1079 * than one character in the platform's native encoding. You can only be sure
1080 * that the position increases as you read or write to the file.
1082 * Additional complication: for Latin-1 memory and file streams, a character is
1083 * a byte. For Unicode memory and file streams (those created by
1084 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1085 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1089 * If this bothers you, don't use binary Unicode files. I don't think they're
1090 * good for much anyhow.
1093 * Returns: position of the read/write mark in @str.
1096 glk_stream_get_position(strid_t str)
1098 VALID_STREAM(str, return 0);
1102 case STREAM_TYPE_MEMORY:
1104 case STREAM_TYPE_FILE:
1105 return ftell(str->file_pointer);
1107 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1113 * glk_stream_set_position:
1114 * @str: A file or memory stream.
1115 * @pos: The position to set the mark to, relative to @seekmode.
1116 * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1118 * Sets the position of the read/write mark in @str. The position is controlled
1119 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1120 * <code>seekmode_</code> constants below.
1122 * It is illegal to specify a position before the beginning or after the end of
1125 * In binary files, the mark position is exact — it corresponds with the
1126 * number of characters you have read or written. In text files, this mapping
1127 * can vary, because of linefeed conventions or other character-set
1128 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1129 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1130 * the platform's native encoding — after character cookery. Therefore,
1131 * in a text stream, it is safest to use glk_stream_set_position() only to move
1132 * to the beginning or end of a file, or to a position determined by
1133 * glk_stream_get_position().
1135 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1136 * characters are 32-bit words, or four bytes each.
1139 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1141 VALID_STREAM(str, return);
1142 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1143 g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1147 case STREAM_TYPE_MEMORY:
1150 case seekmode_Start: str->mark = pos; break;
1151 case seekmode_Current: str->mark += pos; break;
1152 case seekmode_End: str->mark = str->buflen + pos; break;
1154 g_return_if_reached();
1158 case STREAM_TYPE_FILE:
1163 case seekmode_Start: whence = SEEK_SET; break;
1164 case seekmode_Current: whence = SEEK_CUR; break;
1165 case seekmode_End: whence = SEEK_END; break;
1167 g_return_if_reached();
1170 if(fseek(str->file_pointer, pos, whence) == -1)
1171 WARNING("Seek failed on file stream");
1175 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);