Isolate basename stuff in a completely separate function bname(),
[neverball] / ball / demo.c
index 3a259e2..2d132c9 100644 (file)
@@ -31,9 +31,8 @@
 
 static FILE *demo_fp;
 
-static struct demo demos[MAXDEMO]; /* Array of scanned demos */
-
-static int count; /* number of scanned demos */
+static struct demo demos[MAXDEMO]; /* Array of scanned demos  */
+static int         count;          /* Number of scanned demos */
 
 /*---------------------------------------------------------------------------*/
 
@@ -66,124 +65,157 @@ void demo_dump_info(const struct demo *d)
            d->time, d->goal, d->score, d->balls, d->times);
 }
 
-/* Open a demo file, fill the demo information structure.  If success, return
- * the file pointer positioned after the header.  If fail, return NULL. */
-
-FILE *demo_header_read(const char *filename, struct demo *d)
+static time_t make_time_from_utc(struct tm *tm)
 {
-    FILE *fp;
+    struct tm local, *utc;
+    time_t t;
 
-    char *basename;
-    char buf[MAXSTR];
+    t = mktime(tm);
 
-    if ((fp = fopen(filename, FMODE_RB)))
-    {
-        int magic;
-        int version;
-        int t;
+    local = *localtime(&t);
+    utc   =  gmtime(&t);
 
-        get_index(fp, &magic);
-        get_index(fp, &version);
+    local.tm_year += local.tm_year - utc->tm_year;
+    local.tm_mon  += local.tm_mon  - utc->tm_mon ;
+    local.tm_mday += local.tm_mday - utc->tm_mday;
+    local.tm_hour += local.tm_hour - utc->tm_hour;
+    local.tm_min  += local.tm_min  - utc->tm_min ;
+    local.tm_sec  += local.tm_sec  - utc->tm_sec ;
 
-        get_index(fp, &t);
+    return mktime(&local);
+}
 
-        if (magic == MAGIC && version == DEMO_VERSION && t)
-        {
-            d->timer = t;
-            strncpy(d->filename, filename, PATHMAX);
+static int demo_header_read(FILE *fp, struct demo *d)
+{
+    int magic;
+    int version;
+    int t;
 
-            /* Remove the directory delimiter */
+    struct tm date;
+    char datestr[20];
 
-            basename = strrchr(filename, '/');
+    get_index(fp, &magic);
+    get_index(fp, &version);
 
-#ifdef _WIN32
-            if (!basename)
-                basename = strrchr(filename, '\\');
-            else
-            {
-                char *tmp;
-                if ((tmp = strrchr(basename, '\\')))
-                    basename = tmp;
-            }
+    get_index(fp, &t);
+
+    if (magic == MAGIC && version == DEMO_VERSION && t)
+    {
+        d->timer = t;
+
+        get_index(fp, &d->coins);
+        get_index(fp, &d->state);
+        get_index(fp, &d->mode);
+
+#if 0
+        get_index(fp, (int *) &d->date);
 #endif
-            strncpy(buf, basename ? basename + 1 : filename, MAXSTR);
+        fread(datestr, 1, 20, fp);
+        sscanf(datestr,
+               "%d-%d-%dT%d:%d:%d",
+               &date.tm_year,
+               &date.tm_mon,
+               &date.tm_mday,
+               &date.tm_hour,
+               &date.tm_min,
+               &date.tm_sec);
 
-            /* Remove the extension */
-            t = strlen(buf) - strlen(REPLAY_EXT);
-            if ((t > 1) && (strcmp(buf + t, REPLAY_EXT) == 0))
-                buf[t] = '\0';
-            strncpy(d->name, buf, PATHMAX);
-            d->name[PATHMAX - 1] = '\0';
+        /* Convert certain values to valid structure member values. */
 
-            get_index (fp, &d->coins);
-            get_index (fp, &d->state);
-            get_index (fp, &d->mode);
-            get_index (fp, (int *) &d->date);
+        date.tm_year -= 1900;
+        date.tm_mon  -= 1;
 
-            fread(d->player, 1, MAXNAM, fp);
+        d->date = make_time_from_utc(&date);
 
-            fread(d->shot, 1, PATHMAX, fp);
-            fread(d->file, 1, PATHMAX, fp);
-            fread(d->back, 1, PATHMAX, fp);
-            fread(d->grad, 1, PATHMAX, fp);
-            fread(d->song, 1, PATHMAX, fp);
+        fread(d->player, 1, MAXNAM, fp);
 
-            get_index (fp, &d->time);
-            get_index (fp, &d->goal);
-            get_index (fp, &d->score);
-            get_index (fp, &d->balls);
-            get_index (fp, &d->times);
+        fread(d->shot, 1, PATHMAX, fp);
+        fread(d->file, 1, PATHMAX, fp);
+        fread(d->back, 1, PATHMAX, fp);
+        fread(d->grad, 1, PATHMAX, fp);
+        fread(d->song, 1, PATHMAX, fp);
 
-            fread(d->nb_version, 1, 20, fp);
+        get_index(fp, &d->time);
+        get_index(fp, &d->goal);
+        get_index(fp, &d->score);
+        get_index(fp, &d->balls);
+        get_index(fp, &d->times);
 
-            return fp;
-        }
-        fclose(fp);
+        fread(d->nb_version, 1, 20, fp);
+
+        return 1;
     }
-    return NULL;
+    return 0;
 }
 
-/* Create a new demo file, write the demo information structure.  If success,
- * return the file pointer positioned after the header.  If fail, return NULL.
- */
+static char *bname(const char *name, const char *suffix)
+{
+    static char buf[MAXSTR];
+
+    char *base;
+    size_t l;
+
+    /* Remove the directory delimiter */
+
+    base = strrchr(name, '/');
+#ifdef _WIN32
+    if (!base)
+        base = strrchr(name, '\\');
+    else
+    {
+        char *tmp;
+        if ((tmp = strrchr(base, '\\')))
+            base = tmp;
+    }
+#endif
+    strncpy(buf, base ? base + 1 : name, MAXSTR);
 
-static FILE *demo_header_write(struct demo *d)
+    /* Remove the extension */
+
+    l = strlen(buf) - strlen(suffix);
+    if ((l > 1) && (strcmp(buf + l, suffix) == 0))
+        buf[l] = '\0';
+
+    return buf;
+}
+
+static void demo_header_write(FILE *fp, struct demo *d)
 {
     int magic = MAGIC;
     int version = DEMO_VERSION;
     int zero  = 0;
 
-    FILE *fp;
+    char datestr[20];
 
-    if (d->filename && (fp = fopen(d->filename, FMODE_WB)))
-    {
-        put_index(fp, &magic);
-        put_index(fp, &version);
-        put_index(fp, &zero);
-        put_index(fp, &zero);
-        put_index(fp, &zero);
-        put_index(fp, &d->mode);
-        put_index(fp, (int *) &d->date);
-
-        fwrite(d->player, 1, MAXNAM, fp);
-
-        fwrite(d->shot, 1, PATHMAX, fp);
-        fwrite(d->file, 1, PATHMAX, fp);
-        fwrite(d->back, 1, PATHMAX, fp);
-        fwrite(d->grad, 1, PATHMAX, fp);
-        fwrite(d->song, 1, PATHMAX, fp);
-
-        put_index(fp, &d->time);
-        put_index(fp, &d->goal);
-        put_index(fp, &d->score);
-        put_index(fp, &d->balls);
-        put_index(fp, &d->times);
-
-        fwrite(d->nb_version, 1, 20, fp);
-
-        return fp;
-    }
-    return NULL;
+    strftime(datestr, 20, "%Y-%m-%dT%H:%M:%S", gmtime(&d->date));
+
+    put_index(fp, &magic);
+    put_index(fp, &version);
+    put_index(fp, &zero);
+    put_index(fp, &zero);
+    put_index(fp, &zero);
+    put_index(fp, &d->mode);
+
+#if 0
+    put_index(fp, (int *) &d->date);
+#endif
+    fwrite(datestr, 1, 20, fp);
+
+    fwrite(d->player, 1, MAXNAM, fp);
+
+    fwrite(d->shot, 1, PATHMAX, fp);
+    fwrite(d->file, 1, PATHMAX, fp);
+    fwrite(d->back, 1, PATHMAX, fp);
+    fwrite(d->grad, 1, PATHMAX, fp);
+    fwrite(d->song, 1, PATHMAX, fp);
+
+    put_index(fp, &d->time);
+    put_index(fp, &d->goal);
+    put_index(fp, &d->score);
+    put_index(fp, &d->balls);
+    put_index(fp, &d->times);
+
+    fwrite(d->nb_version, 1, 20, fp);
 }
 
 /* Update the demo header using the final level state. */
@@ -206,9 +238,18 @@ void demo_header_stop(FILE *fp, int coins, int timer, int state)
 static void demo_scan_file(const char *filename)
 {
     FILE *fp;
-    if ((fp = demo_header_read(config_user(filename), &demos[count])))
+    struct demo *d = &demos[count];
+
+    if ((fp = fopen(config_user(filename), FMODE_RB)))
     {
-        count++;
+        if (demo_header_read(fp, d))
+        {
+            strncpy(d->filename, config_user(filename),       MAXSTR);
+            strncpy(d->name,     bname(filename, REPLAY_EXT), PATHMAX);
+            d->name[PATHMAX - 1] = '\0';
+
+            count++;
+        }
         fclose(fp);
     }
 }
@@ -265,7 +306,7 @@ const char *demo_pick(void)
     return (n > 0) ? demos[(rand() >> 4) % n].filename : NULL;
 }
 
