fixed compilation problem
[supertux.git] / src / level_subset.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20
21 #include <assert.h>
22 #include <unistd.h>
23 #include "app/setup.h"
24 #include "level.h"
25 #include "app/globals.h"
26 #include "video/surface.h"
27 #include "level_subset.h"
28
29 using namespace SuperTux;
30
31 static bool has_suffix(const std::string& data, const std::string& suffix)
32 {
33   if (data.length() >= suffix.length())
34     return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
35   else
36     return false;
37 }
38
39 LevelSubset::LevelSubset()
40   : levels(0)
41 {
42 }
43
44 LevelSubset::~LevelSubset()
45 {
46 }
47
48 void LevelSubset::create(const std::string& subset_name)
49 {
50   Level new_lev;
51   LevelSubset new_subset;
52   new_subset.name = subset_name;
53   new_subset.title = "Unknown Title";
54   new_subset.description = "No description so far.";
55   new_subset.save();
56 }
57
58 void LevelSubset::read_info_file(const std::string& info_file)
59 {
60   lisp_object_t* root_obj = lisp_read_from_file(info_file);
61   lisp_object_t* cur = lisp_car(root_obj);
62
63   if (lisp_symbol_p(cur) && strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
64     {
65       LispReader reader(lisp_cdr(root_obj));
66
67       reader.read_string("title", title, true);
68       reader.read_string("description", description, true);
69       reader.read_string_vector("levels", levels);
70     }
71   else
72     {
73       std::cout << "LevelSubset: parse error in info file: " << info_file << std::endl;
74     }
75
76   lisp_free(root_obj);
77 }
78
79 void LevelSubset::load(const char* subset)
80 {
81   name = subset;
82   
83   // Check in which directory our subset is located (ie. ~/.supertux/
84   // or SUPERTUX_DATADIR)
85   char filename[1024];
86   snprintf(filename, 1024, "%s/levels/%s/", st_dir, subset);
87   if (access(filename, R_OK) == 0)
88     {
89       directory = filename;
90     }
91   else
92     {
93       snprintf(filename, 1024, "%s/levels/%s/", datadir.c_str(), subset);
94       if (access(filename, R_OK) == 0)
95         directory = filename;
96       else
97         std::cout << "Error: LevelSubset: couldn't find subset: " << subset << std::endl;
98     }
99   
100   read_info_file(directory + "info");
101
102   if (levels.empty())
103     { // Level info file doesn't define any levels, so read the
104       // directory to see what we can find
105       std::set<std::string> files;
106   
107       snprintf(filename, 1024, "%s/levels/%s/", st_dir, subset);
108       if(access(filename, R_OK) == 0)
109         {
110           files = FileSystem::read_directory(filename);
111         }
112       else
113         {
114           snprintf(filename, 1024, "%s/levels/%s/", datadir.c_str(), subset);
115           files = FileSystem::read_directory(filename);
116         }
117   
118       for(std::set<std::string>::iterator i = files.begin(); i != files.end(); ++i)
119         {
120           if (has_suffix(*i, ".stl"))
121             levels.push_back(*i);
122         }
123     }
124 }
125
126 void
127 LevelSubset::save()
128 {
129   FILE* fi;
130   std::string filename;
131
132   /* Save data file: */
133   filename = "/levels/" + name + "/";
134
135   FileSystem::fcreatedir(filename.c_str());
136   filename = std::string(st_dir) + "/levels/" + name + "/info";
137   if(!FileSystem::fwriteable(filename.c_str()))
138     filename = datadir + "/levels/" + name + "/info";
139   if(FileSystem::fwriteable(filename.c_str()))
140     {
141       fi = fopen(filename.c_str(), "w");
142       if (fi == NULL)
143         {
144           perror(filename.c_str());
145         }
146
147       /* Write header: */
148       fprintf(fi,";; SuperTux-Level-Subset\n");
149       fprintf(fi,"(supertux-level-subset\n");
150
151       /* Save title info: */
152       fprintf(fi,"  (title \"%s\")\n", title.c_str());
153
154       /* Save the description: */
155       fprintf(fi,"  (description \"%s\")\n", description.c_str());
156
157       fprintf( fi,")");
158       fclose(fi);
159     }
160 }
161
162 void
163 LevelSubset::add_level(const std::string& name)
164 {
165   levels.push_back(name);
166 }
167
168 std::string
169 LevelSubset::get_level_filename(unsigned int num)
170 {
171   assert(num < levels.size());
172
173   return directory + levels[num];
174 }
175
176 int
177 LevelSubset::get_num_levels() const
178 {
179   return levels.size();
180 }
181
182 /* EOF */