]> icculus.org git repositories - taylor/freespace2.git/blob - src/platform/platform.cpp
fix special dot sliders in FS1
[taylor/freespace2.git] / src / platform / platform.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <sys/types.h>
15
16 #include "pstypes.h"
17
18 // use system versions of this stuff in here rather than the vm_* versions
19 #undef malloc
20 #undef free
21 #undef strdup
22
23
24 #define MAX_LINE_WIDTH 128
25
26
27 int TotalRam = 0;
28
29
30 int vm_init(int min_heap_size)
31 {
32         return 1;
33 }
34
35 #if defined(__MACOSX__)
36 #define MALLOC_SIZE(x)          malloc_size(x)
37 #elif defined(__GNUC__)
38 #define MALLOC_SIZE(x)          malloc_usable_size(x)
39 #elif defined(__WIN32__)
40 #define MALLOC_SIZE(x)          _msize(x)
41 #else
42 #define MALLOC_SIZE(x)          0
43 #endif
44
45 int Watch_malloc = 0;
46
47 DCF_BOOL(watch_malloc, Watch_malloc)
48
49 #ifndef NDEBUG
50 static const char *clean_filename(const char *name)
51 {
52         const char *p = name+strlen(name)-1;
53         // Move p to point to first letter of EXE filename
54         while( (*p!='\\') && (*p!='/') && (*p!=':') )
55                 p--;
56         p++;
57
58         return p;
59 }
60 #endif
61
62 #ifndef NDEBUG
63 void vm_free(void* ptr, const char *file, int line)
64 #else
65 void vm_free(void* ptr)
66 #endif
67 {
68         if ( !ptr ) {
69 #ifndef NDEBUG
70                 mprintf(("Why are you trying to free a NULL pointer?  [%s(%d)]\n", clean_filename(file), line));
71 #endif
72                 return;
73         }
74
75 #ifndef NDEBUG
76         size_t actual_size = MALLOC_SIZE(ptr);
77
78         if (Watch_malloc) {
79                 mprintf(( "Free %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line ));
80         }
81
82         TotalRam -= actual_size;
83 #endif
84
85         free(ptr);
86 }
87
88 #ifndef NDEBUG
89 void *vm_malloc(int size, const char *file, int line)
90 #else
91 void *vm_malloc(int size)
92 #endif
93 {
94         void *ptr = malloc(size);
95
96         if ( !ptr )     {
97                 mprintf(( "Malloc failed!!!!!!!!!!!!!!!!!!!\n" ));
98
99                 Error(LOCATION, "Out of memory.  Try closing down other applications, increasing your\n"
100                                 "virtual memory size, or installing more physical RAM.\n");
101         }
102
103 #ifndef NDEBUG
104         size_t actual_size = MALLOC_SIZE(ptr);
105
106         if ( Watch_malloc )     {
107                 mprintf(( "Malloc %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line ));
108         }
109
110         TotalRam += actual_size;
111 #endif
112
113         return ptr;
114 }
115
116 #ifndef NDEBUG
117 char *vm_strdup(char const* str, const char *file, int line)
118 #else
119 char *vm_strdup(char const* str)
120 #endif
121 {
122         char *ptr = strdup(str);
123
124         if ( !ptr )     {
125                 mprintf(( "Strdup failed!!!!!!!!!!!!!!!!!!!\n" ));
126
127                 Error(LOCATION, "Out of memory.  Try closing down other applications, increasing your\n"
128                                 "virtual memory size, or installing more physical RAM.\n");
129         }
130
131 #ifndef NDEBUG
132         size_t actual_size = MALLOC_SIZE(ptr);
133
134         if ( Watch_malloc )     {
135                 mprintf(( "Strdup %d bytes [%s(%d)]\n", actual_size, clean_filename(file), line ));
136         }
137
138         TotalRam += actual_size;
139 #endif
140
141         return ptr;
142 }
143
144 void windebug_memwatch_init()
145 {
146         TotalRam = 0;
147 }
148
149
150 extern void gr_force_windowed();
151
152 void Warning( const char * filename, int line, const char * format, ... )
153 {
154 #ifndef NDEBUG
155         char tmp[MAX_LINE_WIDTH*4] = { 0 };
156         char tmp2[MAX_LINE_WIDTH*4] = { 0 };
157         va_list args;
158
159         va_start(args, format);
160         SDL_vsnprintf(tmp, SDL_arraysize(tmp), format, args);
161         va_end(args);
162
163         SDL_snprintf(tmp2, SDL_arraysize(tmp2), "Warning: %s\n\nFile:%s\nLine: %d", tmp, filename, line);
164
165         gr_force_windowed();
166
167         SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "Warning!", tmp2, NULL);
168 #endif
169 }
170
171 void Error( const char * filename, int line, const char * format, ... )
172 {
173         char tmp[MAX_LINE_WIDTH*4] = { 0 };
174         char tmp2[MAX_LINE_WIDTH*4] = { 0 };
175         va_list args;
176
177         va_start (args, format);
178         SDL_vsnprintf (tmp, SDL_arraysize(tmp), format, args);
179         va_end(args);
180
181         SDL_snprintf(tmp2, SDL_arraysize(tmp2), "Error: %s\n\nFile:%s\nLine: %d", tmp, filename, line);
182
183         gr_force_windowed();
184
185         SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", tmp2, NULL);
186
187         exit (1);
188 }
189
190 void base_filename(const char *path, char *filename, const int max_fname)
191 {
192         if ( (filename == NULL) || (max_fname <= 0) ) {
193                 return;
194         }
195
196         if (path == NULL) {
197                 filename[0] = '\0';
198                 return;
199         }
200
201         const char *sep = SDL_strrchr(path, DIR_SEPARATOR_CHAR);
202
203         if (sep) {
204                 sep++;  // move past separator
205         } else {
206                 sep = path;
207         }
208
209         const char *ext = SDL_strrchr(path, '.');
210
211         if (ext == NULL) {
212                 ext = sep + SDL_strlen(sep);    // to end
213         }
214
215         // NOTE: 'size' must include NULL terminator
216         int size = SDL_min((int)(ext - sep + 1), max_fname);
217
218         if (size <= 0) {
219                 filename[0] = '\0';
220         } else {
221                 SDL_strlcpy(filename, sep, size);
222         }
223 }