(weekday, daytime, daytime_range): New; replace kw_parse(). Parsing

is somewhat stricter.
(is_wday_allowed, is_daytime_allowed, is_daytime_nar,
min_to_next_daytime): New.
(demand_update_time, scheduled_update_time, next_scheduled_time,
update_time, gamehours): Use them to replace kw_parse().
(kw_parse): No longer used, remove.
This commit is contained in:
Markus Armbruster 2004-04-04 15:52:26 +00:00
parent ded3daf70d
commit acdee1e3b6
12 changed files with 211 additions and 295 deletions

View file

@ -1,49 +0,0 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* keyword.h: Definitions for parsed keyword package
*
* Known contributors to this file:
*
*/
#ifndef _KEYWORD_H_
#define _KEYWORD_H_
#include <stdio.h>
#include "misc.h"
#define CF_VALUE 1
#define CF_TIME 2
#define CF_TIMERANGE 3
#define CF_WEEKDAY 4
extern s_char *kw_parse(int, s_char *, int *);
extern s_char *get_time(s_char *, int *);
extern s_char *weekday(s_char *, int *);
#endif /* _KEYWORD_H_ */

View file

@ -119,7 +119,11 @@ extern void filetruncate(s_char *);
/* hap_fact.c */
extern double hap_fact(struct natstr *, struct natstr *);
/* hours.c */
extern int is_wday_allowed(int, char *);
extern int is_daytime_allowed(int, char *);
extern int gamehours(time_t);
extern int is_daytime_near(int, char *, int);
extern int min_to_next_daytime(int, char *);
/* land.c */
extern int has_units(coord, coord, natid, struct lndstr *);
extern int has_units_with_mob(coord, coord, natid);

View file

@ -37,7 +37,6 @@
#include "player.h"
#include "commands.h"
#include "optlist.h"
#include "keyword.h"
#include "wantupd.h"
#include "server.h"

View file

@ -36,12 +36,12 @@ LIB = $(SRCDIR)/lib/libcommon.a
NTLIB = $(SRCDIR)\lib\libcommon.lib
OBJS = bestpath.o bridgefall.o check.o damage.o file.o fsize.o \
hap_fact.o hours.o keyword.o land.o log.o mailbox.o maps.o move.o \
hap_fact.o hours.o land.o log.o mailbox.o maps.o move.o \
nat.o path.o res_pop.o sectdamage.o \
stmtch.o tfact.o type.o vlist.o wantupd.o xy.o
NTOBJS = bestpath.obj bridgefall.obj check.obj damage.obj file.obj fsize.obj \
hap_fact.obj hours.obj keyword.obj land.obj log.obj \
hap_fact.obj hours.obj land.obj log.obj \
mailbox.obj maps.obj move.obj nat.obj path.obj res_pop.obj \
sectdamage.obj stmtch.obj tfact.obj \
type.obj vlist.obj wantupd.obj xy.obj

View file

