1 /* Copyright (C) 1995-2002  FSGames. Ported by Sean Ford and Yan Shosh
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17 //
18 // util.cpp
19 //
20 // random helper functions
21 //
22 #include "util.h"
23 #include "config.h"
24 #include <stdio.h>
25 #include <time.h>
26 #include <string.h> //buffers: for strlen
27 #include <string>
28 #include <sys/stat.h>
29 #include "base.h"
30 
31 using namespace std;
32 
33 unsigned long start_time=0;
34 unsigned long reset_value=0;
35 
change_time(unsigned long new_count)36 void change_time(unsigned long new_count)
37 {}
38 
grab_timer()39 void grab_timer()
40 {}
41 
release_timer()42 void release_timer()
43 {}
44 
reset_timer()45 void reset_timer()
46 {
47 	reset_value = SDL_GetTicks();
48 }
49 
query_timer()50 long query_timer()
51 {
52 	// Zardus: why 13.6? With DOS timing, you had to divide 1,193,180 by the desired frequency and
53 	// that would return ticks / second. Gladiator used to use a frequency of 65536/4 ticks per hour,
54 	// or 1193180/16383 = 72.3 ticks per second. This translates into 13.6 milliseconds / tick
55 	return (long) ((SDL_GetTicks() - reset_value) / 13.6);
56 }
57 
query_timer_control()58 long query_timer_control()
59 {
60 	return (long) (SDL_GetTicks() / 13.6);
61 }
62 
time_delay(long delay)63 void time_delay(long delay)
64 {
65 	if (delay < 0) return;
66 	SDL_Delay((unsigned long) (delay * 13.6));
67 }
68 
lowercase(char * str)69 void lowercase(char * str)
70 {
71 	int i;
72 	for (i = 0; i < strlen(str);i++)
73 		str[i] = tolower(str[i]);
74 }
75 
76 //buffers: add: another extra routine.
uppercase(char * str)77 void uppercase(char *str)
78 {
79 	int i;
80 	for(i=0;i<strlen(str);i++)
81 		str[i] = toupper(str[i]);
82 }
83 
84 // kari: yet two extra
lowercase(std::string & str)85 void lowercase(std::string &str)
86 {
87 	for(std::string::iterator iter = str.begin(); iter!=str.end(); ++iter)
88 		*iter = tolower(*iter);
89 }
90 
uppercase(std::string & str)91 void uppercase(std::string &str)
92 {
93 	for(std::string::iterator iter = str.begin(); iter!=str.end(); ++iter)
94 		*iter = toupper(*iter);
95 }
96 
open_misc_file(char * file,char * pos_dir,char * attr)97 FILE * open_misc_file(char * file, char * pos_dir, char * attr)
98 {
99 	FILE * infile;
100 	char * filename = get_file_path(file, pos_dir, attr);
101 
102 	if (filename && (infile = fopen(filename, attr)))
103 	{
104 		delete filename;
105                 return infile;
106 	}
107 
108 	// if it got here, it didn't find the file
109 	return NULL;
110 }
111 
open_misc_file(char * file,char * pos_dir)112 FILE * open_misc_file(char * file, char * pos_dir)
113 {
114 	return open_misc_file(file, pos_dir, "rb");
115 }
116 
open_misc_file(char * file)117 FILE * open_misc_file(char * file)
118 {
119 	return open_misc_file(file, "", "rb");
120 }
121 
create_dataopenglad()122 void create_dataopenglad()
123 {
124 #ifndef WINDOWS
125 	string path(getenv("HOME"));
126 	path += "/.openglad/";
127 	mkdir(path.c_str(), 0755);
128 	path.reserve(path.size()+10);
129 	string::iterator subdirpos = path.end();
130 	path += "pix/";
131 	mkdir(path.c_str(), 0755);
132 	path.replace(subdirpos, path.end(), "scen/", 4);
133 	mkdir(path.c_str(), 0755);
134 	path.replace(subdirpos, path.end(), "save/", 5);
135 	mkdir(path.c_str(), 0755);
136 	path.replace(subdirpos, path.end(), "sound/", 5);
137 	mkdir(path.c_str(), 0755);
138 #endif
139 }
140 
get_file_path(char * file,char * pos_dir,char * attr)141 char * get_file_path(char * file, char * pos_dir, char * attr)
142 {
143 	FILE * infile;
144 	string filepath(file);
145 
146 #ifndef	WINDOWS
147 	filepath = getenv("HOME");
148 	filepath += "/.openglad/";
149 	filepath += pos_dir;
150 	filepath += file;
151 
152 	if ((infile = fopen(filepath.c_str(), attr)))
153 	{
154 		fclose(infile);
155 		return strdup(filepath.c_str());
156 	}
157 #endif
158 
159 	// Lets try the datadir option now.
160 	if (cfg.query("dirs", "data"))
161 	{
162 		filepath = cfg.query("dirs", "data");
163 		filepath += "/";
164 		filepath += pos_dir;
165 		filepath += file;
166 
167 		if ((infile = fopen(filepath.c_str(), attr)))
168 		{
169 			fclose(infile);
170 			return strdup(filepath.c_str());
171 		}
172 	}
173 
174 	filepath = DATADIR;
175 	filepath += "/";
176 	filepath += pos_dir;
177 	filepath += file;
178 
179 	if ((infile = fopen(filepath.c_str(), attr)))
180 	{
181 		fclose(infile);
182 		return strdup(filepath.c_str());
183 	}
184 
185 	// as a last resort, look in ./posdir/file
186 	filepath = pos_dir;
187 	filepath += file;
188 	if ((infile = fopen(filepath.c_str(), attr)))
189 	{
190 		fclose (infile);
191 		return strdup(filepath.c_str());
192 	}
193 
194 	// if it got here, it didn't find the file
195 	return NULL;
196 }
197 
get_file_path(char * file,char * pos_dir)198 char * get_file_path(char * file, char * pos_dir)
199 {
200 	return get_file_path(file, pos_dir, "rb");
201 }
202 
get_file_path(char * file)203 char * get_file_path(char * file)
204 {
205 	return get_file_path(file, "", "rb");
206 }
207