diff --git a/source/src/audiomanager.cpp b/source/src/audiomanager.cpp index dd92c883..26019560 100644 --- a/source/src/audiomanager.cpp +++ b/source/src/audiomanager.cpp @@ -978,10 +978,8 @@ void getmapsoundlist() // create a list of mapsound filenames } } } - if(res.length()) res.last() = '\0'; - else res.add('\0'); files.deletearrays(); - result(res.getbuf()); + resultcharvector(res, -1); } COMMAND(getmapsoundlist, ""); diff --git a/source/src/autodownload.cpp b/source/src/autodownload.cpp index f9f50d62..acdf174e 100644 --- a/source/src/autodownload.cpp +++ b/source/src/autodownload.cpp @@ -60,9 +60,7 @@ void getpckserver() // return a table of all package servers with four columns loopv(pckservers) cvecprintf(res, "\"%s\" %d %d %d\n", pckservers[i]->host, pckservers[i]->priority, pckservers[i]->ping, pckservers[i]->resolved); sem_pckservers.post(); } - if(res.length()) res.last() = '\0'; - else res.add('\0'); - result(res.getbuf()); + resultcharvector(res, -1); } COMMAND(getpckserver, ""); diff --git a/source/src/command.cpp b/source/src/command.cpp index f217fdb6..21996432 100644 --- a/source/src/command.cpp +++ b/source/src/command.cpp @@ -335,32 +335,29 @@ const char *getalias(const char *name) } void _getalias(char *name) { - string o; + string o = ""; + const char *res = o; ident *id = idents->access(name); - const char *action = getalias(name); if(id) { switch(id->type) { case ID_VAR: formatstring(o)("%d", *id->storage.i); - result(o); break; case ID_FVAR: formatstring(o)("%.3f", *id->storage.f); - result(o); break; case ID_SVAR: if(id->getfun) ((void (__cdecl *)())id->getfun)(); - formatstring(o)("%s", *id->storage.s); - result(o); + res = *id->storage.s; break; case ID_ALIAS: - result(action ? action : ""); + if(id->action) res = id->action; break; - default: break; } } + result(res); } COMMANDN(getalias, _getalias, "s"); @@ -536,6 +533,12 @@ void floatret(float v, bool neat) void result(const char *s) { commandret = newstring(s); } COMMAND(result, "s"); +void resultcharvector(const vector &res, int adj) // use char vector as result, optionally remove some bytes at the end +{ + adj += res.length(); + commandret = adj <= 0 ? newstring("", 0) : newstring(res.getbuf(), (size_t) adj); +} + #if 0 // seer : script evaluation excessive recursion static int seer_count = 0; // count calls to executeret, check time every n1 (100) calls @@ -1168,14 +1171,8 @@ COMMANDF(concatword, "w", (const char *s) { result(s); }); void format(char **args, int numargs) { - if(numargs < 1) - { - result(""); - return; - } - vector s; - char *f = args[0]; + const char *f = numargs > 0 ? args[0] : ""; while(*f) { int c = *f++; @@ -1192,8 +1189,7 @@ void format(char **args, int numargs) } else s.add(c); } - s.add('\0'); - result(s.getbuf()); + resultcharvector(s, 0); } COMMAND(format, "v"); @@ -1581,9 +1577,7 @@ void enumalias(char *prefix) enumerate(*idents, ident, id, if(id.type == ID_ALIAS && id.persist && !id.isconst && !id.istemp && !strncmp(id.name, prefix, np)) sids.add(&id); ); sids.sort(sortident); loopv(sids) cvecprintf(res, "%s %s\n", sids[i]->name, sids[i]->name + np); - if(res.length()) res.last() = '\0'; - else res.add('\0'); - result(res.getbuf()); + resultcharvector(res, -1); } COMMAND(enumalias, "s"); diff --git a/source/src/console.cpp b/source/src/console.cpp index d2124c1c..691f738e 100644 --- a/source/src/console.cpp +++ b/source/src/console.cpp @@ -180,35 +180,32 @@ keym *findbindc(int code) void findkey(int *code) { - for (int i = 0; i < keyms.length(); i++) + const char *res = "-255"; + loopv(keyms) { - if(keyms[i].code==*code) + if(keyms[i].code == *code) { - defformatstring(out)("%s", keyms[i].name); - result(out); - return; + res = keyms[i].name; + break; } } - result("-255"); - return; + result(res); } +COMMAND(findkey, "i"); void findkeycode(const char* s) { - for (int i = 0; i < keyms.length(); i++) - { - if(strcmp(s, keyms[i].name) == 0) - { - defformatstring(out)("%d", keyms[i].code); - result(out); - return; - } - } - result("-255"); - return; + int res = -255; + loopv(keyms) + { + if(!strcmp(s, keyms[i].name)) + { + res = keyms[i].code; + break; + } + } + intret(res); } - -COMMAND(findkey, "i"); COMMAND(findkeycode, "s"); keym *keypressed = NULL; @@ -265,8 +262,7 @@ void searchbinds(const char *action, int type) names.put(keyms[i].name, strlen(keyms[i].name)); } } - names.add('\0'); - result(names.getbuf()); + resultcharvector(names, 0); } COMMANDF(keybind, "s", (const char *key) { keybind(key, keym::ACTION_DEFAULT); } ); diff --git a/source/src/docs.cpp b/source/src/docs.cpp index 0a3dedea..1077fe7a 100644 --- a/source/src/docs.cpp +++ b/source/src/docs.cpp @@ -248,9 +248,7 @@ void docfind(char *search, int *silent) } } } - if(res.length()) res.last() = '\0'; - else res.add('\0'); - result(res.getbuf()); + resultcharvector(res, -1); } COMMAND(docfind, "si"); diff --git a/source/src/protos.h b/source/src/protos.h index da37df22..e04e65b2 100644 --- a/source/src/protos.h +++ b/source/src/protos.h @@ -1006,6 +1006,7 @@ extern void intret(int v); extern const char *floatstr(float v, bool neat = false); extern void floatret(float v, bool neat = false); extern void result(const char *s); +extern void resultcharvector(const vector &res, int adj); extern void exec(const char *cfgfile); extern bool execfile(const char *cfgfile); extern int listlen(const char *s); diff --git a/source/src/rendermodel.cpp b/source/src/rendermodel.cpp index 991da688..ebf7147e 100644 --- a/source/src/rendermodel.cpp +++ b/source/src/rendermodel.cpp @@ -543,10 +543,8 @@ void listallmapmodelattributes(char **args, int numargs) // create a list of map loopvj(a) if(a[j] < MMA_NUM) cvecprintf(res, " %s", escapestring(mmas[i]->n[a[j]])); res.add('\n'); } - if(res.length()) res.last() = '\0'; - else res.add('\0'); loopv(emmas) { delstring(emmas[i]->n[MMA_KEYWORDS]); delete emmas[i]; } // only delete what was exploded - result(res.getbuf()); + resultcharvector(res, -1); } COMMAND(listallmapmodelattributes, "v"); diff --git a/source/src/texture.cpp b/source/src/texture.cpp index 6064db70..fd82ff1c 100644 --- a/source/src/texture.cpp +++ b/source/src/texture.cpp @@ -930,13 +930,11 @@ void gettexturelist(char *_filter, char *_exclude, char *_exts) // create a list } res.add('\n'); } - if(res.length()) res.last() = '\0'; - else res.add('\0'); filter.deletearrays(); exclude.deletearrays(); exts.deletearrays(); files.deletearrays(); - result(res.getbuf()); + resultcharvector(res, -1); } COMMAND(gettexturelist, "sss"); @@ -1015,8 +1013,7 @@ void textureslotusagelist(char *what) vector res; if(onlymostvisible) cvecprintf(res, "wall %d floor %d ceiling %d \"upper wall\" %d ", mostvisible[0], mostvisible[1], mostvisible[2], mostvisible[3]); else loopi(256) cvecprintf(res, "%d %d ", used[i], visible[i]); - res.last() = '\0'; - result(res.getbuf()); + resultcharvector(res, -1); } COMMAND(textureslotusagelist, "s"); diff --git a/source/src/world.cpp b/source/src/world.cpp index f6c94907..0fc55ffa 100644 --- a/source/src/world.cpp +++ b/source/src/world.cpp @@ -313,9 +313,7 @@ void getdeletedentities() int t = e.type < MAXENTTYPES ? e.type : NOTUSED; cvecprintf(res,"%s %d %d %d %s\n", entnames[t], e.x, e.y, e.z, formatentityattributes(e)); } - if(res.length()) res.last() = '\0'; - else res.add('\0'); - result(res.getbuf()); + resultcharvector(res, -1); } COMMAND(getdeletedentities, ""); @@ -518,9 +516,7 @@ void enumentities(char *type) vector res; int t = getlistindex(type, entnames, false, -1); loopv(ents) if(ents[i].type == t) cvecprintf(res, "%d ", i); - if(!res.length()) res.add('\0'); - else res.last() = '\0'; - result(res.getbuf()); + resultcharvector(res, -1); } COMMAND(enumentities, "s"); @@ -554,9 +550,7 @@ void enumtodoentities() { vector res; loopv(todoents) cvecprintf(res, "%d %s\n", todoents[i], escapestring(todoentdescs[i], true)); - if(!res.length()) res.add('\0'); - else res.last() = '\0'; - result(res.getbuf()); + resultcharvector(res, -1); } COMMAND(enumtodoentities, ""); @@ -1024,8 +1018,7 @@ void mapareacheck(char *what) // "vdelta" | "steepest" | "total" | "pprest" | "p cvecprintf(res, "%d %d %d %d %d %.5g\n", x, y, z, ms.ppa[i], ms.ppv[i], ms.ppa[i] ? float(ms.ppv[i]) / ms.ppa[i] : 0.0f); } } - res.last() = '\0'; - result(res.getbuf()); + resultcharvector(res, -1); } COMMAND(mapareacheck, "s"); diff --git a/source/src/zip.cpp b/source/src/zip.cpp index ea934b36..9bd01c4e 100644 --- a/source/src/zip.cpp +++ b/source/src/zip.cpp @@ -620,9 +620,7 @@ void zipmodlist(char *which) // "active", "inactive", "all"(default) } files.deletearrays(); } - if(res.length()) res.last() = '\0'; - else res.add('\0'); - result(res.getbuf()); + resultcharvector(res, -1); } COMMAND(zipmodlist, "s"); @@ -664,9 +662,7 @@ void zipmodgetdesc(char *name) // open zip, read desc.txt and mount preview pict } zipmanualclose(mz); } - if(res.length()) res.last() = '\0'; - else res.add('\0'); - result(res.getbuf()); + resultcharvector(res, -1); } COMMAND(zipmodgetdesc, "s"); @@ -683,9 +679,7 @@ void zipmodgetfiles(char *name) // open zip, read list of files loopv(files) cvecprintf(res, "%s\n", escapestring(files[i])); zipmanualclose(mz); } - if(res.length()) res.last() = '\0'; - else res.add('\0'); - result(res.getbuf()); + resultcharvector(res, -1); } COMMAND(zipmodgetfiles, "s");