Fixed includes and bugs so that it compiles
[construo.git] / unix_system.cxx
blob9bc41465e8199e9c8ecb9cb4bc0bca021a43dc66
1 // $Id: unix_system.cxx,v 1.15 2003/07/25 13:16:04 grumbel Exp $
2 //
3 // Construo - A wire-frame construction game
4 // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
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.
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 02111-1307, USA.
20 #include <stdio.h>
21 #include <string.h>
22 #include <assert.h>
23 #include <unistd.h>
24 #include <sys/time.h>
25 #include <time.h>
26 #include <string>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <dirent.h>
30 #include <pwd.h>
31 #include <errno.h>
32 #include <iostream>
33 #include <algorithm>
34 #include "construo_error.hxx"
35 #include "string_utils.hxx"
36 #include "construo.hxx"
37 #include "path_manager.hxx"
38 #include "unix_system.hxx"
40 using namespace StringUtils;
42 UnixSystem::UnixSystem ()
43 { // riped out of ClanLib-0.7
44 timeval tv;
45 gettimeofday(&tv, NULL);
46 start_time = (long) tv.tv_sec*(long) 1000+(long) tv.tv_usec/(long) 1000;
48 char* home = getenv("HOME");
49 if (home)
51 construo_rc_path = std::string(home) + std::string("/.construo/");
53 else
55 std::cout << "UnixSystem: FATAL ERROR: couldn't find env variable $HOME" << std::endl;
56 throw ConstruoError ("UnixSystem: Couldn't find $HOME!");
59 // create $HOME directory if not already there
60 struct stat buf;
62 if (stat(construo_rc_path.c_str(), &buf) != 0) // Couldn't find directory, create it
64 if (mkdir(construo_rc_path.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) != 0)
66 throw ConstruoError(std::string("UnixSystem: ") + construo_rc_path + ": "
67 + strerror(errno));
70 else
72 if (S_ISDIR(buf.st_rdev)) // Is not a directory
74 throw ConstruoError("Error: " + construo_rc_path + " is not a directory!");
77 if (access(construo_rc_path.c_str (), R_OK | W_OK | X_OK) != 0) // not readable/writeable
79 throw ConstruoError("Error: " + construo_rc_path + " is not read or writeable!");
84 UnixSystem::~UnixSystem ()
88 unsigned int
89 UnixSystem::get_time ()
90 { // riped out of ClanLib-0.7
91 timeval tv;
92 gettimeofday(&tv, NULL);
94 long tid = (long) tv.tv_sec*(long) 1000 + (long) tv.tv_usec/(long) 1000 - start_time;
96 return tid;
99 void
100 UnixSystem::sleep (unsigned long t)
102 usleep (t);
105 std::string
106 UnixSystem::get_construo_rc_path()
108 std::cout << "Returning: \n" << construo_rc_path << std::endl;
109 return construo_rc_path;
112 std::string
113 UnixSystem::get_user_realname()
115 struct passwd* pw;
117 pw = getpwuid(getuid());
118 if (pw)
120 return pw->pw_gecos;
122 else
124 return "";
128 std::string
129 UnixSystem::get_user_email()
131 const char* s_email = getenv("EMAIL");
132 if (s_email)
134 return s_email;
136 else
137 return "";
140 unsigned int
141 UnixSystem::get_mtime (const std::string& filename)
143 std::string sys_name = translate_filename(filename);
145 struct stat buf;
146 if (stat(sys_name.c_str(), &buf) != 0)
148 return 0;
150 else
152 return buf.st_mtime;
156 FileType
157 UnixSystem::get_file_type(const std::string& filename)
159 if (filename == "/examples/"
160 || filename == "/user/")
161 return FT_DIRECTORY;
163 std::string sys_name = translate_filename(filename);
165 struct stat buf;
166 if (stat(sys_name.c_str(), &buf) != 0)
168 std::cout << "UnixSystem: ERROR: Couldn't stat: '" << sys_name << "'" << std::endl;
169 return FT_UNKNOWN_FILE;
171 else
173 if (S_ISDIR(buf.st_mode))
175 return FT_DIRECTORY;
177 else if (S_ISREG(buf.st_mode))
179 if (has_suffix(filename, ".construo") || has_suffix(filename, ".construo.gz"))
180 return FT_CONSTRUO_FILE;
181 else
183 return FT_UNKNOWN_FILE;
186 else
188 return FT_UNKNOWN_FILE;
193 std::string
194 UnixSystem::translate_filename (const std::string& filename)
196 if (filename == "/")
198 assert("root directory is not translatable");
199 return "";
201 else if (has_prefix(filename, "/user/"))
203 return construo_rc_path + filename.substr(6);
205 else if (has_prefix(filename, "/examples/"))
207 return path_manager.complete("examples/") + filename.substr(10);
209 else
210 return filename;
213 FILE*
214 UnixSystem::open_input_file(const std::string& filename)
216 std::cout << "UnixSystem: open_input_file: " << translate_filename (filename) << std::endl;
217 return fopen(translate_filename (filename).c_str(), "r");
220 FILE*
221 UnixSystem::open_output_file(const std::string& filename)
223 std::cout << "UnixSystem: open_output_file: " << translate_filename (filename) << std::endl;
224 return fopen(translate_filename (filename).c_str(), "w");
228 /** Sort directories before files and sort them all
229 alphabetically */
230 struct DirectorySorter
232 std::string pathname;
234 DirectorySorter(const std::string& p)
235 : pathname(p)
239 bool operator()(const std::string& lhs, const std::string& rhs)
241 FileType lhs_type = system_context->get_file_type(pathname + "/" + lhs);
242 FileType rhs_type = system_context->get_file_type(pathname + "/" + rhs);
244 if (lhs_type == rhs_type)
245 return (lhs < rhs);
246 else if (lhs_type == FT_DIRECTORY)
248 return true;
250 else if (rhs_type == FT_DIRECTORY)
252 return false;
254 else
256 return (lhs < rhs);
261 std::vector<std::string>
262 UnixSystem::read_directory(const std::string& arg_pathname)
264 if (arg_pathname == "/")
266 std::vector<std::string> ret;
267 ret.push_back("examples/");
268 ret.push_back("user/");
269 return ret;
271 else
273 std::vector<std::string> dir_lst;
274 std::string pathname = translate_filename (arg_pathname);
276 DIR* dir = ::opendir (pathname.c_str());
278 if (!dir)
280 std::cout << "UnixSystem: Error couldn't open: '" << pathname << "', ignoring\n"
281 << " error and continuing with an empty directory" << std::endl;
283 else
285 struct dirent* entry;
287 while ((entry = readdir(dir)) != 0)
289 if (strcmp(entry->d_name, ".") != 0
290 && strcmp(entry->d_name, "..") != 0
291 && strcmp(entry->d_name, "CVS") != 0)
292 { // We ignore unusefull directories
293 dir_lst.push_back(entry->d_name);
297 closedir (dir);
300 std::sort(dir_lst.begin(), dir_lst.end(), DirectorySorter(pathname));
302 return dir_lst;
306 /* EOF */