]> icculus.org git repositories - divverent/darkplaces.git/blob - sys_shared.c
m.wantoverbright is gone
[divverent/darkplaces.git] / sys_shared.c
1
2 #include "quakedef.h"
3 #include <time.h>
4 #ifdef WIN32
5 #include <direct.h>
6 #else
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #endif
11 #include <errno.h>
12
13 extern cvar_t   timestamps;
14 extern cvar_t   timeformat;
15
16 static int sys_nostdout = false;
17
18 /* The translation table between the graphical font and plain ASCII  --KB */
19 static char qfont_table[256] = {
20         '\0', '#',  '#',  '#',  '#',  '.',  '#',  '#',
21         '#',  9,    10,   '#',  ' ',  13,   '.',  '.',
22         '[',  ']',  '0',  '1',  '2',  '3',  '4',  '5',
23         '6',  '7',  '8',  '9',  '.',  '<',  '=',  '>',
24         ' ',  '!',  '"',  '#',  '$',  '%',  '&',  '\'',
25         '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',
26         '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',
27         '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?',
28         '@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',
29         'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',
30         'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',
31         'X',  'Y',  'Z',  '[',  '\\', ']',  '^',  '_',
32         '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g',
33         'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',
34         'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
35         'x',  'y',  'z',  '{',  '|',  '}',  '~',  '<',
36
37         '<',  '=',  '>',  '#',  '#',  '.',  '#',  '#',
38         '#',  '#',  ' ',  '#',  ' ',  '>',  '.',  '.',
39         '[',  ']',  '0',  '1',  '2',  '3',  '4',  '5',
40         '6',  '7',  '8',  '9',  '.',  '<',  '=',  '>',
41         ' ',  '!',  '"',  '#',  '$',  '%',  '&',  '\'',
42         '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',
43         '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',
44         '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?',
45         '@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',
46         'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',
47         'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',
48         'X',  'Y',  'Z',  '[',  '\\', ']',  '^',  '_',
49         '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g',
50         'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',
51         'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
52         'x',  'y',  'z',  '{',  '|',  '}',  '~',  '<'
53 };
54
55 #ifdef WIN32
56 extern HANDLE hinput, houtput;
57 #endif
58
59 #define MAX_PRINT_MSG   16384
60 void Sys_Printf (const char *fmt, ...)
61 {
62         va_list         argptr;
63         char            start[MAX_PRINT_MSG];   // String we started with
64         char            stamp[MAX_PRINT_MSG];   // Time stamp
65         char            final[MAX_PRINT_MSG];   // String we print
66
67         time_t          mytime = 0;
68         struct tm       *local = NULL;
69
70         unsigned char           *p;
71 #ifdef WIN32
72         DWORD           dummy;
73 #endif
74
75         va_start (argptr, fmt);
76 #ifdef HAVE_VSNPRINTF
77         vsnprintf (start, sizeof(start), fmt, argptr);
78 #else
79         vsprintf (start, fmt, argptr);
80 #endif
81         va_end (argptr);
82
83         if (sys_nostdout)
84                 return;
85
86         if (timestamps.integer)
87         {
88                 mytime = time (NULL);
89                 local = localtime (&mytime);
90                 strftime (stamp, sizeof (stamp), timeformat.string, local);
91
92                 snprintf (final, sizeof (final), "%s%s", stamp, start);
93         }
94         else
95                 snprintf (final, sizeof (final), "%s", start);
96
97         // LordHavoc: make sure the string is terminated
98         final[MAX_PRINT_MSG - 1] = 0;
99         for (p = (unsigned char *) final;*p; p++)
100                 *p = qfont_table[*p];
101 #ifdef WIN32
102         if (cls.state == ca_dedicated)
103                 WriteFile(houtput, final, strlen (final), &dummy, NULL);
104 #else
105         printf("%s", final);
106 #endif
107 }
108
109 // LordHavoc: 256 pak files (was 10)
110 #define MAX_HANDLES 256
111 QFile *sys_handles[MAX_HANDLES];
112
113 int findhandle (void)
114 {
115         int i;
116
117         for (i = 1;i < MAX_HANDLES;i++)
118                 if (!sys_handles[i])
119                         return i;
120         Sys_Error ("out of handles");
121         return -1;
122 }
123
124 /*
125 ================
126 Sys_FileLength
127 ================
128 */
129 int Sys_FileLength (QFile *f)
130 {
131         int pos, end;
132
133         pos = Qtell (f);
134         Qseek (f, 0, SEEK_END);
135         end = Qtell (f);
136         Qseek (f, pos, SEEK_SET);
137
138         return end;
139 }
140
141 int Sys_FileOpenRead (const char *path, int *handle)
142 {
143         QFile *f;
144         int i, retval;
145
146         i = findhandle ();
147
148         f = Qopen(path, "rbz");
149
150         if (!f)
151         {
152                 *handle = -1;
153                 retval = -1;
154         }
155         else
156         {
157                 sys_handles[i] = f;
158                 *handle = i;
159                 retval = Sys_FileLength(f);
160         }
161
162         return retval;
163 }
164
165 int Sys_FileOpenWrite (const char *path)
166 {
167         QFile   *f;
168         int             i;
169
170         i = findhandle ();
171
172         f = Qopen(path, "wb");
173         if (!f)
174         {
175                 Con_Printf("Sys_FileOpenWrite: Error opening %s: %s", path, strerror(errno));
176                 return 0;
177         }
178         sys_handles[i] = f;
179
180         return i;
181 }
182
183 void Sys_FileClose (int handle)
184 {
185         Qclose (sys_handles[handle]);
186         sys_handles[handle] = NULL;
187 }
188
189 void Sys_FileSeek (int handle, int position)
190 {
191         Qseek (sys_handles[handle], position, SEEK_SET);
192 }
193
194 int Sys_FileRead (int handle, void *dest, int count)
195 {
196         return Qread (sys_handles[handle], dest, count);
197 }
198
199 int Sys_FileWrite (int handle, void *data, int count)
200 {
201         return Qwrite (sys_handles[handle], data, count);
202 }
203
204 int Sys_FileTime (const char *path)
205 {
206 #if WIN32
207         QFile *f;
208
209         f = Qopen(path, "rb");
210         if (f)
211         {
212                 Qclose(f);
213                 return 1;
214         }
215
216         return -1;
217 #else
218         struct stat buf;
219
220         if (stat (path,&buf) == -1)
221                 return -1;
222
223         return buf.st_mtime;
224 #endif
225 }
226
227 void Sys_mkdir (const char *path)
228 {
229 #if WIN32
230         _mkdir (path);
231 #else
232         mkdir (path, 0777);
233 #endif
234 }
235
236 char engineversion[128];
237
238 void Sys_Shared_EarlyInit(void)
239 {
240         Memory_Init ();
241
242         COM_InitArgv();
243         COM_InitGameType();
244
245 #if defined(__linux__)
246         sprintf (engineversion, "%s Linux %s", gamename, buildstring);
247 #elif defined(WIN32)
248         sprintf (engineversion, "%s Windows %s", gamename, buildstring);
249 #else
250         sprintf (engineversion, "%s Unknown %s", gamename, buildstring);
251 #endif
252
253         if (COM_CheckParm("-nostdout"))
254                 sys_nostdout = 1;
255         else
256                 printf("%s\n", engineversion);
257 }
258
259 void Sys_Shared_LateInit(void)
260 {
261 }
262