WIP: optimized study orders, failing 15 study tests

This commit is contained in:
Enno Rehling 2017-10-09 18:25:57 +02:00
parent 905873e48b
commit f62c0866e3

View file

@ -62,10 +62,9 @@ order_data *load_data(int id) {
return NULL; return NULL;
} }
int save_data(order_data *od) { int save_data(keyword_t kwd, order_data *od) {
if (od->_str) { if (od->_str) {
struct cb_entry ent; struct cb_entry ent;
++od->_refcount; ++od->_refcount;
ent.id = ++auto_id; ent.id = ++auto_id;
ent.data = od; ent.data = od;
@ -131,7 +130,7 @@ keyword_t getkeyword(const order * ord)
*/ */
char* get_command(const order *ord, const struct locale *lang, char *sbuffer, size_t size) { char* get_command(const order *ord, const struct locale *lang, char *sbuffer, size_t size) {
char *bufp = sbuffer; char *bufp = sbuffer;
order_data *od; order_data *od = NULL;
const char * text; const char * text;
keyword_t kwd = ORD_KEYWORD(ord); keyword_t kwd = ORD_KEYWORD(ord);
int bytes; int bytes;
@ -155,8 +154,14 @@ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, si
} }
} }
od = load_data(ord->id); if (ord->id < 0) {
text = OD_STRING(od); skill_t sk = (skill_t)(100-ord->id);
assert(kwd == K_STUDY && sk != SK_MAGIC);
text = skillname(sk, lang);
} else {
od = load_data(ord->id);
text = OD_STRING(od);
}
if (kwd != NOKEYWORD) { if (kwd != NOKEYWORD) {
if (size > 0) { if (size > 0) {
const char *str = (const char *)LOC(lang, keyword(kwd)); const char *str = (const char *)LOC(lang, keyword(kwd));
@ -187,7 +192,9 @@ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, si
} }
} }
} }
release_data(od); if (od) {
release_data(od);
}
if (size > 0) *bufp = 0; if (size > 0) *bufp = 0;
return sbuffer; return sbuffer;
} }
@ -236,54 +243,48 @@ static char *mkdata(order_data **pdata, size_t len, const char *str)
char *result; char *result;
data = malloc(sizeof(order_data) + len + 1); data = malloc(sizeof(order_data) + len + 1);
result = (char *)(data + 1); result = (char *)(data + 1);
data->_refcount = 0; data->_refcount = 1;
data->_str = (len > 0) ? result : NULL; data->_str = (len > 0) ? result : NULL;
if (str) strcpy(result, str); if (str) strcpy(result, str);
if (pdata) *pdata = data; if (pdata) *pdata = data;
return result; return result;
} }
static order_data *create_data(keyword_t kwd, const char *sptr) static int create_data(keyword_t kwd, const char *s,
const struct locale *lang)
{ {
const char *s = sptr;
order_data *data; order_data *data;
int id;
if (kwd != NOKEYWORD) assert(kwd!=NOKEYWORD);
s = (*sptr) ? sptr : NULL;
/* orders with no parameter, only one order_data per order required */ if (!s || *s == 0) {
if (kwd != NOKEYWORD && *sptr == 0) { return 0;
mkdata(&data, 0, NULL);
data->_refcount = 1;
return data;
} }
if (kwd==K_STUDY) {
const char * sptr = s;
skill_t sk = get_skill(parse_token_depr(&sptr), lang);
if (sk != SK_MAGIC && sk != NOSKILL) {
return ((int)sk)-100;
}
}
/* TODO: between mkdata and release_data, this object is very
* short-lived. */
mkdata(&data, s ? strlen(s) : 0, s); mkdata(&data, s ? strlen(s) : 0, s);
data->_refcount = 1; id = save_data(kwd, data);
return data; release_data(data);
return id;
} }
static order *create_order_i(order *ord, keyword_t kwd, const char *sptr, bool persistent, static order *create_order_i(order *ord, const struct locale *lang,
bool noerror) keyword_t kwd, const char *sptr, bool persistent, bool noerror)
{ {
order_data *od;
assert(ord); assert(ord);
if (kwd == NOKEYWORD || keyword_disabled(kwd)) { if (kwd == NOKEYWORD || keyword_disabled(kwd)) {
log_error("trying to create an order for disabled keyword %s.", keyword(kwd)); log_error("trying to create an order for disabled keyword %s.", keyword(kwd));
return NULL; return NULL;
} }
/* if this is just nonsense, then we skip it. */
if (lomem) {
switch (kwd) {
case K_KOMMENTAR:
case NOKEYWORD:
return NULL;
default:
break;
}
}
ord->command = (int)kwd; ord->command = (int)kwd;
if (persistent) ord->command |= CMD_PERSIST; if (persistent) ord->command |= CMD_PERSIST;
if (noerror) ord->command |= CMD_QUIET; if (noerror) ord->command |= CMD_QUIET;
@ -291,10 +292,7 @@ static order *create_order_i(order *ord, keyword_t kwd, const char *sptr, bool p
while (isspace(*(unsigned char *)sptr)) ++sptr; while (isspace(*(unsigned char *)sptr)) ++sptr;
od = create_data(kwd, sptr); ord->id = create_data(kwd, sptr, lang);
ord->id = save_data(od);
release_data(od);
return ord; return ord;
} }
@ -353,7 +351,7 @@ order *create_order(keyword_t kwd, const struct locale * lang,
zBuffer[0] = 0; zBuffer[0] = 0;
} }
ord = (order *)malloc(sizeof(order)); ord = (order *)malloc(sizeof(order));
return create_order_i(ord, kwd, zBuffer, false, false); return create_order_i(ord, lang, kwd, zBuffer, false, false);
} }
order *parse_order(const char *s, const struct locale * lang) order *parse_order(const char *s, const struct locale * lang)
@ -385,7 +383,7 @@ order *parse_order(const char *s, const struct locale * lang)
} }
if (kwd != NOKEYWORD) { if (kwd != NOKEYWORD) {
order *ord = (order *)malloc(sizeof(order)); order *ord = (order *)malloc(sizeof(order));
return create_order_i(ord, kwd, sptr, persistent, noerror); return create_order_i(ord, lang, kwd, sptr, persistent, noerror);
} }
} }
return NULL; return NULL;