File: redirect.c

package info (click to toggle)
frotz 2.32r2-10
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 324 kB
  • ctags: 680
  • sloc: ansic: 5,206; makefile: 93; sh: 16
file content (159 lines) | stat: -rw-r--r-- 2,546 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * redirect.c
 *
 * Output redirection to Z-machine memory
 *
 */

#include "frotz.h"

#define MAX_NESTING 16

extern zword get_max_width (zword);

static depth = -1;

static struct {
    zword xsize;
    zword table;
    zword width;
    zword total;
} redirect[MAX_NESTING];

/*
 * memory_open
 *
 * Begin output redirection to the memory of the Z-machine.
 *
 */

void memory_open (zword table, zword xsize, bool buffering)
{

    if (++depth < MAX_NESTING) {

	if (!buffering)
	    xsize = 0xffff;
	if (buffering && (short) xsize <= 0)
	    xsize = get_max_width ((zword) (- (short) xsize));

	storew (table, 0);

	redirect[depth].table = table;
	redirect[depth].width = 0;
	redirect[depth].total = 0;
	redirect[depth].xsize = xsize;

	ostream_memory = TRUE;

   } else runtime_error ("Nesting stream #3 too deep");

}/* memory_open */

/*
 * memory_new_line
 *
 * Redirect a newline to the memory of the Z-machine.
 *
 */

void memory_new_line (void)
{
    zword size;
    zword addr;

    redirect[depth].total += redirect[depth].width;
    redirect[depth].width = 0;

    addr = redirect[depth].table;

    LOW_WORD (addr, size)
    addr += 2;

    if (redirect[depth].xsize != 0xffff) {

	redirect[depth].table = addr + size;
	size = 0;

    } else storeb ((zword) (addr + (size++)), 13);

    storew (redirect[depth].table, size);

}/* memory_new_line */

/*
 * memory_word
 *
 * Redirect a string of characters to the memory of the Z-machine.
 *
 */

void memory_word (const zchar *s)
{
    zword size;
    zword addr;
    zchar c;

    if (h_version == V6) {

	int width = os_string_width (s);

	if (redirect[depth].xsize != 0xffff)

	    if (redirect[depth].width + width > redirect[depth].xsize) {

		if (*s == ' ' || *s == ZC_INDENT || *s == ZC_GAP)
		    width = os_string_width (++s);

		memory_new_line ();

	    }

	redirect[depth].width += width;

    }

    addr = redirect[depth].table;

    LOW_WORD (addr, size)
    addr += 2;

    while ((c = *s++) != 0)
	storeb ((zword) (addr + (size++)), translate_to_zscii (c));

    storew (redirect[depth].table, size);

}/* memory_word */

/*
 * memory_close
 *
 * End of output redirection.
 *
 */

void memory_close (void)
{

    if (depth >= 0) {

	if (redirect[depth].xsize != 0xffff)
	    memory_new_line ();

	if (h_version == V6) {

	    h_line_width = (redirect[depth].xsize != 0xffff) ?
		redirect[depth].total : redirect[depth].width;

	    SET_WORD (H_LINE_WIDTH, h_line_width)

	}

	if (depth == 0)
	    ostream_memory = FALSE;

	depth--;

    }

}/* memory_close */