]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/camera/misc.cpp
some updates to the Linux build system - obtained a core binary and all required...
[xonotic/netradiant.git] / contrib / camera / misc.cpp
1 /*\r
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.\r
4 \r
5 This file is part of GtkRadiant.\r
6 \r
7 GtkRadiant is free software; you can redistribute it and/or modify\r
8 it under the terms of the GNU General Public License as published by\r
9 the Free Software Foundation; either version 2 of the License, or\r
10 (at your option) any later version.\r
11 \r
12 GtkRadiant is distributed in the hope that it will be useful,\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 GNU General Public License for more details.\r
16 \r
17 You should have received a copy of the GNU General Public License\r
18 along with GtkRadiant; if not, write to the Free Software\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
20 */\r
21 \r
22 /*\r
23 Camera plugin for GtkRadiant\r
24 Copyright (C) 2002 Splash Damage Ltd.\r
25 */\r
26 \r
27 #include "camera.h"\r
28 \r
29 void Sys_ERROR( char* text, ... )\r
30 {\r
31   va_list argptr;\r
32   char  buf[32768];\r
33 \r
34   va_start (argptr,text);\r
35   vsprintf (buf, text,argptr);\r
36   va_end (argptr);\r
37 \r
38   Sys_Printf("Camera::ERROR->%s", buf);\r
39 }\r
40 \r
41 char* UnixToDosPath( char* path )\r
42 {\r
43 #ifndef _WIN32\r
44         return path;\r
45 #else\r
46         for(char* p = path; *p; p++)\r
47         {\r
48                 if(*p == '/')\r
49                         *p = '\\';\r
50         }\r
51         return path;\r
52 #endif\r
53 }\r
54 \r
55 void ExtractFilePath( const char *path, char *dest )\r
56 {\r
57   const char *src;\r
58 \r
59   src = path + strlen(path) - 1;\r
60 \r
61 //\r
62 // back up until a \ or the start\r
63 //\r
64   while (src != path && *(src-1) != '/' && *(src-1) != '\\')\r
65     src--;\r
66 \r
67   memcpy (dest, path, src-path);\r
68   dest[src-path] = 0;\r
69 }\r
70 \r
71 const char* ExtractFilename( const char* path )\r
72 {\r
73         char* p = (char *)strrchr(path, '/');\r
74         if(!p)\r
75         {\r
76                 p = (char *)strrchr(path, '\\');\r
77 \r
78                 if(!p)\r
79                         return path;\r
80         }\r
81         return ++p;\r
82 }\r
83 \r
84 int Q_stricmp (const char *s1, const char *s2) {\r
85         return stricmp( s1, s2 );\r
86 }\r
87 \r
88 /*\r
89 ==============\r
90 FileExists\r
91 ==============\r
92 */\r
93 bool FileExists (const char *filename)\r
94 {\r
95         FILE    *f;\r
96 \r
97         f = fopen( filename, "r" );\r
98         if( !f )\r
99                 return false;\r
100         fclose( f );\r
101         return true;\r
102 }\r
103 \r
104 //\r
105 // command buffer\r
106 // empty wrappers, don't really use them here\r
107 //\r
108 void Cbuf_AddText( const char *text ) {};\r
109 void Cbuf_Execute (void) {};\r
110 \r
111 //\r
112 // Common\r
113 //\r
114 \r
115 void CDECL Com_Error( int level, const char *error, ... )\r
116 {\r
117   va_list argptr;\r
118   char  buf[32768];\r
119 \r
120   va_start (argptr,error);\r
121   vsprintf (buf, error,argptr);\r
122   va_end (argptr);\r
123 \r
124   Sys_Printf("Camera::ERROR->%s", buf);\r
125 }\r
126 \r
127 void CDECL Com_Printf( const char* msg, ... )\r
128 {\r
129   va_list argptr;\r
130   char  buf[32768];\r
131 \r
132   va_start (argptr,msg);\r
133   vsprintf (buf, msg,argptr);\r
134   va_end (argptr);\r
135 \r
136   Sys_Printf("Camera::%s", buf);\r
137 }\r
138 \r
139 void CDECL Com_DPrintf( const char* msg, ... )\r
140 {\r
141 #ifdef _DEBUG\r
142   va_list argptr;\r
143   char  buf[32768];\r
144 \r
145   va_start (argptr,msg);\r
146   vsprintf (buf, msg,argptr);\r
147   va_end (argptr);\r
148 \r
149   Sys_Printf("Camera::%s", buf);\r
150 #endif\r
151 }\r
152 \r
153 void *Com_Allocate( int bytes ) {\r
154   return( malloc( bytes ) );\r
155 }\r
156 \r
157 void Com_Dealloc( void *ptr ) {\r
158   free( ptr );\r
159 }\r
160 \r
161 //\r
162 // Filesystem\r
163 //\r
164 \r
165 #ifdef _WIN32\r
166         #pragma warning(disable : 4311)\r
167         #pragma warning(disable : 4312)\r
168 #endif\r
169 \r
170 int FS_Read( void *buffer, int len, fileHandle_t f ) {\r
171   return fread( buffer, len, 1, (FILE *)f );\r
172 }\r
173 \r
174 int FS_Write( const void *buffer, int len, fileHandle_t h ) {\r
175   return fwrite( buffer, len, 1, (FILE *)h );\r
176 }\r
177 \r
178 int FS_ReadFile( const char *qpath, void **buffer ) {\r
179   fileHandle_t  h;\r
180   byte*                 buf;\r
181   int                           len;\r
182 \r
183   buf = NULL;\r
184 \r
185   len = FS_FOpenFileRead( qpath, &h, qfalse );\r
186 \r
187   if( h == 0 ) {\r
188     if ( buffer ) {\r
189       *buffer = NULL;\r
190     }\r
191 \r
192     return -1;\r
193   }\r
194 \r
195   buf = (byte *)Com_Allocate( len + 1 );\r
196 \r
197   *buffer = buf;\r
198 \r
199   FS_Read (buf, len, h);\r
200 \r
201   buf[len] = 0;\r
202   FS_FCloseFile( h );\r
203 \r
204   return len;\r
205 }\r
206 \r
207 void FS_FreeFile( void *buffer ) {\r
208   Com_Dealloc( buffer );\r
209 }\r
210 \r
211 int FS_FOpenFileRead( const char *filename, fileHandle_t *file, qboolean uniqueFILE ) {\r
212   FILE  *fh;\r
213         long len;\r
214 \r
215   fh = fopen( filename, "rb" );\r
216   *file = *(fileHandle_t *)&fh;\r
217 \r
218   if( file )\r
219         {\r
220                 fseek (fh, 0, SEEK_END);\r
221                 len = ftell (fh);\r
222                 rewind (fh);            \r
223     return len;\r
224         }\r
225   else\r
226     return -1;\r
227 }\r
228 \r
229 fileHandle_t FS_FOpenFileWrite( const char *filename ) {\r
230   FILE          *fh;\r
231   fileHandle_t  f;\r
232 \r
233   memset( &f, 0, sizeof(f) );\r
234 \r
235   fh = fopen( filename, "wb" );\r
236 \r
237   f = (fileHandle_t)fh;\r
238   return f;\r
239 }\r
240 \r
241 void FS_FCloseFile( fileHandle_t f ) {\r
242   fclose( (FILE *)f );\r
243 }\r