MCPcopy Create free account
hub / github.com/NetHack/NetHack / short_oname

Function short_oname

src/objnam.c:2008–2085  ·  view source on GitHub ↗

xname,doname,&c with long results reformatted to omit some stuff */

Source from the content-addressed store, hash-verified

2006
2007/* xname,doname,&c with long results reformatted to omit some stuff */
2008char *
2009short_oname(
2010 struct obj *obj,
2011 char *(*func)(OBJ_P), /* main formatting routine */
2012 char *(*altfunc)(OBJ_P), /* alternate for shortest result */
2013 unsigned lenlimit)
2014{
2015 struct obj save_obj;
2016 char unamebuf[12], onamebuf[12], *save_oname, *save_uname, *outbuf;
2017
2018 outbuf = (*func)(obj);
2019 if ((unsigned) strlen(outbuf) <= lenlimit)
2020 return outbuf;
2021
2022 /* shorten called string to fairly small amount */
2023 save_uname = objects[obj->otyp].oc_uname;
2024 if (save_uname && strlen(save_uname) >= sizeof unamebuf) {
2025 (void) strncpy(unamebuf, save_uname, sizeof unamebuf - 4);
2026 Strcpy(unamebuf + sizeof unamebuf - 4, "...");
2027 objects[obj->otyp].oc_uname = unamebuf;
2028 releaseobuf(outbuf);
2029 outbuf = (*func)(obj);
2030 objects[obj->otyp].oc_uname = save_uname; /* restore called string */
2031 if ((unsigned) strlen(outbuf) <= lenlimit)
2032 return outbuf;
2033 }
2034
2035 /* shorten named string to fairly small amount */
2036 save_oname = has_oname(obj) ? ONAME(obj) : 0;
2037 if (save_oname && strlen(save_oname) >= sizeof onamebuf) {
2038 (void) strncpy(onamebuf, save_oname, sizeof onamebuf - 4);
2039 Strcpy(onamebuf + sizeof onamebuf - 4, "...");
2040 ONAME(obj) = onamebuf;
2041 releaseobuf(outbuf);
2042 outbuf = (*func)(obj);
2043 ONAME(obj) = save_oname; /* restore named string */
2044 if ((unsigned) strlen(outbuf) <= lenlimit)
2045 return outbuf;
2046 }
2047
2048 /* shorten both called and named strings;
2049 unamebuf and onamebuf have both already been populated */
2050 if (save_uname && strlen(save_uname) >= sizeof unamebuf && save_oname
2051 && strlen(save_oname) >= sizeof onamebuf) {
2052 objects[obj->otyp].oc_uname = unamebuf;
2053 ONAME(obj) = onamebuf;
2054 releaseobuf(outbuf);
2055 outbuf = (*func)(obj);
2056 if ((unsigned) strlen(outbuf) <= lenlimit) {
2057 objects[obj->otyp].oc_uname = save_uname;
2058 ONAME(obj) = save_oname;
2059 return outbuf;
2060 }
2061 }
2062
2063 /* still long; strip several name-lengthening attributes;
2064 called and named strings are still in truncated form */
2065 save_obj = *obj;

Callers 2

dodipFunction · 0.85
safe_qbufFunction · 0.85

Calls 1

releaseobufFunction · 0.85

Tested by

no test coverage detected