2 * Copyright (C) 2003 Robert Kooima
4 * NEVERPUTT is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
25 /*---------------------------------------------------------------------------*/
35 static int course_state = 0;
40 static struct course course_v[MAXCRS];
42 /*---------------------------------------------------------------------------*/
44 static int course_load(struct course *course, const char *filename)
49 memset(course, 0, sizeof (*course));
51 strncpy(course->holes, filename, MAXSTR - 1);
53 if ((fin = fs_open(filename, "r")))
55 if (fs_gets(course->shot, sizeof (course->shot), fin) &&
56 fs_gets(course->desc, sizeof (course->desc), fin))
58 strip_newline(course->shot);
59 strip_newline(course->desc);
70 static int cmp_dir_items(const void *A, const void *B)
72 const struct dir_item *a = A, *b = B;
73 return strcmp(a->path, b->path);
76 static int course_is_loaded(const char *path)
80 for (i = 0; i < count; i++)
81 if (strcmp(course_v[i].holes, path) == 0)
87 static int is_unseen_course(struct dir_item *item)
89 return (str_starts_with(base_name(item->path), "holes-") &&
90 str_ends_with(item->path, ".txt") &&
91 !course_is_loaded(item->path));
107 if ((fin = fs_open(COURSE_FILE, "r")))
109 while (count < MAXCRS && read_line(&line, fin))
111 if (course_load(&course_v[count], line))
122 if ((items = fs_dir_scan("", is_unseen_course)))
124 array_sort(items, cmp_dir_items);
126 for (i = 0; i < array_len(items) && count < MAXCRS; i++)
127 if (course_load(&course_v[count], DIR_ITEM_GET(items, i)->path))
136 int course_exists(int i)
138 return (0 <= i && i < count);
141 int course_count(void)
146 void course_goto(int i)
148 hole_init(course_v[i].holes);
152 int course_curr(void)
157 void course_free(void)
163 void course_rand(void)
165 course_goto(rand() % count);
166 hole_goto(rand() % curr_count(), 4);
169 /*---------------------------------------------------------------------------*/
171 const char *course_desc(int i)
173 return course_exists(i) ? course_v[i].desc : "";
176 const char *course_shot(int i)
178 return course_exists(i) ? course_v[i].shot : course_v[0].shot;
181 /*---------------------------------------------------------------------------*/