]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/tools/radiant/cmdlib.cpp
Various Mac OS X tweaks to get this to build. Probably breaking things.
[icculus/iodoom3.git] / neo / tools / radiant / cmdlib.cpp
1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. 
6
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).  
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.
23
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25
26 ===========================================================================
27 */
28
29 #include "../../idlib/precompiled.h"
30 #pragma hdrstop
31
32 #include "qe3.h"
33 #include "cmdlib.h"
34
35 #define PATHSEPERATOR   '/'
36
37 // rad additions
38 // 11.29.99
39 PFN_ERR *g_pfnError = NULL;
40 PFN_PRINTF *g_pfnPrintf = NULL;
41 PFN_ERR_NUM *g_pfnErrorNum = NULL;
42 PFN_PRINTF_NUM *g_pfnPrintfNum = NULL;
43
44
45 void Error(const char *pFormat, ...)
46 {
47         if (g_pfnError)
48         {
49                 va_list arg_ptr;
50                 va_start(arg_ptr, pFormat);
51                 g_pfnError(pFormat, arg_ptr);
52                 va_end(arg_ptr);
53         }
54 }
55
56 void Printf(const char *pFormat, ...)
57 {
58         if (g_pfnPrintf)
59         {
60                 va_list arg_ptr;
61                 va_start(arg_ptr, pFormat);
62                 g_pfnPrintf(pFormat, arg_ptr);
63                 va_end(arg_ptr);
64         }
65 }
66
67 void ErrorNum(int nErr, const char *pFormat, ...)
68 {
69         if (g_pfnErrorNum)
70         {
71                 va_list arg_ptr;
72                 va_start(arg_ptr, pFormat);
73                 g_pfnErrorNum(nErr, pFormat, arg_ptr);
74                 va_end(arg_ptr);
75         }
76 }
77
78 void PrintfNum(int nErr, const char *pFormat, ...)
79 {
80         if (g_pfnPrintfNum)
81         {
82                 va_list arg_ptr;
83                 va_start(arg_ptr, pFormat);
84                 g_pfnPrintfNum(nErr, pFormat, arg_ptr);
85                 va_end(arg_ptr);
86         }
87 }
88
89 void SetErrorHandler(PFN_ERR pe)
90 {
91         g_pfnError = pe;
92 }
93
94 void SetPrintfHandler(PFN_PRINTF pe)
95 {
96         g_pfnPrintf = pe;
97 }
98
99 void SetErrorHandlerNum(PFN_ERR_NUM pe)
100 {
101         g_pfnErrorNum = pe;
102 }
103
104 void SetPrintfHandler(PFN_PRINTF_NUM pe)
105 {
106         g_pfnPrintfNum = pe;
107 }
108
109
110 /*
111 ================
112 Q_filelength
113 ================
114 */
115 int Q_filelength (FILE *f)
116 {
117         int             pos;
118         int             end;
119
120         pos = ftell (f);
121         fseek (f, 0, SEEK_END);
122         end = ftell (f);
123         fseek (f, pos, SEEK_SET);
124
125         return end;
126 }
127
128 /*
129 ==============
130 LoadFile
131 ==============
132 */
133 int LoadFile (const char *filename, void **bufferptr)
134 {
135         FILE    *f;
136         int    length;
137         void    *buffer;
138
139         *bufferptr = NULL;
140
141         if ( filename == NULL || strlen(filename) == 0 ) {
142                 return -1;
143         }
144
145         f = fopen( filename, "rb" );
146         if ( !f ) {
147                 return -1;
148         }
149         length = Q_filelength( f );
150         buffer = Mem_ClearedAlloc( length+1 );
151         ((char *)buffer)[length] = 0;
152         if ( (int)fread( buffer, 1, length, f ) != length ) {
153                 Error( "File read failure" );
154         }
155         fclose( f );
156
157         *bufferptr = buffer;
158         return length;
159 }
160
161 /*
162 ==============
163 DefaultExtension
164 ==============
165 */
166 void DefaultExtension (char *path, char *extension)
167 {
168         char    *src;
169         //
170         // if path doesn't have a .EXT, append extension
171         // (extension should include the .)
172         //
173         src = path + strlen(path) - 1;
174
175         while (*src != PATHSEPERATOR && src != path)
176         {
177                 if (*src == '.')
178                         return;                 // it has an extension
179                 src--;
180         }
181
182         strcat (path, extension);
183 }
184
185 /*
186 ==============
187 DefaultPath
188 ==============
189 */
190 void DefaultPath (char *path, char *basepath)
191 {
192         char    temp[128];
193
194         if (path[0] == PATHSEPERATOR)
195                 return;                   // absolute path location
196         strcpy (temp,path);
197         strcpy (path,basepath);
198         strcat (path,temp);
199 }
200
201 /*
202 ==============
203 StripFilename
204 ==============
205 */
206 void StripFilename (char *path)
207 {
208         int             length;
209
210         length = strlen(path)-1;
211         while (length > 0 && path[length] != PATHSEPERATOR) {
212                 length--;
213         }
214         path[length] = 0;
215 }
216
217 /*
218 ==============
219 StripExtension
220 ==============
221 */
222 void StripExtension (char *path)
223 {
224         int             length;
225
226         length = strlen(path)-1;
227         while (length > 0 && path[length] != '.')
228         {
229                 length--;
230                 if (path[length] == '/')
231                         return;         // no extension
232         }
233         if (length) {
234                 path[length] = 0;
235         }
236 }