Use std::map for cvar_list

This commit is contained in:
Kp 2015-06-07 16:20:46 +00:00
parent 2be3ce1f01
commit ddb521c5e7

View file

@ -12,6 +12,7 @@
*/ */
#include <cstdarg> #include <cstdarg>
#include <map>
#include <stdlib.h> #include <stdlib.h>
#include <physfs.h> #include <physfs.h>
@ -20,18 +21,17 @@
#include "dxxerror.h" #include "dxxerror.h"
#include "strutil.h" #include "strutil.h"
#include "u_mem.h" #include "u_mem.h"
#include "hash.h"
#include "game.h" #include "game.h"
#include "physfsx.h" #include "physfsx.h"
#include "dxxsconf.h"
#include "compiler-range_for.h"
#define CVAR_MAX_LENGTH 1024 #define CVAR_MAX_LENGTH 1024
#define CVAR_MAX_CVARS 1024
/* The list of cvars */ /* The list of cvars */
hashtable cvar_hash; typedef std::map<const char *, std::reference_wrapper<cvar_t>> cvar_list_type;
cvar_t *cvar_list[CVAR_MAX_CVARS]; static cvar_list_type cvar_list;
int Num_cvars;
const char *cvar_t::operator=(const char *s) const char *cvar_t::operator=(const char *s)
{ {
@ -59,8 +59,8 @@ void cvar_cmd_set(int argc, char **argv)
} }
if (argc == 1) { if (argc == 1) {
for (i = 0; i < Num_cvars; i++) range_for (const auto &i, cvar_list)
con_printf(CON_NORMAL, "%s: %s", cvar_list[i]->name, cvar_list[i]->string.c_str()); con_printf(CON_NORMAL, "%s: %s", i.first, i.second.get().string.c_str());
return; return;
} }
@ -91,29 +91,19 @@ void cvar_init(void)
cvar_t *cvar_find(const char *cvar_name) cvar_t *cvar_find(const char *cvar_name)
{ {
int i; const auto i = cvar_list.find(cvar_name);
return i == cvar_list.end() ? nullptr : &i->second.get();
i = hashtable_search( &cvar_hash, cvar_name );
if ( i < 0 )
return NULL;
return cvar_list[i];
} }
const char *cvar_complete(char *text) const char *cvar_complete(char *text)
{ {
int i;
uint_fast32_t len = strlen(text); uint_fast32_t len = strlen(text);
if (!len) if (!len)
return NULL; return NULL;
range_for (const auto &i, cvar_list)
for (i = 0; i < Num_cvars; i++) if (!d_strnicmp(text, i.first, len))
if (!d_strnicmp(text, cvar_list[i]->name, len)) return i.first;
return cvar_list[i]->name;
return NULL; return NULL;
} }
@ -126,17 +116,14 @@ void cvar_registervariable (cvar_t *cvar)
cvar->value = fl2f(strtod(cvar->string.c_str(), NULL)); cvar->value = fl2f(strtod(cvar->string.c_str(), NULL));
cvar->intval = static_cast<int>(strtol(cvar->string.c_str(), NULL, 10)); cvar->intval = static_cast<int>(strtol(cvar->string.c_str(), NULL, 10));
if (cvar_find(cvar->name)) const auto i = cvar_list.insert(cvar_list_type::value_type(cvar->name, *cvar));
if (!i.second)
{ {
Int3(); Int3();
con_printf(CON_URGENT, "cvar %s already exists!", cvar->name); con_printf(CON_URGENT, "cvar %s already exists!", cvar->name);
return; return;
} }
/* insert at end of list */ /* insert at end of list */
hashtable_insert(&cvar_hash, cvar->name, Num_cvars);
cvar_list[Num_cvars++] = cvar;
} }
@ -197,9 +184,7 @@ void cvar_set(const char *cvar_name, char *value)
/* Write archive cvars to file */ /* Write archive cvars to file */
void cvar_write(PHYSFS_file *file) void cvar_write(PHYSFS_file *file)
{ {
int i; range_for (const auto &i, cvar_list)
if (i.second.get().flags & CVAR_ARCHIVE)
for (i = 0; i < Num_cvars; i++) PHYSFSX_printf(file, "%s=%s\n", i.first, i.second.get().string.c_str());
if (cvar_list[i]->flags & CVAR_ARCHIVE)
PHYSFSX_printf(file, "%s=%s\n", cvar_list[i]->name, cvar_list[i]->string.c_str());
} }