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