5 #include <glib/gstdio.h>
9 **************** WRITING FUNCTIONS ********************************************
13 /* Internal function: change illegal (control) characters in a string to a
14 placeholder character. Must free returned string afterwards. */
16 remove_latin1_control_characters(unsigned char *s, gsize len)
18 /* If len == 0, then return an empty string, not NULL */
22 gchar *retval = g_new0(gchar, len);
24 for(i = 0; i < len; i++)
25 if( (s[i] < 32 && s[i] != 10) || (s[i] >= 127 && s[i] <= 159) )
27 /* Our placeholder character is '?'; other options are possible,
28 like printing "0x7F" or something */
34 /* Internal function: convert a Latin-1 string to a UTF-8 string, replacing
35 Latin-1 control characters by a placeholder first. The UTF-8 string must be
36 freed afterwards. Returns NULL on error. */
38 convert_latin1_to_utf8(gchar *s, gsize len)
42 gchar *canonical = remove_latin1_control_characters( (unsigned char *)s,
44 utf8 = g_convert(canonical, len, "UTF-8", "ISO-8859-1", NULL, NULL, &error);
49 error_dialog(NULL, error, "Error during latin1->utf8 conversion: ");
56 /* Internal function: write a UTF-8 string to a window's text buffer. */
58 write_utf8_to_window(winid_t win, gchar *s)
62 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
65 gtk_text_buffer_get_end_iter(buffer, &iter);
66 gtk_text_buffer_insert(buffer, &iter, s, -1);
71 /* Internal function: write a Latin-1 buffer with length to a stream. */
73 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
77 case STREAM_TYPE_WINDOW:
78 /* Each window type has a different way of printing to it */
79 switch(str->window->type)
81 /* Printing to these windows' streams does nothing */
84 case wintype_Graphics:
85 str->write_count += len;
87 /* Text buffer window */
88 case wintype_TextBuffer:
90 gchar *utf8 = convert_latin1_to_utf8(buf, len);
93 write_utf8_to_window(str->window, utf8);
97 str->write_count += len;
100 g_warning("%s: Writing to this kind of window unsupported.", __func__);
103 /* Now write the same buffer to the window's echo stream */
104 if(str->window->echo_stream != NULL)
105 write_buffer_to_stream(str->window->echo_stream, buf, len);
109 case STREAM_TYPE_MEMORY:
110 if(str->unicode && str->ubuffer)
113 while(str->mark < str->buflen && foo < len)
114 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
116 if(!str->unicode && str->buffer)
118 int copycount = MIN(len, str->buflen - str->mark);
119 memmove(str->buffer + str->mark, buf, copycount);
120 str->mark += copycount;
123 str->write_count += len;
126 case STREAM_TYPE_FILE:
131 /* Convert to four-byte big-endian */
132 gchar *writebuffer = g_new0(gchar, len * 4);
134 for(i = 0; i < len; i++)
135 writebuffer[i * 4 + 3] = buf[i];
136 fwrite(writebuffer, sizeof(gchar), len * 4,
139 else /* Regular file */
141 fwrite(buf, sizeof(gchar), len, str->file_pointer);
144 else /* Text mode is the same for Unicode and regular files */
146 gchar *utf8 = convert_latin1_to_utf8(buf, len);
147 g_fprintf(str->file_pointer, "%s", utf8);
151 str->write_count += len;
154 g_warning("%s: Writing to this kind of stream unsupported.", __func__);
159 * glk_put_char_stream:
160 * @str: An output stream.
161 * @ch: A character in Latin-1 encoding.
163 * Prints one character @ch to the stream @str. It is illegal for @str to be
164 * %NULL, or an input-only stream.
167 glk_put_char_stream(strid_t str, unsigned char ch)
169 g_return_if_fail(str != NULL);
170 g_return_if_fail(str->file_mode != filemode_Read);
172 write_buffer_to_stream(str, (gchar *)&ch, 1);
176 * glk_put_string_stream:
177 * @str: An output stream.
178 * @s: A null-terminated string in Latin-1 encoding.
180 * Prints @s to the stream @str. It is illegal for @str to be %NULL, or an
184 glk_put_string_stream(strid_t str, char *s)
186 g_return_if_fail(str != NULL);
187 g_return_if_fail(str->file_mode != filemode_Read);
189 write_buffer_to_stream(str, (gchar *)s, strlen(s));
193 * glk_put_buffer_stream:
194 * @str: An output stream.
195 * @buf: An array of characters in Latin-1 encoding.
196 * @len: Length of @buf.
198 * Prints @buf to the stream @str. It is illegal for @str to be %NULL, or an
202 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
204 g_return_if_fail(str != NULL);
205 g_return_if_fail(str->file_mode != filemode_Read);
207 write_buffer_to_stream(str, (gchar *)buf, len);
212 **************** READING FUNCTIONS ********************************************
216 /* Internal function: Read one big-endian four-byte character from file fp and
217 return it as a Unicode code point, or -1 on EOF */
219 read_ucs4be_char_from_file(FILE *fp)
221 unsigned char readbuffer[4];
222 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
225 readbuffer[0] << 24 |
226 readbuffer[1] << 16 |
231 /* Internal function: Read one UTF-8 character, which may be more than one byte,
232 from file fp and return it as a Unicode code point, or -1 on EOF */
234 read_utf8_char_from_file(FILE *fp)
236 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
238 gunichar charresult = (gunichar)-2;
239 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
244 readbuffer[foo] = (gchar)ch;
245 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
246 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
249 /* Silently return unknown characters as 0xFFFD, Replacement Character */
250 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
255 /* Internal function: Tell whether this code point is a Unicode newline. The
256 file pointer and eight-bit flag are included in case the newline is a CR
257 (U+000D). If the next character is LF (U+000A) then it also belongs to the
260 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
262 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
265 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
266 read_ucs4be_char_from_file(fp);
268 fseek(fp, utf8? -1 : -4, SEEK_CUR);
275 * glk_get_char_stream:
276 * @str: An input stream.
278 * Reads one character from the stream @str. (There is no notion of a ``current
279 * input stream.'') It is illegal for @str to be %NULL, or an output-only
282 * The result will be between 0 and 255. As with all basic text functions, Glk
283 * assumes the Latin-1 encoding. If the end of the stream has been reached, the
284 * result will be -1. Note that high-bit characters (128..255) are
285 * <emphasis>not</emphasis> returned as negative numbers.
287 * If the stream contains Unicode data --- for example, if it was created with
288 * glk_stream_open_file_uni() or glk_stream_open_memory_uni() --- then
289 * characters beyond 255 will be returned as 0x3F ("?").
291 * Returns: A character value between 0 and 255, or -1 on end of stream.
294 glk_get_char_stream(strid_t str)
296 g_return_val_if_fail(str != NULL, -1);
297 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
301 case STREAM_TYPE_MEMORY:
304 if(!str->ubuffer || str->mark >= str->buflen)
306 glui32 ch = str->ubuffer[str->mark++];
308 return (ch > 0xFF)? 0x3F : ch;
312 if(!str->buffer || str->mark >= str->buflen)
314 char ch = str->buffer[str->mark++];
320 case STREAM_TYPE_FILE:
325 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
329 return (ch > 0xFF)? 0x3F : ch;
331 else /* Regular file */
333 int ch = fgetc(str->file_pointer);
341 else /* Text mode is the same for Unicode and regular files */
343 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
348 return (ch > 0xFF)? 0x3F : ch;
351 g_warning("%s: Reading from this kind of stream unsupported.", __func__);
357 * glk_get_buffer_stream:
358 * @str: An input stream.
359 * @buf: A buffer with space for at least @len characters.
360 * @len: The number of characters to read.
362 * Reads @len characters from @str, unless the end of stream is reached first.
363 * No terminal null is placed in the buffer.
365 * Returns: The number of characters actually read.
368 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
370 g_return_val_if_fail(str != NULL, 0);
371 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
372 g_return_val_if_fail(buf != NULL, 0);
376 case STREAM_TYPE_MEMORY:
381 while(copycount < len && str->ubuffer && str->mark < str->buflen)
383 glui32 ch = str->ubuffer[str->mark++];
384 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
389 if(str->buffer) /* if not, copycount stays 0 */
390 copycount = MIN(len, str->buflen - str->mark);
391 memmove(buf, str->buffer + str->mark, copycount);
392 str->mark += copycount;
395 str->read_count += copycount;
398 case STREAM_TYPE_FILE:
401 if(str->unicode) /* Binary file with 4-byte characters */
403 /* Read len characters of 4 bytes each */
404 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
405 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
406 /* If there was an incomplete character */
410 g_warning("%s: Incomplete character in binary Unicode file.", __func__);
413 str->read_count += count / 4;
415 for(foo = 0; foo < count; foo += 4)
417 glsi32 ch = readbuffer[foo] << 24
418 | readbuffer[foo + 1] << 16
419 | readbuffer[foo + 2] << 8
420 | readbuffer[foo + 3];
421 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
426 else /* Regular binary file */
428 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
429 str->read_count += count;
433 else /* Text mode is the same for Unicode and regular files */
435 /* Do it character-by-character */
437 for(foo = 0; foo < len; foo++)
439 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
443 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
448 g_warning("%s: Reading from this kind of stream unsupported.", __func__);
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 || str->file_mode == filemode_ReadWrite, 0);
475 g_return_val_if_fail(buf != NULL, 0);
479 case STREAM_TYPE_MEMORY:
484 /* Do it character-by-character */
485 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
487 glui32 ch = str->ubuffer[str->mark++];
488 /* Check for Unicode newline; slightly different than
490 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
492 buf[copycount++] = '\n';
497 if(str->ubuffer[str->mark] == 0x0A)
498 str->mark++; /* skip past next newline */
499 buf[copycount++] = '\n';
502 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
504 buf[copycount] = '\0';
508 if(str->buffer) /* if not, copycount stays 0 */
509 copycount = MIN(len - 1, str->buflen - str->mark);
510 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
511 if(endptr) /* newline was found */
512 copycount = endptr - buf; /* Real copy count */
513 buf[copycount] = '\0';
514 str->mark += copycount;
517 str->read_count += copycount;
520 case STREAM_TYPE_FILE:
523 if(str->unicode) /* Binary file with 4-byte characters */
525 /* Do it character-by-character */
527 for(foo = 0; foo < len - 1; foo++)
529 glsi32 ch = 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.", __func__);
586 **************** SEEKING FUNCTIONS ********************************************
591 * glk_stream_get_position:
592 * @str: A file or memory stream.
594 * Returns the position of the read/write mark in @str. For memory streams and
595 * binary file streams, this is exactly the number of characters read or written
596 * from the beginning of the stream (unless you have moved the mark with
597 * glk_stream_set_position().) For text file streams, matters are more
598 * ambiguous, since (for example) writing one byte to a text file may store more
599 * than one character in the platform's native encoding. You can only be sure
600 * that the position increases as you read or write to the file.
602 * Additional complication: for Latin-1 memory and file streams, a character is
603 * a byte. For Unicode memory and file streams (those created by
604 * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
605 * a 32-bit word. So in a binary Unicode file, positions are multiples of four
608 * Returns: position of the read/write mark in @str.
611 glk_stream_get_position(strid_t str)
613 g_return_val_if_fail(str != NULL, 0);
617 case STREAM_TYPE_MEMORY:
619 case STREAM_TYPE_FILE:
620 return ftell(str->file_pointer);
622 g_warning("%s: Seeking not supported on this type of stream.",
629 * glk_stream_set_position:
630 * @str: A file or memory stream.
631 * @pos: The position to set the mark to, relative to @seekmode.
632 * @seekmode: One of #seekmode_Start, #seekmode_Current, or #seekmode_End.
634 * Sets the position of the read/write mark in @str. The position is controlled
635 * by @pos, and the meaning of @pos is controlled by @seekmode:
637 * <listitem>#seekmode_Start: @pos characters after the beginning of the file.
639 * <listitem>#seekmode_Current: @pos characters after the current position
640 * (moving backwards if @pos is negative.)</listitem>
641 * <listitem>#seekmode_End: @pos characters after the end of the file. (@pos
642 * should always be zero or negative, so that this will move backwards to a
643 * position within the file.</listitem>
645 * It is illegal to specify a position before the beginning or after the end of
648 * In binary files, the mark position is exact --- it corresponds with the
649 * number of characters you have read or written. In text files, this mapping
650 * can vary, because of linefeed conventions or other character-set
651 * approximations. glk_stream_set_position() and glk_stream_get_position()
652 * measure positions in the platform's native encoding --- after character
653 * cookery. Therefore, in a text stream, it is safest to use
654 * glk_stream_set_position() only to move to the beginning or end of a file, or
655 * to a position determined by glk_stream_get_position().
657 * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
658 * characters are 32-bit words, or four bytes each.
661 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
663 g_return_if_fail(str != NULL);
664 g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
665 g_return_if_fail(!(seekmode == seekmode_End || pos > 0));
669 case STREAM_TYPE_MEMORY:
672 case seekmode_Start: str->mark = pos; break;
673 case seekmode_Current: str->mark += pos; break;
674 case seekmode_End: str->mark = str->buflen + pos; break;
676 g_assert_not_reached();
680 case STREAM_TYPE_FILE:
685 case seekmode_Start: whence = SEEK_SET; break;
686 case seekmode_Current: whence = SEEK_CUR; break;
687 case seekmode_End: whence = SEEK_END; break;
689 g_assert_not_reached();
692 fseek(str->file_pointer, pos, whence);
696 g_warning("%s: Seeking not supported on this type of stream.", __func__);