@ -33,54 +33,198 @@
* Steve McClure, 1998
*/
#include <errno.h>
#include <stdio.h>
#include <limits.h>
#include <time.h>
#include "misc.h"
#include "nat.h"
#include "tel.h"
#include "proto.h"
#include "com.h"
#include "keyword.h"
#include "file.h"
#include "common.h"
#include "optlist.h"
#include <time.h>
static char *weekday(char *str, int *wday);
static char *daytime(char *str, int *min);
static char *daytime_range(char *str, int *from_min, int *to_min);
/*
* returns true if game can be played now.
* Is week day WDAY (Sunday is 0) allowed by restriction DAYS?
* If DAYS is not empty, it lists the allowed week day names. See
* weekday() for syntax.
*/
int
gamehours(time_t now)
is_wday_allowed(int wday, char *days)
{
register s_char *bp;
register struct tm *tm;
int day;
int curtime;
int okday[7];
int hour[2];
int wd;
tm = localtime(&now);
curtime = tm->tm_min + tm->tm_hour * 60;
bp = game_days;
if (*bp != 0) {
for (day = 0; day < 7; day++)
okday[day] = 0;
while (NULL != (bp = kw_parse(CF_WEEKDAY, bp, &day)))
okday[day] = 1;
} else {
for (day = 0; day < 7; day++)
okday[day] = 1;
}
if (!okday[tm->tm_wday])
return 0;
bp = game_hours;
if (*bp != 0) {
while (NULL != (bp = kw_parse(CF_TIMERANGE, bp, hour)))
if (curtime >= hour[0] && curtime < hour[1])
break;
if (bp == 0)
return 0;
}
if (!days || !*days)
return 1;
while (NULL != (days = weekday(days, &wd)))
if (wd == wday)
return 1;
return 0;
}
/*
* Is day time DTIME (minutes since midnight) allowed by restriction TIMES?
* If TIMES is not empty, it lists the allowed day time ranges. See
* daytime_range() for syntax.
*/
int
is_daytime_allowed(int dtime, char *times)
{
int from, to;
if (!times || !*times)
return 1;
while (NULL != (times = daytime_range(times, &from, &to)))
if (from <= dtime && dtime < to)
return 1;
return 0;
}
/*
* Can the game played at time T?
*/
int
gamehours(time_t t)
{
struct tm *tm;
tm = localtime(&t);
if (!is_wday_allowed(tm->tm_wday, game_days))
return 0;
return is_daytime_allowed(60 * tm->tm_hour + tm->tm_min, game_hours);
}
/*
* Is day time DTIME (minutes since midnight) near a time in TIMES?
* TIMES is a list of day times. See daytime() for syntax.
* DTIME is near a listed time T if its within T and T + SLOP minutes.
*/
int
is_daytime_near(int dtime, char *times, int slop)
{
int dt;
if (times)
while (NULL != (times = daytime(times, &dt)))
if (dt <= dtime && dtime < dt + slop)
return 1;
return 0;
}
/*
* Return time in minutes between DTIME and next time in TIMES.
* If TIMES doesn't give a time, return -1.
* DTIME is day time in minutes since midnight.
* TIMES is a list of day times. See daytime() for syntax.
*/
int
min_to_next_daytime(int dtime, char *times)
{
int mindt = INT_MAX;
int dt;
if (times) {
while (NULL != (times = daytime(times, &dt))) {
if (dt < dtime)
dt += 24 * 60; /* tomorrow */
if (dt < mindt)
mindt = dt;
}
}
if (mindt == INT_MAX)
return -1;
return mindt - dtime;
}
/*
* Parse weekday name in STR.
* On success assign day number (Sunday is 0) to *WDAY and return
* pointer to first character not parsed.
* Else return NULL.
* Abbreviated names are recognized, but not single characters.
* Initial whitespace is ignored.
*/
static char *
weekday(char *str, int *wday)
{
/*
* strptime() format " %a" would do fine, but it's XPG and Windows
* doesn't have it. Besides, Empire accepts more abbreviations.
*/
static char *day_name[7] = {
"sunday", "monday", "tuesday", "wednesday",
"thursday", "friday", "saturday" };
int i, j;
for (; isspace(*str); ++str) ;
for (i = 0; i < 7; ++i) {
j = 0;
while (str[j] && tolower(str[j]) == day_name[i][j])
++j;
if (j > 1) {
*wday = i;
return str + j;
}
}
return NULL;
}
/*
* Parse day time in STR.
* On success store minutes since midnight in *MIN and return pointer
* to first character not parsed.
* Else return NULL.
* Time format is HOUR:MINUTE. Initial whitespace is ignored.
*/
char *
daytime(char *str, int *min)
{
/*
* strptime() format " %H:%M" would do fine, but it's XPG and
* Windows doesn't have it.
*/
char *end;
unsigned long h, m;
h = strtoul(str, &end, 10);
if (end == str || h > 23)
return NULL;
if (*end++ != ':')
return NULL;
str = end;
m = strtoul(str, &end, 10);
if (end == str || m > 59)
return NULL;
*min = 60 * h + m;
return end;
}
/*
* Parse a day time range in STR.
* On success store minutes since midnight in *FROM and *TO, return
* pointer to first character not parsed.
* Else return NULL.
* Format is HOUR:MINUTE-HOUR:MINUTE. Initial whitespace is ignored.
*/
char *
daytime_range(char *str, int *from_min, int *to_min)
{
char *end;
end = daytime(str, from_min);
if (!end)
return NULL;
while (isspace(*end)) ++end;
if (*end++ != '-')
return NULL;
return daytime(end, to_min);
}

