* Added initial support for styles! (no style hints as of yet though)
[rodin/chimara.git] / src / strio.c
index 98140ada9fd1891656722a00d04719440b2332eb..921b75cc771f0331828220061dbfc9030726fcd1 100644 (file)
@@ -1,5 +1,7 @@
 #include "charset.h"
+#include "magic.h"
 #include "stream.h"
+#include <errno.h>
 #include <stdio.h>
 #include <string.h>
 #include <glib.h>
 static void
 write_utf8_to_grid(winid_t win, gchar *s)
 {
+       if(win->input_request_type == INPUT_REQUEST_LINE || win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
+       {
+               ILLEGAL("Tried to print to a text grid window with line input pending.");
+               return;
+       }
+       
     /* Number of characters to insert */
     glong length = g_utf8_strlen(s, -1);
     glong chars_left = length;
@@ -57,13 +65,19 @@ write_utf8_to_grid(winid_t win, gchar *s)
 static void
 write_utf8_to_window(winid_t win, gchar *s)
 {
+       if(win->input_request_type == INPUT_REQUEST_LINE || win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
+       {
+               ILLEGAL("Tried to print to a text buffer window with line input pending.");
+               return;
+       }
+       
        gdk_threads_enter();
 
        GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
 
        GtkTextIter iter;
        gtk_text_buffer_get_end_iter(buffer, &iter);
-       gtk_text_buffer_insert(buffer, &iter, s, -1);
+       gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, s, -1, win->window_stream->style, NULL);
 
        gdk_threads_leave();
 }
@@ -112,7 +126,7 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
                                        str->write_count += len;
                                        break;
                                default:
-                                       g_warning("%s: Writing to this kind of window unsupported.", __func__);
+                                       ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
                        }
                        
                        /* Now write the same buffer to the window's echo stream */
@@ -165,7 +179,7 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
                        str->write_count += len;
                        break;
                default:
-                       g_warning("%s: Writing to this kind of stream unsupported.", __func__);
+                       ILLEGAL_PARAM("Unknown stream type: %u", str->type);
        }
 }
 
@@ -213,7 +227,7 @@ write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
                                        str->write_count += len;
                                        break;
                                default:
-                                       g_warning("%s: Writing to this kind of window unsupported.", __func__);
+                                       ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
                        }
                        
                        /* Now write the same buffer to the window's echo stream */
@@ -270,7 +284,7 @@ write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
                        str->write_count += len;
                        break;
                default:
-                       g_warning("%s: Writing to this kind of stream unsupported.", __func__);
+                       ILLEGAL_PARAM("Unknown stream type: %u", str->type);
        }
 }
 
@@ -279,13 +293,14 @@ write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
  * @str: An output stream.
  * @ch: A character in Latin-1 encoding.
  *
