From 1ba88a08f6d3e71c39ea3e72267292958c40e148 Mon Sep 17 00:00:00 2001 From: Ari Johnson Date: Thu, 7 Jun 2007 23:16:33 +0000 Subject: [PATCH] default() can take multiple attributes to try in order (cherry picked from commit d7669ebeddb88df81ef55175eed8553bf5ef8850) --- game/txt/changes/0.73 | 1 + game/txt/hlp/cobra_func.hlp | 16 ++++++++-------- src/function.c | 2 +- src/fundb.c | 32 ++++++++++++++++++-------------- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/game/txt/changes/0.73 b/game/txt/changes/0.73 index e050cab..96e9c5c 100644 --- a/game/txt/changes/0.73 +++ b/game/txt/changes/0.73 @@ -198,4 +198,5 @@ CobraMUSH Version 0.73 * crecall() [AEJ] * @poll with no argument now displays the current poll; use @poll/clear to clear it [AEJ] + * default() can take multiple attributes to try in order [AEJ] diff --git a/game/txt/hlp/cobra_func.hlp b/game/txt/hlp/cobra_func.hlp index 6aafe9c..542d08e 100644 --- a/game/txt/hlp/cobra_func.hlp +++ b/game/txt/hlp/cobra_func.hlp @@ -886,15 +886,15 @@ the string and the same password. & DEFAULT() - Function: default([/],) - - This function returns the value of /, as if retrieved via - the get() function, if the attribute exists and is readable by you. - Otherwise, it evaluates the default case, and returns that. - Note that the default case is only evaluated if the attribute does - not exist or cannot be read. Note further than an empty attribute - counts as an existing attribute. + Function: default([/],[[]/,...]) + This function returns the value of the first possible /, + as if retrieved via the get() function, if the attribute exists and + is readable by you. Otherwise, it evaluates the default case, and + returns that. Note that the default case is only evaluated if the + attribute does not exist or cannot be read. Note further that an + empty attribute counts as an existing attribute. + This is useful for code that needs to return the value of an attribute, or an error message or default case, if that attribute does not exist. diff --git a/src/function.c b/src/function.c index 328b40c..aab3fd6 100644 --- a/src/function.c +++ b/src/function.c @@ -347,7 +347,7 @@ FUNTAB flist[] = { {"DEC", fun_dec, 1, 1, FN_REG}, {"DECOMPOSE", fun_decompose, 1, -1, FN_REG}, {"DECRYPT", fun_decrypt, 2, 2, FN_REG}, - {"DEFAULT", fun_default, 2, 2, FN_NOPARSE}, + {"DEFAULT", fun_default, 2, INT_MAX, FN_NOPARSE}, {"DELETE", fun_delete, 3, 3, FN_REG}, {"DIE", fun_die, 2, 3, FN_REG}, {"DIG", fun_dig, 1, 3, FN_REG}, diff --git a/src/fundb.c b/src/fundb.c index 6212e3e..e416168 100644 --- a/src/fundb.c +++ b/src/fundb.c @@ -221,22 +221,26 @@ FUNCTION(fun_default) char *dp; const char *sp; char mstr[BUFFER_LEN]; + int i; /* find our object and attribute */ - dp = mstr; - sp = args[0]; - process_expression(mstr, &dp, &sp, executor, caller, enactor, - PE_DEFAULT, PT_DEFAULT, pe_info); - *dp = '\0'; - parse_attrib(executor, mstr, &thing, &attrib); - if (GoodObject(thing) && attrib && Can_Read_Attr(executor, thing, attrib)) { - /* Ok, we've got it */ - dp = safe_atr_value(attrib); - safe_str(dp, buff, bp); - free(dp); - return; + for (i = 1; i < nargs; i++) { + mstr[0] = '\0'; + dp = mstr; + sp = args[i - 1]; + process_expression(mstr, &dp, &sp, executor, caller, enactor, PE_DEFAULT, + PT_DEFAULT, pe_info); + *dp = '\0'; + parse_attrib(executor, mstr, &thing, &attrib); + if (GoodObject(thing) && attrib && Can_Read_Attr(executor, thing, attrib)) { + /* Ok, we've got it */ + dp = safe_atr_value(attrib); + safe_str(dp, buff, bp); + free(dp); + return; + } } - /* We couldn't get it. Evaluate args[1] and return it */ - sp = args[1]; + /* We couldn't get it. Evaluate the last arg and return it */ + sp = args[nargs - 1]; process_expression(buff, bp, &sp, executor, caller, enactor, PE_DEFAULT, PT_DEFAULT, pe_info); return; -- 2.30.2