]> icculus.org git repositories - taylor/freespace2.git/blob - src/platform/unix.cpp
stub/warning removal.
[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 "unix.h"
12
13 #define MAX_LINE_WIDTH 128
14
15 void strlwr (char * str)
16 {
17         while (*str) {*str = tolower (*str); str++; }
18 }
19
20 int filelength (int fd)
21 {
22         struct stat buf;
23         if (fstat (fd, &buf) == -1)
24                 return -1;
25                 
26         return buf.st_size;
27 }
28
29 unsigned long _beginthread (void (*pfuncStart)(void *), unsigned unStackSize, void* pArgList)
30 {
31         STUB_FUNCTION;
32         
33         return 0;
34 }
35
36 void Sleep (int mili)
37 {
38         usleep (mili * 1000);
39 }
40
41 void OutputDebugString (const char *str)
42 {
43         fprintf(stderr, "OutputDebugString: %s\n", str);
44 }
45
46 int WSAGetLastError()
47 {
48         return errno;
49 }
50
51 int MulDiv(int a, int b, int c)
52 {
53         /* slow long long version */
54         __extension__ long long aa = a;
55         __extension__ long long bb = b;
56         __extension__ long long cc = c;
57         
58         __extension__ long long dd = aa * bb;
59         __extension__ long long ee = dd / cc;
60         
61         int retr = (int) ee;
62         
63         return retr;
64 }
65
66 /* mem debug junk */
67 #ifndef NDEBUG
68 //#define WANT_DEBUG
69 #endif
70
71 int TotalRam = 0;
72
73 #ifdef WANT_DEBUG
74 typedef struct RAM {
75         void *addr;
76         int size;
77         
78         char *file;
79         int line;
80         
81         RAM *next;
82 } RAM;
83
84 static RAM *RamTable;
85 #endif
86
87 void vm_free(void* ptr, char *file, int line)
88 {
89 #ifdef WANT_DEBUG
90         fprintf(stderr, "FREE: %s:%d addr = %p\n", file, line, ptr);
91         
92         RAM *item = RamTable;
93         RAM **mark = &RamTable;
94         
95         while (item != NULL) {
96                 if (item->addr == ptr) {
97                         RAM *tmp = item;
98                         
99                         *mark = item->next;
100                         
101                         free(tmp->addr);
102                         free(tmp);
103                         
104                         return;
105                 }
106                 
107                 mark = &(item->next);
108                 
109                 item = item->next;
110         }
111         
112         fprintf(stderr, "ERROR: vm_free caught invalid free: addr = %p, file = %s/%d\n", ptr, file, line);
113 #else   
114         free(ptr);
115 #endif
116 }
117
118 void *vm_malloc(int size, char *file, int line)
119 {
120 #ifdef WANT_DEBUG
121         fprintf(stderr, "MALLOC: %s:%d %d bytes\n", file, line, size);
122         
123         RAM *next = (RAM *)malloc(sizeof(RAM));
124         
125         next->addr = malloc(size);
126         next->size = size;
127         next->file = file;
128         next->line = line;
129         
130         next->next = RamTable;
131         RamTable = next;
132         
133         return next->addr;
134 #else
135         return malloc(size);
136 #endif  
137 }
138
139 char *vm_strdup(char const* str, char *file, int line)
140 {
141 #ifdef WANT_DEBUG
142         fprintf(stderr, "STRDUP: %s:%d\n", file, line);
143         
144         RAM *next = (RAM *)malloc(sizeof(RAM));
145         
146         next->addr = strdup(str);
147         next->size = strlen(str)+1;
148         next->file = file;
149         next->line = line;
150         
151         next->next = RamTable;
152         RamTable = next;
153         
154         return (char *)next->addr;
155 #else
156         return strdup(str);
157 #endif
158 }
159
160 void vm_dump()
161 {
162 #ifdef WANT_DEBUG
163         int i = 0;
164         int mem = 0;
165         fprintf(stderr, "\nDumping allocated memory:\n");
166         
167         RAM *ptr = RamTable;
168         while (ptr) {
169                 fprintf(stderr, "%d: file: %s:%d: addr:%p size:%d\n", i, ptr->file, ptr->line, ptr->addr, ptr->size);
170                 mem += ptr->size;
171                 ptr = ptr->next;
172                 i++;
173         }
174         
175         fprintf(stderr, "\nTotal of %d left-over bytes from %d allocations\n", mem, i);
176 #endif  
177 }
178
179 void windebug_memwatch_init()
180 {
181         TotalRam = 0;
182 }
183
184 /* error message debugging junk */
185 int Log_debug_output_to_file = 0;
186
187 void load_filter_info(void)
188 {
189 //      STUB_FUNCTION;
190 }
191
192 void outwnd_printf(char* id, char* format, ...)
193 {
194         char tmp[MAX_LINE_WIDTH*4];
195         va_list args;
196
197         va_start (args, format);
198         vsprintf (tmp, format, args);
199         va_end(args);
200         fprintf (stderr, "%s: %s\n", id, tmp);
201 }
202
203 void outwnd_printf2(char* format, ...)
204 {
205         char tmp[MAX_LINE_WIDTH*4];
206         va_list args;
207
208         va_start (args, format);
209         vsprintf (tmp, format, args);
210         va_end(args);
211         fprintf (stderr, "General: %s", tmp);
212 }
213
214 void outwnd_close()
215 {
216 //      STUB_FUNCTION;
217 }
218
219 void Warning( char * filename, int line, char * format, ... )
220 {
221         char tmp[MAX_LINE_WIDTH*4];
222         va_list args;
223
224         va_start (args, format);
225         vsprintf (tmp, format, args);
226         va_end(args);
227         fprintf (stderr, "Warning: (%s:%d): %s\n", filename, line, tmp);
228 }
229
230 void Error( char * filename, int line, char * format, ... )
231 {
232         char tmp[MAX_LINE_WIDTH*4];
233         va_list args;
234
235         va_start (args, format);
236         vsprintf (tmp, format, args);
237         va_end(args);
238         fprintf (stderr, "Error: (%s:%d): %s\n", filename, line, tmp);
239         exit (1);
240 }
241
242 void WinAssert(char * text,char *filename, int line)
243 {
244         fprintf (stderr, "Assertion: (%s:%d) %s\n", filename, line, text);
245 //      exit(1);
246 }