]> icculus.org git repositories - taylor/freespace2.git/blob - src/platform/unix.cpp
Update to newest movie code with much better video support and audio support from
[taylor/freespace2.git] / src / platform / unix.cpp
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <errno.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10
11 #include "pstypes.h"
12
13 // use system versions of this stuff in here rather than the vm_* versions
14 #undef malloc
15 #undef free
16 #undef strdup
17
18
19 #define MAX_LINE_WIDTH 128
20
21 void strlwr (char * str)
22 {
23         while (*str) {*str = tolower (*str); str++; }
24 }
25
26 void strupr (char * str)
27 {
28         while (*str) {*str = toupper (*str); str++; }
29 }
30
31 int filelength (int fd)
32 {
33         struct stat buf;
34         if (fstat (fd, &buf) == -1)
35                 return -1;
36                 
37         return buf.st_size;
38 }
39
40 unsigned long _beginthread (void (*pfuncStart)(void *), unsigned unStackSize, void* pArgList)
41 {
42         STUB_FUNCTION;
43         
44         return 0;
45 }
46
47 void Sleep (int mili)
48 {
49         SDL_Delay( long(mili) );
50 }
51
52 void OutputDebugString (const char *str)
53 {
54         fprintf(stderr, "OutputDebugString: %s\n", str);
55 }
56
57 int WSAGetLastError()
58 {
59         return errno;
60 }
61
62 void _splitpath (const char *path, char *drive, char *dir, char *fname, char *ext)
63 {
64         if (path == NULL)
65                 return;
66
67         /* fs2 only uses fname */
68         if (fname != NULL) {
69                 const char *ls = strrchr(path, '/');
70                 if (ls != NULL) {
71                         ls++;           // move past '/'
72                 } else {
73                         ls = path;
74                 }
75         
76                 const char *lp = strrchr(path, '.');
77                 if (lp == NULL) {
78                         lp = ls + strlen(ls);   // move to the end
79                 }
80         
81                 int dist = lp-ls;
82                 if (dist > (_MAX_FNAME-1))
83                         dist = _MAX_FNAME-1;
84                 
85                 strncpy(fname, ls, dist);
86                 fname[dist] = 0;        // add null, just in case
87         }
88 }
89
90 int MulDiv(int a, int b, int c)
91 {
92         /* slow long long version */
93         __extension__ long long aa = a;
94         __extension__ long long bb = b;
95         __extension__ long long cc = c;
96         
97         __extension__ long long dd = aa * bb;
98         __extension__ long long ee = dd / cc;
99         
100         int retr = (int) ee;
101         
102         return retr;
103 }
104
105 /* mem debug junk */
106 #ifndef NDEBUG
107 //#define WANT_DEBUG
108 #endif
109
110 int TotalRam = 0;
111
112 #ifdef WANT_DEBUG
113 typedef struct RAM {
114         void *addr;
115         int size;
116         
117         char *file;
118         int line;
119         
120         RAM *next;
121 } RAM;
122
123 static RAM *RamTable;
124 #endif
125
126 #ifndef NDEBUG
127 void vm_free(void* ptr, char *file, int line)
128 #else
129 void vm_free(void* ptr)
130 #endif
131 {
132 #ifdef WANT_DEBUG
133         fprintf(stderr, "FREE: %s:%d addr = %p\n", file, line, ptr);
134         
135         RAM *item = RamTable;
136         RAM **mark = &RamTable;
137         
138         while (item != NULL) {
139                 if (item->addr == ptr) {
140                         RAM *tmp = item;
141                         
142                         *mark = item->next;
143                         
144                         free(tmp->addr);
145                         free(tmp);
146                         
147                         return;
148                 }
149                 
150                 mark = &(item->next);
151                 
152                 item = item->next;
153         }
154         
155         fprintf(stderr, "ERROR: vm_free caught invalid free: addr = %p, file = %s/%d\n", ptr, file, line);
156 #else   
157         free(ptr);
158 #endif
159 }
160
161 #ifndef NDEBUG
162 void *vm_malloc(int size, char *file, int line)
163 #else
164 void *vm_malloc(int size)
165 #endif
166 {
167 #ifdef WANT_DEBUG
168         fprintf(stderr, "MALLOC: %s:%d %d bytes\n", file, line, size);
169         
170         RAM *next = (RAM *)malloc(sizeof(RAM));
171         
172         next->addr = malloc(size);
173         next->size = size;
174         next->file = file;
175         next->line = line;
176         
177         next->next = RamTable;
178         RamTable = next;
179         
180         return next->addr;
181 #else
182         return malloc(size);
183 #endif  
184 }
185
186 #ifndef NDEBUG
187 char *vm_strdup(char const* str, char *file, int line)
188 #else
189 char *vm_strdup(char const* str)
190 #endif
191 {
192 #ifdef WANT_DEBUG
193         fprintf(stderr, "STRDUP: %s:%d\n", file, line);
194         
195         RAM *next = (RAM *)malloc(sizeof(RAM));
196         
197         next->addr = strdup(str);
198         next->size = strlen(str)+1;
199         next->file = file;
200         next->line = line;
201         
202         next->next = RamTable;
203         RamTable = next;
204         
205         return (char *)next->addr;
206 #else
207         return strdup(str);
208 #endif
209 }
210
211 void vm_dump()
212 {
213 #ifdef WANT_DEBUG
214         int i = 0;
215         int mem = 0;
216         fprintf(stderr, "\nDumping allocated memory:\n");
217         
218         RAM *ptr = RamTable;
219         while (ptr) {
220                 fprintf(stderr, "%d: file: %s:%d: addr:%p size:%d\n", i, ptr->file, ptr->line, ptr->addr, ptr->size);
221                 mem += ptr->size;
222                 ptr = ptr->next;
223                 i++;
224         }
225         
226         fprintf(stderr, "\nTotal of %d left-over bytes from %d allocations\n", mem, i);
227 #endif  
228 }
229
230 void windebug_memwatch_init()
231 {
232         TotalRam = 0;
233 }
234
235 /* error message debugging junk */
236 /*
237 int Log_debug_output_to_file = 0;
238
239 void load_filter_info(void)
240 {
241 //      STUB_FUNCTION;
242 }
243
244 void outwnd_printf(char* id, char* format, ...)
245 {
246         char tmp[MAX_LINE_WIDTH*4];
247         va_list args;
248
249         va_start (args, format);
250         vsprintf (tmp, format, args);
251         va_end(args);
252         fprintf (stderr, "%s: %s\n", id, tmp);
253 }
254
255 void outwnd_printf2(char* format, ...)
256 {
257         char tmp[MAX_LINE_WIDTH*4];
258         va_list args;
259
260         va_start (args, format);
261         vsprintf (tmp, format, args);
262         va_end(args);
263         fprintf (stderr, "General: %s", tmp);
264 }
265
266 void outwnd_close()
267 {
268 //      STUB_FUNCTION;
269 }
270 */
271 void Warning( char * filename, int line, char * format, ... )
272 {
273         char tmp[MAX_LINE_WIDTH*4];
274         va_list args;
275
276         va_start (args, format);
277         vsprintf (tmp, format, args);
278         va_end(args);
279         fprintf (stderr, "Warning: (%s:%d): %s\n", filename, line, tmp);
280 }
281
282 void Error( char * filename, int line, char * format, ... )
283 {
284         char tmp[MAX_LINE_WIDTH*4];
285         va_list args;
286
287         va_start (args, format);
288         vsprintf (tmp, format, args);
289         va_end(args);
290         fprintf (stderr, "Error: (%s:%d): %s\n", filename, line, tmp);
291         exit (1);
292 }
293
294 void WinAssert(char * text,char *filename, int line)
295 {
296         fprintf (stderr, "Assertion: (%s:%d) %s\n", filename, line, text);
297 //      exit(1);
298 }