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 /* Number of characters to insert */
21 glong length = g_utf8_strlen(s, -1);
22 glong chars_left = length;
26 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
27 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
29 /* Get cursor position */
31 gtk_text_buffer_get_iter_at_mark(buffer, &start, cursor);
32 /* Spaces available on this line */
33 gint available_space = win->width - gtk_text_iter_get_line_offset(&start);
35 while(chars_left > available_space && !gtk_text_iter_is_end(&start))
37 GtkTextIter end = start;
38 gtk_text_iter_forward_to_line_end(&end);
39 gtk_text_buffer_delete(buffer, &start, &end);
40 gtk_text_buffer_insert(buffer, &start, s + (length - chars_left), available_space);
41 chars_left -= available_space;
42 gtk_text_iter_forward_line(&start);
43 available_space = win->width;
45 if(!gtk_text_iter_is_end(&start))
47 GtkTextIter end = start;
48 gtk_text_iter_forward_chars(&end, chars_left);
49 gtk_text_buffer_delete(buffer, &start, &end);
50 gtk_text_buffer_insert(buffer, &start, s + (length - chars_left), -1);
53 gtk_text_buffer_move_mark(buffer, cursor, &start);
58 /* Internal function: write a UTF-8 string to a text buffer window's text buffer. */
60 write_utf8_to_window(winid_t win, gchar *s)
64 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
67 gtk_text_buffer_get_end_iter(buffer, &iter);
68 gtk_text_buffer_insert(buffer, &iter, s, -1);
73 /* Internal function: write a Latin-1 buffer with length to a stream. */
75 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
79 case STREAM_TYPE_WINDOW:
80 /* Each window type has a different way of printing to it */
81 switch(str->window->type)
83 /* Printing to these windows' streams does nothing */
86 case wintype_Graphics:
87 str->write_count += len;
90 /* Text grid window */
91 case wintype_TextGrid:
93 gchar *utf8 = convert_latin1_to_utf8(buf, len);
96 /* FIXME: What to do if string contains \n? Split the input string at newlines and write each string separately? */
97 write_utf8_to_grid(str->window, utf8);
101 str->write_count += len;
104 /* Text buffer window */
105 case wintype_TextBuffer:
107 gchar *utf8 = convert_latin1_to_utf8(buf, len);
110 write_utf8_to_window(str->window, utf8);
114 str->write_count += len;
117 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
120 /* Now write the same buffer to the window's echo stream */
121 if(str->window->echo_stream != NULL)
122 write_buffer_to_stream(str->window->echo_stream, buf, len);
126 case STREAM_TYPE_MEMORY:
127 if(str->unicode && str->ubuffer)
130 while(str->mark < str->buflen && foo < len)
131 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
133 if(!str->unicode && str->buffer)
135 int copycount = MIN(len, str->buflen - str->mark);
136 memmove(str->buffer + str->mark, buf, copycount);
137 str->mark += copycount;
140 str->write_count += len;
143 case STREAM_TYPE_FILE:
148 gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
149 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
152 else /* Regular file */
154 fwrite(buf, sizeof(gchar), len, str->file_pointer);
157 else /* Text mode is the same for Unicode and regular files */
159 gchar *utf8 = convert_latin1_to_utf8(buf, len);
162 g_fprintf(str->file_pointer, "%s", utf8);
167 str->write_count += len;
170 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
174 /* Internal function: write a Unicode buffer with length to a stream. */
176 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
180 case STREAM_TYPE_WINDOW:
181 /* Each window type has a different way of printing to it */
182 switch(str->window->type)
184 /* Printing to these windows' streams does nothing */
187 case wintype_Graphics:
188 str->write_count += len;
191 /* Text grid window */
192 case wintype_TextGrid:
194 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
197 /* FIXME: What to do if string contains \n? Split the input string at newlines and write each string separately? */
198 write_utf8_to_grid(str->window, utf8);
202 str->write_count += len;
205 /* Text buffer window */
206 case wintype_TextBuffer:
208 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
211 write_utf8_to_window(str->window, utf8);
215 str->write_count += len;
218 ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
221 /* Now write the same buffer to the window's echo stream */
222 if(str->window->echo_stream != NULL)
223 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
227 case STREAM_TYPE_MEMORY:
228 if(str->unicode && str->ubuffer)
230 int copycount = MIN(len, str->buflen - str->mark);
231 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
232 str->mark += copycount;
234 if(!str->unicode && str->buffer)
236 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
237 int copycount = MIN(len, str->buflen - str->mark);
238 memmove(str->buffer + str->mark, latin1, copycount);
240 str->mark += copycount;
243 str->write_count += len;
246 case STREAM_TYPE_FILE:
251 gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
252 fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
255 else /* Regular file */
257 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
258 fwrite(latin1, sizeof(gchar), len, str->file_pointer);
262 else /* Text mode is the same for Unicode and regular files */
264 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
267 g_fprintf(str->file_pointer, "%s", utf8);
272 str->write_count += len;
275 ILLEGAL_PARAM("Unknown stream type: %u", str->type);
280 * glk_put_char_stream:
281 * @str: An output stream.
282 * @ch: A character in Latin-1 encoding.
284 * The same as glk_put_char(), except that you specify a stream @str to print
285 * to, instead of using the current stream. It is illegal for @str to be %NULL,
286 * or an input-only stream.
289 glk_put_char_stream(strid_t str, unsigned char ch)
291 VALID_STREAM(str, return);
292 g_return_if_fail(str->file_mode != filemode_Read);
294 write_buffer_to_stream(str, (gchar *)&ch, 1);
298 * glk_put_char_stream_uni:
299 * @str: An output stream.
300 * @ch: A Unicode code point.
302 * The same as glk_put_char_uni(), except that you specify a stream @str to
303 * print to, instead of using the current stream. It is illegal for @str to be
304 * %NULL, or an input-only stream.
307 glk_put_char_stream_uni(strid_t str, glui32 ch)
309 VALID_STREAM(str, return);
310 g_return_if_fail(str->file_mode != filemode_Read);
312 write_buffer_to_stream_uni(str, &ch, 1);
316 * glk_put_string_stream:
317 * @str: An output stream.
318 * @s: A null-terminated string in Latin-1 encoding.
320 * The same as glk_put_string(), except that you specify a stream @str to print
321 * to, instead of using the current stream. It is illegal for @str to be %NULL,
322 * or an input-only stream.
325 glk_put_string_stream(strid_t str, char *s)
327 VALID_STREAM(str, return);
328 g_return_if_fail(str->file_mode != filemode_Read);
330 write_buffer_to_stream(str, s, strlen(s));
334 * glk_put_string_stream_uni:
335 * @str: An output stream.
336 * @s: A null-terminated array of Unicode code points.
338 * The same as glk_put_string_uni(), except that you specify a stream @str to
339 * print to, instead of using the current stream. It is illegal for @str to be
340 * %NULL, or an input-only stream.
343 glk_put_string_stream_uni(strid_t str, glui32 *s)
345 VALID_STREAM(str, return);
346 g_return_if_fail(str->file_mode != filemode_Read);
348 /* An impromptu strlen() for glui32 arrays */
353 write_buffer_to_stream_uni(str, s, len);
357 * glk_put_buffer_stream:
358 * @str: An output stream.
359 * @buf: An array of characters in Latin-1 encoding.
360 * @len: Length of @buf.
362 * The same as glk_put_buffer(), except that you specify a stream @str to print
363 * to, instead of using the current stream. It is illegal for @str to be %NULL,
364 * or an input-only stream.
367 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
369 VALID_STREAM(str, return);
370 g_return_if_fail(str->file_mode != filemode_Read);
372 write_buffer_to_stream(str, buf, len);
376 * glk_put_buffer_stream_uni:
377 * @str: An output stream.
378 * @buf: An array of Unicode code points.
379 * @len: Length of @buf.
381 * The same as glk_put_buffer_uni(), except that you specify a stream @str to
382 * print to, instead of using the current stream. It is illegal for @str to be
383 * %NULL, or an input-only stream.
386 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
388 VALID_STREAM(str, return);
389 g_return_if_fail(str->file_mode != filemode_Read);
391 write_buffer_to_stream_uni(str, buf, len);
396 **************** READING FUNCTIONS ********************************************
400 /* Internal function: Read one big-endian four-byte character from file fp and
401 return it as a Unicode code point, or -1 on EOF */
403 read_ucs4be_char_from_file(FILE *fp)
405 unsigned char readbuffer[4];
406 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
409 readbuffer[0] << 24 |
410 readbuffer[1] << 16 |
415 /* Internal function: Read one UTF-8 character, which may be more than one byte,
416 from file fp and return it as a Unicode code point, or -1 on EOF */
418 read_utf8_char_from_file(FILE *fp)
420 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
422 gunichar charresult = (gunichar)-2;
423 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
428 readbuffer[foo] = (gchar)ch;
429 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
430 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
433 /* Silently return unknown characters as 0xFFFD, Replacement Character */
434 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
439 /* Internal function: Tell whether this code point is a Unicode newline. The
440 file pointer and eight-bit flag are included in case the newline is a CR
441 (U+000D). If the next character is LF (U+000A) then it also belongs to the
444 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
446 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
449 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
450 read_ucs4be_char_from_file(fp);
452 if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
453 WARNING_S("Seek failed on stream", g_strerror(errno) );
459 /* Internal function: Read one character from a stream. Returns a value which
460 can be returned unchanged by glk_get_char_stream_uni(), but
461 glk_get_char_stream() must replace high values by the placeholder character. */
463 get_char_stream_common(strid_t str)
467 case STREAM_TYPE_MEMORY:
470 if(!str->ubuffer || str->mark >= str->buflen)
472 glui32 ch = str->ubuffer[str->mark++];
478 if(!str->buffer || str->mark >= str->buflen)
480 unsigned char ch = str->buffer[str->mark++];
486 case STREAM_TYPE_FILE:
491 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
497 else /* Regular file */
499 int ch = fgetc(str->file_pointer);
507 else /* Text mode is the same for Unicode and regular files */
509 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
517 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
523 * glk_get_char_stream:
524 * @str: An input stream.
526 * Reads one character from the stream @str. (There is no notion of a
527 * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
528 * an output-only stream.
530 * The result will be between 0 and 255. As with all basic text functions, Glk
531 * assumes the Latin-1 encoding. See <link
532 * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
533 * of the stream has been reached, the result will be -1.
536 * Note that high-bit characters (128..255) are <emphasis>not</emphasis>
537 * returned as negative numbers.
540 * If the stream contains Unicode data — for example, if it was created
541 * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() — then
542 * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
544 * It is usually more efficient to read several characters at once with
545 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
546 * glk_get_char_stream() several times.
548 * Returns: A character value between 0 and 255, or -1 on end of stream.
551 glk_get_char_stream(strid_t str)
553 VALID_STREAM(str, return -1);
554 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
556 glsi32 ch = get_char_stream_common(str);
557 return (ch > 0xFF)? PLACEHOLDER : ch;
561 * glk_get_char_stream_uni:
562 * @str: An input stream.
564 * Reads one character from the stream @str. The result will be between 0 and
565 * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
567 * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
570 glk_get_char_stream_uni(strid_t str)
572 VALID_STREAM(str, return -1);
573 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
575 return get_char_stream_common(str);
579 * glk_get_buffer_stream:
580 * @str: An input stream.
581 * @buf: A buffer with space for at least @len characters.
582 * @len: The number of characters to read.
584 * Reads @len characters from @str, unless the end of stream is reached first.
585 * No terminal null is placed in the buffer.
587 * Returns: The number of characters actually read.
590 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
592 VALID_STREAM(str, return 0);
593 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
594 g_return_val_if_fail(buf != NULL, 0);
598 case STREAM_TYPE_MEMORY:
603 while(copycount < len && str->ubuffer && str->mark < str->buflen)
605 glui32 ch = str->ubuffer[str->mark++];
606 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
611 if(str->buffer) /* if not, copycount stays 0 */
612 copycount = MIN(len, str->buflen - str->mark);
613 memmove(buf, str->buffer + str->mark, copycount);
614 str->mark += copycount;
617 str->read_count += copycount;
620 case STREAM_TYPE_FILE:
623 if(str->unicode) /* Binary file with 4-byte characters */
625 /* Read len characters of 4 bytes each */
626 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
627 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
628 /* If there was an incomplete character */
632 WARNING("Incomplete character in binary Unicode file");
636 for(foo = 0; foo < count; foo += 4)
638 glsi32 ch = readbuffer[foo] << 24
639 | readbuffer[foo + 1] << 16
640 | readbuffer[foo + 2] << 8
641 | readbuffer[foo + 3];
642 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
645 str->read_count += count / 4;
648 else /* Regular binary file */
650 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
651 str->read_count += count;
655 else /* Text mode is the same for Unicode and regular files */
657 /* Do it character-by-character */
659 for(foo = 0; foo < len; foo++)
661 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
665 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
670 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
676 * glk_get_buffer_stream_uni:
677 * @str: An input stream.
678 * @buf: A buffer with space for at least @len Unicode code points.
679 * @len: The number of characters to read.
681 * Reads @len Unicode characters from @str, unless the end of stream is reached
682 * first. No terminal null is placed in the buffer.
684 * Returns: The number of Unicode characters actually read.
687 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
689 VALID_STREAM(str, return 0);
690 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
691 g_return_val_if_fail(buf != NULL, 0);
695 case STREAM_TYPE_MEMORY:
700 if(str->ubuffer) /* if not, copycount stays 0 */
701 copycount = MIN(len, str->buflen - str->mark);
702 memmove(buf, str->ubuffer + str->mark, copycount * 4);
703 str->mark += copycount;
707 while(copycount < len && str->buffer && str->mark < str->buflen)
709 unsigned char ch = str->buffer[str->mark++];
710 buf[copycount++] = ch;
714 str->read_count += copycount;
717 case STREAM_TYPE_FILE:
720 if(str->unicode) /* Binary file with 4-byte characters */
722 /* Read len characters of 4 bytes each */
723 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
724 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
725 /* If there was an incomplete character */
729 WARNING("Incomplete character in binary Unicode file");
733 for(foo = 0; foo < count; foo += 4)
734 buf[foo / 4] = readbuffer[foo] << 24
735 | readbuffer[foo + 1] << 16
736 | readbuffer[foo + 2] << 8
737 | readbuffer[foo + 3];
739 str->read_count += count / 4;
742 else /* Regular binary file */
744 unsigned char *readbuffer = g_new0(unsigned char, len);
745 size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
747 for(foo = 0; foo < count; foo++)
748 buf[foo] = readbuffer[foo];
750 str->read_count += count;
754 else /* Text mode is the same for Unicode and regular files */
756 /* Do it character-by-character */
758 for(foo = 0; foo < len; foo++)
760 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
769 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
775 * glk_get_line_stream:
776 * @str: An input stream.
777 * @buf: A buffer with space for at least @len characters.
778 * @len: The number of characters to read, plus one.
780 * Reads characters from @str, until either
782 * <alt>@len - 1</alt>
783 * <mathphrase>@len - 1</mathphrase>
785 * characters have been read or a newline has been read. It then puts a
786 * terminal null (<code>'\0'</code>) aracter on
787 * the end. It returns the number of characters actually read, including the
788 * newline (if there is one) but not including the terminal null.
790 * Returns: The number of characters actually read.
793 glk_get_line_stream(strid_t str, char *buf, glui32 len)
795 VALID_STREAM(str, return 0);
796 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
797 g_return_val_if_fail(buf != NULL, 0);
801 case STREAM_TYPE_MEMORY:
806 /* Do it character-by-character */
807 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
809 glui32 ch = str->ubuffer[str->mark++];
810 /* Check for Unicode newline; slightly different than
812 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
814 buf[copycount++] = '\n';
819 if(str->ubuffer[str->mark] == 0x0A)
820 str->mark++; /* skip past next newline */
821 buf[copycount++] = '\n';
824 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
826 buf[copycount] = '\0';
830 if(str->buffer) /* if not, copycount stays 0 */
831 copycount = MIN(len - 1, str->buflen - str->mark);
832 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
833 if(endptr) /* newline was found */
834 copycount = endptr - buf; /* Real copy count */
835 buf[copycount] = '\0';
836 str->mark += copycount;
839 str->read_count += copycount;
842 case STREAM_TYPE_FILE:
845 if(str->unicode) /* Binary file with 4-byte characters */
847 /* Do it character-by-character */
849 for(foo = 0; foo < len - 1; foo++)
851 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
858 if(is_unicode_newline(ch, str->file_pointer, FALSE))
864 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
869 else /* Regular binary file */
871 fgets(buf, len, str->file_pointer);
872 str->read_count += strlen(buf);
876 else /* Text mode is the same for Unicode and regular files */
878 /* Do it character-by-character */
880 for(foo = 0; foo < len - 1; foo++)
882 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
889 if(is_unicode_newline(ch, str->file_pointer, TRUE))
895 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
901 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
907 * glk_get_line_stream_uni:
908 * @str: An input stream.
909 * @buf: A buffer with space for at least @len Unicode code points.
910 * @len: The number of characters to read, plus one.
912 * Reads Unicode characters from @str, until either
914 * <alt>@len - 1</alt>
915 * <mathphrase>@len - 1</mathphrase>
917 * Unicode characters have been read or a newline has been read. It then puts a
918 * terminal null (a zero value) on the end.
920 * Returns: The number of characters actually read, including the newline (if
921 * there is one) but not including the terminal null.
924 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
926 VALID_STREAM(str, return 0);
927 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
928 g_return_val_if_fail(buf != NULL, 0);
932 case STREAM_TYPE_MEMORY:
937 /* Do it character-by-character */
938 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
940 glui32 ch = str->ubuffer[str->mark++];
941 /* Check for Unicode newline; slightly different than
943 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
945 buf[copycount++] = '\n';
950 if(str->ubuffer[str->mark] == 0x0A)
951 str->mark++; /* skip past next newline */
952 buf[copycount++] = '\n';
955 buf[copycount++] = ch;
957 buf[copycount] = '\0';
961 /* No recourse to memccpy(), so do it character-by-character */
962 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
964 gchar ch = str->buffer[str->mark++];
965 /* Check for newline */
966 if(ch == '\n') /* Also check for \r and \r\n? */
968 buf[copycount++] = '\n';
971 buf[copycount++] = (unsigned char)ch;
976 str->read_count += copycount;
979 case STREAM_TYPE_FILE:
982 if(str->unicode) /* Binary file with 4-byte characters */
984 /* Do it character-by-character */
986 for(foo = 0; foo < len - 1; foo++)
988 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
995 if(is_unicode_newline(ch, str->file_pointer, FALSE))
997 buf[foo] = ch; /* Preserve newline types??? */
1006 else /* Regular binary file */
1008 gchar *readbuffer = g_new0(gchar, len);
1009 fgets(readbuffer, len, str->file_pointer);
1010 glui32 count = strlen(readbuffer) + 1; /* Copy terminator */
1012 for(foo = 0; foo < count; foo++)
1013 buf[foo] = (unsigned char)(readbuffer[foo]);
1014 str->read_count += count;
1018 else /* Text mode is the same for Unicode and regular files */
1020 /* Do it character-by-character */
1022 for(foo = 0; foo < len - 1; foo++)
1024 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1031 if(is_unicode_newline(ch, str->file_pointer, TRUE))
1033 buf[foo] = ch; /* Preserve newline types??? */
1043 ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1050 **************** SEEKING FUNCTIONS ********************************************
1055 * glk_stream_get_position:
1056 * @str: A file or memory stream.
1058 * Returns the position of the read/write mark in @str. For memory streams and
1059 * binary file streams, this is exactly the number of characters read or written
1060 * from the beginning of the stream (unless you have moved the mark with
1061 * glk_stream_set_position().) For text file streams, matters are more
1062 * ambiguous, since (for example) writing one byte to a text file may store more
1063 * than one character in the platform's native encoding. You can only be sure
1064 * that the position increases as you read or write to the file.
1066 * Additional complication: for Latin-1 memory and file streams, a character is
1067 * a byte. For Unicode memory and file streams (those created by
1068 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1069 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1073 * If this bothers you, don't use binary Unicode files. I don't think they're
1074 * good for much anyhow.
1077 * Returns: position of the read/write mark in @str.
1080 glk_stream_get_position(strid_t str)
1082 VALID_STREAM(str, return 0);
1086 case STREAM_TYPE_MEMORY:
1088 case STREAM_TYPE_FILE:
1089 return ftell(str->file_pointer);
1091 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1097 * glk_stream_set_position:
1098 * @str: A file or memory stream.
1099 * @pos: The position to set the mark to, relative to @seekmode.
1100 * @seekmode: One of #seekmode_Start, #seekmode_Current, or #seekmode_End.
1102 * Sets the position of the read/write mark in @str. The position is controlled
1103 * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1104 * <code>seekmode_</code> constants below.
1106 * It is illegal to specify a position before the beginning or after the end of
1109 * In binary files, the mark position is exact — it corresponds with the
1110 * number of characters you have read or written. In text files, this mapping
1111 * can vary, because of linefeed conventions or other character-set
1112 * approximations. See <link linkend="chimara-Streams">Streams</link>.
1113 * glk_stream_set_position() and glk_stream_get_position() measure positions in
1114 * the platform's native encoding — after character cookery. Therefore,
1115 * in a text stream, it is safest to use glk_stream_set_position() only to move
1116 * to the beginning or end of a file, or to a position determined by
1117 * glk_stream_get_position().
1119 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1120 * characters are 32-bit words, or four bytes each.
1123 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1125 VALID_STREAM(str, return);
1126 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1127 g_return_if_fail(!(seekmode == seekmode_End || pos > 0));
1131 case STREAM_TYPE_MEMORY:
1134 case seekmode_Start: str->mark = pos; break;
1135 case seekmode_Current: str->mark += pos; break;
1136 case seekmode_End: str->mark = str->buflen + pos; break;
1138 g_return_if_reached();
1142 case STREAM_TYPE_FILE:
1147 case seekmode_Start: whence = SEEK_SET; break;
1148 case seekmode_Current: whence = SEEK_CUR; break;
1149 case seekmode_End: whence = SEEK_END; break;
1151 g_return_if_reached();
1154 if(fseek(str->file_pointer, pos, whence) == -1)
1155 WARNING("Seek failed on file stream");
1159 ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);