3 #include "file_system.h"
11 #include "resources.h"
14 #define mkdir(dir, mode) mkdir(dir)
20 /* Does the given file exist and is it accessible? */
21 bool faccessible(const std::string& filename)
23 FILE* f = fopen(filename.c_str(), "r");
31 /* Can we write to this location? */
32 bool fwriteable(const std::string& filename)
34 FILE* f = fopen(filename.c_str(), "wa");
42 /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/
43 bool fcreatedir(const std::string& relative_dir)
45 std::string path = user_dir + "/" + relative_dir + "/";
46 if(mkdir(path.c_str(),0755) == 0)
49 path = datadir + "/" + relative_dir + "/";
50 if(mkdir(path.c_str(),0755) == 0)
56 /* Get all names of sub-directories in a certain directory. */
57 /* Returns the number of sub-directories found. */
58 /* Note: The user has to free the allocated space. */
59 std::set<std::string> dsubdirs(const std::string &rel_path,
60 const std::string& expected_file)
63 struct dirent *direntp;
64 std::set<std::string> sdirs;
66 std::string path = user_dir + "/" + rel_path;
68 if((dirStructP = opendir(path.c_str())) != NULL)
70 while((direntp = readdir(dirStructP)) != NULL)
72 std::string absolute_filename;
75 absolute_filename = path + "/" + direntp->d_name;
77 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode))
79 if(!expected_file.empty())
81 filename = path + "/" + direntp->d_name + "/" + expected_file;
82 if(!faccessible(filename))
86 sdirs.insert(direntp->d_name);
92 path = datadir + "/" + rel_path;
93 if((dirStructP = opendir(path.c_str())) != NULL)
95 while((direntp = readdir(dirStructP)) != NULL)
97 std::string absolute_filename;
100 absolute_filename = path + "/" + direntp->d_name;
102 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode))
104 if(!expected_file.empty())
106 filename = path + "/" + direntp->d_name + "/" + expected_file;
107 if(!faccessible(filename.c_str()))
113 filename = user_dir + "/" + rel_path + "/" + direntp->d_name + "/" + expected_file;
114 if(faccessible(filename.c_str()))
119 sdirs.insert(direntp->d_name);
122 closedir(dirStructP);
128 std::set<std::string> dfiles(const std::string& rel_path,
129 const std::string& glob, const std::string& exception_str)
132 struct dirent *direntp;
133 std::set<std::string> sdirs;
134 std::string path = user_dir + "/" + rel_path;
136 if((dirStructP = opendir(path.c_str())) != NULL)
138 while((direntp = readdir(dirStructP)) != NULL)
140 std::string absolute_filename;
143 absolute_filename = path + "/" + direntp->d_name;
145 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISREG(buf.st_mode))
147 if(!exception_str.empty())
149 if(strstr(direntp->d_name,exception_str.c_str()) != NULL)
153 if(strstr(direntp->d_name,glob.c_str()) == NULL)
156 sdirs.insert(direntp->d_name);
159 closedir(dirStructP);
162 path = datadir + "/" + rel_path;
163 if((dirStructP = opendir(path.c_str())) != NULL)
165 while((direntp = readdir(dirStructP)) != NULL)
167 std::string absolute_filename;
170 absolute_filename = path + "/" + direntp->d_name;
172 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISREG(buf.st_mode))
174 if(!exception_str.empty())
176 if(strstr(direntp->d_name,exception_str.c_str()) != NULL)
180 if(strstr(direntp->d_name,glob.c_str()) == NULL)
183 sdirs.insert(direntp->d_name);
186 closedir(dirStructP);
192 std::string dirname(const std::string& filename)
194 std::string::size_type p = filename.find_last_of('/');
195 if(p == std::string::npos)
198 return filename.substr(0, p+1);
201 std::set<std::string> read_directory(const std::string& pathname)
203 std::set<std::string> dirnames;
205 DIR* dir = opendir(pathname.c_str());
208 struct dirent *direntp;
210 while((direntp = readdir(dir)))
212 dirnames.insert(direntp->d_name);