Moved extension and basename stuff to demo_scan_file(), so
authorparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Sat, 15 Jul 2006 21:56:41 +0000 (21:56 +0000)
committerparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Sat, 15 Jul 2006 21:56:41 +0000 (21:56 +0000)
demo_header_read() now does just that:  reads the header.  The
"replay" field in demo status screen is broken, so I left it empty for
now.

I'm a bit over my head here, so this commit might introduce a thousand
bugs:  if you find any, this is most likely what caused it.

git-svn-id: https://s.snth.net/svn/neverball/trunk@509 78b8d119-cf0a-0410-b17c-f493084dd1d7

ball/demo.c
ball/demo.h
ball/st_demo.c

index 3a259e2..66a6a79 100644 (file)
@@ -66,81 +66,45 @@ 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 int demo_header_read(FILE *fp, struct demo *d)
 {
-    FILE *fp;
+    int magic;
+    int version;
+    int t;
 
-    char *basename;
-    char buf[MAXSTR];
-
-    if ((fp = fopen(filename, FMODE_RB)))
-    {
-        int magic;
-        int version;
-        int t;
-
-        get_index(fp, &magic);
-        get_index(fp, &version);
-
-        get_index(fp, &t);
-
-        if (magic == MAGIC && version == DEMO_VERSION && t)
-        {
-            d->timer = t;
-            strncpy(d->filename, filename, PATHMAX);
+    get_index(fp, &magic);
+    get_index(fp, &version);
 
-            /* Remove the directory delimiter */
+    get_index(fp, &t);
 
-            basename = strrchr(filename, '/');
-
-#ifdef _WIN32
-            if (!basename)
-                basename = strrchr(filename, '\\');
-            else
-            {
-                char *tmp;
-                if ((tmp = strrchr(basename, '\\')))
-                    basename = tmp;
-            }
-#endif
-            strncpy(buf, basename ? basename + 1 : filename, MAXSTR);
-
-            /* 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';
+    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);
-            get_index (fp, (int *) &d->date);
+        get_index (fp, &d->coins);
+        get_index (fp, &d->state);
+        get_index (fp, &d->mode);
+        get_index (fp, (int *) &d->date);
 
-            fread(d->player, 1, MAXNAM, fp);
+        fread(d->player, 1, MAXNAM, fp);
 
-            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->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);
 
-            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);
+        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->nb_version, 1, 20, fp);
+        fread(d->nb_version, 1, 20, fp);
 
-            return fp;
-        }
-        fclose(fp);
+        return 1;
     }
-    return NULL;
+    return 0;
 }
 
 /* Create a new demo file, write the demo information structure.  If success,
@@ -206,9 +170,28 @@ 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))
+        {
+            char buf[PATHMAX];
+            int l;
+
+            strncpy(d->filename, config_user(filename), MAXSTR);
+
+            strncpy(buf, filename, PATHMAX);
+            l = strlen(buf) - strlen(REPLAY_EXT);
+
+            if ((l > 1) && (strcmp(buf + l, REPLAY_EXT) == 0))
+                buf[l] = '\0';
+
+            strncpy(d->name, buf, PATHMAX);
+            d->name[PATHMAX - 1] = '\0';
+
+            count++;
+        }
         fclose(fp);
     }
 }
@@ -265,7 +248,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;
 }
@@ -320,7 +303,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;
@@ -398,7 +381,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 +388,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 +403,12 @@ 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);
+
         if (!demo_load_level(&demo_replay, &demo_level_replay))
             return 0;
 
@@ -440,10 +425,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;
 }
 
index 83fed8c..7da5e17 100644 (file)
@@ -10,7 +10,7 @@
 struct demo
 {
     char   name[PATHMAX];      /* demo basename */
-    char   filename[PATHMAX];  /* demo path */
+    char   filename[MAXSTR];  /* demo path */
 
     /* The following reflects the file structure. */
 
@@ -42,7 +42,7 @@ struct demo
 
 int                demo_scan(void);
 const char        *demo_pick(void);
-const struct demo *get_demo(int);
+const struct demo *demo_get(int);
 
 const char *date_to_str(time_t);
 
index d50324b..832531c 100644 (file)
@@ -62,7 +62,7 @@ static int demo_action(int i)
         break;
 
     default:
-        if (level_replay(get_demo(i)->filename))
+        if (level_replay(demo_get(i)->filename))
             demo_play_goto(0);
             return goto_state(&st_demo_play);
     }
@@ -80,10 +80,10 @@ static void demo_replay(int id, int i)
     if ((jd = gui_vstack(id)))
     {
         gui_space(jd);
+        gui_image(jd, demo_get(i)->shot, w / 6, h / 6);
 
-        gui_image(jd, get_demo(i)->shot, w / 6, h / 6);
         nam[MAXNAM - 1] = '\0';
-        strncpy(nam, get_demo(i)->name, MAXNAM);
+        strncpy(nam, demo_get(i)->name, MAXNAM);
         if (nam[MAXNAM - 1] != '\0')
         {
             nam[MAXNAM - 2] = '.';
@@ -97,7 +97,9 @@ static void demo_replay(int id, int i)
     }
 }
 
+#if 0
 static int name_id;
+#endif
 static int time_id;
 static int coin_id;
 static int date_id;
@@ -180,8 +182,10 @@ static int gui_demo_status(int id, const struct demo *d)
                 }
                 if ((md = gui_vstack(ld)))
                 {
+#if 0
                     name_id = gui_label(md, (d ? d->name : noname),
                                         GUI_SML, GUI_RGT, 0, 0);
+#endif
                     time_id = gui_clock(md, (d ? d->timer : 35000),
                                         GUI_SML, GUI_RGT);
                     mode_id = gui_label(md, mode, GUI_SML, GUI_RGT, 0, 0);
@@ -194,7 +198,9 @@ static int gui_demo_status(int id, const struct demo *d)
         }
         if ((kd = gui_vstack(jd)))
         {
+#if 0
             gui_label(kd, _("Replay"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
+#endif
             gui_label(kd, _("Time"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
             gui_label(kd, _("Mode"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
             gui_label(kd, _("Level"),  GUI_SML, GUI_LFT, gui_wht, gui_wht);
@@ -208,9 +214,11 @@ static int gui_demo_status(int id, const struct demo *d)
 
 static void gui_demo_update_status(int i)
 {
-    const struct demo *d = get_demo(i);
+    const struct demo *d = demo_get(i);
 
+#if 0
     gui_set_label(name_id,   d->name);
+#endif
     gui_set_label(date_id,   date_to_str(d->date));
     gui_set_label(level_id,  d->file);
     gui_set_label(player_id, d->player);
@@ -284,6 +292,7 @@ static void demo_stick(int id, int a, int v)
 {
     int jd = shared_stick_basic(id, a, v);
     int i  = gui_token(jd);
+
     if (jd && i >= 0)
         gui_demo_update_status(i);
 }