# HG changeset patch # User David Anderson # Date 1192989515 0 # Node ID fd5c786ca8e5d849f756a6a922f93527ea091913 # Parent 7817a6dd67d71ba56d94538a4dd5eb4f468b1c19 atcprintf() is now fully re-entrant, meaning translations can be used from inside translations diff -r 7817a6dd67d7 -r fd5c786ca8e5 core/sm_stringutil.cpp --- a/core/sm_stringutil.cpp Sun Oct 21 15:18:01 2007 +0000 +++ b/core/sm_stringutil.cpp Sun Oct 21 17:58:35 2007 +0000 @@ -87,23 +87,43 @@ return (err == Trans_Okay) ? true : false; } -size_t Translate(char *buffer, size_t maxlen, IPluginContext *pCtx, const char *key, cell_t target, const cell_t *params, int *arg, bool *error) +inline void ReorderTranslationParams(const Translation *pTrans, cell_t *params) +{ + cell_t new_params[MAX_TRANSLATE_PARAMS]; + for (unsigned int i = 0; i < pTrans->fmt_count; i++) + { + new_params[i] = params[pTrans->fmt_order[i]]; + } + memcpy(params, new_params, pTrans->fmt_count * sizeof(cell_t)); +} + +size_t Translate(char *buffer, + size_t maxlen, + IPluginContext *pCtx, + const char *key, + cell_t target, + const cell_t *params, + int *arg, + bool *error) { unsigned int langid; *error = false; Translation pTrans; CPlugin *pl = (CPlugin *)g_PluginSys.FindPluginByContext(pCtx->GetContext()); size_t langcount = pl->GetLangFileCount(); - void *new_params[MAX_TRANSLATE_PARAMS]; unsigned int max_params = 0; try_serverlang: if (target == LANG_SERVER) { langid = g_Translator.GetServerLanguage(); - } else if ((target >= 1) && (target <= g_Players.GetMaxClients())) { + } + else if ((target >= 1) && (target <= g_Players.GetMaxClients())) + { langid = g_Translator.GetClientLanguage(target); - } else { + } + else + { pCtx->ThrowNativeErrorEx(SP_ERROR_PARAM, "Translation failed: invalid client index %d", target); goto error_out; } @@ -114,13 +134,17 @@ { target = LANG_SERVER; goto try_serverlang; - } else if (langid != LANGUAGE_ENGLISH) { + } + else if (langid != LANGUAGE_ENGLISH) + { if (!TryTranslation(pl, key, LANGUAGE_ENGLISH, langcount, &pTrans)) { pCtx->ThrowNativeErrorEx(SP_ERROR_PARAM, "Language phrase \"%s\" not found", key); goto error_out; } - } else { + } + else + { pCtx->ThrowNativeErrorEx(SP_ERROR_PARAM, "Language phrase \"%s\" not found", key); goto error_out; } @@ -140,22 +164,18 @@ goto error_out; } - /* Translate the parameters to raw pointers */ - for (size_t i=0; iLocalToPhysAddr(params[*arg], reinterpret_cast(&new_params[i])); - (*arg)++; - } + /* Re-order the parameters that this translation requires */ + ReorderTranslationParams(&pTrans, const_cast(¶ms[*arg])); } - return g_Translator.Translate(buffer, maxlen, new_params, &pTrans); + /* Now, call back into atcprintf() which is re-entrant */ + return atcprintf(buffer, maxlen, pTrans.szPhrase, pCtx, params, arg); + error_out: *error = true; return 0; } -//:TODO: review this code before we choose a license - void AddString(char **buf_p, size_t &maxlen, const char *string, int width, int prec) { int size = 0;