ball/demo_dir: move replay scanning stuff here
authorparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Fri, 8 May 2009 19:35:17 +0000 (19:35 +0000)
committerparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Fri, 8 May 2009 19:35:17 +0000 (19:35 +0000)
git-svn-id: https://s.snth.net/svn/neverball/trunk@2851 78b8d119-cf0a-0410-b17c-f493084dd1d7

Makefile
ball/demo.c
ball/demo.h
ball/demo_dir.c [new file with mode: 0644]
ball/demo_dir.h [new file with mode: 0644]
ball/st_demo.c
ball/st_title.c

index 390bac8..715c2c8 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -180,6 +180,7 @@ BALL_OBJS := \
        ball/progress.o     \
        ball/set.o          \
        ball/demo.o         \
+       ball/demo_dir.o     \
        ball/util.o         \
        ball/st_conf.o      \
        ball/st_demo.o      \
index e6a38c1..b644ad1 100644 (file)
@@ -40,7 +40,6 @@
 #define DATELEN 20
 
 static FILE *demo_fp;
-static Array items;
 
 /*---------------------------------------------------------------------------*/
 
@@ -69,7 +68,7 @@ void demo_dump_info(const struct demo *d)
            d->time, d->goal, d->goal_e, d->score, d->balls, d->times);
 }
 
-static int demo_header_read(FILE *fp, struct demo *d)
+int demo_header_read(FILE *fp, struct demo *d)
 {
     int magic;
     int version;
@@ -157,79 +156,6 @@ static void demo_header_write(FILE *fp, struct demo *d)
 
 /*---------------------------------------------------------------------------*/
 
-static void free_items(void)
-{
-    if (items)
-    {
-        struct dir_item *item;
-        int i;
-
-        for (i = 0; i < array_len(items); i++)
-        {
-            item = array_get(items, i);
-
-            free(item->data);
-            item->data = NULL;
-        }
-
-        dir_free(items);
-        items = NULL;
-    }
-}
-
-static int scan_item(struct dir_item *item)
-{
-    FILE *fp;
-    struct demo *d;
-    int keep = 0;
-
-    if ((fp = fopen(item->path, FMODE_RB)))
-    {
-        d = malloc(sizeof (struct demo));
-
-        if (demo_header_read(fp, d))
-        {
-            strncpy(d->filename, item->path, MAXSTR);
-            strncpy(d->name, base_name(d->filename, REPLAY_EXT), PATHMAX);
-            d->name[PATHMAX - 1] = '\0';
-
-            item->data = d;
-            keep = 1;
-        }
-        else free(d);
-
-        fclose(fp);
-    }
-
-    return keep;
-}
-
-int demo_scan(void)
-{
-    free_items();
-
-    items = dir_scan(config_user(""), scan_item);
-
-    return array_len(items);
-}
-
-const char *demo_pick(void)
-{
-    struct dir_item *item;
-    struct demo *d;
-
-    demo_scan();
-
-    return (item = array_rnd(items)) && (d = item->data) ? d->filename : NULL;
-}
-
-const struct demo *demo_get(int i)
-{
-    return DIR_ITEM_GET(items, i)->data;
-}
-
-/*---------------------------------------------------------------------------*/
-
 int demo_exists(const char *name)
 {
     char buf[MAXSTR];
index 17b7a2b..824baf0 100644 (file)
@@ -34,9 +34,7 @@ struct demo
 
 /*---------------------------------------------------------------------------*/
 
-int                demo_scan(void);
-const char        *demo_pick(void);
-const struct demo *demo_get(int);
+int demo_header_read(FILE *, struct demo *);
 
 int  demo_exists(const char *);
 
diff --git a/ball/demo_dir.c b/ball/demo_dir.c
new file mode 100644 (file)
index 0000000..f610fbf
--- /dev/null
@@ -0,0 +1,86 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "array.h"
+#include "common.h"
+#include "demo.h"
+#include "demo_dir.h"
+#include "dir.h"
+
+/*---------------------------------------------------------------------------*/
+
+static Array items;
+
+static void free_items(void)
+{
+    if (items)
+    {
+        struct dir_item *item;
+        int i;
+
+        for (i = 0; i < array_len(items); i++)
+        {
+            item = array_get(items, i);
+
+            free(item->data);
+            item->data = NULL;
+        }
+
+        dir_free(items);
+        items = NULL;
+    }
+}
+
+static int scan_item(struct dir_item *item)
+{
+    FILE *fp;
+    struct demo *d;
+    int keep = 0;
+
+    if ((fp = fopen(item->path, FMODE_RB)))
+    {
+        d = malloc(sizeof (struct demo));
+
+        if (demo_header_read(fp, d))
+        {
+            strncpy(d->filename, item->path, MAXSTR);
+            strncpy(d->name, base_name(d->filename, REPLAY_EXT), PATHMAX);
+            d->name[PATHMAX - 1] = '\0';
+
+            item->data = d;
+            keep = 1;
+        }
+        else free(d);
+
+        fclose(fp);
+    }
+
+    return keep;
+}
+
+int demo_scan(void)
+{
+    free_items();
+
+    items = dir_scan(config_user(""), scan_item);
+
+    return array_len(items);
+}
+
+const char *demo_pick(void)
+{
+    struct dir_item *item;
+    struct demo *d;
+
+    demo_scan();
+
+    return (item = array_rnd(items)) && (d = item->data) ? d->filename : NULL;
+}
+
+const struct demo *demo_get(int i)
+{
+    return DIR_ITEM_GET(items, i)->data;
+}
+
+/*---------------------------------------------------------------------------*/
+
diff --git a/ball/demo_dir.h b/ball/demo_dir.h
new file mode 100644 (file)
index 0000000..d7114ad
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef DEMO_SCAN
+#define DEMO_SCAN
+
+#include "demo.h"
+
+int                demo_scan(void);
+const char        *demo_pick(void);
+const struct demo *demo_get(int);
+
+#endif
index 62adce7..0326e73 100644 (file)
@@ -25,6 +25,7 @@
 #include "st_shared.h"
 #include "util.h"
 #include "common.h"
+#include "demo_dir.h"
 
 #include "game_common.h"
 #include "game_server.h"
index 8529ea0..4460f13 100644 (file)
@@ -21,6 +21,7 @@
 #include "config.h"
 #include "st_shared.h"
 #include "cmd.h"
+#include "demo_dir.h"
 
 #include "game_common.h"
 #include "game_server.h"