-
Notifications
You must be signed in to change notification settings - Fork 0
/
rr-send.5c
226 lines (204 loc) · 6.41 KB
/
rr-send.5c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* $Id: send.5c,v 1.7 2003/05/31 16:00:09 keithp Exp $
*
* Copyright © 2003 Keith Packard
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Keith Packard not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Keith Packard makes no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
autoload RR
extend namespace RR {
public namespace Send {
bool needsquote (string s) {
int len = String::length (s);
if (len == 0)
return true;
for (int i = 0; i < len; i++)
if (Ctype::isblank (s[i]) || s[i] == '"' || s[i] == '\\')
return true;
return false;
}
public exception too_few_arguments (string format, poly[] args);
public exception too_many_arguments (string format, poly[] args);
public void send (file f, string format, poly args...)
{
void put_string (string s) {
void quote (string s) {
int i;
File::putc ('"', f);
for (i = 0; i < String::length (s); i++)
{
if (s[i] == '"' || s[i] == '\\')
File::putc ('\\', f);
File::putc (s[i], f);
}
File::putc ('"', f);
}
if (needsquote (s))
quote (s);
else
File::fprintf (f, "%s", s);
}
void put_color (Color c) {
switch (c) {
case Color.Red: File::fprintf (f, "red"); break;
case Color.Yellow: File::fprintf (f, "yellow"); break;
case Color.Green: File::fprintf (f, "green"); break;
case Color.Blue: File::fprintf (f, "blue"); break;
case Color.Whirl: File::fprintf (f, "whirl"); break;
}
}
void put_shape (Shape c) {
switch (c) {
case Shape.Triangle: File::fprintf (f, "triangle"); break;
case Shape.Square: File::fprintf (f, "square"); break;
case Shape.Octagon: File::fprintf (f, "octagon"); break;
case Shape.Circle: File::fprintf (f, "circle"); break;
case Shape.Whirl: File::fprintf (f, "whirl"); break;
}
}
void put_direction (Direction d) {
switch (d) {
case Direction.North: File::fprintf (f, "north"); break;
case Direction.East: File::fprintf (f, "east"); break;
case Direction.South: File::fprintf (f, "south"); break;
case Direction.West: File::fprintf (f, "west"); break;
}
}
void put_gamestate (GameState s) {
switch (s) {
case GameState.NEW: File::fprintf (f, "NEW"); break;
case GameState.BID: File::fprintf (f, "BID"); break;
case GameState.SHOW: File::fprintf (f, "SHOW"); break;
case GameState.DONE: File::fprintf (f, "DONE"); break;
}
}
void put_error (Error e) {
switch (e) {
case Error.NOGAME: File::fprintf (f, "NOGAME"); break;
case Error.NOUSER: File::fprintf (f, "NOUSER"); break;
case Error.NOTINGAME: File::fprintf (f, "NOTINGAME"); break;
case Error.NOTPLAYING: File::fprintf (f, "NOTPLAYING"); break;
case Error.NOTBIDDING: File::fprintf (f, "NOTBIDDING"); break;
case Error.NOTLOWER: File::fprintf (f, "NOTLOWER"); break;
case Error.NOBID: File::fprintf (f, "NOBID"); break;
case Error.NOTACTIVE: File::fprintf (f, "NOTACTIVE"); break;
case Error.NOTDONE: File::fprintf (f, "NOTDONE"); break;
case Error.NOTNUMBER: File::fprintf (f, "NOTNUMBER"); break;
case Error.BLOCKED: File::fprintf (f, "BLOCKED"); break;
case Error.COMMAND: File::fprintf (f, "COMMAND"); break;
case Error.SYNTAX: File::fprintf (f, "SYNTAX"); break;
case Error.NOTCOLOR: File::fprintf (f, "NOTCOLOR"); break;
case Error.NOTSHAPE: File::fprintf (f, "NOTSHAPE"); break;
case Error.NOTDIRECTION:File::fprintf (f, "NOTDIRECTION"); break;
case Error.TOOMANYMOVES:File::fprintf (f, "TOOMANYMOVES"); break;
case Error.NONAMESET: File::fprintf (f, "NONAMESET"); break;
case Error.INVALIDNAME: File::fprintf (f, "INVALIDNAME"); break;
}
}
void put_number (int n) {
File::fprintf (f, "%d", n);
}
void put_bool (bool b) {
File::fprintf (f, "%s", b ? "true" : "false");
}
/*
* A private namespace to walk the format string
*/
namespace fmt {
int i = 0;
int len = String::length(format);
/*
* Return whether the end of the string has been reached
*/
public bool done () {
return i >= len;
}
/*
* Return current character, raising an exception if
* the end of the string has been reached
*/
public int c() {
if (done())
raise invalid_argument ("invalid format", 1, format);
return format[i];
}
/*
* Step to next character
*/
public void step () {
++i;
}
/*
* Return true and step if current character matches 'm'
*/
public bool match(int m) {
if (c() == m)
{
step();
return true;
}
return false;
}
/*
* Return a number from the format string
*/
public int number () {
int n = 0;
while ('0' <= c() && c() <= '9')
{
n = n * 10 + c() - '0';
step();
}
return n;
}
}
namespace arg {
int n = 0;
public poly next () {
if (n == dim(args))
raise too_few_arguments (format, args);
return args[n++];
}
public void done () {
if (n != dim(args))
raise too_many_arguments (format, args);
}
}
while (!fmt::done () && !File::error (f)) {
if (fmt::match ('%') && fmt::c() != '%')
{
poly a = arg::next ();
switch (fmt::c()) {
case 's': put_string (a); break;
case 'd': put_number (a); break;
case 'b': put_bool (a); break;
case 'C': put_color (a); break;
case 'D': put_direction (a); break;
case 'S': put_shape (a); break;
case 'G': put_gamestate (a); break;
case 'E': put_error (a); break;
}
}
else
File::putc (fmt::c(), f);
fmt::step ();
}
}
}
}