]> granicus.if.org Git - nethack/commitdiff
safe_oname (trunk only)
authornethack.allison <nethack.allison>
Sat, 15 Apr 2006 15:40:27 +0000 (15:40 +0000)
committernethack.allison <nethack.allison>
Sat, 15 Apr 2006 15:40:27 +0000 (15:40 +0000)
There were routines that were passed the
object name as an argument.  Before the oextra
patch, ONAME() always returned a valid pointer
to a location within the obj struct. The oextra
patch worked around those cases by
using a temporary variable that was either set
to ONAME (if the obj passed the has_oname() test),
or to ""  (pointer to an empty string) if no name was
present.

Since that might be a common thing to do, provide
the safe_oname() routine that you can use as a
function parameter without having to worry about
about whether ONAME(obj) is valid, and without
the need for the temporary variable.

include/extern.h
src/bones.c
src/do_name.c
src/objnam.c

index f18a69b75d6c4d45138c2c0001faeb3ea672622f..26e4cb1830ec3522b7a15e08fe10c09ad1da7317 100644 (file)
@@ -353,6 +353,7 @@ E void FDECL(new_mname, (struct monst *,int));
 E void FDECL(free_mname, (struct monst *));
 E void FDECL(new_oname, (struct obj *,int));
 E void FDECL(free_oname, (struct obj *));
+E const char *FDECL(safe_oname, (struct obj *));
 E struct monst *FDECL(christen_monst, (struct monst *,const char *));
 E int NDECL(do_mname);
 E struct obj *FDECL(oname, (struct obj *,const char *));
index 8220afb5adb99db7244b4eea90457f38d0805f2b..fc51a518820ff41c51c492993671bc88a4026f1e 100644 (file)
@@ -68,16 +68,14 @@ boolean restore;
                        /* artifact bookeeping needs to be done during
                           restore; other fixups are done while saving */
                        if (otmp->oartifact) {
-                           char *tmponame = "";
-                           if (has_oname(otmp)) tmponame = ONAME(otmp);
-                           if (exist_artifact(otmp->otyp, tmponame) ||
+                           if (exist_artifact(otmp->otyp, safe_oname(otmp)) ||
                                    is_quest_artifact(otmp)) {
                                /* prevent duplicate--revert to ordinary obj */
                                otmp->oartifact = 0;
                                if (has_oname(otmp))
                                        free_oname(otmp);
                            } else {
-                               artifact_exists(otmp, tmponame, TRUE);
+                               artifact_exists(otmp, safe_oname(otmp), TRUE);
                            }
                        }
                } else {        /* saving */
index 7e5802c43dde79974052897936592151eb47c74b..fd1f2d98b580a28c372a9b316c82b1650a8eab67 100644 (file)
@@ -268,6 +268,21 @@ struct obj *obj;
     }
 }
 
+/*  safe_oname() always returns a valid pointer to
+ *  a string, either the pointer to an object's name
+ *  if it has one, or a pointer to an empty string
+ *  if it doesn't.
+ */
+const char *
+safe_oname(obj)
+struct obj *obj;
+{
+    if (has_oname(obj))
+       return ONAME(obj);
+    else
+       return "";
+}
+
 /* historical note: this returns a monster pointer because it used to
    allocate a new bigger block of memory to hold the monster and its name */
 struct monst *
index 80cf59b54eeb55d2b767837ea35e2008d4f856f8..44252b23f436724c6d6d66ff67651d72a01a6d48 100644 (file)
@@ -2942,10 +2942,7 @@ typfnd:
            && !wizard
 #endif
            ) {
-           char *tnam = "";
-           if (has_oname(otmp))
-               tnam = ONAME(otmp);
-           artifact_exists(otmp, tnam, FALSE);
+           artifact_exists(otmp, safe_oname(otmp), FALSE);
            obfree(otmp, (struct obj *) 0);
            otmp = &zeroobj;
            pline("For a moment, you feel %s in your %s, but it disappears!",