1 // Copyright (C) 2000, 2001, 2002, 2003 Michael Bartl
2 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Ulf Lorenz
3 // Copyright (C) 2004, 2005, 2006 Andrea Paternesi
4 // Copyright (C) 2006, 2007, 2008, 2009 Ben Asselstine
5 // Copyright (C) 2007 Ole Laursen
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Library General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include <glibmm/fileutils.h>
28 #include <glibmm/ustring.h>
29 #include <glibmm/convert.h>
30 #include <sys/types.h>
38 #include "Configuration.h"
42 #include "shieldset.h"
45 #define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<std::flush;}
50 // returns a list of full paths of the immediate subdirs of path
51 std::list<std::string> get_immediate_subdirs(std::string path)
54 std::list<std::string> dirlist;
56 for (Glib::Dir::iterator i = dir.begin(), end = dir.end();
59 std::string entry = path + *i;
60 if (Glib::file_test(entry, Glib::FILE_TEST_IS_DIR))
61 dirlist.push_back(entry);
67 // returns a list of the XML file names in path with the ".xml" extension
69 std::list<std::string> get_xml_files(std::string path, std::string ext)
71 std::list<std::string> retlist;
74 for (Glib::Dir::iterator i = dir.begin(), end = dir.end(); i != end; ++i)
76 std::string entry = *i;
77 std::string::size_type idx = entry.rfind(ext);
78 if (idx != std::string::npos &&
79 idx == entry.length() - ext.length())
81 entry.replace(idx, ext.length(), ""); //substitute the ".xml" with ""
82 retlist.push_back(Glib::filename_to_utf8(path + entry));
88 // returns a list of the XML file names in the immediate subdirs of path
89 // with the ".xml" extension stripped
90 std::list<std::string> get_xml_files_in_immediate_subdirs(std::string path, std::string ext)
92 std::list<std::string> retlist, dirlist = get_immediate_subdirs(path);
93 for (std::list<std::string>::iterator i = dirlist.begin(),
94 end = dirlist.end(); i != end; ++i)
96 std::list<std::string> files =
97 get_xml_files(File::add_slash_if_necessary(*i), ext);
99 retlist.insert(retlist.end(), files.begin(), files.end());
105 std::string File::add_slash_if_necessary(std::string dir)
107 if (dir.c_str()[strlen(dir.c_str())-1] == '/')
113 std::string File::getMiscFile(std::string filename)
115 return Configuration::s_dataPath + "/" + filename;
118 std::string File::getItemDescription()
120 return Configuration::s_dataPath + "/various/items/items.xml";
123 std::string File::getEditorFile(std::string filename)
125 return Configuration::s_dataPath + "/various/editor/" + filename + ".png";
128 std::string File::getMusicFile(std::string filename)
130 return std::string(Configuration::s_dataPath + "/music/" + filename.c_str());
133 std::string File::getDataPath()
135 return add_slash_if_necessary(Configuration::s_dataPath);
138 std::string File::getSavePath()
140 return add_slash_if_necessary(Configuration::s_savePath);
143 std::string File::getUserMapDir()
145 return add_slash_if_necessary(Configuration::s_savePath) + MAPDIR + "/";
148 std::string File::getMapDir()
150 return add_slash_if_necessary(Configuration::s_dataPath) + MAPDIR + "/";
153 std::string File::getUserMapFile(std::string file)
155 return getUserMapDir() + file;
158 std::string File::getMapFile(std::string file)
160 return getMapDir() + file;
162 std::list<std::string> File::scanUserMaps()
164 std::string path = File::getUserMapDir();
166 std::list<std::string> retlist;
169 for (Glib::Dir::iterator i = dir.begin(), end = dir.end(); i != end; ++i)
171 std::string entry = *i;
172 std::string::size_type idx = entry.find(".map");
173 if (idx != std::string::npos)
175 if (entry == "random.map")
177 retlist.push_back(Glib::filename_to_utf8(entry));
184 std::list<std::string> File::scanMaps()
186 std::string path = File::getMapDir();
188 std::list<std::string> retlist;
191 for (Glib::Dir::iterator i = dir.begin(), end = dir.end(); i != end; ++i)
193 std::string entry = *i;
194 std::string::size_type idx = entry.find(".map");
195 if (idx != std::string::npos)
197 retlist.push_back(Glib::filename_to_utf8(entry));
203 std::cerr << "Couldn't find a single map!" << std::endl;
204 std::cerr << "Please check the path settings in /etc/lordsawarrc or ~/.lordsawarrc" << std::endl;
210 std::string File::get_dirname(std::string path)
212 return Glib::path_get_dirname(path);
214 std::string File::get_basename(std::string path, bool keep_ext)
217 file = Glib::path_get_basename(path);
220 //now strip everything past the last dot.
221 const char *tmp = strrchr (file.c_str(), '.');
224 int npos = tmp - file.c_str() + 1;
225 file = file.substr(0, npos - 1);
228 //copy_file taken from ardour-2.0rc2, gplv2+.
229 int File::copy (Glib::ustring from, Glib::ustring to)
231 std::ifstream in (from.c_str());
232 std::ofstream out (to.c_str());
250 bool File::create_dir(std::string dir)
254 if (stat(dir.c_str(), &testdir) || !S_ISDIR(testdir.st_mode))
256 guint32 mask = 0755; //make directory only readable for user and group
257 if (mkdir(dir.c_str(), mask))
266 bool File::is_writable(std::string file)
269 if (access (file.c_str(), W_OK) != 0)
277 bool File::exists(std::string f)
279 FILE *fileptr = fopen (f.c_str(), "r");
280 bool retval = fileptr != NULL;
288 std::string File::getArmysetDir()
290 return add_slash_if_necessary(Configuration::s_dataPath) + ARMYSETDIR + "/";
293 std::string File::getUserArmysetDir()
295 std::string dir = getSavePath() + ARMYSETDIR + "/";
299 std::list<std::string> File::scanFiles(std::string dir, std::string extension)
301 return get_xml_files_in_immediate_subdirs(dir, extension);
304 std::string File::getTilesetDir()
306 return add_slash_if_necessary(Configuration::s_dataPath) + TILESETDIR + "/";
309 std::string File::getUserTilesetDir()
311 std::string dir = getSavePath() + TILESETDIR + "/";
316 std::string File::getShieldsetDir()
318 return add_slash_if_necessary(Configuration::s_dataPath) + SHIELDSETDIR + "/";
321 std::string File::getUserShieldsetDir()
323 std::string dir = getSavePath() + SHIELDSETDIR + "/";
327 std::string File::getCitysetDir()
329 return add_slash_if_necessary(Configuration::s_dataPath) + CITYSETDIR + "/";
332 std::string File::getUserCitysetDir()
334 std::string dir = getSavePath() + CITYSETDIR + "/";
339 bool File::nameEndsWith(std::string filename, std::string extension)
341 std::string::size_type idx = filename.rfind(extension);
342 if (idx == std::string::npos)
344 if (idx == filename.length() - extension.length())
349 void File::erase(std::string filename)
351 remove(filename.c_str());
354 void File::erase_dir(std::string filename)
356 rmdir(filename.c_str());
358 std::string File::getSetConfigurationFilename(std::string dir, std::string subdir, std::string ext)
360 return add_slash_if_necessary(dir) + subdir + "/" + subdir + ext;