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