1 /*
2 * Holotz's Castle
3 * Copyright (C) 2004 Juan Carlos Seijo P�rez
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc., 59
17 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Juan Carlos Seijo P�rez
20 * jacob@mainreactor.net
21 */
22
23 /** Playlist for Holotz's castle.
24 * @file HCPlaylist.cpp
25 * @author Juan Carlos Seijo P�rez
26 * @date 25/08/2004
27 * @version 0.0.1 - 25/08/2004 - First version.
28 */
29
30 #include <HCPlaylist.h>
31
32 #include <string.h>
33
34 #ifdef _WIN32
35 #define FILESYS_BAR '\\'
36 #else
37 #define FILESYS_BAR '/'
38 #endif
39
Load(const char * file)40 bool HCPlaylist::Load(const char *file)
41 {
42 if (file == 0)
43 {
44 if (!HCUtil::FindStories())
45 {
46 fprintf(stderr, "No stories found. Check manual.\n");
47
48 return false;
49 }
50
51 Destroy();
52
53 for (u32 i = 0; i < HCUtil::Stories().size(); ++i)
54 {
55 stories.push_back(strdup(HCUtil::Stories().at(i).Str()));
56 }
57
58 OrderStories();
59 GoTo(stories[0]);
60
61 return true;
62 }
63
64 if (!HCUtil::FindFile(file))
65 {
66 fprintf(stderr, "Couldn't find playlist file %s. Check manual.\n", file);
67 }
68
69 JTextFile f;
70 if (!f.Load(HCUtil::File()))
71 {
72 return false;
73 }
74
75 Destroy();
76
77 JString strFile;
78 char str[4096];
79
80 // Fills the playlist with each line in the file
81 while (f.ReadLine(str))
82 {
83 strFile = "stories/";
84 strFile += str;
85 if (HCUtil::FindFile(strFile))
86 {
87 stories.push_back(strdup(str));
88 }
89 else
90 {
91 fprintf(stderr, "Playlist entry not found: %s. Check manual.\n", str);
92 }
93 }
94
95 if (stories.size() > 0)
96 {
97 OrderStories();
98 GoTo(stories[0]);
99 }
100
101 return stories.size() > 0;
102 }
103
NextStory()104 bool HCPlaylist::NextStory()
105 {
106 if (curStory + 1 >= (s32)stories.size())
107 {
108 return false;
109 }
110
111 ++curStory;
112
113 if (!HCUtil::FindFile(JString("stories/") + stories[curStory]))
114 {
115 --curStory;
116 return false;
117 }
118
119 strncpy(storyDir, HCUtil::Path(), sizeof(storyDir));
120 strncat(storyDir, "stories/", sizeof(storyDir));
121
122 return true;
123 }
124
GoTo(const char * _storyName)125 bool HCPlaylist::GoTo(const char *_storyName)
126 {
127 bool found = false;
128
129 for (u32 i = 0; !found && i < stories.size(); ++i)
130 {
131 if (strcmp(stories[i], _storyName) == 0)
132 {
133 curStory = i;
134 found = true;
135 }
136 }
137
138 if (found)
139 {
140 if (!HCUtil::FindFile(JString("stories/") + stories[curStory]))
141 {
142 return false;
143 }
144
145 strncpy(storyDir, HCUtil::Path(), sizeof(storyDir));
146 strncat(storyDir, "stories/", sizeof(storyDir));
147 }
148
149 return found;
150 }
151
OrderStories()152 void HCPlaylist::OrderStories()
153 {
154 char *tmp;
155
156 for (u32 i = 0; i < stories.size() - 1; ++i)
157 {
158 for (u32 j = 0; j < stories.size() - 1 - i; ++j)
159 {
160 if (strcmp(stories[j+1], stories[j]) < 0)
161 {
162 tmp = stories[j];
163 stories[j] = stories[j+1];
164 stories[j+1] = tmp;
165 }
166 }
167 }
168 }
169
Destroy()170 void HCPlaylist::Destroy()
171 {
172 for (u32 i = 0; i < stories.size(); ++i)
173 {
174 free(stories[i]);
175 }
176
177 stories.clear();
178 }
179