- * Prints one character @ch to the stream @str. It is illegal for @str to be
- * %NULL, or an input-only stream.
+ * The same as glk_put_char(), except that you specify a stream @str to print 
+ * to, instead of using the current stream. It is illegal for @str to be %NULL,
+ * or an input-only stream.
  */
 void
 glk_put_char_stream(strid_t str, unsigned char ch)
 {
-       g_return_if_fail(str != NULL);
+       VALID_STREAM(str, return);
        g_return_if_fail(str->file_mode != filemode_Read);
        
        write_buffer_to_stream(str, (gchar *)&ch, 1);
@@ -296,13 +311,14 @@ glk_put_char_stream(strid_t str, unsigned char ch)
  * @str: An output stream.
  * @ch: A Unicode code point.
  *
- * Prints one character @ch to the stream @str. It is illegal for @str to be
+ * The same as glk_put_char_uni(), except that you specify a stream @str to
+ * print to, instead of using the current stream. It is illegal for @str to be 
  * %NULL, or an input-only stream.
  */
 void
 glk_put_char_stream_uni(strid_t str, glui32 ch)
 {
-       g_return_if_fail(str != NULL);
+       VALID_STREAM(str, return);
        g_return_if_fail(str->file_mode != filemode_Read);
        
        write_buffer_to_stream_uni(str, &ch, 1);
@@ -313,13 +329,14 @@ glk_put_char_stream_uni(strid_t str, glui32 ch)
  * @str: An output stream.
  * @s: A null-terminated string in Latin-1 encoding.
  *
- * Prints @s to the stream @str. It is illegal for @str to be %NULL, or an
- * input-only stream.
+ * The same as glk_put_string(), except that you specify a stream @str to print 
+ * to, instead of using the current stream. It is illegal for @str to be %NULL,
+ * or an input-only stream.
  */
 void
 glk_put_string_stream(strid_t str, char *s)
 {
-       g_return_if_fail(str != NULL);
+       VALID_STREAM(str, return);
        g_return_if_fail(str->file_mode != filemode_Read);
 
        write_buffer_to_stream(str, s, strlen(s));
@@ -330,13 +347,14 @@ glk_put_string_stream(strid_t str, char *s)
  * @str: An output stream.
  * @s: A null-terminated array of Unicode code points.
  *
- * Prints @s to the stream @str. It is illegal for @str to be %NULL, or an
- * input-only stream.
+ * The same as glk_put_string_uni(), except that you specify a stream @str to
+ * print to, instead of using the current stream. It is illegal for @str to be 
+ * %NULL, or an input-only stream.
  */
 void
 glk_put_string_stream_uni(strid_t str, glui32 *s)
 {
-       g_return_if_fail(str != NULL);
+       VALID_STREAM(str, return);
        g_return_if_fail(str->file_mode != filemode_Read);
        
        /* An impromptu strlen() for glui32 arrays */
@@ -353,13 +371,14 @@ glk_put_string_stream_uni(strid_t str, glui32 *s)
  * @buf: An array of characters in Latin-1 encoding.
  * @len: Length of @buf.
  *
- * Prints @buf to the stream @str. It is illegal for @str to be %NULL, or an
- * input-only stream.
+ * The same as glk_put_buffer(), except that you specify a stream @str to print 
+ * to, instead of using the current stream. It is illegal for @str to be %NULL,
+ * or an input-only stream.
  */
 void
 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
 {
-       g_return_if_fail(str != NULL);
+       VALID_STREAM(str, return);
        g_return_if_fail(str->file_mode != filemode_Read);
        
        write_buffer_to_stream(str, buf, len);
@@ -371,13 +390,14 @@ glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
  * @buf: An array of Unicode code points.
  * @len: Length of @buf.
  *
- * Prints @buf to the stream @str. It is illegal for @str to be %NULL, or an
- * input-only stream.
+ * The same as glk_put_buffer_uni(), except that you specify a stream @str to
+ * print to, instead of using the current stream. It is illegal for @str to be 
+ * %NULL, or an input-only stream.
  */
 void
 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
 {
-       g_return_if_fail(str != NULL);
+       VALID_STREAM(str, return);
        g_return_if_fail(str->file_mode != filemode_Read);
        
        write_buffer_to_stream_uni(str, buf, len);
@@ -441,7 +461,8 @@ is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
                glsi32 ch2 = utf8? read_utf8_char_from_file(fp) : 
                        read_ucs4be_char_from_file(fp);
                if(ch2 != 0x0A)
-                       fseek(fp, utf8? -1 : -4, SEEK_CUR);
+                       if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
+                               WARNING_S("Seek failed on stream", g_strerror(errno) );
                return TRUE;
        }
        return FALSE;
@@ -450,7 +471,7 @@ is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
 /* Internal function: Read one character from a stream. Returns a value which
  can be returned unchanged by glk_get_char_stream_uni(), but 
  glk_get_char_stream() must replace high values by the placeholder character. */
-glsi32
+static glsi32
 get_char_stream_common(strid_t str)
 {
        switch(str->type)
@@ -505,7 +526,7 @@ get_char_stream_common(strid_t str)
                                return ch;
                        }
                default:
-                       g_warning("%s: Reading from this kind of stream unsupported.", __func__);
+                       ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
                        return -1;
        }
 }
@@ -514,25 +535,34 @@ get_char_stream_common(strid_t str)
  * glk_get_char_stream:
  * @str: An input stream.
  *
- * Reads one character from the stream @str. (There is no notion of a ``current
- * input stream.'') It is illegal for @str to be %NULL, or an output-only
- * stream.
+ * Reads one character from the stream @str. (There is no notion of a
+ * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
+ * an output-only stream.
  *
  * The result will be between 0 and 255. As with all basic text functions, Glk
- * assumes the Latin-1 encoding. If the end of the stream has been reached, the
- * result will be -1. Note that high-bit characters (128..255) are
- * <emphasis>not</emphasis> returned as negative numbers.
+ * assumes the Latin-1 encoding. See <link 
+ * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
+ * of the stream has been reached, the result will be -1. 
+ *
+ * <note><para>
+ *   Note that high-bit characters (128..255) are <emphasis>not</emphasis>
+ *   returned as negative numbers.
+ * </para></note>
  *
- * If the stream contains Unicode data --- for example, if it was created with
- * glk_stream_open_file_uni() or glk_stream_open_memory_uni() --- then
- * characters beyond 255 will be returned as 0x3F ("?").
+ * If the stream contains Unicode data &mdash; for example, if it was created
+ * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() &mdash; then
+ * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
+ *
+ * It is usually more efficient to read several characters at once with
+ * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
+ * glk_get_char_stream() several times.
  *
  * Returns: A character value between 0 and 255, or -1 on end of stream.
  */
 glsi32
 glk_get_char_stream(strid_t str)
 {
-       g_return_val_if_fail(str != NULL, -1);
+       VALID_STREAM(str, return -1);
        g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
        
        glsi32 ch = get_char_stream_common(str);
@@ -546,12 +576,12 @@ glk_get_char_stream(strid_t str)
  * Reads one character from the stream @str. The result will be between 0 and 
  * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
  *
- * Returns: A character value between 0 and 255, or -1 on end of stream.
+ * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
  */
 glsi32
 glk_get_char_stream_uni(strid_t str)
 {
-       g_return_val_if_fail(str != NULL, -1);
+       VALID_STREAM(str, return -1);
        g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
        
        return get_char_stream_common(str);
@@ -571,7 +601,7 @@ glk_get_char_stream_uni(strid_t str)
 glui32
 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
 {
-       g_return_val_if_fail(str != NULL, 0);
+       VALID_STREAM(str, return 0);
        g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
        g_return_val_if_fail(buf != NULL, 0);
        
@@ -611,7 +641,7 @@ glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
                                        if(count % 4 != 0) 
                                        {
                                                count -= count % 4;
-                                               g_warning("%s: Incomplete character in binary Unicode file.", __func__);
+                                               WARNING("Incomplete character in binary Unicode file");
                                        }
                                        
                                        int foo;
@@ -649,7 +679,7 @@ glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
                                return foo;
                        }
                default:
-                       g_warning("%s: Reading from this kind of stream unsupported.", __func__);
+                       ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
                        return 0;
        }
 }
@@ -668,7 +698,7 @@ glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
 glui32
 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
 {
-       g_return_val_if_fail(str != NULL, 0);
+       VALID_STREAM(str, return 0);
        g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
        g_return_val_if_fail(buf != NULL, 0);
        
@@ -708,7 +738,7 @@ glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
                                        if(count % 4 != 0) 
                                        {
                                                count -= count % 4;
-                                               g_warning("%s: Incomplete character in binary Unicode file.", __func__);
+                                               WARNING("Incomplete character in binary Unicode file");
                                        }
                                        
                                        int foo;
@@ -748,7 +778,7 @@ glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
                                return foo;
                        }
                default:
-                       g_warning("%s: Reading from this kind of stream unsupported.", __func__);
+                       ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
                        return 0;
        }
 }
@@ -759,21 +789,22 @@ glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
  * @buf: A buffer with space for at least @len characters.
  * @len: The number of characters to read, plus one.
  *
- * Reads characters from @str, until either @len - 1 characters have been read
- * or a newline has been read. It then puts a terminal null ('\0') aracter on
+ * Reads characters from @str, until either 
+ * <inlineequation>
+ *   <alt>@len - 1</alt>
+ *   <mathphrase>@len - 1</mathphrase>
+ * </inlineequation>
+ * characters have been read or a newline has been read. It then puts a
+ * terminal null (<code>'\0'</code>) aracter on
  * the end. It returns the number of characters actually read, including the
  * newline (if there is one) but not including the terminal null.
  *
- * It is usually more efficient to read several characters at once with
- * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
- * glk_get_char_stream() several times.
- *
  * Returns: The number of characters actually read.
  */
 glui32
 glk_get_line_stream(strid_t str, char *buf, glui32 len)
 {
-       g_return_val_if_fail(str != NULL, 0);
+       VALID_STREAM(str, return 0);
        g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
        g_return_val_if_fail(buf != NULL, 0);
 
@@ -879,7 +910,7 @@ glk_get_line_stream(strid_t str, char *buf, glui32 len)
                                return foo;
                        }
                default:
-                       g_warning("%s: Reading from this kind of stream unsupported.", __func__);
+                       ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
                        return 0;
        }
 }
