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