doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cmdlib.cpp
Go to the documentation of this file.
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
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 
90 {
91  g_pfnError = pe;
92 }
93 
95 {
96  g_pfnPrintf = pe;
97 }
98 
100 {
101  g_pfnErrorNum = pe;
102 }
103 
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 }
#define PATHSEPERATOR
Definition: cmdlib.cpp:35
void DefaultPath(char *path, char *basepath)
Definition: cmdlib.cpp:190
PFN_ERR * g_pfnError
Definition: cmdlib.cpp:39
void SetErrorHandlerNum(PFN_ERR_NUM pe)
Definition: cmdlib.cpp:99
void SetPrintfHandler(PFN_PRINTF pe)
Definition: cmdlib.cpp:94
GLuint src
Definition: glext.h:5390
void StripFilename(char *path)
Definition: cmdlib.cpp:206
void( PFN_PRINTF_NUM)(int nNum, const char *pFormat,...)
Definition: cmdlib.h:51
void( PFN_ERR_NUM)(int nNum, const char *pFormat,...)
Definition: cmdlib.h:50
PFN_PRINTF_NUM * g_pfnPrintfNum
Definition: cmdlib.cpp:42
void Error(const char *pFormat,...)
Definition: cmdlib.cpp:45
#define SEEK_END
Definition: Unzip.cpp:131
GLuint GLuint end
Definition: glext.h:2845
void ErrorNum(int nErr, const char *pFormat,...)
Definition: cmdlib.cpp:67
#define NULL
Definition: Lib.h:88
GLuint buffer
Definition: glext.h:3108
void Printf(const char *pFormat,...)
Definition: cmdlib.cpp:56
const char * path
Definition: sws.c:117
PFN_PRINTF * g_pfnPrintf
Definition: cmdlib.cpp:40
void DefaultExtension(char *path, char *extension)
Definition: cmdlib.cpp:166
int Q_filelength(FILE *f)
Definition: cmdlib.cpp:115
void PrintfNum(int nErr, const char *pFormat,...)
Definition: cmdlib.cpp:78
void( PFN_PRINTF)(const char *pFormat,...)
Definition: cmdlib.h:49
size_t fread(void *, size_t, size_t, FILE *)
#define SEEK_SET
Definition: Unzip.cpp:129
tuple f
Definition: idal.py:89
int LoadFile(const char *filename, void **bufferptr)
Definition: cmdlib.cpp:133
GLsizei const GLcharARB const GLint * length
Definition: glext.h:3599
void * Mem_ClearedAlloc(const int size)
Definition: Heap.cpp:1149
void StripExtension(char *path)
Definition: cmdlib.cpp:222
void SetErrorHandler(PFN_ERR pe)
Definition: cmdlib.cpp:89
void( PFN_ERR)(const char *pFormat,...)
Definition: cmdlib.h:48
PFN_ERR_NUM * g_pfnErrorNum
Definition: cmdlib.cpp:41