-const struct demo *get_demo(int i)
+const struct demo *demo_get(int i)
 {
     return (0 <= i && i < count) ? &demos[i] : NULL;
 }
@@ -273,8 +314,8 @@ const struct demo *get_demo(int i)
 const char *date_to_str(time_t i)
 {
     static char str[MAXSTR];
-    struct tm *tm = localtime(&i);
-    strftime(str, MAXSTR, "%c", tm);
+
+    strftime(str, MAXSTR, "%c", localtime(&i));
     return str;
 }
 
@@ -320,7 +361,7 @@ int demo_play_init(const char *name,
 
     memset(&demo, 0, sizeof (demo));
 
-    strncpy(demo.filename, config_user(name), PATHMAX);
+    strncpy(demo.filename, config_user(name), MAXSTR);
     strcat(demo.filename, REPLAY_EXT);
 
     demo.mode = lg->mode;
@@ -342,10 +383,9 @@ int demo_play_init(const char *name,
 
     strncpy(demo.nb_version, VERSION, 20);
 
-    demo_fp = demo_header_write(&demo);
-
-    if (demo_fp)
+    if (demo.filename && (demo_fp = fopen(demo.filename, FMODE_WB)))
     {
+        demo_header_write(demo_fp, &demo);
         audio_music_fade_to(2.0f, level->song);
         return game_init(level, lg->time, lg->goal);
     }
@@ -391,6 +431,10 @@ void demo_play_save(const char *name)
         strncpy(dst, config_user(name), PATHMAX);
         strcat(dst, REPLAY_EXT);
 
+#ifdef _WIN32
+        if (demo_exists(name))
+            remove(dst);
+#endif
         rename(src, dst);
     }
 }
