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 /** Utility functions for Holotz's Castle.
24 * @file HCUtil.cpp
25 * @author Juan Carlos Seijo P�rez
26 * @date 05/02/2005
27 * @version 0.0.1 - 05/02/2005 - First version.
28 */
29
30 #include <HCUtil.h>
31
32 const char * HCUtil::curDir = "";
33 const char * HCUtil::installHCDir = HC_DATA_DIR;
34 const char * HCUtil::installHCedDir = HCED_DATA_DIR;
35 char HCUtil::homeDir[4096];
36 char HCUtil::lastFile[4096];
37 const char *HCUtil::lastPath;
38 std::vector<JString> HCUtil::themes;
39 std::vector<JString> HCUtil::stories;
40
FindFile(const char * filename)41 bool HCUtil::FindFile(const char *filename)
42 {
43 char *str;
44
45 #ifndef _WIN32
46 if (strstr(filename, "holotzcastle") == 0 && 0 != (str = getenv("HOME")))
47 {
48 snprintf(homeDir, sizeof(homeDir), "%s/.holotz-castle/", str);
49 }
50 else
51 {
52 homeDir[0] = 0;
53 }
54
55 lastFile[0] = 0;
56
57
58 if (homeDir[0] != 0)
59 {
60 // Searchs in the home dir
61 snprintf(lastFile, sizeof(lastFile), "%s%s", homeDir, filename);
62
63 if (JFile::Exists(lastFile))
64 {
65 lastPath = homeDir;
66 return true;
67 }
68 }
69 #endif // _WIN32
70
71 // Searchs in the current dir
72 if (JFile::Exists(filename))
73 {
74 strncpy(lastFile, filename, sizeof(lastFile));
75 lastPath = curDir;
76 return true;
77 }
78
79 // Searchs in the HC installation dir
80 snprintf(lastFile, sizeof(lastFile), "%s%s", installHCDir, filename);
81
82 if (JFile::Exists(lastFile))
83 {
84 lastPath = installHCDir;
85 return true;
86 }
87
88 // Searchs in the HCED installation dir
89 snprintf(lastFile, sizeof(lastFile), "%s%s", installHCedDir, filename);
90
91 if (JFile::Exists(lastFile))
92 {
93 lastPath = installHCedDir;
94 return true;
95 }
96
97 lastFile[0] = 0;
98 lastPath = 0;
99
100 return false;
101 }
102
103
FindThemes(bool onlyEdit)104 bool HCUtil::FindThemes(bool onlyEdit)
105 {
106 char *str;
107
108 #ifndef _WIN32
109 if (0 != (str = getenv("HOME")))
110 {
111 snprintf(homeDir, sizeof(homeDir), "%s/.holotz-castle/", str);
112 }
113 else
114 {
115 homeDir[0] = 0;
116 }
117
118 const char *dirs[] = {homeDir, curDir, installHCDir, installHCedDir};
119 #else
120 const char *dirs[] = {0, curDir, installHCDir, installHCedDir};
121 #endif // _WIN32
122
123 #ifndef _WIN32
124 if (onlyEdit)
125 {
126 dirs[2] = dirs[3] = 0;
127 }
128 #endif // _WIN32
129
130 DIR *dp;
131 struct dirent *ep;
132 char themesDir[4096];
133
134 // Starts with a new set
135 themes.clear();
136
137 for (s32 i = 0; i < 4; ++i)
138 {
139 if (dirs[i])
140 {
141 snprintf(themesDir, sizeof(themesDir), "%stheme", dirs[i]);
142 dp = opendir(themesDir);
143
144 if (dp != 0)
145 {
146 while ((ep = readdir(dp)))
147 {
148 if (0 != strcmp(ep->d_name, ".") && 0 != strcmp(ep->d_name, ".."))
149 {
150 u32 j;
151 for (j = 0; j < themes.size(); ++j)
152 {
153 if (themes[j] == ep->d_name)
154 break;
155 }
156
157 if (j == themes.size())
158 {
159 themes.push_back(ep->d_name);
160 }
161 }
162 }
163
164 closedir(dp);
165 }
166 }
167 }
168
169 return themes.size() > 0;
170 }
171
FindStories(bool onlyEdit)172 bool HCUtil::FindStories(bool onlyEdit)
173 {
174 char *str;
175
176 #ifndef _WIN32
177 if (0 != (str = getenv("HOME")))
178 {
179 snprintf(homeDir, sizeof(homeDir), "%s/.holotz-castle/", str);
180 }
181 else
182 {
183 homeDir[0] = 0;
184 }
185
186 const char *dirs[] = {homeDir, curDir, installHCDir, installHCedDir};
187 #else
188 const char *dirs[] = {0, curDir, installHCDir, installHCedDir};
189 #endif // _WIN32
190
191 #ifndef _WIN32
192 if (onlyEdit)
193 {
194 dirs[2] = dirs[3] = 0;
195 }
196 #endif // _WIN32
197
198 DIR *dp;
199 struct dirent *ep;
200 char storiesDir[4096];
201
202 // Starts with a new set
203 stories.clear();
204
205 for (s32 i = 0; i < 4; ++i)
206 {
207 if (dirs[i])
208 {
209 snprintf(storiesDir, sizeof(storiesDir), "%sstories", dirs[i]);
210 dp = opendir(storiesDir);
211
212 if (dp != 0)
213 {
214 while ((ep = readdir(dp)))
215 {
216 if (0 != strcmp(ep->d_name, ".") && 0 != strcmp(ep->d_name, ".."))
217 {
218 u32 j;
219 for (j = 0; j < stories.size(); ++j)
220 {
221 if (stories[j] == ep->d_name)
222 break;
223 }
224
225 if (j == stories.size())
226 {
227 stories.push_back(ep->d_name);
228 }
229 }
230 }
231
232 closedir(dp);
233 }
234 }
235 }
236
237 return stories.size() > 0;
238 }
239
CreateStory(const char * story)240 s32 HCUtil::CreateStory(const char *story)
241 {
242 if (FindFile("stories"))
243 {
244 // Directory 'stories' found
245 char name[4096];
246 snprintf(name, sizeof(name), "%s%c%s", lastFile, FILESYS_BAR, story);
247
248 if (!FindFile(name))
249 {
250 // The story does not exist, creates its directory
251 #ifndef _WIN32
252 if (0 != mkdir(name, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
253 #else
254 if (0 != mkdir(name))
255 #endif //_WIN32
256 {
257 // Can't create new dir
258 return 3;
259 }
260 else
261 {
262 // Ok!
263 return 0;
264 }
265 }
266 else
267 {
268 // Story already exists
269 return 1;
270 }
271 }
272 else
273 {
274 // Stories directory not found
275 return 2;
276 }
277 }
278
Destroy()279 void HCUtil::Destroy()
280 {
281 themes.clear();
282 stories.clear();
283 }
284