// $Id: system.cxx,v 1.6 2002/09/15 00:19:19 grumbel Exp $
//
// Pingus - A free Lemmings clone
// Copyright (C) 1999 Ingo Ruhnke
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "globals.hxx"
#include "string_converter.hxx"
#include "system.hxx"
/* Headers needed for i18n / gettext */
#include
#include
#include "my_gettext.hxx"
System::Directory
System::opendir(const std::string& pathname, const std::string& pattern)
{
std::list dir_list;
DIR* dp;
dirent* de;
dp = ::opendir(pathname.c_str());
if (dp == 0)
{
std::cout << _("System: Couldn't open: ") << pathname << std::endl;
}
else
{
while ((de = ::readdir(dp)) != 0)
{
if (fnmatch(pattern.c_str(), de->d_name, FNM_PATHNAME) == 0)
{
struct stat buf;
stat ((pathname + "/" + de->d_name).c_str (), &buf);
if (S_ISDIR(buf.st_mode))
{
dir_list.push_back(DirectoryEntry(de->d_name, DirectoryEntry::DE_DIRECTORY));
}
else
{
dir_list.push_back(DirectoryEntry(de->d_name, DirectoryEntry::DE_FILE));
}
}
}
closedir(dp);
}
return dir_list;
}
bool
System::exist(std::string filename)
{
return !access(filename.c_str(), F_OK);
}
void
System::create_dir(std::string directory)
{
if (!exist(directory))
{
if (mkdir(directory.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) != 0)
{
throw Error(directory + ": " + strerror(errno));
}
else
{
std::cout << _("Successfully created: ") << directory << std::endl;
}
}
else
{
if (verbose) std::cout << _("Found: ") << directory << std::endl;
}
}
/** Change into the directory named dir, on error throw an PingusError */
void
System::change_dir (std::string dir)
{
std::cout << "System: change_dir: " << dir << std::endl;
_chdir (dir.c_str ());
}
std::string
System::get_statdir()
{
char* homedir = getenv("HOME");
if (homedir)
{
return std::string(homedir) + "/.pingus/";
}
else
{
throw Error(_("Environment variable $HOME not set, fix that and start again."));
}
}
std::string
System::get_vardir()
{
return "/var/games/pingus/";
}
std::string
System::get_tmpdir()
{
return "/tmp/";
}
/* EOF */