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 =
63 gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
66 gtk_text_buffer_get_end_iter(buffer, &iter);
67 gtk_text_buffer_insert(buffer, &iter, s, -1);
72 /* Internal function: write a Latin-1 buffer with length to a stream. */
74 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
78 case STREAM_TYPE_WINDOW:
79 /* Each window type has a different way of printing to it */
80 switch(str->window->type)
82 /* Printing to these windows' streams does nothing */
85 case wintype_Graphics:
86 str->write_count += len;
88 /* Text buffer window */
89 case wintype_TextBuffer:
91 gchar *utf8 = convert_latin1_to_utf8(buf, len);
94 write_utf8_to_window(str->window, utf8);
98 str->write_count += len;
101 g_warning("%s: Writing to this kind of window unsupported.", __func__);
104 /* Now write the same buffer to the window's echo stream */
105 if(str->window->echo_stream != NULL)
106 write_buffer_to_stream(str->window->echo_stream, buf, len);
110 case STREAM_TYPE_MEMORY:
111 if(str->unicode && str->ubuffer)
114 while(str->mark < str->buflen && foo < len)
115 str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
117 if(!str->unicode && str->buffer)
119 int copycount = MIN(len, str->buflen - str->mark);
120 memmove(str->buffer + str->mark, buf, copycount);
121 str->mark += copycount;
124 str->write_count += len;
127 case STREAM_TYPE_FILE:
132 /* Convert to four-byte big-endian */
133 gchar *writebuffer = g_new0(gchar, len * 4);
135 for(i = 0; i < len; i++)
136 writebuffer[i * 4 + 3] = buf[i];
137 fwrite(writebuffer, sizeof(gchar), len * 4,
140 else /* Regular file */
142 fwrite(buf, sizeof(gchar), len, str->file_pointer);
145 else /* Text mode is the same for Unicode and regular files */
147 gchar *utf8 = convert_latin1_to_utf8(buf, len);
148 g_fprintf(str->file_pointer, "%s", utf8);
152 str->write_count += len;
155 g_warning("%s: Writing to this kind of stream unsupported.", __func__);
160 * glk_put_char_stream:
161 * @str: An output stream.
162 * @ch: A character in Latin-1 encoding.
164 * Prints one character @ch to the stream @str. It is illegal for @str to be
165 * %NULL, or an input-only stream.
168 glk_put_char_stream(strid_t str, unsigned char ch)
170 g_return_if_fail(str != NULL);
171 g_return_if_fail(str->file_mode != filemode_Read);
173 write_buffer_to_stream(str, (gchar *)&ch, 1);
177 * glk_put_string_stream:
178 * @str: An output stream.
179 * @s: A null-terminated string in Latin-1 encoding.
181 * Prints @s to the stream @str. It is illegal for @str to be %NULL, or an
185 glk_put_string_stream(strid_t str, char *s)
187 g_return_if_fail(str != NULL);
188 g_return_if_fail(str->file_mode != filemode_Read);
190 write_buffer_to_stream(str, (gchar *)s, strlen(s));
194 * glk_put_buffer_stream:
195 * @str: An output stream.
196 * @buf: An array of characters in Latin-1 encoding.
197 * @len: Length of @buf.
199 * Prints @buf to the stream @str. It is illegal for @str to be %NULL, or an
203 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
205 g_return_if_fail(str != NULL);
206 g_return_if_fail(str->file_mode != filemode_Read);
208 write_buffer_to_stream(str, (gchar *)buf, len);
213 **************** READING FUNCTIONS ********************************************
217 /* Internal function: Read one big-endian four-byte character from file fp and
218 return it as a Unicode code point, or -1 on EOF */
220 read_ucs4be_char_from_file(FILE *fp)
222 unsigned char readbuffer[4];
223 if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
226 readbuffer[0] << 24 |
227 readbuffer[1] << 16 |
232 /* Internal function: Read one UTF-8 character, which may be more than one byte,
233 from file fp and return it as a Unicode code point, or -1 on EOF */
235 read_utf8_char_from_file(FILE *fp)
237 gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
239 gunichar charresult = (gunichar)-2;
240 for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++)
245 readbuffer[foo] = (gchar)ch;
246 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
247 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
250 /* Silently return unknown characters as 0xFFFD, Replacement Character */
251 if(charresult == (gunichar)-1 || charresult == (gunichar)-2)
256 /* Internal function: Tell whether this code point is a Unicode newline. The
257 file pointer and eight-bit flag are included in case the newline is a CR
258 (U+000D). If the next character is LF (U+000A) then it also belongs to the
261 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
263 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
266 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) :
267 read_ucs4be_char_from_file(fp);
269 fseek(fp, utf8? -1 : -4, SEEK_CUR);
276 * glk_get_char_stream:
277 * @str: An input stream.
279 * Reads one character from the stream @str. (There is no notion of a ``current
280 * input stream.'') It is illegal for @str to be %NULL, or an output-only
283 * The result will be between 0 and 255. As with all basic text functions, Glk
284 * assumes the Latin-1 encoding. If the end of the stream has been reached, the
285 * result will be -1. Note that high-bit characters (128..255) are
286 * <emphasis>not</emphasis> returned as negative numbers.
288 * If the stream contains Unicode data --- for example, if it was created with
289 * glk_stream_open_file_uni() or glk_stream_open_memory_uni() --- then
290 * characters beyond 255 will be returned as 0x3F ("?").
292 * Returns: A character value between 0 and 255, or -1 on end of stream.
295 glk_get_char_stream(strid_t str)
297 g_return_val_if_fail(str != NULL, -1);
298 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
302 case STREAM_TYPE_MEMORY:
305 if(!str->ubuffer || str->mark >= str->buflen)
307 glui32 ch = str->ubuffer[str->mark++];
309 return (ch > 0xFF)? 0x3F : ch;
313 if(!str->buffer || str->mark >= str->buflen)
315 char ch = str->buffer[str->mark++];
321 case STREAM_TYPE_FILE:
326 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
330 return (ch > 0xFF)? 0x3F : ch;
332 else /* Regular file */
334 int ch = fgetc(str->file_pointer);
342 else /* Text mode is the same for Unicode and regular files */
344 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
349 return (ch > 0xFF)? 0x3F : ch;
352 g_warning("%s: Reading from this kind of stream unsupported.", __func__);
358 * glk_get_buffer_stream:
359 * @str: An input stream.
360 * @buf: A buffer with space for at least @len characters.
361 * @len: The number of characters to read.
363 * Reads @len characters from @str, unless the end of stream is reached first.
364 * No terminal null is placed in the buffer.
366 * Returns: The number of characters actually read.
369 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
371 g_return_val_if_fail(str != NULL, 0);
372 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
373 g_return_val_if_fail(buf != NULL, 0);
377 case STREAM_TYPE_MEMORY:
382 while(copycount < len && str->ubuffer && str->mark < str->buflen)
384 glui32 ch = str->ubuffer[str->mark++];
385 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
390 if(str->buffer) /* if not, copycount stays 0 */
391 copycount = MIN(len, str->buflen - str->mark);
392 memmove(buf, str->buffer + str->mark, copycount);
393 str->mark += copycount;
396 str->read_count += copycount;
399 case STREAM_TYPE_FILE:
402 if(str->unicode) /* Binary file with 4-byte characters */
404 /* Read len characters of 4 bytes each */
405 unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
406 size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
407 /* If there was an incomplete character */
411 g_warning("%s: Incomplete character in binary Unicode file.", __func__);
414 str->read_count += count / 4;
416 for(foo = 0; foo < count; foo += 4)
418 glsi32 ch = readbuffer[foo] << 24
419 | readbuffer[foo + 1] << 16
420 | readbuffer[foo + 2] << 8
421 | readbuffer[foo + 3];
422 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
427 else /* Regular binary file */
429 size_t count = fread(buf, sizeof(char), len, str->file_pointer);
430 str->read_count += count;
434 else /* Text mode is the same for Unicode and regular files */
436 /* Do it character-by-character */
438 for(foo = 0; foo < len; foo++)
440 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
444 buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
449 g_warning("%s: Reading from this kind of stream unsupported.", __func__);
455 * glk_get_line_stream:
456 * @str: An input stream.
457 * @buf: A buffer with space for at least @len characters.
458 * @len: The number of characters to read, plus one.
460 * Reads characters from @str, until either @len - 1 characters have been read
461 * or a newline has been read. It then puts a terminal null ('\0') aracter on
462 * the end. It returns the number of characters actually read, including the
463 * newline (if there is one) but not including the terminal null.
465 * It is usually more efficient to read several characters at once with
466 * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
467 * glk_get_char_stream() several times.
469 * Returns: The number of characters actually read.
472 glk_get_line_stream(strid_t str, char *buf, glui32 len)
474 g_return_val_if_fail(str != NULL, 0);
475 g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
476 g_return_val_if_fail(buf != NULL, 0);
480 case STREAM_TYPE_MEMORY:
485 /* Do it character-by-character */
486 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen)
488 glui32 ch = str->ubuffer[str->mark++];
489 /* Check for Unicode newline; slightly different than
491 if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
493 buf[copycount++] = '\n';
498 if(str->ubuffer[str->mark] == 0x0A)
499 str->mark++; /* skip past next newline */
500 buf[copycount++] = '\n';
503 buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
505 buf[copycount] = '\0';
509 if(str->buffer) /* if not, copycount stays 0 */
510 copycount = MIN(len - 1, str->buflen - str->mark);
511 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
512 if(endptr) /* newline was found */
513 copycount = endptr - buf; /* Real copy count */
514 buf[copycount] = '\0';
515 str->mark += copycount;
518 str->read_count += copycount;
521 case STREAM_TYPE_FILE:
524 if(str->unicode) /* Binary file with 4-byte characters */
526 /* Do it character-by-character */
528 for(foo = 0; foo < len - 1; foo++)
530 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
537 if(is_unicode_newline(ch, str->file_pointer, FALSE))
543 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
548 else /* Regular binary file */
550 fgets(buf, len, str->file_pointer);
551 str->read_count += strlen(buf);
555 else /* Text mode is the same for Unicode and regular files */
557 /* Do it character-by-character */
559 for(foo = 0; foo < len - 1; foo++)
561 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
568 if(is_unicode_newline(ch, str->file_pointer, TRUE))
574 buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
580 g_warning("%s: Reading from this kind of stream unsupported.", __func__);
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);
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));
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.", __func__);