View file

@ -1,137 +0,0 @@
/*
* Empire - A multi-player, client/server Internet based war game.
* Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---
*
* See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
* related information and legal notices. It is expected that any future
* projects/authors will amend these files as needed.
*
* ---
*
* keyword.c: Find keywords in a file
*
* Known contributors to this file:
*
*/
#include "misc.h"
#include "keyword.h"
#include "gen.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "common.h"
/*
* destructive parse
*/
s_char *
kw_parse(int type, s_char *text, int *data)
{
s_char *next;
while (isspace(*text))
text++;
switch (type) {
case CF_VALUE:
*data = atoip(&text);
break;
case CF_TIME:
text = get_time(text, &data[0]);
break;
case CF_TIMERANGE:
if ((next = strchr(text, '-')) == 0)
return 0;
next++;
if ((text = get_time(text, &data[0])) == 0)
return 0;
text = get_time(next, &data[1]);
break;
case CF_WEEKDAY:
text = weekday(text, &data[0]);
break;
default:
text = 0;
break;
}
return text;
}
struct day {
s_char *string;
int day[7];
} day[] = {
{
"smtwtfs", {
-1, 0, -1, 2, -1, 4, -1}}, {
"uouehra", {
0, 1, 2, 3, 4, 5, 6}}
};
s_char *
weekday(s_char *ptr, int *data)
{
register s_char *string;
register int c;
register int n;
c = *ptr++;
if (isupper(c))
c = tolower(c);
string = day[0].string;
for (n = 0; n < 7; n++) {
if (string[n] != c)
continue;
if (day[0].day[n] >= 0)
break;
if (day[1].string[n] == *ptr)
break;
}
if (n == 7)
return 0;
*data = day[1].day[n];
while (*ptr && !isspace(*ptr))
ptr++;
return ptr;
}
s_char *
get_time(s_char *ptr, int *data)
{
int hour;
int minute;
if (!isdigit(*ptr))
return 0;
hour = atoip(&ptr);
minute = 0;
if (*ptr) {
if (*ptr != ':')
return 0;
ptr++;
if (!isdigit(*ptr))
return 0;
minute = atoip(&ptr);
}
*data = (hour * 60) + minute;
return ptr;
}

View file

