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