SuperTux lib became a bit more independend of SupeTux.
[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 "app/setup.h"
23 #include "level.h"
24 #include "app/globals.h"
25 #include "video/surface.h"
26 #include "level_subset.h"
27
28 using namespace SuperTux;
29
30 static bool has_suffix(const std::string& data, const std::string& suffix)
31 {
32   if (data.length() >= suffix.length())
33     return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
34   else
35     return false;
36 }
37
38 LevelSubset::LevelSubset()
39   : levels(0)
40 {
41 }
42
43 LevelSubset::~LevelSubset()
44 {
45 }
46
47 void LevelSubset::create(const std::string& subset_name)
48 {
49   Level new_lev;
50   LevelSubset new_subset;
51   new_subset.name = subset_name;
52   new_subset.title = "Unknown Title";
53   new_subset.description = "No description so far.";
54   new_subset.save();
55 }
56
57 void LevelSubset::read_info_file(const std::string& info_file)
58 {
59   lisp_object_t* root_obj = lisp_read_from_file(info_file);
60   lisp_object_t* cur = lisp_car(root_obj);
61
62   if (lisp_symbol_p(cur) && strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
63     {
64       LispReader reader(lisp_cdr(root_obj));
65
66       reader.read_string("title", title, true);
67       reader.read_string("description", description, true);
68       reader.read_string_vector("levels", levels);
69     }
70   else
71     {
72       std::cout << "LevelSubset: parse error in info file: " << info_file << std::endl;
73     }
74
75   lisp_free(root_obj);
76 }
77
78 void LevelSubset::load(const char* subset)
79 {
80   name = subset;
81   
82   // Check in which directory our subset is located (ie. ~/.supertux/
83   // or SUPERTUX_DATADIR)
84   char filename[1024];
85   snprintf(filename, 1024, "%s/levels/%s/", st_dir, subset);
86   if (access(filename, R_OK) == 0)
87     {
88       directory = filename;
89     }
90   else
91     {
92       snprintf(filename, 1024, "%s/levels/%s/", datadir.c_str(), subset);
93       if (access(filename, R_OK) == 0)
94         directory = filename;
95       else
96         std::cout << "Error: LevelSubset: couldn't find subset: " << subset << std::endl;
97     }
98   
99   read_info_file(directory + "info");
100
101   if (levels.empty())
102     { // Level info file doesn't define any levels, so read the
103       // directory to see what we can find
104       std::vector<std::string> files;
105   
106       snprintf(filename, 1024, "%s/levels/%s/", st_dir, subset);
107       if(access(filename, R_OK) == 0)
108         {
109           files = read_directory(filename);
110         }
111       else
112         {
113           snprintf(filename, 1024, "%s/levels/%s/", datadir.c_str(), subset);
114           files = read_directory(filename);
115         }
116   
117       for(std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i)
118         {
119           if (has_suffix(*i, ".stl"))
120             levels.push_back(*i);
121         }
122     }
123 }
124
125 void
126 LevelSubset::save()
127 {
128   FILE* fi;
129   std::string filename;
130
131   /* Save data file: */
132   filename = "/levels/" + name + "/";
133
134   fcreatedir(filename.c_str());
135   filename = std::string(st_dir) + "/levels/" + name + "/info";
136   if(!fwriteable(filename.c_str()))
137     filename = datadir + "/levels/" + name + "/info";
138   if(fwriteable(filename.c_str()))
139     {
140       fi = fopen(filename.c_str(), "w");
141       if (fi == NULL)
142         {
143           perror(filename.c_str());
144         }
145
146       /* Write header: */
147       fprintf(fi,";; SuperTux-Level-Subset\n");
148       fprintf(fi,"(supertux-level-subset\n");
149
150       /* Save title info: */
151       fprintf(fi,"  (title \"%s\")\n", title.c_str());
152
153       /* Save the description: */
154       fprintf(fi,"  (description \"%s\")\n", description.c_str());
155
156       fprintf( fi,")");
157       fclose(fi);
158     }
159 }
160
161 void
162 LevelSubset::add_level(const std::string& name)
163 {
164   levels.push_back(name);
165 }
166
167 std::string
168 LevelSubset::get_level_filename(unsigned int num)
169 {
170   assert(num < levels.size());
171
172   return directory + levels[num];
173 }
174
175 int
176 LevelSubset::get_num_levels() const
177 {
178   return levels.size();
179 }
180
181 /* EOF */