[gamecode] Add function to test if string exists

As it returns the string id, it is useful for getting an string id
without risking creating a new one.
This commit is contained in:
Bill Currie 2020-04-04 12:32:12 +09:00
parent 686fc4c43e
commit 59f48e5e32
2 changed files with 25 additions and 0 deletions

View file

@ -1290,6 +1290,15 @@ struct dstring_s *PR_GetMutableString(progs_t *pr, string_t num) __attribute__((
*/ */
string_t PR_SetString(progs_t *pr, const char *s); string_t PR_SetString(progs_t *pr, const char *s);
/** Get the progs string if it exists.
Only static strings are searched.
\param pr pointer to ::progs_t VM struct
\param s C string to be found
\return string index of the progs string if it exists, otherwise
0 (ambiguous with "").
*/
string_t PR_FindString(progs_t *pr, const char *s);
/** Make a temporary progs string that will survive across function returns. /** Make a temporary progs string that will survive across function returns.
Will not duplicate a permanent string. If a new progs string is created, Will not duplicate a permanent string. If a new progs string is created,
it will be freed after ::PR_RS_SLOTS calls to this function. ie, up to it will be freed after ::PR_RS_SLOTS calls to this function. ie, up to

View file

@ -440,6 +440,22 @@ PR_SetString (progs_t *pr, const char *s)
return string_index (res, sr); return string_index (res, sr);
} }
VISIBLE string_t
PR_FindString (progs_t *pr, const char *s)
{
prstr_resources_t *res = pr->pr_string_resources;
strref_t *sr;
if (!s)
s = "";
sr = Hash_Find (res->strref_hash, s);
if (sr) {
return string_index (res, sr);
}
return 0;
}
VISIBLE string_t VISIBLE string_t
PR_SetReturnString (progs_t *pr, const char *s) PR_SetReturnString (progs_t *pr, const char *s)
{ {