implement JSON prefixes. fix free_prefixes not resetting size (TODO: quicklist).

This commit is contained in:
Enno Rehling 2015-09-12 12:49:12 +02:00
parent f0e255924f
commit 47c95aee35
2 changed files with 25 additions and 3 deletions

View File

@ -31,6 +31,9 @@ without prior permission by the authors of Eressea.
#include "spellbook.h" #include "spellbook.h"
#include "calendar.h" #include "calendar.h"
/* game modules */
#include "prefix.h"
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
#include <util/bsdstring.h> #include <util/bsdstring.h>
@ -487,6 +490,17 @@ static void json_race(cJSON *json, race *rc) {
} }
} }
static void json_prefixes(cJSON *json) {
cJSON *child;
if (json->type != cJSON_Array) {
log_error("prefixes is not a json array: %d", json->type);
return;
}
for (child = json->child; child; child = child->next) {
add_raceprefix(child->valuestring);
}
}
static void json_terrains(cJSON *json) { static void json_terrains(cJSON *json) {
cJSON *child; cJSON *child;
if (json->type != cJSON_Object) { if (json->type != cJSON_Object) {
@ -837,6 +851,9 @@ void json_config(cJSON *json) {
else if (strcmp(child->string, "spells") == 0) { else if (strcmp(child->string, "spells") == 0) {
json_spells(child); json_spells(child);
} }
else if (strcmp(child->string, "prefixes") == 0) {
json_prefixes(child);
}
else if (strcmp(child->string, "terrains") == 0) { else if (strcmp(child->string, "terrains") == 0) {
json_terrains(child); json_terrains(child);
init_terrains(); init_terrains();

View File

@ -1,18 +1,23 @@
#include <platform.h> #include <platform.h>
#include "prefix.h" #include "prefix.h"
#include <assert.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
char **race_prefixes = NULL; char **race_prefixes = NULL;
static size_t size = 4;
static unsigned int next = 0;
void add_raceprefix(const char *prefix) void add_raceprefix(const char *prefix)
{ {
static size_t size = 4; assert(prefix);
static unsigned int next = 0; if (race_prefixes == NULL) {
if (race_prefixes == NULL) next = 0;
size = 4;
race_prefixes = malloc(size * sizeof(char *)); race_prefixes = malloc(size * sizeof(char *));
}
if (next + 1 == size) { if (next + 1 == size) {
size *= 2; size *= 2;
race_prefixes = realloc(race_prefixes, size * sizeof(char *)); race_prefixes = realloc(race_prefixes, size * sizeof(char *));