]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/camera/misc.cpp
uncrustify! now the code is only ugly on the *inside*
[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         va_list argptr;
31         char buf[32768];
32
33         va_start( argptr,text );
34         vsprintf( buf, text,argptr );
35         va_end( argptr );
36
37         Sys_Printf( "Camera::ERROR->%s", buf );
38 }
39
40 char* UnixToDosPath( char* path ){
41 #ifndef _WIN32
42         return path;
43 #else
44         for ( char* p = path; *p; p++ )
45         {
46                 if ( *p == '/' ) {
47                         *p = '\\';
48                 }
49         }
50         return path;
51 #endif
52 }
53
54 void CAMERA_ExtractFilePath( const char *path, char *dest ){
55         const char *src;
56
57         src = path + strlen( path ) - 1;
58
59 //
60 // back up until a \ or the start
61 //
62         while ( src != path && *( src - 1 ) != '/' && *( src - 1 ) != '\\' )
63                 src--;
64
65         memcpy( dest, path, src - path );
66         dest[src - path] = 0;
67 }
68
69 const char* ExtractFilename( const char* path ){
70         char* p = (char *)strrchr( path, '/' );
71         if ( !p ) {
72                 p = (char *)strrchr( path, '\\' );
73
74                 if ( !p ) {
75                         return path;
76                 }
77         }
78         return ++p;
79 }
80
81 int Q_stricmp( const char *s1, const char *s2 ) {
82         return stricmp( s1, s2 );
83 }
84
85 /*
86    ==============
87    FileExists
88    ==============
89  */
90 bool FileExists( const char *filename ){
91         FILE    *f;
92
93         f = fopen( filename, "r" );
94         if ( !f ) {
95                 return false;
96         }
97         fclose( f );
98         return true;
99 }
100
101 //
102 // command buffer
103 // empty wrappers, don't really use them here
104 //
105 void Cbuf_AddText( const char *text ) {};
106 void Cbuf_Execute( void ) {};
107
108 //
109 // Common
110 //
111
112 void CDECL Com_Error( int level, const char *error, ... ){
113         va_list argptr;
114         char buf[32768];
115
116         va_start( argptr,error );
117         vsprintf( buf, error,argptr );
118         va_end( argptr );
119
120         Sys_Printf( "Camera::ERROR->%s", buf );
121 }
122
123 void CDECL Com_Printf( const char* msg, ... ){
124         va_list argptr;
125         char buf[32768];
126
127         va_start( argptr,msg );
128         vsprintf( buf, msg,argptr );
129         va_end( argptr );
130
131         Sys_Printf( "Camera::%s", buf );
132 }
133
134 void CDECL Com_DPrintf( const char* msg, ... ){
135 #ifdef _DEBUG
136         va_list argptr;
137         char buf[32768];
138
139         va_start( argptr,msg );
140         vsprintf( buf, msg,argptr );
141         va_end( argptr );
142
143         Sys_Printf( "Camera::%s", buf );
144 #endif
145 }
146
147 void *Com_Allocate( int bytes ) {
148         return( malloc( bytes ) );
149 }
150
151 void Com_Dealloc( void *ptr ) {
152         free( ptr );
153 }
154
155 //
156 // Filesystem
157 //
158
159 #ifdef _WIN32
160         #pragma warning(disable : 4311)
161         #pragma warning(disable : 4312)
162 #endif
163
164 int FS_Read( void *buffer, int len, fileHandle_t f ) {
165         return fread( buffer, len, 1, (FILE *)f );
166 }
167
168 int FS_Write( const void *buffer, int len, fileHandle_t h ) {
169         return fwrite( buffer, len, 1, (FILE *)h );
170 }
171
172 int FS_ReadFile( const char *qpath, void **buffer ) {
173         fileHandle_t h;
174         byte*         buf;
175         int len;
176
177         buf = NULL;
178
179         len = FS_FOpenFileRead( qpath, &h, qfalse );
180
181         if ( h == 0 ) {
182                 if ( buffer ) {
183                         *buffer = NULL;
184                 }
185
186                 return -1;
187         }
188
189         buf = (byte *)Com_Allocate( len + 1 );
190
191         *buffer = buf;
192
193         FS_Read( buf, len, h );
194
195         buf[len] = 0;
196         FS_FCloseFile( h );
197
198         return len;
199 }
200
201 void FS_FreeFile( void *buffer ) {
202         Com_Dealloc( buffer );
203 }
204
205 int FS_FOpenFileRead( const char *filename, fileHandle_t *file, qboolean uniqueFILE ) {
206         FILE  *fh;
207         long len;
208
209         fh = fopen( filename, "rb" );
210         *file = *(fileHandle_t *)&fh;
211
212         if ( file ) {
213                 fseek( fh, 0, SEEK_END );
214                 len = ftell( fh );
215                 rewind( fh );
216                 return len;
217         }
218         else{
219                 return -1;
220         }
221 }
222
223 fileHandle_t FS_FOpenFileWrite( const char *filename ) {
224         FILE          *fh;
225         fileHandle_t f;
226
227         memset( &f, 0, sizeof( f ) );
228
229         fh = fopen( filename, "wb" );
230
231         f = (fileHandle_t)fh;
232         return f;
233 }
234
235 void FS_FCloseFile( fileHandle_t f ) {
236         fclose( (FILE *)f );
237 }