File: io.c

package info (click to toggle)
kgames 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,596 kB
  • sloc: ansic: 31,589; yacc: 1,067; lex: 216; sh: 149; makefile: 9
file content (227 lines) | stat: -rw-r--r-- 3,892 bytes parent folder | download | duplicates (3)
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
227
/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

# include	<ctype.h>
# include	<stdarg.h>
# include	<stdio.h>
# include	"deck.h"
# include	"cribbage.h"

# define	LINESIZE		128

char		linebuf[ LINESIZE ];

char		*rankname[ RANKS ]	= { "ACE", "TWO", "THREE", "FOUR",
					    "FIVE", "SIX", "SEVEN", "EIGHT",
					    "NINE", "TEN", "JACK", "QUEEN",
					    "KING" };

char            *rankchar[ RANKS ]      = { "A", "2", "3", "4", "5", "6", "7",
					    "8", "9", "T", "J", "Q", "K" };

char            *suitname[ SUITS ]      = { "SPADES", "HEARTS", "DIAMONDS",
					    "CLUBS" };

char            *suitchar[ SUITS ]      = { "S", "H", "D", "C" };


/*
 * msgcard:
 *	Call msgcrd in one of two forms
 */
int
msgcard(CARD c, BOOLEAN brief)
{
	if (brief)
		return msgcrd(c, TRUE, (char *) NULL, TRUE);
	else
		return msgcrd(c, FALSE, " of ", FALSE);
}



/*
 * msgcrd:
 *	Print the value of a card in ascii
 */
int
msgcrd(CARD c, BOOLEAN brfrank, char *mid, BOOLEAN brfsuit)
{
	if (c.rank == EMPTY || c.suit == EMPTY)
	    return FALSE;
	if (brfrank)
	    addmsg("%1.1s", rankchar[c.rank]);
	else
	    addmsg(rankname[c.rank]);
	if (mid != NULL)
	    addmsg(mid);
	if (brfsuit)
	    addmsg("%1.1s", suitchar[c.suit]);
	else
	    addmsg(suitname[c.suit]);
	return TRUE;
}

/*
 * getuchar:
 *	Reads and converts to upper case
 */
int
getuchar(void)
{
	register int		c;

	c = UIReadChar ();
	if (islower(c))
	    c = toupper(c);
	UIEchoChar (c);
	return c;
}

/*
 * number:
 *	Reads in a decimal number and makes sure it is between "lo" and
 *	"hi" inclusive.
 */
int
number(int lo, int hi, char *prompt)
{
	register char		*p;
	register int		sum;

	sum = 0;
	for (;;) {
	    msg(prompt);
	    if(!(p = getline()) || *p == '\0') {
		msg(quiet ? "Not a number" : "That doesn't look like a number");
		continue;
	    }
	    sum = 0;

	    if (!isdigit(*p))
		sum = lo - 1;
	    else
		while (isdigit(*p)) {
		    sum = 10 * sum + (*p - '0');
		    ++p;
		}

	    if (*p != ' ' && *p != '\t' && *p != '\0')
		sum = lo - 1;
	    if (sum >= lo && sum <= hi)
		return sum;
	    if (sum == lo - 1)
		msg("that doesn't look like a number, try again --> ");
	    else
		msg("%d is not between %d and %d inclusive, try again --> ",
								sum, lo, hi);
	}
}

/*
 * msg:
 *	Display a message at the top of the screen.
 */
char		Msgbuf[BUFSIZ] = { '\0' };

static int	Newpos = 0;

/* VARARGS1 */
void
msg(char *fmt, ...)
{
    va_list args;

    va_start(args, fmt);
    doadd(fmt, args);
    va_end(args);
    endmsg(TRUE);
}

/*
 * addmsg:
 *	Add things to the current message
 */
/* VARARGS1 */
void
addmsg(char *fmt, ...)
{
    va_list args;

    va_start (args, fmt);
    doadd(fmt, args);
    va_end (args);
}

/*
 * endmsg:
 *	Display a new msg.
 */

void
endmsg(BOOLEAN newline)
{
    int		linelen, msglen, len;
    char	*mp;
    
    linelen = UIGetMessageSize ();
    msglen = strlen (Msgbuf);
    mp = Msgbuf;
    do 
    {
	len = msglen;
	if (msglen > linelen)
	{
	    for (len = linelen; len >= 0; len--)
		if (mp[len] == ' ')
		    break;
	    while (mp[len] == ' ')
		len++;
	    mp[len-1] = '\0';
	}
	UIMessage (mp, newline);
	newline = TRUE;
	mp += len;
	msglen -= len;
    } while (msglen);
    Newpos = 0;
}


/*
 * doadd:
 *	Perform an add onto the message buffer
 */
void
doadd(char *fmt, va_list args)
{
    vsprintf (&Msgbuf[Newpos], fmt, args);
    Newpos = strlen(Msgbuf);
}

/*
 * getline:
 *      Reads the next line up to '\n' or EOF.  Multiple spaces are
 *	compressed to one space; a space is inserted before a ','
 */
char *
getline(void)
{
    UIReadLine (linebuf, LINESIZE);
    return linebuf;
}

/*
 * quit:
 *	Leave the program, cleaning things up as we go.
 */

void
quit(int status)
{
    UIFinish ();
    exit(status);
}