5 #include <glib/gstdio.h>
7 #define min(x,y) ( (x > y)? y : x )
11 **************** WRITING FUNCTIONS ********************************************
15 /* Internal function: change illegal (control) characters in a string to a
16 placeholder character. Must free returned string afterwards. */
18 remove_latin1_control_characters(unsigned char *s, gssize len)
20 gchar *retval = g_new0(gchar, len);
22 for(i = 0; i < len; i++)
23 if( (s[i] < 32 && s[i] != 10) || (s[i] >= 127 && s[i] <= 159) )
25 /* Our placeholder character is '?'; other options are possible,
26 like printing "0x7F" or something */
32 /* Internal function: convert a Latin-1 string to a UTF-8 string, replacing
33 Latin-1 control characters by a placeholder first. The UTF-8 string must be
34 freed afterwards. Returns NULL on error. */
36 convert_latin1_to_utf8(gchar *s, gssize len)
40 gchar *canonical = remove_latin1_control_characters( (unsigned char *)s,
42 utf8 = g_convert(canonical, len, "UTF-8", "ISO-8859-1", NULL, NULL, &error);
47 error_dialog(NULL, error, "Error during latin1->utf8 conversion: ");
54 /* Internal function: write a UTF-8 string to a window's text buffer. */
56 write_utf8_to_window(winid_t win, gchar *s)
58 GtkTextBuffer *buffer =
59 gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
62 gtk_text_buffer_get_end_iter(buffer, &iter);
63 gtk_text_buffer_insert(buffer, &iter, s, -1);
66 /* Internal function: write a UTF-8 buffer with length to a stream. */
68 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
70 switch(str->stream_type)
72 case STREAM_TYPE_WINDOW:
73 /* Each window type has a different way of printing to it */
74 switch(str->window->window_type)
76 /* Printing to these windows' streams does nothing */
79 case wintype_Graphics:
80 str->write_count += len;
82 /* Text buffer window */
83 case wintype_TextBuffer:
85 gchar *utf8 = convert_latin1_to_utf8(buf, len);
86 write_utf8_to_window(str->window, utf8);
89 str->write_count += len;
92 g_warning("%s: Writing to this kind of window unsupported.",
96 /* Now write the same buffer to the window's echo stream */
97 if(str->window->echo_stream != NULL)
98 write_buffer_to_stream(str->window->echo_stream, buf, len);
102 case STREAM_TYPE_MEMORY:
103 if(str->unicode && str->ubuffer)
106 while(str->mark < str->buflen && foo < len)
107 str->ubuffer[str->mark++] = (glui32)buf[foo++];
109 if(!str->unicode && str->buffer)
111 memmove(str->buffer + str->mark, buf,
112 min(len, str->buflen - str->mark));
115 str->write_count += len;
118 case STREAM_TYPE_FILE:
123 /* Convert to four-byte big-endian */
124 gchar *writebuffer = g_new0(gchar, len * 4);
126 for(i = 0; i < len; i++)
127 writebuffer[i * 4 + 3] = buf[i];
128 fwrite(writebuffer, sizeof(gchar), len * 4,
131 else /* Regular file */
133 fwrite(buf, sizeof(gchar), len, str->file_pointer);
136 else /* Text mode is the same for Unicode and regular files */
138 gchar *utf8 = convert_latin1_to_utf8(buf, len);
139 g_fprintf(str->file_pointer, "%s", utf8);
143 str->write_count += len;
146 g_warning("%s: Writing to this kind of stream unsupported.",
152 * glk_put_char_stream:
153 * @str: An output stream.
154 * @ch: A character in Latin-1 encoding.
156 * Prints one character @ch to the stream @str. It is illegal for @str to be
157 * %NULL, or an input-only stream.
160 glk_put_char_stream(strid_t str, unsigned char ch)
162 g_return_if_fail(str != NULL);
163 g_return_if_fail(str->file_mode != filemode_Read);
165 write_buffer_to_stream(str, (gchar *)&ch, 1);
169 * glk_put_string_stream:
170 * @str: An output stream.
171 * @s: A null-terminated string in Latin-1 encoding.
173 * Prints @s to the stream @str. It is illegal for @str to be %NULL, or an
177 glk_put_string_stream(strid_t str, char *s)
179 g_return_if_fail(str != NULL);
180 g_return_if_fail(str->file_mode != filemode_Read);
182 write_buffer_to_stream(str, (gchar *)s, strlen(s));
186 * glk_put_buffer_stream:
187 * @str: An output stream.
188 * @buf: An array of characters in Latin-1 encoding.
189 * @len: Length of @buf.
191 * Prints @buf to the stream @str. It is illegal for @str to be %NULL, or an
195 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
197 g_return_if_fail(str != NULL);
198 g_return_if_fail(str->file_mode != filemode_Read);
200 write_buffer_to_stream(str, (gchar *)buf, len);
205 **************** READING FUNCTIONS ********************************************
209 /* Internal function: Read one big-endian four-byte character from file fp and
210 return it as a Unicode code point, or -1 on EOF */
212 read_ucs4be_char_from_file(FILE *fp)
214 unsigned char readbuffer[4];
215 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
218 readbuffer[0] << 24 |
219 readbuffer[1] << 16 |
224 /* Internal function: Read one UTF-8 character, which may be more than one byte,
225 from file fp and return it as a Unicode code point, or -1 on EOF */
227 read_utf8_char_from_file(FILE *fp)
229 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
231 gunichar charresult = (gunichar)-2;
232 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
237 readbuffer[foo] = (gchar)ch;
238 charresult = g_utf8_get_char_validated(readbuffer, foo);
239 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
242 /* Silently return unknown characters as 0xFFFD, Replacement Character */
243 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
248 /* Internal function: Tell whether this code point is a Unicode newline. The
249 file pointer and eight-bit flag are included in case the newline is a CR
250 (U+000D). If the next character is LF (U+000A) then it also belongs to the
253 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
255 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
258 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
259 read_ucs4be_char_from_file(fp);
261 fseek(fp, utf8? -1 : -4, SEEK_CUR);
268 * glk_get_char_stream:
269 * @str: An input stream.
271 * Reads one character from the stream @str. (There is no notion of a ``current
272 * input stream.'') It is illegal for @str to be %NULL, or an output-only
275 * The result will be between 0 and 255. As with all basic text functions, Glk
276 * assumes the Latin-1 encoding. If the end of the stream has been reached, the
277 * result will be -1. Note that high-bit characters (128..255) are
278 * <emphasis>not</emphasis> returned as negative numbers.
280 * If the stream contains Unicode data --- for example, if it was created with
281 * glk_stream_open_file_uni() or glk_stream_open_memory_uni() --- then
282 * characters beyond 255 will be returned as 0x3F ("?").
284 * Returns: A character value between 0 and 255, or -1 on end of stream.
287 glk_get_char_stream(strid_t str)
289 g_return_val_if_fail(str != NULL, -1);
290 g_return_val_if_fail(str->file_mode == filemode_Read
291 || str->file_mode == filemode_ReadWrite, -1);
293 switch(str->stream_type)
295 case STREAM_TYPE_MEMORY:
298 if(!str->ubuffer || str->mark >= str->buflen)
300 glui32 ch = str->ubuffer[str->mark++];
302 return (ch > 0xFF)? 0x3F : ch;
306 if(!str->buffer || str->mark >= str->buflen)
308 char ch = str->buffer[str->mark++];
314 case STREAM_TYPE_FILE:
319 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
323 return (ch > 0xFF)? 0x3F : ch;
325 else /* Regular file */
327 int ch = fgetc(str->file_pointer);
335 else /* Text mode is the same for Unicode and regular files */
337 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
342 return (ch > 0xFF)? 0x3F : ch;
345 g_warning("%s: Reading from this kind of stream unsupported.",
352 * glk_get_buffer_stream:
353 * @str: An input stream.
354 * @buf: A buffer with space for at least @len characters.
355 * @len: The number of characters to read.
357 * Reads @len characters from @str, unless the end of stream is reached first.
358 * No terminal null is placed in the buffer.
360 * Returns: The number of characters actually read.
363 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
365 g_return_val_if_fail(str != NULL, 0);
366 g_return_val_if_fail(str->file_mode == filemode_Read
367 || str->file_mode == filemode_ReadWrite, 0);
368 g_return_val_if_fail(buf != NULL, 0);
370 switch(str->stream_type)
372 case STREAM_TYPE_MEMORY:
377 while(copycount < len && str->ubuffer
378 && str->mark < str->buflen)
380 glui32 ch = str->ubuffer[str->mark++];
381 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
386 if(str->buffer) /* if not, copycount stays 0 */
387 copycount = min(len, str->buflen - str->mark);
388 memmove(buf, str->buffer + str->mark, copycount);
391 str->read_count += copycount;
394 case STREAM_TYPE_FILE:
397 if(str->unicode) /* Binary file with 4-byte characters */
399 /* Read len characters of 4 bytes each */
400 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
401 size_t count = fread(readbuffer, sizeof(unsigned char),
402 4 * len, str->file_pointer);
403 /* If there was an incomplete character */
407 g_warning("%s: Incomplete character in binary Unicode "
411 str->read_count += count / 4;
413 for(foo = 0; foo < count; foo += 4)
415 glsi32 ch = readbuffer[foo] << 24
416 | readbuffer[foo + 1] << 16
417 | readbuffer[foo + 2] << 8
418 | readbuffer[foo + 3];
419 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
424 else /* Regular binary file */
426 size_t count = fread(buf, sizeof(char), len,
428 str->read_count += count;
432 else /* Text mode is the same for Unicode and regular files */
434 /* Do it character-by-character */
436 for(foo = 0; foo < len; foo++)
438 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
442 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
447 g_warning("%s: Reading from this kind of stream unsupported.",
454 * glk_get_line_stream:
455 * @str: An input stream.
456 * @buf: A buffer with space for at least @len characters.
457 * @len: The number of characters to read, plus one.
459 * Reads characters from @str, until either @len - 1 characters have been read
460 * or a newline has been read. It then puts a terminal null ('\0') aracter on
461 * the end. It returns the number of characters actually read, including the
462 * newline (if there is one) but not including the terminal null.
464 * It is usually more efficient to read several characters at once with
465 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
466 * glk_get_char_stream() several times.
468 * Returns: The number of characters actually read.
471 glk_get_line_stream(strid_t str, char *buf, glui32 len)
473 g_return_val_if_fail(str != NULL, 0);
474 g_return_val_if_fail(str->file_mode == filemode_Read
475 || str->file_mode == filemode_ReadWrite, 0);
476 g_return_val_if_fail(buf != NULL, 0);
478 switch(str->stream_type)
480 case STREAM_TYPE_MEMORY:
485 /* Do it character-by-character */
486 while(copycount < len - 1 && str->ubuffer
487 && str->mark < str->buflen)
489 glui32 ch = str->ubuffer[str->mark++];
490 /* Check for Unicode newline; slightly different than
492 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028
495 buf[copycount++] = '\n';
500 if(str->ubuffer[str->mark] == 0x0A)
501 str->mark++; /* skip past next newline */
502 buf[copycount++] = '\n';
505 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
507 buf[copycount] = '\0';
511 if(str->buffer) /* if not, copycount stays 0 */
512 copycount = min(len, str->buflen - str->mark);
513 memccpy(buf, str->buffer + str->mark, '\n', copycount);
516 str->read_count += copycount;
519 case STREAM_TYPE_FILE:
522 if(str->unicode) /* Binary file with 4-byte characters */
524 /* Do it character-by-character */
526 for(foo = 0; foo < len - 1; foo++)
529 read_ucs4be_char_from_file(str->file_pointer);
536 if(is_unicode_newline(ch, str->file_pointer, FALSE))
542 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
547 else /* Regular binary file */
549 fgets(buf, len, str->file_pointer);
550 str->read_count += strlen(buf);
554 else /* Text mode is the same for Unicode and regular files */
556 /* Do it character-by-character */
558 for(foo = 0; foo < len - 1; foo++)
560 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
567 if(is_unicode_newline(ch, str->file_pointer, TRUE))
573 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
579 g_warning("%s: Reading from this kind of stream unsupported.",
587 **************** SEEKING FUNCTIONS ********************************************
592 * glk_stream_get_position:
593 * @str: A file or memory stream.
595 * Returns the position of the read/write mark in @str. For memory streams and
596 * binary file streams, this is exactly the number of characters read or written
597 * from the beginning of the stream (unless you have moved the mark with
598 * glk_stream_set_position().) For text file streams, matters are more
599 * ambiguous, since (for example) writing one byte to a text file may store more
600 * than one character in the platform's native encoding. You can only be sure
601 * that the position increases as you read or write to the file.
603 * Additional complication: for Latin-1 memory and file streams, a character is
604 * a byte. For Unicode memory and file streams (those created by
605 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
606 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
609 * Returns: position of the read/write mark in @str.
612 glk_stream_get_position(strid_t str)
614 g_return_val_if_fail(str != NULL, 0);
616 switch(str->stream_type)
618 case STREAM_TYPE_MEMORY:
620 case STREAM_TYPE_FILE:
621 return ftell(str->file_pointer);
623 g_warning("%s: Seeking not supported on this type of stream.",
630 * glk_stream_set_position:
631 * @str: A file or memory stream.
632 * @pos: The position to set the mark to, relative to @seekmode.
633 * @seekmode: One of #seekmode_Start, #seekmode_Current, or #seekmode_End.
635 * Sets the position of the read/write mark in @str. The position is controlled
636 * by @pos, and the meaning of @pos is controlled by @seekmode:
638 * <listitem>#seekmode_Start: @pos characters after the beginning of the file.
640 * <listitem>#seekmode_Current: @pos characters after the current position
641 * (moving backwards if @pos is negative.)</listitem>
642 * <listitem>#seekmode_End: @pos characters after the end of the file. (@pos
643 * should always be zero or negative, so that this will move backwards to a
644 * position within the file.</listitem>
646 * It is illegal to specify a position before the beginning or after the end of
649 * In binary files, the mark position is exact --- it corresponds with the
650 * number of characters you have read or written. In text files, this mapping
651 * can vary, because of linefeed conventions or other character-set
652 * approximations. glk_stream_set_position() and glk_stream_get_position()
653 * measure positions in the platform's native encoding --- after character
654 * cookery. Therefore, in a text stream, it is safest to use
655 * glk_stream_set_position() only to move to the beginning or end of a file, or
656 * to a position determined by glk_stream_get_position().
658 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
659 * characters are 32-bit words, or four bytes each.
662 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
664 g_return_if_fail(str != NULL);
665 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
666 g_return_if_fail(!(seekmode == seekmode_End || pos > 0));
668 switch(str->stream_type)
670 case STREAM_TYPE_MEMORY:
673 case seekmode_Start: str->mark = pos; break;
674 case seekmode_Current: str->mark += pos; break;
675 case seekmode_End: str->mark = str->buflen + pos; break;
677 g_assert_not_reached();
681 case STREAM_TYPE_FILE:
686 case seekmode_Start: whence = SEEK_SET; break;
687 case seekmode_Current: whence = SEEK_CUR; break;
688 case seekmode_End: whence = SEEK_END; break;
690 g_assert_not_reached();
693 fseek(str->file_pointer, pos, whence);
697 g_warning("%s: Seeking not supported on this type of stream.",