]> git.pond.sub.org Git - empserver/commitdiff
(weekday, daytime, daytime_range): New; replace kw_parse(). Parsing
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 4 Apr 2004 15:52:26 +0000 (15:52 +0000)
committerMarkus Armbruster <armbru@pond.sub.org>
Sun, 4 Apr 2004 15:52:26 +0000 (15:52 +0000)
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.

12 files changed:
include/keyword.h [deleted file]
include/prototypes.h
src/lib/commands/upda.c
src/lib/common/Makefile
src/lib/common/hours.c
src/lib/common/keyword.c [deleted file]
src/lib/common/wantupd.c
src/lib/player/empdis.c
src/server/lostitem.c
src/server/marketup.c
src/server/timestamp.c
src/server/update.c

diff --git a/include/keyword.h b/include/keyword.h
deleted file mode 100644 (file)
index f3a40cd..0000000
+++ /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 <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_ */
index 803a6f6c2efe7bd69b803f3ea4c9c3be6ff92ba2..9ccb2c491b74be4a2632ff7fd994d629f532eaed 100644 (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);
index ec80aaabd59c24b2215d4159735b8aa91608c309..a078fc66e163f1a4f2ebd6f76432b1fa0a50e144 100644 (file)
@@ -37,7 +37,6 @@
 #include "player.h"
 #include "commands.h"
 #include "optlist.h"
-#include "keyword.h"
 #include "wantupd.h"
 #include "server.h"
 
index 42de25ba3f28b2af2c87d7c48a9f3ce0de221413..0c4bca5082ab89aaa44dd6a919c449f18a4a9698 100644 (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
index 35a33833f6379fe89ad9c9abf75b4fec0ec4f916..9325c4cc08f2d8d7849498f7a3278a36948df685 100644 (file)
  *     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];
-
-    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 (file)
index 70aa277..0000000
+++ /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 <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;
-}
index 059cb30cd911e0343d6de66f6faac2b5de27b946..5ee7061517b8a2ea657b130b5f28baae2f4460f8 100644 (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"
@@ -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 */
index 8aa35f8878d43f60821a9a328509b0aa6133beaa..5c63ca32dfac5a8ce023f48b12f32573938b0bc9 100644 (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"
index e39abd0aa077ed981ec2632246ec49613624fa59..d0cfed88200bdfc569ace9a2c1fd0d2bafddbd30 100644 (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"
index 4351c4df26c9a766a08c4934efcf353defd4997f..adbf515870e8ef2eba3f34ab74d1419450eafae7 100644 (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"
index b0ee6dd1efa8e65fc7f8190289bdb726f47b2035..eb0018ae67a9252b27194a9d4b1aba97e7f1df6a 100644 (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);
index c8a528bef40653fb152e609696078d076fe62ff7..9325b4f79c36aaaff15cd07186d400d838abd040 100644 (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;