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