@@ -890,9 +921,13 @@ glk_get_line_stream(strid_t str, char *buf, glui32 len)
  * @buf: A buffer with space for at least @len Unicode code points.
  * @len: The number of characters to read, plus one.
  *
- * Reads Unicode characters from @str, until either @len - 1 Unicode characters
- * have been read or a newline has been read. It then puts a terminal null (a
- * zero value) on the end.
+ * Reads Unicode characters from @str, until either 
+ * <inlineequation>
+ *   <alt>@len - 1</alt>
+ *   <mathphrase>@len - 1</mathphrase>
+ * </inlineequation> 
+ * Unicode characters have been read or a newline has been read. It then puts a
+ * terminal null (a zero value) on the end.
  *
  * Returns: The number of characters actually read, including the newline (if
  * there is one) but not including the terminal null.
@@ -900,7 +935,7 @@ glk_get_line_stream(strid_t str, char *buf, glui32 len)
 glui32
 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
 {
-       g_return_val_if_fail(str != NULL, 0);
+       VALID_STREAM(str, return 0);
        g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
        g_return_val_if_fail(buf != NULL, 0);
 
@@ -1017,7 +1052,7 @@ glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
                                return foo;
                        }
                default:
-                       g_warning("%s: Reading from this kind of stream unsupported.", __func__);
+                       ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
                        return 0;
        }
 }
