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