finish new order parser, maybe?

This commit is contained in:
Enno Rehling 2018-10-01 21:32:59 +02:00
parent 90b80aba89
commit eed3ab3acf
3 changed files with 48 additions and 34 deletions

View File

@ -249,6 +249,8 @@ static void handle_faction(void *userData, int no, const char *password) {
log_debug("invalid password for faction %s", itoa36(no));
ADDMSG(&f->msgs, msg_message("wrongpasswd", "password", password));
}
state->u = NULL;
state->next_order = NULL;
}
}
@ -272,10 +274,36 @@ static void handle_unit(void *userData, int no) {
static void handle_order(void *userData, const char *str) {
parser_state *state = (parser_state *)userData;
unit * u = state->u;
order *ord;
const char * tok, *input = str;
char buffer[16];
const struct locale *lang;
param_t p;
faction * f = state->f;
ord = parse_order(str, u->faction->locale);
lang = f ? f->locale : default_locale;
tok = parse_token(&input, buffer, sizeof(buffer));
p = findparam(tok, lang);
if (p == P_FACTION || p == P_GAMENAME) {
tok = parse_token(&input, buffer, sizeof(buffer));
if (tok) {
int no = atoi36(tok);
tok = parse_token(&input, buffer, sizeof(buffer));
handle_faction(userData, no, tok);
}
else {
/* TODO: log_error() */
}
}
else if (p == P_UNIT) {
tok = parse_token(&input, buffer, sizeof(buffer));
if (tok) {
int no = atoi36(tok);
handle_unit(userData, no);
}
}
else if (state->u) {
unit * u = state->u;
order * ord = parse_order(str, lang);
if (ord) {
*state->next_order = ord;
state->next_order = &ord->next;
@ -284,7 +312,7 @@ static void handle_order(void *userData, const char *str) {
ADDMSG(&u->faction->msgs, msg_message("parse_error", "unit command", u, str));
}
}
}
int parseorders(FILE *F)
{
@ -298,8 +326,6 @@ int parseorders(FILE *F)
/* TODO: error message */
return errno;
}
OP_SetUnitHandler(parser, handle_unit);
OP_SetFactionHandler(parser, handle_faction);
OP_SetOrderHandler(parser, handle_order);
OP_SetUserData(parser, &state);
@ -323,7 +349,7 @@ int parseorders(FILE *F)
int readorders(FILE *F)
{
#undef NEW_PARSER
#define NEW_PARSER
#ifdef NEW_PARSER
return parseorders(F);
#else

View File

@ -14,22 +14,11 @@ struct OrderParserStruct {
char *m_buffer;
char *m_bufferPtr;
const char *m_bufferEnd;
OP_FactionHandler m_factionHandler;
OP_UnitHandler m_unitHandler;
OP_OrderHandler m_orderHandler;
enum OP_Error m_errorCode;
int m_lineNumber;
};
void OP_SetUnitHandler(OP_Parser parser, OP_UnitHandler handler)
{
parser->m_unitHandler = handler;
}
void OP_SetFactionHandler(OP_Parser parser, OP_FactionHandler handler) {
parser->m_factionHandler = handler;
}
void OP_SetOrderHandler(OP_Parser parser, OP_OrderHandler handler) {
parser->m_orderHandler = handler;
}
@ -95,13 +84,6 @@ static enum OP_Error buffer_append(OP_Parser parser, const char *s, int len)
return OP_ERROR_NONE;
}
static enum OP_Error handle_line(OP_Parser parser) {
if (parser->m_orderHandler) {
parser->m_orderHandler(parser->m_userData, parser->m_bufferPtr);
}
return OP_ERROR_NONE;
}
static char *skip_spaces(char *pos) {
char *next;
for (next = pos; *next && *next != '\n'; ++next) {
@ -111,6 +93,16 @@ static char *skip_spaces(char *pos) {
return next;
}
static enum OP_Error handle_line(OP_Parser parser) {
if (parser->m_orderHandler) {
char * str = skip_spaces(parser->m_bufferPtr);
if (*str) {
parser->m_orderHandler(parser->m_userData, str);
}
}
return OP_ERROR_NONE;
}
static enum OP_Status parse_buffer(OP_Parser parser, int isFinal)
{
char * pos = strpbrk(parser->m_bufferPtr, "\\;\n");

View File

@ -29,16 +29,12 @@ enum OP_Error {
OP_ERROR_SYNTAX
};
typedef void(*OP_FactionHandler) (void *userData, int no, const char *password);
typedef void(*OP_UnitHandler) (void *userData, int no);
typedef void(*OP_OrderHandler) (void *userData, const char *str);
OP_Parser OP_ParserCreate(void);
void OP_ParserFree(OP_Parser parser);
void OP_ParserReset(OP_Parser parser);
enum OP_Status OP_Parse(OP_Parser parser, const char *s, int len, int isFinal);
void OP_SetUnitHandler(OP_Parser parser, OP_UnitHandler handler);
void OP_SetFactionHandler(OP_Parser parser, OP_FactionHandler handler);
void OP_SetOrderHandler(OP_Parser parser, OP_OrderHandler handler);
void OP_SetUserData(OP_Parser parser, void *userData);