Began garglk_set_zcolors() implementation
[projects/chimara/chimara.git] / libchimara / strio.c
1 #include "charset.h"
2 #include "magic.h"
3 #include "stream.h"
4 #include <errno.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <glib.h>
8 #include <glib/gstdio.h>
9
10 /*
11  *
12  **************** WRITING FUNCTIONS ********************************************
13  *
14  */
15
16 /* Internal function: write a UTF-8 string to a text buffer window's text buffer. */
17 static void
18 write_utf8_to_window_buffer(winid_t win, gchar *s)
19 {
20         if(win->input_request_type == INPUT_REQUEST_LINE || win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
21         {
22                 ILLEGAL("Tried to print to a text buffer window with line input pending.");
23                 return;
24         }
25
26         // Write to the buffer  
27         g_string_append(win->buffer, s);
28 }
29         
30 /* Internal function: flush a window's text buffer to the screen. */
31 void
32 flush_window_buffer(winid_t win)
33 {
34         if(win->type != wintype_TextBuffer && win->type != wintype_TextGrid)
35                 return;
36
37         if(win->buffer->len == 0)
38                 return;
39
40         gdk_threads_enter();
41
42         GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
43
44         switch(win->type) {
45         case wintype_TextBuffer:
46         {
47                 GtkTextIter start, end;
48                 gtk_text_buffer_get_end_iter(buffer, &start);
49
50                 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
51
52                 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
53                 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
54                 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
55
56                 gtk_text_buffer_insert(buffer, &start, win->buffer->str, -1);
57                 gtk_text_buffer_get_end_iter(buffer, &end);
58
59                 // Default style
60                 gtk_text_buffer_apply_tag(buffer, default_tag, &start, &end);
61
62                 // Player's style overrides
63                 gtk_text_buffer_apply_tag(buffer, style_tag, &start, &end);
64
65                 // GLK Program's style overrides
66                 gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &end);
67
68                 // Link style overrides
69                 if(win->window_stream->hyperlink_mode) {
70                         GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
71                         GtkTextTag *link_tag = win->current_hyperlink->tag;
72                         gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &end);
73                         gtk_text_buffer_apply_tag(buffer, link_tag, &start, &end);
74                 }
75
76                 // GLK Program's style overrides using garglk_set_zcolors()
77                 if(win->zcolor != NULL)
78                         gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &end);
79
80                 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
81                 g_assert(glk);
82                 g_signal_emit_by_name(glk, "text-buffer-output", win->rock, win->buffer->str);
83         }
84                 break;
85
86         case wintype_TextGrid:
87         {
88                 /* Number of characters to insert */
89                 glong length = win->buffer->len;
90                 glong chars_left = length;
91                 
92                 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
93                 
94                 /* Get cursor position */
95                 GtkTextIter start;
96                 gtk_text_buffer_get_iter_at_mark(buffer, &start, cursor);
97                 /* Spaces available on this line */
98                 gint available_space = win->width - gtk_text_iter_get_line_offset(&start);
99                 
100                 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
101                 GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
102                 GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
103                 GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
104                 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
105
106                 while(chars_left > available_space && !gtk_text_iter_is_end(&start))
107                 {
108                         GtkTextIter end = start;
109                         gtk_text_iter_forward_to_line_end(&end);
110                         gtk_text_buffer_delete(buffer, &start, &end);
111
112                         gtk_text_buffer_insert(buffer, &start, win->buffer->str + (length - chars_left), available_space);
113
114                         // Default style
115                         gtk_text_buffer_apply_tag(buffer, default_tag, &start, &end);
116
117                         // Player's style overrides
118                         gtk_text_buffer_apply_tag(buffer, style_tag, &start, &end);
119
120                         // GLK Program's style overrides
121                         gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &end);
122
123                         // Link style overrides
124                         if(win->window_stream->hyperlink_mode) {
125                                 GtkTextTag *link_tag = win->current_hyperlink->tag;
126                                 gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &end);
127                                 gtk_text_buffer_apply_tag(buffer, link_tag, &start, &end);
128                         }
129
130                         // GLK Program's style overrides using garglk_set_zcolors()
131                         if(win->zcolor != NULL)
132                                 gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &end);
133
134                         chars_left -= available_space;
135                         gtk_text_iter_forward_line(&start);
136                         available_space = win->width;
137                 }
138                 if(!gtk_text_iter_is_end(&start))
139                 {
140                         GtkTextIter end = start;
141                         gtk_text_iter_forward_chars(&end, chars_left);
142                         gtk_text_buffer_delete(buffer, &start, &end);
143
144                         GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
145                         GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
146                         GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
147                         GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
148
149                         if(win->window_stream->hyperlink_mode) {
150                                 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
151                                 GtkTextTag *link_tag = win->current_hyperlink->tag;
152                                 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, default_tag, style_tag, glk_style_tag, link_style_tag, link_tag, NULL);
153                         } else {
154                                 gtk_text_buffer_insert_with_tags(buffer, &start, win->buffer->str + (length - chars_left), -1, default_tag, style_tag, glk_style_tag, NULL);
155                         }
156                 }
157                 
158                 gtk_text_buffer_move_mark(buffer, cursor, &start);
159         }
160                 break;
161         }
162
163         gdk_threads_leave();
164
165         g_string_truncate(win->buffer, 0);
166 }
167
168 /* Internal function: write a Latin-1 buffer with length to a stream. */
169 static void
170 write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
171 {
172         switch(str->type)
173         {
174                 case STREAM_TYPE_WINDOW:
175                         /* Each window type has a different way of printing to it */
176                         switch(str->window->type)
177                         {
178                                 /* Printing to these windows' streams does nothing */
179                                 case wintype_Blank:
180                                 case wintype_Pair:
181                                 case wintype_Graphics:
182                                         str->write_count += len;
183                                         break;
184                                         
185                             /* Text grid/buffer windows */
186                             case wintype_TextGrid:
187                                 case wintype_TextBuffer:
188                             {
189                                 gchar *utf8 = convert_latin1_to_utf8(buf, len);
190                                 if(utf8 != NULL) {
191                                                 write_utf8_to_window_buffer(str->window, utf8);
192                                                 g_free(utf8);
193                                         }
194                                 }       
195                                         str->write_count += len;
196                                         break;
197                                 default:
198                                         ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
199                         }
200                         
201                         /* Now write the same buffer to the window's echo stream */
202                         if(str->window->echo_stream != NULL)
203                                 write_buffer_to_stream(str->window->echo_stream, buf, len);
204                         
205                         break;
206                         
207                 case STREAM_TYPE_MEMORY:
208                         if(str->unicode && str->ubuffer)
209                         {
210                                 int foo = 0;
211                                 while(str->mark < str->buflen && foo < len)
212                                         str->ubuffer[str->mark++] = (unsigned char)buf[foo++];
213                         }
214                         if(!str->unicode && str->buffer)
215                         {
216                                 int copycount = MIN(len, str->buflen - str->mark);
217                                 memmove(str->buffer + str->mark, buf, copycount);
218                                 str->mark += copycount;
219                         }
220
221                         str->write_count += len;
222                         break;
223                         
224                 case STREAM_TYPE_FILE:
225                         if(str->binary) 
226                         {
227                                 if(str->unicode) 
228                                 {
229                                         gchar *writebuffer = convert_latin1_to_ucs4be_string(buf, len);
230                                         fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
231                                         g_free(writebuffer);
232                                 } 
233                                 else /* Regular file */
234                                 {
235                                         fwrite(buf, sizeof(gchar), len, str->file_pointer);
236                                 }
237                         }
238                         else /* Text mode is the same for Unicode and regular files */
239                         {
240                                 gchar *utf8 = convert_latin1_to_utf8(buf, len);
241                                 if(utf8 != NULL)
242                                 {
243                                         g_fprintf(str->file_pointer, "%s", utf8);
244                                         g_free(utf8);
245                                 }
246                         }
247                         
248                         str->write_count += len;
249                         break;
250                 default:
251                         ILLEGAL_PARAM("Unknown stream type: %u", str->type);
252         }
253 }
254
255 /* Internal function: write a Unicode buffer with length to a stream. */
256 static void
257 write_buffer_to_stream_uni(strid_t str, glui32 *buf, glui32 len)
258 {
259         switch(str->type)
260         {
261                 case STREAM_TYPE_WINDOW:
262                         /* Each window type has a different way of printing to it */
263                         switch(str->window->type)
264                         {
265                                 /* Printing to these windows' streams does nothing */
266                                 case wintype_Blank:
267                                 case wintype_Pair:
268                                 case wintype_Graphics:
269                                         str->write_count += len;
270                                         break;
271                                         
272                             /* Text grid/buffer windows */
273                             case wintype_TextGrid:
274                             case wintype_TextBuffer:
275                             {
276                                 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
277                                 if(utf8 != NULL) {
278                                                 write_utf8_to_window_buffer(str->window, utf8);
279                                                 g_free(utf8);
280                                         }
281                                 }       
282                                         str->write_count += len;
283                                         break;
284                                 default:
285                                         ILLEGAL_PARAM("Unknown window type: %u", str->window->type);
286                         }
287                         
288                         /* Now write the same buffer to the window's echo stream */
289                         if(str->window->echo_stream != NULL)
290                                 write_buffer_to_stream_uni(str->window->echo_stream, buf, len);
291                         
292                         break;
293                         
294                 case STREAM_TYPE_MEMORY:
295                         if(str->unicode && str->ubuffer)
296                         {
297                                 int copycount = MIN(len, str->buflen - str->mark);
298                                 memmove(str->ubuffer + str->mark, buf, copycount * sizeof(glui32));
299                                 str->mark += copycount;
300                         }
301                         if(!str->unicode && str->buffer)
302                         {
303                                 gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
304                                 int copycount = MIN(len, str->buflen - str->mark);
305                                 memmove(str->buffer + str->mark, latin1, copycount);
306                                 g_free(latin1);
307                                 str->mark += copycount;
308                         }
309
310                         str->write_count += len;
311                         break;
312                         
313                 case STREAM_TYPE_FILE:
314                         if(str->binary) 
315                         {
316                                 if(str->unicode) 
317                                 {
318                                         gchar *writebuffer = convert_ucs4_to_ucs4be_string(buf, len);
319                                         fwrite(writebuffer, sizeof(gchar), len * 4, str->file_pointer);
320                                         g_free(writebuffer);
321                                 } 
322                                 else /* Regular file */
323                                 {
324                                         gchar *latin1 = convert_ucs4_to_latin1_binary(buf, len);
325                                         fwrite(latin1, sizeof(gchar), len, str->file_pointer);
326                                         g_free(latin1);
327                                 }
328                         }
329                         else /* Text mode is the same for Unicode and regular files */
330                         {
331                                 gchar *utf8 = convert_ucs4_to_utf8(buf, len);
332                                 if(utf8 != NULL) 
333                                 {
334                                         g_fprintf(str->file_pointer, "%s", utf8);
335                                         g_free(utf8);
336                                 }
337                         }
338                         
339                         str->write_count += len;
340                         break;
341                 default:
342                         ILLEGAL_PARAM("Unknown stream type: %u", str->type);
343         }
344 }
345
346 /**
347  * glk_put_char_stream:
348  * @str: An output stream.
349  * @ch: A character in Latin-1 encoding.
350  *
351  * The same as glk_put_char(), except that you specify a stream @str to print 
352  * to, instead of using the current stream. It is illegal for @str to be %NULL,
353  * or an input-only stream.
354  */
355 void
356 glk_put_char_stream(strid_t str, unsigned char ch)
357 {
358         VALID_STREAM(str, return);
359         g_return_if_fail(str->file_mode != filemode_Read);
360         
361         write_buffer_to_stream(str, (gchar *)&ch, 1);
362 }
363
364 /**
365  * glk_put_char_stream_uni:
366  * @str: An output stream.
367  * @ch: A Unicode code point.
368  *
369  * The same as glk_put_char_uni(), except that you specify a stream @str to
370  * print to, instead of using the current stream. It is illegal for @str to be 
371  * %NULL, or an input-only stream.
372  */
373 void
374 glk_put_char_stream_uni(strid_t str, glui32 ch)
375 {
376         VALID_STREAM(str, return);
377         g_return_if_fail(str->file_mode != filemode_Read);
378         
379         write_buffer_to_stream_uni(str, &ch, 1);
380 }
381
382 /**
383  * glk_put_string_stream:
384  * @str: An output stream.
385  * @s: A null-terminated string in Latin-1 encoding.
386  *
387  * The same as glk_put_string(), except that you specify a stream @str to print 
388  * to, instead of using the current stream. It is illegal for @str to be %NULL,
389  * or an input-only stream.
390  */
391 void
392 glk_put_string_stream(strid_t str, char *s)
393 {
394         VALID_STREAM(str, return);
395         if(*s == 0)
396                 return;
397
398         g_return_if_fail(str->file_mode != filemode_Read);
399
400         write_buffer_to_stream(str, s, strlen(s));
401 }
402
403 /**
404  * glk_put_string_stream_uni:
405  * @str: An output stream.
406  * @s: A null-terminated array of Unicode code points.
407  *
408  * The same as glk_put_string_uni(), except that you specify a stream @str to
409  * print to, instead of using the current stream. It is illegal for @str to be 
410  * %NULL, or an input-only stream.
411  */
412 void
413 glk_put_string_stream_uni(strid_t str, glui32 *s)
414 {
415         VALID_STREAM(str, return);
416         if(*s == 0)
417                 return;
418
419         g_return_if_fail(str->file_mode != filemode_Read);
420         
421         /* An impromptu strlen() for glui32 arrays */
422         glong len = 0;
423         glui32 *ptr = s;
424         while(*ptr++)
425                 len++;
426         write_buffer_to_stream_uni(str, s, len);
427 }
428
429 /**
430  * glk_put_buffer_stream:
431  * @str: An output stream.
432  * @buf: An array of characters in Latin-1 encoding.
433  * @len: Length of @buf.
434  *
435  * The same as glk_put_buffer(), except that you specify a stream @str to print 
436  * to, instead of using the current stream. It is illegal for @str to be %NULL,
437  * or an input-only stream.
438  */
439 void
440 glk_put_buffer_stream(strid_t str, char *buf, glui32 len)
441 {
442         VALID_STREAM(str, return);
443         if(len == 0)
444                 return;
445
446         g_return_if_fail(str->file_mode != filemode_Read);
447         
448         write_buffer_to_stream(str, buf, len);
449 }
450
451 /**
452  * glk_put_buffer_stream_uni:
453  * @str: An output stream.
454  * @buf: An array of Unicode code points.
455  * @len: Length of @buf.
456  *
457  * The same as glk_put_buffer_uni(), except that you specify a stream @str to
458  * print to, instead of using the current stream. It is illegal for @str to be 
459  * %NULL, or an input-only stream.
460  */
461 void
462 glk_put_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
463 {
464         VALID_STREAM(str, return);
465         if(len == 0)
466                 return;
467
468         g_return_if_fail(str->file_mode != filemode_Read);
469         
470         write_buffer_to_stream_uni(str, buf, len);
471 }
472
473 /*
474  *
475  **************** READING FUNCTIONS ********************************************
476  *
477  */
478
479 /* Internal function: Read one big-endian four-byte character from file fp and
480 return it as a Unicode code point, or -1 on EOF */
481 static glsi32
482 read_ucs4be_char_from_file(FILE *fp)
483 {
484         unsigned char readbuffer[4];
485         if(fread(readbuffer, sizeof(unsigned char), 4, fp) < 4)
486                 return -1; /* EOF */
487         return
488                 readbuffer[0] << 24 | 
489                 readbuffer[1] << 16 | 
490                 readbuffer[2] << 8  | 
491                 readbuffer[3];
492 }
493
494 /* Internal function: Read one UTF-8 character, which may be more than one byte,
495 from file fp and return it as a Unicode code point, or -1 on EOF */
496 static glsi32
497 read_utf8_char_from_file(FILE *fp)
498 {
499         gchar readbuffer[4] = {0, 0, 0, 0}; /* Max UTF-8 width */
500         int foo;
501         gunichar charresult = (gunichar)-2;
502         for(foo = 0; foo < 4 && charresult == (gunichar)-2; foo++) 
503         {
504                 int ch = fgetc(fp);
505                 if(ch == EOF)
506                         return -1;
507                 readbuffer[foo] = (gchar)ch;
508                 charresult = g_utf8_get_char_validated(readbuffer, foo + 1);
509                 /* charresult is -1 if invalid, -2 if incomplete, and the unicode code
510                 point otherwise */
511         }
512         /* Silently return unknown characters as 0xFFFD, Replacement Character */
513         if(charresult == (gunichar)-1 || charresult == (gunichar)-2) 
514                 return 0xFFFD;
515         return charresult;
516 }
517
518 /* Internal function: Tell whether this code point is a Unicode newline. The
519 file pointer and eight-bit flag are included in case the newline is a CR 
520 (U+000D). If the next character is LF (U+000A) then it also belongs to the
521 newline. */
522 static gboolean
523 is_unicode_newline(glsi32 ch, FILE *fp, gboolean utf8)
524 {
525         if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
526                 return TRUE;
527         if(ch == 0x0D) {
528                 glsi32 ch2 = utf8? read_utf8_char_from_file(fp) : 
529                         read_ucs4be_char_from_file(fp);
530                 if(ch2 != 0x0A)
531                         if(fseek(fp, utf8? -1 : -4, SEEK_CUR) == -1);
532                                 WARNING_S("Seek failed on stream", g_strerror(errno) );
533                 return TRUE;
534         }
535         return FALSE;
536 }
537
538 /* Internal function: Read one character from a stream. Returns a value which
539  can be returned unchanged by glk_get_char_stream_uni(), but 
540  glk_get_char_stream() must replace high values by the placeholder character. */
541 static glsi32
542 get_char_stream_common(strid_t str)
543 {
544         switch(str->type)
545         {
546                 case STREAM_TYPE_MEMORY:
547                         if(str->unicode)
548                         {
549                                 if(!str->ubuffer || str->mark >= str->buflen)
550                                         return -1;
551                                 glui32 ch = str->ubuffer[str->mark++];
552                                 str->read_count++;
553                                 return ch;
554                         }
555                         else
556                         {
557                                 if(!str->buffer || str->mark >= str->buflen)
558                                         return -1;
559                                 unsigned char ch = str->buffer[str->mark++];
560                                 str->read_count++;
561                                 return ch;
562                         }
563                         break;
564                         
565                 case STREAM_TYPE_FILE:
566                         if(str->binary) 
567                         {
568                                 if(str->unicode) 
569                                 {
570                                         glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
571                                         if(ch == -1)
572                                                 return -1;
573                                         str->read_count++;
574                                         return ch;
575                                 }
576                                 else /* Regular file */
577                                 {
578                                         int ch = fgetc(str->file_pointer);
579                                         if(ch == EOF)
580                                                 return -1;
581                                         
582                                         str->read_count++;
583                                         return ch;
584                                 }
585                         }
586                         else /* Text mode is the same for Unicode and regular files */
587                         {
588                                 glsi32 ch = read_utf8_char_from_file(str->file_pointer);
589                                 if(ch == -1)
590                                         return -1;
591                                         
592                                 str->read_count++;
593                                 return ch;
594                         }
595                 default:
596                         ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
597                         return -1;
598         }
599 }
600
601 /**
602  * glk_get_char_stream:
603  * @str: An input stream.
604  *
605  * Reads one character from the stream @str. (There is no notion of a
606  * <quote>current input stream.</quote>) It is illegal for @str to be %NULL, or
607  * an output-only stream.
608  *
609  * The result will be between 0 and 255. As with all basic text functions, Glk
610  * assumes the Latin-1 encoding. See <link 
611  * linkend="chimara-Character-Encoding">Character Encoding</link>. If the end
612  * of the stream has been reached, the result will be -1. 
613  *
614  * <note><para>
615  *   Note that high-bit characters (128..255) are <emphasis>not</emphasis>
616  *   returned as negative numbers.
617  * </para></note>
618  *
619  * If the stream contains Unicode data &mdash; for example, if it was created
620  * with glk_stream_open_file_uni() or glk_stream_open_memory_uni() &mdash; then
621  * characters beyond 255 will be returned as 0x3F (<code>"?"</code>).
622  *
623  * It is usually more efficient to read several characters at once with
624  * glk_get_buffer_stream() or glk_get_line_stream(), as opposed to calling
625  * glk_get_char_stream() several times.
626  *
627  * Returns: A character value between 0 and 255, or -1 on end of stream.
628  */
629 glsi32
630 glk_get_char_stream(strid_t str)
631 {
632         VALID_STREAM(str, return -1);
633         g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
634         
635         glsi32 ch = get_char_stream_common(str);
636         return (ch > 0xFF)? PLACEHOLDER : ch;
637 }
638
639 /**
640  * glk_get_char_stream_uni:
641  * @str: An input stream.
642  *
643  * Reads one character from the stream @str. The result will be between 0 and 
644  * 0x7FFFFFFF. If the end of the stream has been reached, the result will be -1.
645  *
646  * Returns: A value between 0 and 0x7FFFFFFF, or -1 on end of stream.
647  */
648 glsi32
649 glk_get_char_stream_uni(strid_t str)
650 {
651         VALID_STREAM(str, return -1);
652         g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, -1);
653         
654         return get_char_stream_common(str);
655 }
656
657 /**
658  * glk_get_buffer_stream:
659  * @str: An input stream.
660  * @buf: A buffer with space for at least @len characters.
661  * @len: The number of characters to read.
662  *
663  * Reads @len characters from @str, unless the end of stream is reached first.
664  * No terminal null is placed in the buffer.
665  *
666  * Returns: The number of characters actually read.
667  */
668 glui32
669 glk_get_buffer_stream(strid_t str, char *buf, glui32 len)
670 {
671         VALID_STREAM(str, return 0);
672         g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
673         g_return_val_if_fail(buf != NULL, 0);
674         
675         switch(str->type)
676         {
677                 case STREAM_TYPE_MEMORY:
678                 {
679                         int copycount = 0;
680                         if(str->unicode)
681                         {
682                                 while(copycount < len && str->ubuffer && str->mark < str->buflen) 
683                                 {
684                                         glui32 ch = str->ubuffer[str->mark++];
685                                         buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
686                                 }
687                         }
688                         else
689                         {
690                                 if(str->buffer) /* if not, copycount stays 0 */
691                                         copycount = MIN(len, str->buflen - str->mark);
692                                 memmove(buf, str->buffer + str->mark, copycount);
693                                 str->mark += copycount;
694                         }
695
696                         str->read_count += copycount;           
697                         return copycount;
698                 }       
699                 case STREAM_TYPE_FILE:
700                         if(str->binary) 
701                         {
702                                 if(str->unicode) /* Binary file with 4-byte characters */
703                                 {
704                                         /* Read len characters of 4 bytes each */
705                                         unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
706                                         size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
707                                         /* If there was an incomplete character */
708                                         if(count % 4 != 0) 
709                                         {
710                                                 count -= count % 4;
711                                                 WARNING("Incomplete character in binary Unicode file");
712                                         }
713                                         
714                                         int foo;
715                                         for(foo = 0; foo < count; foo += 4)
716                                         {
717                                                 glsi32 ch = readbuffer[foo] << 24
718                                                         | readbuffer[foo + 1] << 16
719                                                         | readbuffer[foo + 2] << 8
720                                                         | readbuffer[foo + 3];
721                                                 buf[foo / 4] = (ch > 255)? 0x3F : (char)ch;
722                                         }
723                                         g_free(readbuffer);
724                                         str->read_count += count / 4;
725                                         return count / 4;
726                                 }
727                                 else /* Regular binary file */
728                                 {
729                                         size_t count = fread(buf, sizeof(char), len, str->file_pointer);
730                                         str->read_count += count;
731                                         return count;
732                                 }
733                         }
734                         else /* Text mode is the same for Unicode and regular files */
735                         {
736                                 /* Do it character-by-character */
737                                 int foo;
738                                 for(foo = 0; foo < len; foo++)
739                                 {
740                                         glsi32 ch = read_utf8_char_from_file(str->file_pointer);
741                                         if(ch == -1)
742                                                 break;
743                                         str->read_count++;
744                                         buf[foo] = (ch > 0xFF)? 0x3F : (gchar)ch;
745                                 }
746                                 return foo;
747                         }
748                 default:
749                         ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
750                         return 0;
751         }
752 }
753
754 /**
755  * glk_get_buffer_stream_uni:
756  * @str: An input stream.
757  * @buf: A buffer with space for at least @len Unicode code points.
758  * @len: The number of characters to read.
759  *
760  * Reads @len Unicode characters from @str, unless the end of stream is reached 
761  * first. No terminal null is placed in the buffer.
762  *
763  * Returns: The number of Unicode characters actually read.
764  */
765 glui32
766 glk_get_buffer_stream_uni(strid_t str, glui32 *buf, glui32 len)
767 {
768         VALID_STREAM(str, return 0);
769         g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
770         g_return_val_if_fail(buf != NULL, 0);
771         
772         switch(str->type)
773         {
774                 case STREAM_TYPE_MEMORY:
775                 {
776                         int copycount = 0;
777                         if(str->unicode)
778                         {
779                                 if(str->ubuffer) /* if not, copycount stays 0 */
780                                         copycount = MIN(len, str->buflen - str->mark);
781                                 memmove(buf, str->ubuffer + str->mark, copycount * 4);
782                                 str->mark += copycount;
783                         }
784                         else
785                         {
786                                 while(copycount < len && str->buffer && str->mark < str->buflen)
787                                 {
788                                         unsigned char ch = str->buffer[str->mark++];
789                                         buf[copycount++] = ch;
790                                 }
791                         }
792
793                         str->read_count += copycount;           
794                         return copycount;
795                 }       
796                 case STREAM_TYPE_FILE:
797                         if(str->binary) 
798                         {
799                                 if(str->unicode) /* Binary file with 4-byte characters */
800                                 {
801                                         /* Read len characters of 4 bytes each */
802                                         unsigned char *readbuffer = g_new0(unsigned char, 4 * len);
803                                         size_t count = fread(readbuffer, sizeof(unsigned char), 4 * len, str->file_pointer);
804                                         /* If there was an incomplete character */
805                                         if(count % 4 != 0) 
806                                         {
807                                                 count -= count % 4;
808                                                 WARNING("Incomplete character in binary Unicode file");
809                                         }
810                                         
811                                         int foo;
812                                         for(foo = 0; foo < count; foo += 4)
813                                                 buf[foo / 4] = readbuffer[foo] << 24
814                                                         | readbuffer[foo + 1] << 16
815                                                         | readbuffer[foo + 2] << 8
816                                                         | readbuffer[foo + 3];
817                                         g_free(readbuffer);
818                                         str->read_count += count / 4;
819                                         return count / 4;
820                                 }
821                                 else /* Regular binary file */
822                                 {
823                                         unsigned char *readbuffer = g_new0(unsigned char, len);
824                                         size_t count = fread(readbuffer, sizeof(unsigned char), len, str->file_pointer);
825                                         int foo;
826                                         for(foo = 0; foo < count; foo++)
827                                                 buf[foo] = readbuffer[foo];
828                                         g_free(readbuffer);
829                                         str->read_count += count;
830                                         return count;
831                                 }
832                         }
833                         else /* Text mode is the same for Unicode and regular files */
834                         {
835                                 /* Do it character-by-character */
836                                 int foo;
837                                 for(foo = 0; foo < len; foo++)
838                                 {
839                                         glsi32 ch = read_utf8_char_from_file(str->file_pointer);
840                                         if(ch == -1)
841                                                 break;
842                                         str->read_count++;
843                                         buf[foo] = ch;
844                                 }
845                                 return foo;
846                         }
847                 default:
848                         ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
849                         return 0;
850         }
851 }
852
853 /**
854  * glk_get_line_stream:
855  * @str: An input stream.
856  * @buf: A buffer with space for at least @len characters.
857  * @len: The number of characters to read, plus one.
858  *
859  * Reads characters from @str, until either 
860  * <inlineequation>
861  *   <alt>@len - 1</alt>
862  *   <mathphrase>@len - 1</mathphrase>
863  * </inlineequation>
864  * characters have been read or a newline has been read. It then puts a
865  * terminal null (<code>'\0'</code>) aracter on
866  * the end. It returns the number of characters actually read, including the
867  * newline (if there is one) but not including the terminal null.
868  *
869  * Returns: The number of characters actually read.
870  */
871 glui32
872 glk_get_line_stream(strid_t str, char *buf, glui32 len)
873 {
874         VALID_STREAM(str, return 0);
875         g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
876         g_return_val_if_fail(buf != NULL, 0);
877
878         switch(str->type)
879         {
880                 case STREAM_TYPE_MEMORY:
881                 {
882                         int copycount = 0;
883                         if(str->unicode)
884                         {
885                                 /* Do it character-by-character */
886                                 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen) 
887                                 {
888                                         glui32 ch = str->ubuffer[str->mark++];
889                                         /* Check for Unicode newline; slightly different than
890                                         in file streams */
891                                         if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
892                                         {
893                                                 buf[copycount++] = '\n';
894                                                 break;
895                                         }
896                                         if(ch == 0x0D)
897                                         {
898                                                 if(str->ubuffer[str->mark] == 0x0A)
899                                                         str->mark++; /* skip past next newline */
900                                                 buf[copycount++] = '\n';
901                                                 break;
902                                         }
903                                         buf[copycount++] = (ch > 0xFF)? '?' : (char)ch;
904                                 }
905                                 buf[copycount] = '\0';
906                         }
907                         else
908                         {
909                                 if(str->buffer) /* if not, copycount stays 0 */
910                                         copycount = MIN(len - 1, str->buflen - str->mark);
911                                 char *endptr = memccpy(buf, str->buffer + str->mark, '\n', copycount);
912                                 if(endptr) /* newline was found */
913                                         copycount = endptr - buf; /* Real copy count */
914                                 buf[copycount] = '\0';
915                                 str->mark += copycount;
916                         }
917                         
918                         str->read_count += copycount;
919                         return copycount;
920                 }       
921                 case STREAM_TYPE_FILE:
922                         if(str->binary) 
923                         {
924                                 if(str->unicode) /* Binary file with 4-byte characters */
925                                 {
926                                         /* Do it character-by-character */
927                                         int foo;
928                                         for(foo = 0; foo < len - 1; foo++)
929                                         {
930                                                 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
931                                                 if(ch == -1) 
932                                                 {
933                                                         buf[foo] = '\0';
934                                                         return foo - 1;
935                                                 }
936                                                 str->read_count++;
937                                                 if(is_unicode_newline(ch, str->file_pointer, FALSE))
938                                                 {
939                                                         buf[foo] = '\n';
940                                                         buf[foo + 1] = '\0';
941                                                         return foo;
942                                                 }
943                                                 buf[foo] = (ch > 0xFF)? '?' : (char)ch;
944                                         }
945                                         buf[len] = '\0';
946                                         return foo;
947                                 }
948                                 else /* Regular binary file */
949                                 {
950                                         if( !fgets(buf, len, str->file_pointer) ) {
951                                                 *buf = 0;
952                                                 return 0;
953                                         }
954
955                                         int nread = strlen(buf);
956                                         str->read_count += nread;
957                                         return nread;
958                                 }
959                         }
960                         else /* Text mode is the same for Unicode and regular files */
961                         {
962                                 /* Do it character-by-character */
963                                 int foo;
964                                 for(foo = 0; foo < len - 1; foo++)
965                                 {
966                                         glsi32 ch = read_utf8_char_from_file(str->file_pointer);
967                                         if(ch == -1)
968                                         {
969                                                 buf[foo] = '\0';
970                                                 return foo - 1;
971                                         }
972                                         str->read_count++;
973                                         if(is_unicode_newline(ch, str->file_pointer, TRUE))
974                                         {
975                                                 buf[foo] = '\n';
976                                                 buf[foo + 1] = '\0';
977                                                 return foo;
978                                         }
979                                         buf[foo] = (ch > 0xFF)? 0x3F : (char)ch;
980                                 }
981                                 buf[len] = '\0';
982                                 return foo;
983                         }
984                 default:
985                         ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
986                         return 0;
987         }
988 }
989
990 /**
991  * glk_get_line_stream_uni:
992  * @str: An input stream.
993  * @buf: A buffer with space for at least @len Unicode code points.
994  * @len: The number of characters to read, plus one.
995  *
996  * Reads Unicode characters from @str, until either 
997  * <inlineequation>
998  *   <alt>@len - 1</alt>
999  *   <mathphrase>@len - 1</mathphrase>
1000  * </inlineequation> 
1001  * Unicode characters have been read or a newline has been read. It then puts a
1002  * terminal null (a zero value) on the end.
1003  *
1004  * Returns: The number of characters actually read, including the newline (if
1005  * there is one) but not including the terminal null.
1006  */
1007 glui32
1008 glk_get_line_stream_uni(strid_t str, glui32 *buf, glui32 len)
1009 {
1010         VALID_STREAM(str, return 0);
1011         g_return_val_if_fail(str->file_mode == filemode_Read || str->file_mode == filemode_ReadWrite, 0);
1012         g_return_val_if_fail(buf != NULL, 0);
1013
1014         switch(str->type)
1015         {
1016                 case STREAM_TYPE_MEMORY:
1017                 {
1018                         int copycount = 0;
1019                         if(str->unicode)
1020                         {
1021                                 /* Do it character-by-character */
1022                                 while(copycount < len - 1 && str->ubuffer && str->mark < str->buflen) 
1023                                 {
1024                                         glui32 ch = str->ubuffer[str->mark++];
1025                                         /* Check for Unicode newline; slightly different than
1026                                         in file streams */
1027                                         if(ch == 0x0A || ch == 0x85 || ch == 0x0C || ch == 0x2028 || ch == 0x2029)
1028                                         {
1029                                                 buf[copycount++] = '\n';
1030                                                 break;
1031                                         }
1032                                         if(ch == 0x0D)
1033                                         {
1034                                                 if(str->ubuffer[str->mark] == 0x0A)
1035                                                         str->mark++; /* skip past next newline */
1036                                                 buf[copycount++] = '\n';
1037                                                 break;
1038                                         }
1039                                         buf[copycount++] = ch;
1040                                 }
1041                                 buf[copycount] = '\0';
1042                         }
1043                         else
1044                         {
1045                                 /* No recourse to memccpy(), so do it character-by-character */
1046                                 while(copycount < len - 1 && str->buffer && str->mark < str->buflen)
1047                                 {
1048                                         gchar ch = str->buffer[str->mark++];
1049                                         /* Check for newline */
1050                                         if(ch == '\n') /* Also check for \r and \r\n? */
1051                                         {
1052                                                 buf[copycount++] = '\n';
1053                                                 break;
1054                                         }
1055                                         buf[copycount++] = (unsigned char)ch;
1056                                 }
1057                                 buf[copycount] = 0;
1058                         }
1059                         
1060                         str->read_count += copycount;
1061                         return copycount;
1062                 }       
1063                 case STREAM_TYPE_FILE:
1064                         if(str->binary) 
1065                         {
1066                                 if(str->unicode) /* Binary file with 4-byte characters */
1067                                 {
1068                                         /* Do it character-by-character */
1069                                         int foo;
1070                                         for(foo = 0; foo < len - 1; foo++)
1071                                         {
1072                                                 glsi32 ch = read_ucs4be_char_from_file(str->file_pointer);
1073                                                 if(ch == -1) 
1074                                                 {
1075                                                         buf[foo] = 0;
1076                                                         return foo - 1;
1077                                                 }
1078                                                 str->read_count++;
1079                                                 if(is_unicode_newline(ch, str->file_pointer, FALSE))
1080                                                 {
1081                                                         buf[foo] = ch; /* Preserve newline types??? */
1082                                                         buf[foo + 1] = 0;
1083                                                         return foo;
1084                                                 }
1085                                                 buf[foo] = ch;
1086                                         }
1087                                         buf[len] = 0;
1088                                         return foo;
1089                                 }
1090                                 else /* Regular binary file */
1091                                 {
1092                                         gchar *readbuffer = g_new0(gchar, len);
1093                                         if( !fgets(readbuffer, len, str->file_pointer) ) {
1094                                                 *buf = 0;
1095                                                 return 0;
1096                                         }
1097
1098                                         glui32 count = strlen(readbuffer);
1099                                         int foo;
1100                                         for(foo = 0; foo < count + 1; foo++) /* Copy terminator */
1101                                                 buf[foo] = (unsigned char)(readbuffer[foo]);
1102                                         str->read_count += count;
1103                                         return count;
1104                                 }
1105                         }
1106                         else /* Text mode is the same for Unicode and regular files */
1107                         {
1108                                 /* Do it character-by-character */
1109                                 int foo;
1110                                 for(foo = 0; foo < len - 1; foo++)
1111                                 {
1112                                         glsi32 ch = read_utf8_char_from_file(str->file_pointer);
1113                                         if(ch == -1)
1114                                         {
1115                                                 buf[foo] = 0;
1116                                                 return foo - 1;
1117                                         }
1118                                         str->read_count++;
1119                                         if(is_unicode_newline(ch, str->file_pointer, TRUE))
1120                                         {
1121                                                 buf[foo] = ch; /* Preserve newline types??? */
1122                                                 buf[foo + 1] = 0;
1123                                                 return foo;
1124                                         }
1125                                         buf[foo] = ch;
1126                                 }
1127                                 buf[len] = 0;
1128                                 return foo;
1129                         }
1130                 default:
1131                         ILLEGAL_PARAM("Reading illegal on stream type: %u", str->type);
1132                         return 0;
1133         }
1134 }
1135
1136 /*
1137  *
1138  **************** SEEKING FUNCTIONS ********************************************
1139  *
1140  */
1141
1142 /**
1143  * glk_stream_get_position:
1144  * @str: A file or memory stream.
1145  *
1146  * Returns the position of the read/write mark in @str. For memory streams and
1147  * binary file streams, this is exactly the number of characters read or written
1148  * from the beginning of the stream (unless you have moved the mark with
1149  * glk_stream_set_position().) For text file streams, matters are more 
1150  * ambiguous, since (for example) writing one byte to a text file may store more
1151  * than one character in the platform's native encoding. You can only be sure
1152  * that the position increases as you read or write to the file.
1153  *
1154  * Additional complication: for Latin-1 memory and file streams, a character is
1155  * a byte. For Unicode memory and file streams (those created by
1156  * glk_stream_open_file_uni() and glk_stream_open_memory_uni()), a character is
1157  * a 32-bit word. So in a binary Unicode file, positions are multiples of four
1158  * bytes.
1159  *
1160  * <note><para>
1161  *   If this bothers you, don't use binary Unicode files. I don't think they're
1162  *   good for much anyhow.
1163  * </para></note>
1164  *
1165  * Returns: position of the read/write mark in @str.
1166  */
1167 glui32
1168 glk_stream_get_position(strid_t str)
1169 {
1170         VALID_STREAM(str, return 0);
1171         
1172         switch(str->type)
1173         {
1174                 case STREAM_TYPE_MEMORY:
1175                         return str->mark;
1176                 case STREAM_TYPE_FILE:
1177                         return ftell(str->file_pointer);
1178                 default:
1179                         ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1180                         return 0;
1181         }
1182 }
1183
1184 /**
1185  * glk_stream_set_position:
1186  * @str: A file or memory stream.
1187  * @pos: The position to set the mark to, relative to @seekmode.
1188  * @seekmode: One of %seekmode_Start, %seekmode_Current, or %seekmode_End.
1189  *
1190  * Sets the position of the read/write mark in @str. The position is controlled
1191  * by @pos, and the meaning of @pos is controlled by @seekmode. See the
1192  * <code>seekmode_</code> constants below.
1193  *
1194  * It is illegal to specify a position before the beginning or after the end of
1195  * the file.
1196  *
1197  * In binary files, the mark position is exact &mdash; it corresponds with the
1198  * number of characters you have read or written. In text files, this mapping 
1199  * can vary, because of linefeed conventions or other character-set 
1200  * approximations. See <link linkend="chimara-Streams">Streams</link>.
1201  * glk_stream_set_position() and glk_stream_get_position() measure positions in
1202  * the platform's native encoding &mdash; after character cookery. Therefore,
1203  * in a text stream, it is safest to use glk_stream_set_position() only to move
1204  * to the beginning or end of a file, or to a position determined by
1205  * glk_stream_get_position().
1206  *
1207  * Again, in Latin-1 streams, characters are bytes. In Unicode streams,
1208  * characters are 32-bit words, or four bytes each.
1209  */
1210 void
1211 glk_stream_set_position(strid_t str, glsi32 pos, glui32 seekmode)
1212 {
1213         VALID_STREAM(str, return);
1214         g_return_if_fail(!(seekmode == seekmode_Start && pos < 0));
1215         g_return_if_fail(!(seekmode == seekmode_End && pos > 0));
1216         
1217         switch(str->type)
1218         {
1219                 case STREAM_TYPE_MEMORY:
1220                         switch(seekmode)
1221                         {
1222                                 case seekmode_Start:   str->mark = pos;  break;
1223                                 case seekmode_Current: str->mark += pos; break;
1224                                 case seekmode_End:     str->mark = str->buflen + pos; break;
1225                                 default:
1226                                         g_return_if_reached();
1227                                         return;
1228                         }
1229                         break;
1230                 case STREAM_TYPE_FILE:
1231                 {
1232                         int whence;
1233                         switch(seekmode)
1234                         {
1235                                 case seekmode_Start:   whence = SEEK_SET; break;
1236                                 case seekmode_Current: whence = SEEK_CUR; break;
1237                                 case seekmode_End:     whence = SEEK_END; break;
1238                                 default:
1239                                         g_return_if_reached();
1240                                         return;
1241                         }
1242                         if(fseek(str->file_pointer, pos, whence) == -1)
1243                                 WARNING("Seek failed on file stream");
1244                         break;
1245                 }
1246                 default:
1247                         ILLEGAL_PARAM("Seeking illegal on stream type: %u", str->type);
1248                         return;
1249         }
1250 }
1251