safe_oname (trunk only)

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.
This commit is contained in:
nethack.allison
2006-04-15 15:40:27 +00:00
parent c16a87d367
commit 78ef9ef991
4 changed files with 19 additions and 8 deletions
+1
View File
@@ -353,6 +353,7 @@ E void FDECL(new_mname, (struct monst *,int));
E void FDECL(free_mname, (struct monst *)); E void FDECL(free_mname, (struct monst *));
E void FDECL(new_oname, (struct obj *,int)); E void FDECL(new_oname, (struct obj *,int));
E void FDECL(free_oname, (struct obj *)); 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 struct monst *FDECL(christen_monst, (struct monst *,const char *));
E int NDECL(do_mname); E int NDECL(do_mname);
E struct obj *FDECL(oname, (struct obj *,const char *)); E struct obj *FDECL(oname, (struct obj *,const char *));
+2 -4
View File
@@ -68,16 +68,14 @@ boolean restore;
/* artifact bookeeping needs to be done during /* artifact bookeeping needs to be done during
restore; other fixups are done while saving */ restore; other fixups are done while saving */
if (otmp->oartifact) { if (otmp->oartifact) {
char *tmponame = ""; if (exist_artifact(otmp->otyp, safe_oname(otmp)) ||
if (has_oname(otmp)) tmponame = ONAME(otmp);
if (exist_artifact(otmp->otyp, tmponame) ||
is_quest_artifact(otmp)) { is_quest_artifact(otmp)) {
/* prevent duplicate--revert to ordinary obj */ /* prevent duplicate--revert to ordinary obj */
otmp->oartifact = 0; otmp->oartifact = 0;
if (has_oname(otmp)) if (has_oname(otmp))
free_oname(otmp); free_oname(otmp);
} else { } else {
artifact_exists(otmp, tmponame, TRUE); artifact_exists(otmp, safe_oname(otmp), TRUE);
} }
} }
} else { /* saving */ } else { /* saving */
+15
View 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 /* 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 */ allocate a new bigger block of memory to hold the monster and its name */
struct monst * struct monst *
+1 -4
View File
@@ -2942,10 +2942,7 @@ typfnd:
&& !wizard && !wizard
#endif #endif
) { ) {
char *tnam = ""; artifact_exists(otmp, safe_oname(otmp), FALSE);
if (has_oname(otmp))
tnam = ONAME(otmp);
artifact_exists(otmp, tnam, FALSE);
obfree(otmp, (struct obj *) 0); obfree(otmp, (struct obj *) 0);
otmp = &zeroobj; otmp = &zeroobj;
pline("For a moment, you feel %s in your %s, but it disappears!", pline("For a moment, you feel %s in your %s, but it disappears!",