Unify getting a count into single function

This commit is contained in:
Pasi Kallinen
2016-01-06 13:54:09 +02:00
parent 1bc05714b5
commit 4c016853d4
4 changed files with 80 additions and 67 deletions
+2
View File
@@ -92,6 +92,8 @@ fix death reason when eating tainted glob of <monster> (not corpse)
use appropriate place name for drum of earthquake shakes use appropriate place name for drum of earthquake shakes
fix unmapped branch stairs on sokoban level fix unmapped branch stairs on sokoban level
redraw map when hilite_pile is toggled to display the highlighting redraw map when hilite_pile is toggled to display the highlighting
make commands that accept a count prefix for item selection
show "Count:" like command repeating does
Platform- and/or Interface-Specific Fixes Platform- and/or Interface-Specific Fixes
+1
View File
@@ -204,6 +204,7 @@ E int FDECL(isok, (int, int));
E int FDECL(get_adjacent_loc, E int FDECL(get_adjacent_loc,
(const char *, const char *, XCHAR_P, XCHAR_P, coord *)); (const char *, const char *, XCHAR_P, XCHAR_P, coord *));
E const char *FDECL(click_to_cmd, (int, int, int)); E const char *FDECL(click_to_cmd, (int, int, int));
E char FDECL(get_count, (char *, char, long, long *));
#ifdef HANGUPHANDLING #ifdef HANGUPHANDLING
E void FDECL(hangup, (int)); E void FDECL(hangup, (int));
E void NDECL(end_of_input); E void NDECL(end_of_input);
+56 -29
View File
@@ -3864,6 +3864,57 @@ int x, y, mod;
return cmd; return cmd;
} }
char
get_count(allowchars, inkey, max, count)
char *allowchars;
char inkey;
long max;
long *count;
{
char qbuf[QBUFSZ];
int key;
long cnt = 0;
boolean backspaced = FALSE;
char *ret;
for (;;) {
if (inkey) {
key = inkey;
inkey = '\0';
} else
key = readchar();
if (digit(key)) {
cnt = 10L * cnt + (long)(key - '0');
} else if (key == '\b') {
cnt = cnt / 10;
backspaced = TRUE;
} else if (key == '\033') {
return '\033';
} else if (allowchars) {
if (ret = index(allowchars, key)) {
*count = cnt;
return *ret;
}
} else {
*count = cnt;
return key;
}
if (max && (cnt > max))
cnt = max;
if (cnt > 9 || backspaced) {
clear_nhwindow(WIN_MESSAGE);
if (backspaced && !cnt)
Sprintf(qbuf, "Count: ");
else {
Sprintf(qbuf, "Count: %d", cnt);
backspaced = FALSE;
}
pline1(qbuf);
mark_synch();
}
}
}
STATIC_OVL char * STATIC_OVL char *
parse() parse()
{ {
@@ -3883,35 +3934,11 @@ parse()
#ifdef ALTMETA #ifdef ALTMETA
alt_esc = iflags.altmeta; /* readchar() hack */ alt_esc = iflags.altmeta; /* readchar() hack */
#endif #endif
if (!Cmd.num_pad || (foo = readchar()) == 'n') if (!Cmd.num_pad || (foo = readchar()) == 'n') {
for (;;) { long tmpmulti = multi;
foo = readchar(); foo = get_count(NULL, '\0', LARGEST_INT, &tmpmulti);
if ((foo >= '0' && foo <= '9') || (foo == '\b')) { last_multi = multi = tmpmulti;
if (foo >= '0' && foo <= '9') }
multi = 10 * multi + foo - '0';
else {
multi = multi / 10;
backspaced = TRUE;
}
if (multi < 0 || multi >= LARGEST_INT)
multi = LARGEST_INT;
if (multi > 9 || backspaced) {
clear_nhwindow(WIN_MESSAGE);
if (backspaced && !multi)
Sprintf(in_line, "Count: ");
else {
Sprintf(in_line, "Count: %d", multi);
backspaced = FALSE;
}
pline1(in_line);
mark_synch();
}
last_multi = multi;
if (!multi && foo == '0')
prezero = TRUE;
} else
break; /* not a digit */
}
#ifdef ALTMETA #ifdef ALTMETA
alt_esc = FALSE; /* readchar() reset */ alt_esc = FALSE; /* readchar() reset */
#endif #endif
+21 -38
View File
@@ -970,8 +970,8 @@ register const char *let, *word;
boolean allownone = FALSE; boolean allownone = FALSE;
boolean useboulder = FALSE; boolean useboulder = FALSE;
xchar foox = 0; xchar foox = 0;
long cnt, prevcnt; long cnt;
boolean prezero; boolean cntgiven = FALSE;
long dummymask; long dummymask;
if (*let == ALLOW_COUNT) if (*let == ALLOW_COUNT)
@@ -1163,9 +1163,7 @@ register const char *let, *word;
} }
for (;;) { for (;;) {
cnt = 0; cnt = 0;
if (allowcnt == 2) cntgiven = FALSE;
allowcnt = 1; /* abort previous count */
prezero = FALSE;
if (!buf[0]) { if (!buf[0]) {
Sprintf(qbuf, "What do you want to %s? [*]", word); Sprintf(qbuf, "What do you want to %s? [*]", word);
} else { } else {
@@ -1175,28 +1173,17 @@ register const char *let, *word;
ilet = readchar(); ilet = readchar();
else else
ilet = yn_function(qbuf, (char *) 0, '\0'); ilet = yn_function(qbuf, (char *) 0, '\0');
if (digit(ilet) && !allowcnt) { if (digit(ilet)) {
pline("No count allowed with this command."); long tmpcnt = 0;
continue; if (!allowcnt) {
} pline("No count allowed with this command.");
if (ilet == '0') continue;
prezero = TRUE; }
while (digit(ilet)) { ilet = get_count(NULL, ilet, LARGEST_INT, &tmpcnt);
if (ilet != '?' && ilet != '*') if (tmpcnt) {
savech(ilet); cnt = tmpcnt;
/* accumulate unless cnt has overflowed */ cntgiven = TRUE;
if (allowcnt < 3) {
prevcnt = cnt;
cnt = 10L * cnt + (long) (ilet - '0');
/* signal presence of cnt */
allowcnt = (cnt >= prevcnt) ? 2 : 3;
} }
ilet = readchar();
}
if (allowcnt == 3) {
/* overflow detected; force cnt to be invalid */
cnt = -1L;
allowcnt = 2;
} }
if (index(quitchars, ilet)) { if (index(quitchars, ilet)) {
if (flags.verbose) if (flags.verbose)
@@ -1237,9 +1224,7 @@ register const char *let, *word;
continue; continue;
if (allowcnt && ctmp >= 0) { if (allowcnt && ctmp >= 0) {
cnt = ctmp; cnt = ctmp;
if (!cnt) cntgiven = TRUE;
prezero = TRUE;
allowcnt = 2;
} }
if (ilet == '\033') { if (ilet == '\033') {
if (flags.verbose) if (flags.verbose)
@@ -1267,23 +1252,21 @@ register const char *let, *word;
* to your money supply. The LRS is the tax bureau * to your money supply. The LRS is the tax bureau
* from Larn. * from Larn.
*/ */
if (allowcnt == 2 && cnt <= 0) { if (cntgiven && cnt <= 0) {
if (cnt < 0 || !prezero) if (cnt < 0)
pline_The( pline_The(
"LRS would be very interested to know you have that much."); "LRS would be very interested to know you have that much.");
return (struct obj *) 0; return (struct obj *) 0;
} }
} }
if (allowcnt == 2 && !strcmp(word, "throw")) { if (cntgiven && !strcmp(word, "throw")) {
/* permit counts for throwing gold, but don't accept /* permit counts for throwing gold, but don't accept
* counts for other things since the throw code will * counts for other things since the throw code will
* split off a single item anyway */ * split off a single item anyway */
if (ilet != def_oc_syms[COIN_CLASS].sym if (cnt == 0)
&& !(otmp && otmp->oclass == COIN_CLASS))
allowcnt = 1;
if (cnt == 0 && prezero)
return (struct obj *) 0; return (struct obj *) 0;
if (cnt > 1) { if (cnt > 1 && (ilet != def_oc_syms[COIN_CLASS].sym
&& !(otmp && otmp->oclass == COIN_CLASS))) {
You("can only throw one item at a time."); You("can only throw one item at a time.");
continue; continue;
} }
@@ -1311,7 +1294,7 @@ register const char *let, *word;
silly_thing(word, otmp); silly_thing(word, otmp);
return (struct obj *) 0; return (struct obj *) 0;
} }
if (allowcnt == 2) { /* cnt given */ if (cntgiven) {
if (cnt == 0) if (cnt == 0)
return (struct obj *) 0; return (struct obj *) 0;
if (cnt != otmp->quan) { if (cnt != otmp->quan) {