1 // $Id: unix_system.cxx,v 1.15 2003/07/25 13:16:04 grumbel Exp $
3 // Construo - A wire-frame construction game
4 // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
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.
28 #include <sys/types.h>
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
45 gettimeofday(&tv
, NULL
);
46 start_time
= (long) tv
.tv_sec
*(long) 1000+(long) tv
.tv_usec
/(long) 1000;
48 char* home
= getenv("HOME");
51 construo_rc_path
= std::string(home
) + std::string("/.construo/");
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
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
+ ": "
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 ()
89 UnixSystem::get_time ()
90 { // riped out of ClanLib-0.7
92 gettimeofday(&tv
, NULL
);
94 long tid
= (long) tv
.tv_sec
*(long) 1000 + (long) tv
.tv_usec
/(long) 1000 - start_time
;
100 UnixSystem::sleep (unsigned long t
)
106 UnixSystem::get_construo_rc_path()
108 std::cout
<< "Returning: \n" << construo_rc_path
<< std::endl
;
109 return construo_rc_path
;
113 UnixSystem::get_user_realname()
117 pw
= getpwuid(getuid());
129 UnixSystem::get_user_email()
131 const char* s_email
= getenv("EMAIL");
141 UnixSystem::get_mtime (const std::string
& filename
)
143 std::string sys_name
= translate_filename(filename
);
146 if (stat(sys_name
.c_str(), &buf
) != 0)
157 UnixSystem::get_file_type(const std::string
& filename
)
159 if (filename
== "/examples/"
160 || filename
== "/user/")
163 std::string sys_name
= translate_filename(filename
);
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
;
173 if (S_ISDIR(buf
.st_mode
))
177 else if (S_ISREG(buf
.st_mode
))
179 if (has_suffix(filename
, ".construo") || has_suffix(filename
, ".construo.gz"))
180 return FT_CONSTRUO_FILE
;
183 return FT_UNKNOWN_FILE
;
188 return FT_UNKNOWN_FILE
;
194 UnixSystem::translate_filename (const std::string
& filename
)
198 assert("root directory is not translatable");
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);
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");
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
230 struct DirectorySorter
232 std::string pathname
;
234 DirectorySorter(const std::string
& 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
)
246 else if (lhs_type
== FT_DIRECTORY
)
250 else if (rhs_type
== FT_DIRECTORY
)
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/");
273 std::vector
<std::string
> dir_lst
;
274 std::string pathname
= translate_filename (arg_pathname
);
276 DIR* dir
= ::opendir (pathname
.c_str());
280 std::cout
<< "UnixSystem: Error couldn't open: '" << pathname
<< "', ignoring\n"
281 << " error and continuing with an empty directory" << std::endl
;
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
);
300 std::sort(dir_lst
.begin(), dir_lst
.end(), DirectorySorter(pathname
));