- Added pkg-config files (.pc.in), so after the library is installed you can
[projects/chimara/chimara.git] / src / strio.c
index 98140ada9fd1891656722a00d04719440b2332eb..4517b38dec72e9122f08969bc766c313e7e9f093 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>
@@ -112,7 +114,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 +167,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 +215,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 +272,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 +281,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 +299,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 +317,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 +335,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 +359,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 +378,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 +449,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 +459,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 +514,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 +523,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. 
  *
- * 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 ("?").
+ * <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 &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 +564,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 +589,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 +629,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 +667,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 +686,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 +726,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 +766,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 +777,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 +898,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 +909,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 +923,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 +1040,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 +1069,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 +1088,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 +1100,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 +1122,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 +1135,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 +1148,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;
        }
 }
+