From acdee1e3b6dcf1bb1e86e430d2147247c73b68d2 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sun, 4 Apr 2004 15:52:26 +0000 Subject: [PATCH] (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. --- include/keyword.h | 49 --------- include/prototypes.h | 4 + src/lib/commands/upda.c | 1 - src/lib/common/Makefile | 4 +- src/lib/common/hours.c | 224 ++++++++++++++++++++++++++++++++------- src/lib/common/keyword.c | 137 ------------------------ src/lib/common/wantupd.c | 77 ++++---------- src/lib/player/empdis.c | 1 - src/server/lostitem.c | 1 - src/server/marketup.c | 1 - src/server/timestamp.c | 3 +- src/server/update.c | 2 - 12 files changed, 210 insertions(+), 294 deletions(-) delete mode 100644 include/keyword.h delete mode 100644 src/lib/common/keyword.c diff --git a/include/keyword.h b/include/keyword.h deleted file mode 100644 index f3a40cde1..000000000 --- a/include/keyword.h +++ /dev/null @@ -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 -#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_ */ diff --git a/include/prototypes.h b/include/prototypes.h index 803a6f6c2..9ccb2c491 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -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); diff --git a/src/lib/commands/upda.c b/src/lib/commands/upda.c index ec80aaabd..a078fc66e 100644 --- a/src/lib/commands/upda.c +++ b/src/lib/commands/upda.c @@ -37,7 +37,6 @@ #include "player.h" #include "commands.h" #include "optlist.h" -#include "keyword.h" #include "wantupd.h" #include "server.h" diff --git a/src/lib/common/Makefile b/src/lib/common/Makefile index 42de25ba3..0c4bca508 100644 --- a/src/lib/common/Makefile +++ b/src/lib/common/Makefile @@ -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 diff --git a/src/lib/common/hours.c b/src/lib/common/hours.c index 35a33833f..9325c4cc0 100644 --- a/src/lib/common/hours.c +++ b/src/lib/common/hours.c @@ -33,54 +33,198 @@ * Steve McClure, 1998 */ -#include -#include +#include +#include #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 +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]; - - 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]) + int wd; + + 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; - 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; + 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 1; + + 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); } diff --git a/src/lib/common/keyword.c b/src/lib/common/keyword.c deleted file mode 100644 index 70aa2778b..000000000 --- a/src/lib/common/keyword.c +++ /dev/null @@ -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 -#include -#include -#include -#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; -} diff --git a/src/lib/common/wantupd.c b/src/lib/common/wantupd.c index 059cb30cd..5ee706151 100644 --- a/src/lib/common/wantupd.c +++ b/src/lib/common/wantupd.c @@ -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" @@ -67,28 +66,18 @@ update_policy_check(void) } static int -demand_update_time(time_t * now) +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. */ static void -regular_update_time(time_t * now, time_t * tim, time_t * delta) +regular_update_time(time_t *now, time_t *tim, time_t *delta) { time_t tw; int secs_per_update; @@ -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) +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 @@ -274,10 +237,8 @@ demandupdatecheck(void) * a demand update can occur. */ int -updatetime(time_t * now) +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); } } @@ -308,7 +269,7 @@ updatetime(time_t * now) * the next possible check. */ void -next_update_time(time_t * now, time_t * tim, time_t * delta) +next_update_time(time_t *now, time_t *tim, time_t *delta) /* From when */ /* Time of next update */ /* Seconds till next update */ @@ -342,7 +303,7 @@ next_update_time(time_t * now, time_t * tim, time_t * delta) } void -next_update_check_time(time_t * now, time_t * tim, time_t * delta) +next_update_check_time(time_t *now, time_t *tim, time_t *delta) /* From when */ /* Time of next update */ /* Seconds till next update check */ diff --git a/src/lib/player/empdis.c b/src/lib/player/empdis.c index 8aa35f887..5c63ca32d 100644 --- a/src/lib/player/empdis.c +++ b/src/lib/player/empdis.c @@ -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" diff --git a/src/server/lostitem.c b/src/server/lostitem.c index e39abd0aa..d0cfed882 100644 --- a/src/server/lostitem.c +++ b/src/server/lostitem.c @@ -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" diff --git a/src/server/marketup.c b/src/server/marketup.c index 4351c4df2..adbf51587 100644 --- a/src/server/marketup.c +++ b/src/server/marketup.c @@ -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" diff --git a/src/server/timestamp.c b/src/server/timestamp.c index b0ee6dd1e..eb0018ae6 100644 --- a/src/server/timestamp.c +++ b/src/server/timestamp.c @@ -35,7 +35,6 @@ #include "options.h" #include "misc.h" #include "player.h" -#include "keyword.h" #include "empthread.h" #include #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(×tamps, sizeof(timestamps), 1, fp); diff --git a/src/server/update.c b/src/server/update.c index c8a528bef..9325b4f79 100644 --- a/src/server/update.c +++ b/src/server/update.c @@ -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; -- 2.43.0