Commit ecb82b4a authored by deuce's avatar deuce
Browse files

Add an optional "default value" second argument to getnum()

If default value is not zero, it is the value returned if enter is
pressed with no input.  In this case, zero is allowed to be entered.
parent 7c7f7853
......@@ -581,7 +581,7 @@ size_t sbbs_t::getstr(char *strout, size_t maxlen, long mode)
/* Returns a valid number between 1 and max, 0 if no number entered, or -1 */
/* if the user hit 'Q' or ctrl-c */
/****************************************************************************/
long sbbs_t::getnum(ulong max)
long sbbs_t::getnum(ulong max, ulong dflt)
{
uchar ch,n=0;
long i=0;
......@@ -610,6 +610,8 @@ long sbbs_t::getnum(ulong max)
else if(ch==CR) {
CRLF;
lncntr=0;
if(!n)
return(dflt);
return(i);
}
else if((ch==BS || ch==DEL) && n) {
......@@ -617,7 +619,7 @@ long sbbs_t::getnum(ulong max)
i/=10;
n--;
}
else if(isdigit(ch) && (i*10UL)+(ch&0xf)<=max && (ch!='0' || n)) {
else if(isdigit(ch) && (i*10UL)+(ch&0xf)<=max && (dflt || ch!='0' || n)) {
i*=10L;
n++;
i+=ch&0xf;
......
......@@ -494,6 +494,7 @@ static JSBool
js_getnum(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
ulong maxnum=~0;
ulong dflt=0;
sbbs_t* sbbs;
if((sbbs=(sbbs_t*)JS_GetContextPrivate(cx))==NULL)
......@@ -501,8 +502,10 @@ js_getnum(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
if(argc && JSVAL_IS_NUMBER(argv[0]))
JS_ValueToInt32(cx,argv[0],(int32*)&maxnum);
if(argc>1 && JSVAL_IS_NUMBER(argv[1]))
JS_ValueToInt32(cx,argv[1],(int32*)&dflt);
*rval = INT_TO_JSVAL(sbbs->getnum(maxnum));
*rval = INT_TO_JSVAL(sbbs->getnum(maxnum,dflt));
return(JS_TRUE);
}
......@@ -1246,8 +1249,8 @@ static jsSyncMethodSpec js_console_functions[] = {
"see <tt>K_*</tt> in <tt>sbbsdefs.js</tt> for <i>mode</i> bits")
,310
},
{"getnum", js_getnum, 0, JSTYPE_NUMBER, JSDOCSTR("[maxnum]")
,JSDOCSTR("get a number between 1 and <i>maxnum</i> from the user")
{"getnum", js_getnum, 0, JSTYPE_NUMBER, JSDOCSTR("[maxnum[, default]]")
,JSDOCSTR("get a number between 1 and <i>maxnum</i> from the user with a default value of <i>default</i>")
,310
},
{"getkeys", js_getkeys, 1, JSTYPE_NUMBER, JSDOCSTR("string keys [,maxnum]")
......
......@@ -520,7 +520,7 @@ public:
/* getstr.cpp */
size_t getstr_offset;
size_t getstr(char *str, size_t length, long mode);
long getnum(ulong max);
long getnum(ulong max, ulong dflt=0);
void insert_indicator(void);
/* getkey.cpp */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment