2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\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
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
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
23 // start of shared cmdlib stuff
\r
29 #include <windows.h>
\r
31 #if defined (__linux__) || defined (__APPLE__)
\r
35 // FIXME TTimo this should be cleaned up ..
\r
36 // NOTE: we don't use this crap .. with the total mess of mixing win32/unix paths we need to recognize both '/' and '\\'
\r
37 #define PATHSEPERATOR '/'
\r
39 #if defined (__linux__) || defined (__APPLE__)
\r
40 bool Q_Exec(const char *cmd, char *cmdline, const char *execdir, bool bCreateConsole)
\r
45 printf("Q_Exec damnit\n");
\r
53 // always concat the command on linux
\r
56 strcpy(fullcmd, cmd);
\r
62 strcat(fullcmd, " ");
\r
63 strcat(fullcmd, cmdline);
\r
66 while (*pCmd == ' ')
\r
69 printf("Running system...\n");
\r
70 printf("Command: %s\n", pCmd);
\r
74 printf ("system() returned\n");
\r
84 // NOTE TTimo windows is VERY nitpicky about the syntax in CreateProcess
\r
85 bool Q_Exec(const char *cmd, char *cmdline, const char *execdir, bool bCreateConsole)
\r
87 PROCESS_INFORMATION ProcessInformation;
\r
88 STARTUPINFO startupinfo = {0};
\r
89 DWORD dwCreationFlags;
\r
90 GetStartupInfo (&startupinfo);
\r
92 dwCreationFlags = CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS;
\r
94 dwCreationFlags = DETACHED_PROCESS | NORMAL_PRIORITY_CLASS;
\r
100 while (*pCmd == ' ')
\r
103 pCmdline = cmdline;
\r
106 while (*pCmdline == ' ')
\r
119 &ProcessInformation
\r
126 #define MEM_BLOCKSIZE 4096
\r
127 void* qblockmalloc(size_t nSize)
\r
130 // round up to threshold
\r
131 int nAllocSize = nSize % MEM_BLOCKSIZE;
\r
132 if ( nAllocSize > 0)
\r
134 nSize += MEM_BLOCKSIZE - nAllocSize;
\r
136 b = malloc(nSize + 1);
\r
137 memset (b, 0, nSize);
\r
141 //++timo NOTE: can be replaced by g_malloc0(nSize+1) when moving to glib memory handling
\r
142 void* qmalloc (size_t nSize)
\r
145 b = malloc(nSize + 1);
\r
146 memset (b, 0, nSize);
\r
155 int Q_filelength (FILE *f)
\r
161 fseek (f, 0, SEEK_END);
\r
163 fseek (f, pos, SEEK_SET);
\r
168 void DefaultExtension (char *path, char *extension)
\r
172 // if path doesn't have a .EXT, append extension
\r
173 // (extension should include the .)
\r
175 src = path + strlen(path) - 1;
\r
177 while (*src != PATHSEPERATOR && src != path)
\r
180 return; // it has an extension
\r
184 strcat (path, extension);
\r
187 void DefaultPath (char *path, char *basepath)
\r
191 if (path[0] == PATHSEPERATOR)
\r
192 return; // absolute path location
\r
193 strcpy (temp,path);
\r
194 strcpy (path,basepath);
\r
195 strcat (path,temp);
\r
199 void StripFilename (char *path)
\r
203 length = strlen(path)-1;
\r
204 while (length > 0 && path[length] != PATHSEPERATOR)
\r
209 void StripExtension (char *path)
\r
213 length = strlen(path)-1;
\r
214 while (length > 0 && path[length] != '.')
\r
217 if (path[length] == '/')
\r
218 return; // no extension
\r
226 ====================
\r
228 ====================
\r
230 void ExtractFilePath (const char *path, char *dest)
\r
234 src = path + strlen(path) - 1;
\r
237 // back up until a \ or the start
\r
239 while (src != path && *(src-1) != '/' && *(src-1) != '\\')
\r
242 memcpy (dest, path, src-path);
\r
243 dest[src-path] = 0;
\r
246 void ExtractFileName (const char *path, char *dest)
\r
250 src = path + strlen(path) - 1;
\r
253 // back up until a \ or the start
\r
255 while (src != path && *(src-1) != '/'
\r
256 && *(src-1) != '\\' )
\r
266 inline const char* path_get_filename_start(const char* path)
\r
269 const char* last_forward_slash = strrchr(path, '/');
\r
270 if(last_forward_slash != NULL)
\r
271 return last_forward_slash + 1;
\r
275 const char* last_backward_slash = strrchr(path, '\\');
\r
276 if(last_backward_slash != NULL)
\r
277 return last_backward_slash + 1;
\r
283 inline unsigned int filename_get_base_length(const char* filename)
\r
285 const char* last_period = strrchr(filename, '.');
\r
286 return (last_period != NULL) ? last_period - filename : strlen(filename);
\r
289 void ExtractFileBase (const char *path, char *dest)
\r
291 const char* filename = path_get_filename_start(path);
\r
292 unsigned int length = filename_get_base_length(filename);
\r
293 strncpy(dest, filename, length);
\r
294 dest[length] = '\0';
\r
297 void ExtractFileExtension (const char *path, char *dest)
\r
301 src = path + strlen(path) - 1;
\r
304 // back up until a . or the start
\r
306 while (src != path && *(src-1) != '.')
\r
310 *dest = 0; // no extension
\r
318 void ConvertDOSToUnixName( char *dst, const char *src )
\r
322 if ( *src == '\\' )
\r
332 char* StrDup(char* pStr)
\r
336 return strcpy(new char[strlen(pStr)+1], pStr);
\r
341 char* StrDup(const char* pStr)
\r
345 return strcpy(new char[strlen(pStr)+1], pStr);
\r
350 void CreateDirectoryPath (const char *path) {
\r
351 char base[PATH_MAX];
\r
355 ExtractFilePath(path, base);
\r
359 while (*src != '\0' && *src != '/' && *src != '\\') {
\r
362 if (*src == '\0') {
\r
365 back = *src; *src = '\0';
\r
366 Q_mkdir(base, 0755);
\r
367 *src = back; src++;
\r
372 ============================================================================
\r
374 BYTE ORDER FUNCTIONS
\r
376 ============================================================================
\r
380 #define __BIG_ENDIAN__
\r
383 #ifdef __BIG_ENDIAN__
\r
385 short LittleShort (short l)
\r
392 return(b1<<8) + b2;
\r
395 short BigShort (short l)
\r
401 int LittleLong (int l)
\r
410 return((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
\r
413 int BigLong (int l)
\r
419 float LittleFloat (float l)
\r
423 byte b[4]; float f;
\r
427 out.b[0] = in.b[3];
\r
428 out.b[1] = in.b[2];
\r
429 out.b[2] = in.b[1];
\r
430 out.b[3] = in.b[0];
\r
435 float BigFloat (float l)
\r
442 short BigShort (short l)
\r
449 return(b1<<8) + b2;
\r
452 short LittleShort (short l)
\r
458 int BigLong (int l)
\r
467 return((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
\r
470 int LittleLong (int l)
\r
475 float BigFloat (float l)
\r
479 byte b[4]; float f;
\r
483 out.b[0] = in.b[3];
\r
484 out.b[1] = in.b[2];
\r
485 out.b[2] = in.b[1];
\r
486 out.b[3] = in.b[0];
\r
491 float LittleFloat (float l)
\r