4 // Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
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.
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.
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
24 #include "app/setup.h"
26 #include "app/globals.h"
27 #include "video/surface.h"
28 #include "level_subset.h"
30 using namespace SuperTux;
32 static bool has_suffix(const std::string& data, const std::string& suffix)
34 if (data.length() >= suffix.length())
35 return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
40 LevelSubset::LevelSubset()
45 LevelSubset::~LevelSubset()
49 void LevelSubset::create(const std::string& subset_name)
52 LevelSubset new_subset;
53 new_subset.name = subset_name;
54 new_subset.title = "Unknown Title";
55 new_subset.description = "No description so far.";
59 void LevelSubset::read_info_file(const std::string& info_file)
61 lisp_object_t* root_obj = lisp_read_from_file(info_file);
62 lisp_object_t* cur = lisp_car(root_obj);
64 if (lisp_symbol_p(cur) && strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
66 LispReader reader(lisp_cdr(root_obj));
68 reader.read_string("title", title, true);
69 reader.read_string("description", description, true);
70 reader.read_string_vector("levels", levels);
74 std::cout << "LevelSubset: parse error in info file: " << info_file << std::endl;
80 void LevelSubset::load(const char* subset)
84 // Check in which directory our subset is located (ie. ~/.supertux/
85 // or SUPERTUX_DATADIR)
87 snprintf(filename, 1024, "%s/levels/%s/", st_dir, subset);
88 if (access(filename, R_OK) == 0)
94 snprintf(filename, 1024, "%s/levels/%s/", datadir.c_str(), subset);
95 if (access(filename, R_OK) == 0)
98 std::cout << "Error: LevelSubset: couldn't find subset: " << subset << std::endl;
101 read_info_file(directory + "info");
104 { // Level info file doesn't define any levels, so read the
105 // directory to see what we can find
106 std::vector<std::string> files;
108 snprintf(filename, 1024, "%s/levels/%s/", st_dir, subset);
109 if(access(filename, R_OK) == 0)
111 files = FileSystem::read_directory(filename);
115 snprintf(filename, 1024, "%s/levels/%s/", datadir.c_str(), subset);
116 files = FileSystem::read_directory(filename);
119 for(std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i)
121 if (has_suffix(*i, ".stl"))
122 levels.push_back(*i);
131 std::string filename;
133 /* Save data file: */
134 filename = "/levels/" + name + "/";
136 FileSystem::fcreatedir(filename.c_str());
137 filename = std::string(st_dir) + "/levels/" + name + "/info";
138 if(!FileSystem::fwriteable(filename.c_str()))
139 filename = datadir + "/levels/" + name + "/info";
140 if(FileSystem::fwriteable(filename.c_str()))
142 fi = fopen(filename.c_str(), "w");
145 perror(filename.c_str());
149 fprintf(fi,";; SuperTux-Level-Subset\n");
150 fprintf(fi,"(supertux-level-subset\n");
152 /* Save title info: */
153 fprintf(fi," (title \"%s\")\n", title.c_str());
155 /* Save the description: */
156 fprintf(fi," (description \"%s\")\n", description.c_str());
164 LevelSubset::add_level(const std::string& name)
166 levels.push_back(name);
170 LevelSubset::get_level_filename(unsigned int num)
172 assert(num < levels.size());
174 return directory + levels[num];
178 LevelSubset::get_num_levels() const
180 return levels.size();