@@ -1046,12 +1081,17 @@ glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
  * a 32-bit word. So in a binary Unicode file, positions are multiples of four
  * bytes.
  *
+ * <note><para>
+ *   If this bothers you, don't use binary Unicode files. I don't think they're
+ *   good for much anyhow.
+ * </para></note>
+ *
  * Returns: position of the read/write mark in @str.
  */
 glui32
 glk_stream_get_position(strid_t str)
 {
-       g_return_val_if_fail(str != NULL, 0);
+       VALID_STREAM(str, return 0);
        
        switch(str->type)
        {
@@ -1060,8 +1100,7 @@ glk_stream_get_position(strid_t str)
                case STREAM_TYPE_FILE:
                        return ftell(str->file_pointer);
                default:
-                       g_warning("%s: Seeking not supported on this type of stream.",
-                               __func__);
+                       ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
                        return 0;
        }
 }
@@ -1073,27 +1112,21 @@ glk_stream_get_position(strid_t str)
  * @seekmode: One of #seekmode_Start, #seekmode_Current, or #seekmode_End.
  *
  * Sets the position of the read/write mark in @str. The position is controlled
- * by @pos, and the meaning of @pos is controlled by @seekmode:
- * <itemizedlist>
- *  <listitem>#seekmode_Start: @pos characters after the beginning of the file.
- *  </listitem>
- *  <listitem>#seekmode_Current: @pos characters after the current position
- *  (moving backwards if @pos is negative.)</listitem>
- *  <listitem>#seekmode_End: @pos characters after the end of the file. (@pos
- *  should always be zero or negative, so that this will move backwards to a
- *  position within the file.</listitem>
- * </itemizedlist>
+ * by @pos, and the meaning of @pos is controlled by @seekmode. See the
+ * <code>seekmode_</code> constants below.
+ *
  * It is illegal to specify a position before the beginning or after the end of
  * the file.
  *
- * In binary files, the mark position is exact --- it corresponds with the
+ * In binary files, the mark position is exact &mdash; it corresponds with the
  * number of characters you have read or written. In text files, this mapping 
  * can vary, because of linefeed conventions or other character-set 
- * approximations. glk_stream_set_position() and glk_stream_get_position()
- * measure positions in the platform's native encoding --- after character
- * cookery. Therefore, in a text stream, it is safest to use
- * glk_stream_set_position() only to move to the beginning or end of a file, or
- * to a position determined by glk_stream_get_position().
+ * approximations. See <link linkend="chimara-Streams">Streams</link>.
+ * glk_stream_set_position() and glk_stream_get_position() measure positions in
+ * the platform's native encoding &mdash; after character cookery. Therefore,
+ * in a text stream, it is safest to use glk_stream_set_position() only to move
+ * to the beginning or end of a file, or to a position determined by
+ * glk_stream_get_position().
  *
  * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
  * characters are 32-bit words, or four bytes each.
@@ -1101,7 +1134,7 @@ glk_stream_get_position(strid_t str)
 void
 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
 {
-       g_return_if_fail(str != NULL);
+       VALID_STREAM(str, return);
        g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
        g_return_if_fail(!(seekmode == seekmode_End || pos > 0));
        
@@ -1114,7 +1147,7 @@ glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
                                case seekmode_Current: str->mark += pos; break;
                                case seekmode_End:     str->mark = str->buflen + pos; break;
                                default:
-                                       g_assert_not_reached();
+                                       g_return_if_reached();
                                        return;
                        }
                        break;
@@ -1127,15 +1160,16 @@ glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
                                case seekmode_Current: whence = SEEK_CUR; break;
                                case seekmode_End:     whence = SEEK_END; break;
                                default:
-                                       g_assert_not_reached();
+                                       g_return_if_reached();
                                        return;
                        }
-                       fseek(str->file_pointer, pos, whence);
+                       if(fseek(str->file_pointer, pos, whence) == -1)
+                               WARNING("Seek failed on file stream");
                        break;
                }
                default:
-                       g_warning("%s: Seeking not supported on this type of stream.", __func__);
+                       ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
                        return;
        }
 }
+