Cleanup: Removing parser-code from kernel/eressea.[hc] and putting it into

separate parser files in util/
This commit is contained in:
Enno Rehling 2007-06-21 22:31:28 +00:00
parent 5dbeb63531
commit a5b7b13a6d
20 changed files with 181 additions and 141 deletions

View File

@ -61,6 +61,7 @@
#include <util/goodies.h>
#include <util/lists.h>
#include <util/message.h>
#include <util/parser.h>
#include <util/rng.h>
/* libs includes */

View File

@ -24,6 +24,7 @@
#include <util/attrib.h>
#include <util/functions.h>
#include <util/parser.h>
#include <util/rand.h>
#include <limits.h>

View File

@ -78,6 +78,7 @@
#include <util/goodies.h>
#include <util/lists.h>
#include <util/log.h>
#include <util/parser.h>
#include <util/rand.h>
#include <util/rng.h>
#include <util/sql.h>

View File

@ -46,6 +46,7 @@
/* util includes */
#include <util/attrib.h>
#include <util/base36.h>
#include <util/parser.h>
#include <util/rand.h>
/* libc includes */

View File

@ -58,6 +58,7 @@
#include <util/cvector.h>
#include <util/lists.h>
#include <util/log.h>
#include <util/parser.h>
#include <util/rand.h>
#include <util/rng.h>

View File

@ -49,6 +49,7 @@
#include <base36.h>
#include <event.h>
#include <goodies.h>
#include <parser.h>
#include <resolve.h>
#include <xml.h>

View File

@ -64,6 +64,7 @@
#include <util/functions.h>
#include <util/log.h>
#include <util/lists.h>
#include <util/parser.h>
#include <util/rng.h>
#include <util/sql.h>
#include <util/translation.h>
@ -1288,87 +1289,11 @@ count_maxmigrants(const faction * f)
return x;
}
/*------------------------------------------------------------------*/
/* GET STR, I zur Eingabe von Daten liest diese aus dem Buffer, der beim ersten
* Aufruf inititialisiert wird? */
typedef struct parser_state {
const unsigned char *current_token;
char * current_cmd;
struct parser_state * next;
} parser_state;
static parser_state * state;
void
init_tokens_str(const char * initstr)
{
if (state==NULL) {
state = malloc(sizeof(parser_state));
state->current_cmd = NULL;
}
state->current_token = (const unsigned char *)initstr;
}
void
init_tokens(const struct order * ord)
{
if (state==NULL) state = malloc(sizeof(parser_state));
else if (state->current_cmd!=NULL) free(state->current_cmd);
state->current_cmd = getcommand(ord);
init_tokens_str(state->current_cmd);
}
void
parser_pushstate(void)
{
parser_state * new_state = malloc(sizeof(parser_state));
new_state->current_cmd = NULL;
new_state->current_token = NULL;
new_state->next = state;
state = new_state;
}
void
parser_popstate(void)
{
parser_state * new_state = state->next;
if (state->current_cmd!=NULL) free(state->current_cmd);
free(state);
state = new_state;
}
boolean
parser_end(void)
{
while (isspace(*state->current_token)) ++state->current_token;
return *state->current_token == 0;
}
void
skip_token(void)
{
char quotechar = 0;
while (isspace(*state->current_token)) ++state->current_token;
while (*state->current_token) {
if (isspace(*state->current_token) && quotechar==0) {
return;
} else {
switch(*state->current_token) {
case '"':
case '\'':
if (*state->current_token==quotechar) return;
quotechar = *state->current_token;
break;
case ESCAPE_CHAR:
++state->current_token;
break;
}
}
++state->current_token;
}
char * cmd = getcommand(ord);
init_tokens_str(cmd, cmd);
}
void
@ -1396,64 +1321,14 @@ parse(keyword_t kword, int (*dofun)(unit *, struct order *), boolean thisorder)
}
}
const char *
parse_token(const char ** str)
{
static char lbuf[DISPLAYSIZE + 1];
char * cursor = lbuf;
char quotechar = 0;
boolean escape = false;
const unsigned char * ctoken = (const unsigned char*)*str;
assert(ctoken);
while (isspace(*ctoken)) ++ctoken;
while (*ctoken && cursor-lbuf < DISPLAYSIZE) {
if (escape) {
*cursor++ = *ctoken++;
} else if (isspace(*ctoken)) {
if (quotechar==0) break;
*cursor++ = *ctoken++;
} else if (*ctoken=='"' || *ctoken=='\'') {
if (*ctoken==quotechar) {
++ctoken;
break;
} else if (quotechar==0) {
quotechar = *ctoken;
++ctoken;
} else {
*cursor++ = *ctoken++;
}
} else if (*ctoken==SPACE_REPLACEMENT) {
*cursor++ = ' ';
++ctoken;
} else if (*ctoken==ESCAPE_CHAR) {
escape = true;
++ctoken;
} else {
*cursor++ = *ctoken++;
}
}
*cursor = '\0';
*str = (const char *)ctoken;
return lbuf;
}
const char *
igetstrtoken(const char * initstr)
{
if (initstr!=NULL) {
init_tokens_str(initstr);
init_tokens_str(initstr, NULL);
}
return parse_token((const char**)&state->current_token);
}
const char *
getstrtoken(void)
{
return parse_token((const char**)&state->current_token);
return getstrtoken();
}
int

