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