@ -38,7 +38,6 @@
#include "misc.h"
#include "nat.h"
#include "file.h"
#include "keyword.h"
#include "wantupd.h"
#include "optlist.h"
#include "common.h"
@ -70,20 +69,10 @@ static int
demand_update_time(time_t *now)
{
struct tm *tm;
s_char *p;
int curtime;
int hour[2];
tm = localtime(now);
curtime = tm->tm_min + tm->tm_hour * 60;
p = update_demandtimes;
if (*p == 0)
return (1);
while (NULL != (p = kw_parse(CF_TIMERANGE, p, &hour[0]))) {
if (curtime >= hour[0] && curtime < hour[1])
return (1);
}
return (0);
return is_daytime_allowed(60 * tm->tm_hour + tm->tm_min,
update_demandtimes);
}
/* When is the next regularly scheduled update from now. */
@ -101,54 +90,28 @@ regular_update_time(time_t * now, time_t * tim, time_t * delta)
/* Is this a valid time for a scheduled update. */
static int
scheduled_update_time(time_t * now, int *which)
scheduled_update_time(time_t *now)
{
struct tm *tm;
s_char *p;
int curtime;
int hour;
*which = -1;
p = update_times;
if (*p == 0)
return (0);
tm = localtime(now);
curtime = tm->tm_min + tm->tm_hour * 60;
while (NULL != (p = kw_parse(CF_TIME, p, &hour))) {
(*which)++;
if (curtime >= hour && curtime < hour + hourslop)
return (1);
}
return 0;
return is_daytime_near(60 * tm->tm_hour + tm->tm_min,
update_times, hourslop);
}
static int
next_scheduled_time(time_t *now, time_t *tim, time_t *delta)
{
struct tm *tm;
s_char *p;
int curtime;
int hour;
int mintime;
p = update_times;
if (*p == 0)
return (0);
int d;
tm = localtime(now);
curtime = tm->tm_min + tm->tm_hour * 60; /* now - in minutes */
mintime = curtime + 24 * 60 + 1; /* start with now + 1 day */
while (NULL != (p = kw_parse(CF_TIME, p, &hour))) {
if (hour <= curtime)
hour += 24 * 60; /* this must be tomorrow */
if (hour < mintime)
mintime = hour; /* this is best bet so far */
}
*delta = 60 * (mintime - curtime);
d = min_to_next_daytime(60 * tm->tm_hour + tm->tm_min, update_times);
if (d < 0)
return 0;
*delta = 60 * d;
*tim = *now + *delta - tm->tm_sec;
return (1);
return 1;
}
int
@ -276,8 +239,6 @@ demandupdatecheck(void)
int
updatetime(time_t *now)
{
int which;
if (opt_BLITZ && update_policy == UDP_BLITZ) {
logerror("BLITZ Update.");
return (1);
@ -289,8 +250,8 @@ updatetime(time_t * now)
}
if (UDP_TIMES == update_policy) {
if (scheduled_update_time(now, &which)) {
logerror("Scheduled update, %d.", which);
if (scheduled_update_time(now)) {
logerror("Scheduled update.");
return (1);
}
}

View file

@ -40,7 +40,6 @@
#include "tel.h"
#include "proto.h"
#include "com.h"
#include "keyword.h"
#include "file.h"
#include "empio.h"
#include "subs.h"

View file

@ -34,7 +34,6 @@
#include "options.h"
#include "misc.h"
#include "player.h"
#include "keyword.h"
#include "empthread.h"
#include "file.h"
#include "lost.h"

View file

@ -34,7 +34,6 @@
#include "options.h"
#include "misc.h"
#include "player.h"
#include "keyword.h"
#include "empthread.h"
#include "file.h"
#include "server.h"

View file

@ -35,7 +35,6 @@
#include "options.h"
#include "misc.h"
#include "player.h"
#include "keyword.h"
#include "empthread.h"
#include <stdio.h>
#include "prototypes.h"
@ -96,7 +95,6 @@ mobility_init(void)
time_t now;
time_t lastsavedtime;
FILE *fp;
int hour[2];
/* During downtime, we don't want mobility to accrue. So, we look
at the timestamp file, and determine how far forward to push
@ -109,6 +107,7 @@ mobility_init(void)
if ((fp = fopen(timestampfil, "r+b")) == NULL) {
#endif
logerror("Unable to edit timestamp file.");
/* FIXME safe to continue? */
} else {
rewind(fp);
fread(&timestamps, sizeof(timestamps), 1, fp);

View file

@ -34,7 +34,6 @@
#include "misc.h"
#include "player.h"
#include "keyword.h"
#include "empthread.h"
#include "prototypes.h"
#include "optlist.h"
@ -49,7 +48,6 @@ time_t update_time;
void
update_sched(void *unused)
{
s_char *kw;
int wind;
time_t now, delta;