View File

@ -94,8 +94,6 @@ extern "C" {
*/
#define ENCCHANCE 10 /* %-Chance für einmalige Zufallsbegegnung */
#define SPACE_REPLACEMENT '~'
#define ESCAPE_CHAR '\\'
#define DISPLAYSIZE 8191 /* max. Länge einer Beschreibung, ohne trailing 0 */
#define NAMESIZE 127 /* max. Länge eines Namens, ohne trailing 0 */
@ -206,16 +204,8 @@ int geti(void);
extern int findstr(const char **v, const char *s, unsigned char n);
extern const char *igetstrtoken(const char *s);
extern const char *getstrtoken(void);
extern void init_tokens_str(const char * initstr); /* initialize token parsing */
extern void init_tokens(const struct order * ord); /* initialize token parsing */
extern void skip_token(void);
extern const char * parse_token(const char ** str);
extern void parser_pushstate(void);
extern void parser_popstate(void);
extern boolean parser_end(void);
extern skill_t findskill(const char *s, const struct locale * lang);
extern keyword_t findkeyword(const char *s, const struct locale * lang);

View File

@ -54,6 +54,7 @@
/* util includes */
#include <util/attrib.h>
#include <util/lists.h>
#include <util/parser.h>
#include <util/resolve.h>
#include <util/rand.h>
#include <util/rng.h>

View File

@ -51,6 +51,7 @@
#include <util/goodies.h>
#include <util/language.h>
#include <util/lists.h>
#include <util/parser.h>
#include <util/rand.h>
#include <util/rng.h>

View File

@ -18,6 +18,7 @@
#include <util/bsdstring.h>
#include <util/language.h>
#include <util/parser.h>
/* libc includes */
#include <assert.h>

View File

@ -31,6 +31,8 @@
#include "race.h"
#include "unit.h"
#include <util/parser.h>
#include <assert.h>
#include <stdlib.h>

View File

@ -59,6 +59,7 @@
#include <util/event.h>
#include <util/goodies.h>
#include <util/lists.h>
#include <util/parser.h>
#include <util/resolve.h>
#include <util/sql.h>
#include <util/rand.h>
@ -533,7 +534,7 @@ unitorders(FILE * F, struct faction * f)
if (!getbuf(F)) break;
init_tokens_str(buf);
init_tokens_str(buf, NULL);
s = getstrtoken();
if (findkeyword(s, u->faction->locale) == NOKEYWORD) {

View File

@ -44,6 +44,7 @@
/* util includes */
#include <util/attrib.h>
#include <util/parser.h>
#include <util/rand.h>
#include <util/rng.h>
#include <util/vset.h>

View File

@ -41,6 +41,7 @@
#include <util/goodies.h>
#include <util/lists.h>
#include <util/umlaut.h>
#include <util/parser.h>
#include <util/rng.h>
/* libc includes */

View File

@ -33,6 +33,7 @@
/* util includes */
#include <util/attrib.h>
#include <util/base36.h>
#include <util/parser.h>
/* libc includes */
#include <stdlib.h>

View File

@ -63,6 +63,7 @@ extern void ct_register(const struct curse_type * ct);
#include <util/base36.h>
#include <util/umlaut.h>
#include <util/message.h>
#include <util/parser.h>
#include <util/event.h>
#include <util/functions.h>
#include <util/lists.h>

View File

@ -25,6 +25,7 @@ SOURCES =
log.c
message.c
nrmessage.c
parser.c
rand.c
resolve.c
sql.c

129
src/common/util/parser.c Normal file
View File

@ -0,0 +1,129 @@
#include <config.h>
#include "parser.h"
#include <assert.h>
#include <ctype.h>
#define SPACE_REPLACEMENT '~'
#define ESCAPE_CHAR '\\'
#define MAXTOKENSIZE 8192
typedef struct parser_state {
const unsigned char *current_token;
char * current_cmd;
struct parser_state * next;
} parser_state;
static parser_state * state;
void
init_tokens_str(const char * initstr, char * cmd)
{
if (state==NULL) {
state = malloc(sizeof(parser_state));
}
if (state->current_cmd) free(state->current_cmd);
state->current_cmd = cmd;
state->current_token = (const unsigned char *)initstr;
}
void
parser_pushstate(void)
{
parser_state * new_state = malloc(sizeof(parser_state));
new_state->current_cmd = NULL;
new_state->current_token = NULL;
new_state->next = state;
state = new_state;
}
void
parser_popstate(void)
{
parser_state * new_state = state->next;
if (state->current_cmd!=NULL) free(state->current_cmd);
free(state);
state = new_state;
}
boolean
parser_end(void)
{
while (isspace(*state->current_token)) ++state->current_token;
return *state->current_token == 0;
}
void
skip_token(void)
{
char quotechar = 0;
while (isspace(*state->current_token)) ++state->current_token;
while (*state->current_token) {
if (isspace(*state->current_token) && quotechar==0) {
return;
} else {
switch(*state->current_token) {
case '"':
case '\'':
if (*state->current_token==quotechar) return;
quotechar = *state->current_token;
break;
case ESCAPE_CHAR:
++state->current_token;
break;
}
}
++state->current_token;
}
}
const char *
parse_token(const char ** str)
{
static char lbuf[MAXTOKENSIZE];
char * cursor = lbuf;
char quotechar = 0;
boolean escape = false;
const unsigned char * ctoken = (const unsigned char*)*str;
assert(ctoken);
while (isspace(*ctoken)) ++ctoken;
while (*ctoken && cursor-lbuf < MAXTOKENSIZE-1) {
if (escape) {
*cursor++ = *ctoken++;
} else if (isspace(*ctoken)) {
if (quotechar==0) break;
*cursor++ = *ctoken++;
} else if (*ctoken=='"' || *ctoken=='\'') {
if (*ctoken==quotechar) {
++ctoken;
break;
} else if (quotechar==0) {
quotechar = *ctoken;
++ctoken;
} else {
*cursor++ = *ctoken++;
}
} else if (*ctoken==SPACE_REPLACEMENT) {
*cursor++ = ' ';
++ctoken;
} else if (*ctoken==ESCAPE_CHAR) {
escape = true;
++ctoken;
} else {
*cursor++ = *ctoken++;
}
}
*cursor = '\0';
*str = (const char *)ctoken;
return lbuf;
}
const char *
getstrtoken(void)
{
return parse_token((const char**)&state->current_token);
}

29
src/common/util/parser.h Normal file
View File

@ -0,0 +1,29 @@
/* vi: set ts=2:
* +-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
* | | Enno Rehling <enno@eressea.de>
* | Eressea PBEM host | Katja Zedel <katze@felidae.kn-bremen.de>
* | (c) 1998 - 2007 |
* | | This program may not be used, modified or distributed
* +-------------------+ without prior permission by the authors of Eressea.
*
*/
#ifndef UTIL_PARSER_H
#define UTIL_PARSER_H
#ifdef __cplusplus
extern "C" {
#endif
extern void init_tokens_str(const char * initstr, char * cmd); /* initialize token parsing, take ownership of cmd */
extern void skip_token(void);
extern const char * parse_token(const char ** str);
extern void parser_pushstate(void);
extern void parser_popstate(void);
extern boolean parser_end(void);
extern const char *getstrtoken(void);
#ifdef __cplusplus
}
#endif
#endif