@@ -398,7 +442,6 @@ void demo_play_save(const char *name)
 /*---------------------------------------------------------------------------*/
 
 static int demo_load_level(const struct demo *demo, struct level *level)
-/* Load the level of the demo and fill the level structure */
 {
     if (level_load(demo->file, level))
     {
@@ -406,8 +449,7 @@ static int demo_load_level(const struct demo *demo, struct level *level)
         level->goal = demo->goal;
         return 1;
     }
-    else
-        return 0;
+    return 0;
 }
 
 static struct demo  demo_replay;       /* The current demo */
@@ -422,8 +464,13 @@ const struct demo *curr_demo_replay(void)
 
 int demo_replay_init(const char *name, struct level_game *lg)
 {
-    if ((demo_fp = demo_header_read(name, &demo_replay)))
+    demo_fp = fopen(name, FMODE_RB);
+
+    if (demo_fp && demo_header_read(demo_fp, &demo_replay))
     {
+        strncpy(demo_replay.filename, name,                    MAXSTR);
+        strncpy(demo_replay.name,     bname(name, REPLAY_EXT), PATHMAX);
+
         if (!demo_load_level(&demo_replay, &demo_level_replay))
             return 0;
 
@@ -440,10 +487,9 @@ int demo_replay_init(const char *name, struct level_game *lg)
             return game_init(&demo_level_replay, demo_replay.time,
                              demo_replay.goal);
         }
-        else                    /* A title screen demo */
+        else /* A title screen demo */
             return game_init(&demo_level_replay, demo_replay.time, 0);
     }
-
     return 0;
 }