Skip to content
Snippets Groups Projects
Commit 9bd2d550 authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Cache the text.dat string ID look-ups (mapping from IDs to indexes)

Whoohoo, I'm a reel STL programmerz n0w!
parent d7012b2e
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
Pipeline #4828 failed
......@@ -920,7 +920,7 @@ const char* sbbs_t::atcode(const char* sp, char* str, size_t maxlen, int* pmode,
if(i >= 1 && i <= TOTAL_TEXT)
return text[i - 1];
else
return gettext(sp + 5);
return get_text(sp + 5);
}
/* NOSTOP */
......@@ -2189,7 +2189,7 @@ const char* sbbs_t::atcode(const char* sp, char* str, size_t maxlen, int* pmode,
}
}
return gettext(sp);
return get_text(sp);
}
char* sbbs_t::expand_atcodes(const char* src, char* buf, size_t size)
......
......@@ -26,6 +26,10 @@
/* Standard library headers */
/****************************/
#if defined(__cplusplus)
#include <unordered_map>
#endif
/***************/
/* OS-specific */
/***************/
......@@ -523,12 +527,13 @@ public:
size_t batup_total();
size_t batdn_total();
/*********************************/
/* Color Configuration Variables */
/*********************************/
/********************************/
/* Text Configuration Variables */
/********************************/
char *text[TOTAL_TEXT]{}; /* Text from text.dat/text.ini */
char *text_sav[TOTAL_TEXT]{}; /* Text from text.dat/text.ini */
bool text_replaced[TOTAL_TEXT]{};
std::unordered_map<std::string, int> text_id_map{};
char yes_key(void) { return toupper(*text[Yes]); }
char no_key(void) { return toupper(*text[No]); }
char quit_key(void) { return toupper(*text[Quit]); }
......@@ -731,7 +736,8 @@ public:
bool gettimeleft_inside = false;
/* str.cpp */
const char* gettext(const char* id);
int get_text_num(const char* id);
const char* get_text(const char* id);
bool load_user_text(void);
bool replace_text(const char* path);
void revert_text(void);
......
......@@ -22,9 +22,37 @@
#include "sbbs.h"
#include "dat_rec.h"
const char* sbbs_t::gettext(const char* id)
/****************************************************************************/
// Returns 0-based text string index
// Caches the result
/****************************************************************************/
int sbbs_t::get_text_num(const char* id)
{
int i;
if (isdigit(*id)) {
i = atoi(id);
if (i < 1)
return TOTAL_TEXT;
return i - 1;
}
auto index = text_id_map.find(id);
if (index != text_id_map.end())
i = index->second;
else {
for (i = 0; i < TOTAL_TEXT; ++i) {
if (strcmp(text_id[i], id) == 0) {
text_id_map[id] = i;
break;
}
}
}
return i;
}
/****************************************************************************/
/****************************************************************************/
const char* sbbs_t::get_text(const char* id)
{
// TODO: hash/cache results
int i = get_text_num(id);
if(i >= 0 && i < TOTAL_TEXT)
return text[i];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment