Merge pull request #196 from badgerman/develop

bugfixes in recent refactorings: report writing, syntax errors.
This commit is contained in:
Enno Rehling 2015-05-19 19:23:18 +02:00
commit b3214a1bf8
20 changed files with 131 additions and 95 deletions

View File

@ -142,6 +142,7 @@ function process(rules, orders)
callbacks(rules, 'update')
write_files(config.locales)
dbupdate()
file = '' .. get_turn() .. '.dat'
if eressea.write_game(file)~=0 then
@ -161,9 +162,6 @@ function run_turn(rules)
orderfile = orderfile or config.basepath .. '/orders.' .. turn
eressea.log.debug("executing turn " .. get_turn() .. " with " .. orderfile .. " with rules=" .. config.rules)
local result = process(rules, orderfile)
if result==0 then
dbupdate()
end
return result
end

View File

@ -287,7 +287,6 @@ void syntax_error(const struct unit *u, struct order *ord)
message * result;
result = msg_error(u, ord, 10);
ADDMSG(&u->faction->msgs, result);
msg_release(result);
}
extern unsigned int new_hashstring(const char *s);
@ -306,6 +305,7 @@ void free_messagelist(message_list * msgs)
message *add_message(message_list ** pm, message * m)
{
assert(m->type);
if (!lomem && m != NULL) {
struct mlist *mnew = malloc(sizeof(struct mlist));
if (*pm == NULL) {

View File

@ -42,7 +42,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* util includes */
#include <util/attrib.h>
#include <util/bsdstring.h>
#include <util/goodies.h>
#include <util/strings.h>
#include <util/lists.h>
#include <util/log.h>
#include <util/resolve.h>

View File

@ -22,7 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* util includes */
#include <critbit.h>
#include <util/goodies.h>
#include <util/strings.h>
#include <util/language.h>
#include <util/log.h>
#include <util/umlaut.h>

View File

@ -48,7 +48,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/base36.h>
#include <util/bsdstring.h>
#include <util/event.h>
#include <util/goodies.h>
#include <util/strings.h>
#include <util/language.h>
#include <util/lists.h>
#include <util/log.h>

View File

@ -2894,7 +2894,7 @@ void restack_units(void)
cmistake(u, ord, 260, MSG_EVENT);
}
else if (v == u) {
cmistake(u, ord, 10, MSG_EVENT);
syntax_error(u, ord);
}
else {
switch (p) {
@ -4381,17 +4381,17 @@ void init_processor(void)
add_proc_order(p, K_GROUP, group_cmd, 0, NULL);
p += 10;
add_proc_order(p, K_QUIT, &quit_cmd, 0, NULL);
// add_proc_order(p, K_URSPRUNG, &origin_cmd, 0, NULL);
add_proc_order(p, K_ALLY, &ally_cmd, 0, NULL);
add_proc_order(p, K_PREFIX, &prefix_cmd, 0, NULL);
add_proc_order(p, K_SETSTEALTH, &setstealth_cmd, 0, NULL);
add_proc_order(p, K_STATUS, &status_cmd, 0, NULL);
add_proc_order(p, K_COMBATSPELL, &combatspell_cmd, 0, NULL);
add_proc_order(p, K_DISPLAY, &display_cmd, 0, NULL);
add_proc_order(p, K_NAME, &name_cmd, 0, NULL);
add_proc_order(p, K_GUARD, &guard_off_cmd, 0, NULL);
add_proc_order(p, K_RESHOW, &reshow_cmd, 0, NULL);
add_proc_order(p, K_QUIT, quit_cmd, 0, NULL);
// add_proc_order(p, K_URSPRUNG, origin_cmd, 0, NULL);
add_proc_order(p, K_ALLY, ally_cmd, 0, NULL);
add_proc_order(p, K_PREFIX, prefix_cmd, 0, NULL);
add_proc_order(p, K_SETSTEALTH, setstealth_cmd, 0, NULL);
add_proc_order(p, K_STATUS, status_cmd, 0, NULL);
add_proc_order(p, K_COMBATSPELL, combatspell_cmd, 0, NULL);
add_proc_order(p, K_DISPLAY, display_cmd, 0, NULL);
add_proc_order(p, K_NAME, name_cmd, 0, NULL);
add_proc_order(p, K_GUARD, guard_off_cmd, 0, NULL);
add_proc_order(p, K_RESHOW, reshow_cmd, 0, NULL);
if (get_param_int(global.parameters, "rules.alliances", 0) == 1) {
p += 10;
@ -4403,7 +4403,7 @@ void init_processor(void)
add_proc_order(p, K_MAIL, mail_cmd, 0, "Botschaften");
p += 10; /* all claims must be done before we can USE */
add_proc_region(p, &enter_1, "Betreten (1. Versuch)"); /* for GIVE CONTROL */
add_proc_region(p, enter_1, "Betreten (1. Versuch)"); /* for GIVE CONTROL */
add_proc_order(p, K_USE, use_cmd, 0, "Benutzen");
p += 10; /* in case it has any effects on alliance victories */
@ -4426,11 +4426,11 @@ void init_processor(void)
p += 10; /* can't allow reserve before siege (weapons) */
add_proc_region(p, enter_1, "Betreten (3. Versuch)"); /* to claim a castle after a victory and to be able to DESTROY it in the same turn */
if (get_param_int(global.parameters, "rules.reserve.twophase", 0)) {
add_proc_order(p, K_RESERVE, &reserve_self, 0, "RESERVE (self)");
add_proc_order(p, K_RESERVE, reserve_self, 0, "RESERVE (self)");
p += 10;
}
add_proc_order(p, K_RESERVE, &reserve_cmd, 0, "RESERVE (all)");
add_proc_order(p, K_CLAIM, &claim_cmd, 0, NULL);
add_proc_order(p, K_RESERVE, reserve_cmd, 0, "RESERVE (all)");
add_proc_order(p, K_CLAIM, claim_cmd, 0, NULL);
add_proc_unit(p, follow_unit, "Folge auf Einheiten setzen");
p += 10; /* rest rng again before economics */
@ -4438,13 +4438,13 @@ void init_processor(void)
add_proc_region(p, force_leave, "kick non-allies out of buildings/ships");
}
add_proc_region(p, economics, "Zerstoeren, Geben, Rekrutieren, Vergessen");
add_proc_order(p, K_PROMOTION, &promotion_cmd, 0, "Heldenbefoerderung");
add_proc_order(p, K_PROMOTION, promotion_cmd, 0, "Heldenbefoerderung");
p += 10;
if (!keyword_disabled(K_PAY)) {
add_proc_order(p, K_PAY, &pay_cmd, 0, "Gebaeudeunterhalt (disable)");
add_proc_order(p, K_PAY, pay_cmd, 0, "Gebaeudeunterhalt (disable)");
}
add_proc_postregion(p, &maintain_buildings_1,
add_proc_postregion(p, maintain_buildings_1,
"Gebaeudeunterhalt (1. Versuch)");
p += 10; /* QUIT fuer sich alleine */
@ -4452,68 +4452,68 @@ void init_processor(void)
if (!keyword_disabled(K_CAST)) {
p += 10;
add_proc_global(p, &magic, "Zaubern");
add_proc_global(p, magic, "Zaubern");
}
p += 10;
add_proc_order(p, K_TEACH, &teach_cmd, PROC_THISORDER | PROC_LONGORDER,
add_proc_order(p, K_TEACH, teach_cmd, PROC_THISORDER | PROC_LONGORDER,
"Lehren");
p += 10;
add_proc_order(p, K_STUDY, &learn_cmd, PROC_THISORDER | PROC_LONGORDER,
add_proc_order(p, K_STUDY, learn_cmd, PROC_THISORDER | PROC_LONGORDER,
"Lernen");
p += 10;
add_proc_order(p, K_MAKE, &make_cmd, PROC_THISORDER | PROC_LONGORDER,
add_proc_order(p, K_MAKE, make_cmd, PROC_THISORDER | PROC_LONGORDER,
"Produktion");
add_proc_postregion(p, &produce, "Arbeiten, Handel, Rekruten");
add_proc_postregion(p, &split_allocations, "Produktion II");
add_proc_postregion(p, produce, "Arbeiten, Handel, Rekruten");
add_proc_postregion(p, split_allocations, "Produktion II");
p += 10;
add_proc_region(p, &enter_2, "Betreten (4. Versuch)"); /* Once again after QUIT */
add_proc_region(p, enter_2, "Betreten (4. Versuch)"); /* Once again after QUIT */
p += 10;
add_proc_region(p, &sinkships, "Schiffe sinken");
add_proc_region(p, sinkships, "Schiffe sinken");
p += 10;
add_proc_global(p, &movement, "Bewegungen");
add_proc_global(p, movement, "Bewegungen");
if (get_param_int(global.parameters, "work.auto", 0)) {
p += 10;
add_proc_region(p, &auto_work, "Arbeiten (auto)");
add_proc_region(p, auto_work, "Arbeiten (auto)");
}
p += 10;
add_proc_order(p, K_GUARD, &guard_on_cmd, 0, "Bewache (an)");
add_proc_order(p, K_GUARD, guard_on_cmd, 0, "Bewache (an)");
if (get_param_int(global.parameters, "rules.encounters", 0)) {
p += 10;
add_proc_global(p, &encounters, "Zufallsbegegnungen");
add_proc_global(p, encounters, "Zufallsbegegnungen");
}
p += 10;
add_proc_unit(p, &monster_kills_peasants,
add_proc_unit(p, monster_kills_peasants,
"Monster fressen und vertreiben Bauern");
p += 10;
add_proc_global(p, &randomevents, "Zufallsereignisse");
add_proc_global(p, randomevents, "Zufallsereignisse");
p += 10;
add_proc_global(p, &monthly_healing, "Regeneration (HP)");
add_proc_global(p, &regenerate_aura, "Regeneration (Aura)");
add_proc_global(p, monthly_healing, "Regeneration (HP)");
add_proc_global(p, regenerate_aura, "Regeneration (Aura)");
if (!keyword_disabled(K_DEFAULT)) {
add_proc_global(p, &defaultorders, "Defaults setzen");
add_proc_global(p, defaultorders, "Defaults setzen");
}
add_proc_global(p, &demographics, "Nahrung, Seuchen, Wachstum, Wanderung");
add_proc_global(p, demographics, "Nahrung, Seuchen, Wachstum, Wanderung");
if (!keyword_disabled(K_SORT)) {
p += 10;
add_proc_global(p, restack_units, "Einheiten sortieren");
}
if (!keyword_disabled(K_NUMBER)) {
add_proc_order(p, K_NUMBER, &renumber_cmd, 0, "Neue Nummern (Einheiten)");
add_proc_order(p, K_NUMBER, renumber_cmd, 0, "Neue Nummern (Einheiten)");
p += 10;
add_proc_global(p, &renumber_factions, "Neue Nummern");
add_proc_global(p, renumber_factions, "Neue Nummern");
}
}

View File

@ -2527,7 +2527,7 @@ static castorder *cast_cmd(unit * u, order * ord)
level = _min(p, level);
if (level < 1) {
/* Fehler "Das macht wenig Sinn" */
cmistake(u, ord, 10, MSG_MAGIC);
syntax_error(u, ord);
return 0;
}
s = gettoken(token, sizeof(token));
@ -2557,7 +2557,7 @@ static castorder *cast_cmd(unit * u, order * ord)
level = _min(p, level);
if (level < 1) {
/* Fehler "Das macht wenig Sinn" */
cmistake(u, ord, 10, MSG_MAGIC);
syntax_error(u, ord);
return 0;
}
s = gettoken(token, sizeof(token));

View File

@ -49,7 +49,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/base36.h>
#include <util/event.h>
#include <util/functions.h>
#include <util/goodies.h>
#include <util/strings.h>
#include <util/lists.h>
#include <util/log.h>
#include <util/resolve.h>

View File

@ -43,7 +43,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/attrib.h>
#include <util/base36.h>
#include <util/functions.h>
#include <util/goodies.h>
#include <util/strings.h>
#include <util/language.h>
#include <storage.h>

View File

@ -120,7 +120,14 @@ void newline(stream *out) {
sputs("", out);
}
static const char *spaces = " ";
void write_spaces(stream *out, size_t num) {
static const char spaces[REPORTWIDTH] = " ";
while (num > 0) {
size_t bytes = (num > REPORTWIDTH) ? REPORTWIDTH : num;
swrite(spaces, sizeof(char), bytes, out);
num -= bytes;
}
}
static void centre(stream *out, const char *s, bool breaking)
{
@ -140,9 +147,8 @@ static void centre(stream *out, const char *s, bool breaking)
freestrlist(T);
}
else {
swrite(spaces, sizeof(char), (REPORTWIDTH - strlen(s) + 1) / 2, out);
write_spaces(out, (REPORTWIDTH - strlen(s) + 1) / 2);
sputs(s, out);
newline(out);
}
}
@ -176,16 +182,16 @@ char marker)
const char *last_space = begin;
if (mark && indent >= 2) {
swrite(spaces, sizeof(char), indent - 2, out);
write_spaces(out, indent - 2);
swrite(mark, sizeof(char), 1, out);
swrite(spaces, sizeof(char), 1, out);
write_spaces(out, 1);
mark = 0;
}
else if (begin == str) {
swrite(spaces, sizeof(char), indent, out);
write_spaces(out, indent);
}
else {
swrite(spaces, sizeof(char), indent + hanging_indent, out);
write_spaces(out, indent + hanging_indent);
}
while (*end && end <= begin + length - indent) {
if (*end == ' ') {
@ -518,7 +524,7 @@ void sparagraph(strlist ** SP, const char *s, unsigned int indent, char mark)
width = REPORTWIDTH - indent;
firstline = 1;
for (;;) {
while (s[0]) {
unsigned int j = 0, i;
for (i=0; s[j]; j=i) {
@ -535,7 +541,7 @@ void sparagraph(strlist ** SP, const char *s, unsigned int indent, char mark)
break;
}
i = j + 1;
} while (s[j]);
}
for (j = 0; j != indent; j++)
buf[j] = ' ';

View File

@ -15,8 +15,10 @@
extern "C" {
#endif
struct stream;
void register_nr(void);
void report_cleanup(void);
void write_spaces(struct stream *out, size_t num);
#ifdef __cplusplus
}

View File

@ -1,6 +1,7 @@
#include <platform.h>
#include <config.h>
#include "reports.h"
#include "report.h"
#include <kernel/building.h>
#include <kernel/faction.h>
@ -10,6 +11,8 @@
#include <kernel/unit.h>
#include <quicklist.h>
#include <stream.h>
#include <memstream.h>
#include <CuTest.h>
#include <tests.h>
@ -98,11 +101,43 @@ static void test_seen_faction(CuTest *tc) {
CuAssertTrue(tc, f1->no < f2->no);
}
static void test_write_spaces(CuTest *tc) {
stream out = { 0 };
char buf[1024];
size_t len;
mstream_init(&out);
write_spaces(&out, 4);
out.api->rewind(out.handle);
len = out.api->read(out.handle, buf, sizeof(buf));
buf[len] = '\0';
CuAssertStrEquals(tc, " ", buf);
CuAssertIntEquals(tc, ' ', buf[3]);
mstream_done(&out);
}
static void test_write_many_spaces(CuTest *tc) {
stream out = { 0 };
char buf[1024];
size_t len;
mstream_init(&out);
write_spaces(&out, 100);
out.api->rewind(out.handle);
len = out.api->read(out.handle, buf, sizeof(buf));
buf[len] = '\0';
CuAssertIntEquals(tc, 100, (int)len);
CuAssertIntEquals(tc, ' ', buf[99]);
mstream_done(&out);
}
CuSuite *get_reports_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_reorder_units);
SUITE_ADD_TEST(suite, test_seen_faction);
SUITE_ADD_TEST(suite, test_regionid);
SUITE_ADD_TEST(suite, test_write_spaces);
SUITE_ADD_TEST(suite, test_write_many_spaces);
return suite;
}

View File

@ -15,7 +15,7 @@
#include "crmessage.h"
#include "message.h"
#include "goodies.h"
#include "strings.h"
#include "log.h"
#include <stdio.h>

View File

@ -19,10 +19,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <platform.h>
#include "goodies.h"
#include "unicode.h"
/* libc includes */
#include <wctype.h>
#include <stdlib.h>
#include <string.h>
@ -53,23 +50,6 @@ int *intlist_find(int *i_p, int fi)
return NULL;
}
char *set_string(char **s, const char *neu)
{
if (neu == NULL) {
free(*s);
*s = NULL;
}
else if (*s == NULL) {
*s = malloc(strlen(neu) + 1);
strcpy(*s, neu);
}
else {
*s = realloc(*s, strlen(neu) + 1);
strcpy(*s, neu);
}
return *s;
}
static int spc_email_isvalid(const char *address)
{
int count = 0;

View File

@ -19,18 +19,15 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef GOODIES_H
#define GOODIES_H
#include "strings.h"
#ifdef __cplusplus
extern "C" {
#endif
extern char *set_string(char **s, const char *neu);
extern int set_email(char **pemail, const char *newmail);
int set_email(char **pemail, const char *newmail);
extern int *intlist_init(void);
extern int *intlist_add(int *i_p, int i);
extern int *intlist_find(int *i_p, int i);
int *intlist_init(void);
int *intlist_add(int *i_p, int i);
int *intlist_find(int *i_p, int i);
#ifdef __cplusplus
}

View File

@ -21,7 +21,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "language_struct.h"
#include "log.h"
#include "goodies.h"
#include "strings.h"
#include "umlaut.h"
#include <stdlib.h>

View File

@ -14,7 +14,7 @@
#include <platform.h>
#include "message.h"
#include "goodies.h"
#include "strings.h"
#include "log.h"
#include "quicklist.h"

View File

@ -21,7 +21,7 @@
#include "message.h"
#include "language.h"
#include "translation.h"
#include "goodies.h"
#include "strings.h"
/* libc includes */
#include <assert.h>

View File

@ -21,6 +21,24 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* libc includes */
#include <string.h>
#include <assert.h>
#include <stdlib.h>
char *set_string(char **s, const char *neu)
{
if (neu == NULL) {
free(*s);
*s = NULL;
}
else if (*s == NULL) {
*s = malloc(strlen(neu) + 1);
strcpy(*s, neu);
}
else {
*s = realloc(*s, strlen(neu) + 1);
strcpy(*s, neu);
}
return *s;
}
unsigned int hashstring(const char *s)
{

View File

@ -22,11 +22,11 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
extern "C" {
#endif
extern unsigned int hashstring(const char *s);
extern const char *escape_string(const char *str, char *buffer,
size_t len);
extern unsigned int jenkins_hash(unsigned int a);
extern unsigned int wang_hash(unsigned int a);
char *set_string(char **s, const char *neu);
unsigned int hashstring(const char *s);
const char *escape_string(const char *str, char *buffer, size_t len);
unsigned int jenkins_hash(unsigned int a);
unsigned int wang_hash(unsigned int a);
/* benchmark for units:
* JENKINS_HASH: 5.25 misses/hit (with good cache behavior)