-/*\r
-Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
-For a list of contributors, see the accompanying CONTRIBUTORS file.\r
-\r
-This file is part of GtkRadiant.\r
-\r
-GtkRadiant is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-GtkRadiant is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with GtkRadiant; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\r
-*/\r
-\r
-//\r
-// start of shared cmdlib stuff\r
-// \r
-\r
-#include "cmdlib.h"\r
-\r
-#ifdef _WIN32\r
- #include <windows.h>\r
-#endif\r
-#if defined (__linux__) || defined (__APPLE__)\r
- #include <unistd.h>\r
-#endif\r
-\r
-// FIXME TTimo this should be cleaned up ..\r
-// NOTE: we don't use this crap .. with the total mess of mixing win32/unix paths we need to recognize both '/' and '\\'\r
-#define PATHSEPERATOR '/'\r
-\r
-#if defined (__linux__) || defined (__APPLE__)\r
-bool Q_Exec(const char *cmd, char *cmdline, const char *execdir, bool bCreateConsole)\r
-{\r
- char fullcmd[2048];\r
- char *pCmd;\r
-#ifdef _DEBUG\r
- printf("Q_Exec damnit\n");\r
-#endif\r
- switch (fork())\r
- {\r
- case -1:\r
- return true;\r
- break;\r
- case 0:\r
- // always concat the command on linux\r
- if (cmd)\r
- {\r
- strcpy(fullcmd, cmd);\r
- }\r
- else\r
- fullcmd[0] = '\0';\r
- if (cmdline)\r
- {\r
- strcat(fullcmd, " ");\r
- strcat(fullcmd, cmdline);\r
- }\r
- pCmd = fullcmd;\r
- while (*pCmd == ' ')\r
- pCmd++;\r
-#ifdef _DEBUG\r
- printf("Running system...\n");\r
- printf("Command: %s\n", pCmd);\r
-#endif\r
- system( pCmd );\r
-#ifdef _DEBUG\r
- printf ("system() returned\n");\r
-#endif\r
- _exit (0);\r
- break;\r
- }\r
- return true;\r
-}\r
-#endif\r
-\r
-#ifdef _WIN32\r
-// NOTE TTimo windows is VERY nitpicky about the syntax in CreateProcess\r
-bool Q_Exec(const char *cmd, char *cmdline, const char *execdir, bool bCreateConsole)\r
-{\r
- PROCESS_INFORMATION ProcessInformation;\r
- STARTUPINFO startupinfo = {0};\r
- DWORD dwCreationFlags;\r
- GetStartupInfo (&startupinfo);\r
- if (bCreateConsole)\r
- dwCreationFlags = CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS;\r
- else\r
- dwCreationFlags = DETACHED_PROCESS | NORMAL_PRIORITY_CLASS;\r
- const char *pCmd;\r
- char *pCmdline;\r
- pCmd = cmd;\r
- if (pCmd)\r
- {\r
- while (*pCmd == ' ')\r
- pCmd++;\r
- }\r
- pCmdline = cmdline;\r
- if (pCmdline)\r
- {\r
- while (*pCmdline == ' ')\r
- pCmdline++;\r
- }\r
- if (CreateProcess(\r
- pCmd,\r
- pCmdline,\r
- NULL,\r
- NULL,\r
- FALSE,\r
- dwCreationFlags,\r
- NULL,\r
- execdir,\r
- &startupinfo,\r
- &ProcessInformation\r
- ))\r
- return true;\r
- return false;\r
-}\r
-#endif\r
-\r
-#define MEM_BLOCKSIZE 4096\r
-void* qblockmalloc(size_t nSize)\r
-{\r
- void *b;\r
- // round up to threshold\r
- int nAllocSize = nSize % MEM_BLOCKSIZE;\r
- if ( nAllocSize > 0)\r
- {\r
- nSize += MEM_BLOCKSIZE - nAllocSize;\r
- }\r
- b = malloc(nSize + 1);\r
- memset (b, 0, nSize);\r
- return b;\r
-}\r
-\r
-//++timo NOTE: can be replaced by g_malloc0(nSize+1) when moving to glib memory handling\r
-void* qmalloc (size_t nSize)\r
-{\r
- void *b;\r
- b = malloc(nSize + 1);\r
- memset (b, 0, nSize);\r
- return b;\r
-}\r
-\r
-/*\r
-================\r
-Q_filelength\r
-================\r
-*/\r
-int Q_filelength (FILE *f)\r
-{\r
- int pos;\r
- int end;\r
-\r
- pos = ftell (f);\r
- fseek (f, 0, SEEK_END);\r
- end = ftell (f);\r
- fseek (f, pos, SEEK_SET);\r
-\r
- return end;\r
-}\r
-\r
-void DefaultExtension (char *path, char *extension)\r
-{\r
- char *src;\r
-//\r
-// if path doesn't have a .EXT, append extension\r
-// (extension should include the .)\r
-//\r
- src = path + strlen(path) - 1;\r
-\r
- while (*src != PATHSEPERATOR && src != path)\r
- {\r
- if (*src == '.')\r
- return; // it has an extension\r
- src--;\r
- }\r
-\r
- strcat (path, extension);\r
-}\r
-\r
-void DefaultPath (char *path, char *basepath)\r
-{\r
- char temp[128];\r
-\r
- if (path[0] == PATHSEPERATOR)\r
- return; // absolute path location\r
- strcpy (temp,path);\r
- strcpy (path,basepath);\r
- strcat (path,temp);\r
-}\r
-\r
-\r
-void StripFilename (char *path)\r
-{\r
- int length;\r
-\r
- length = strlen(path)-1;\r
- while (length > 0 && path[length] != PATHSEPERATOR)\r
- length--;\r
- path[length] = 0;\r
-}\r
-\r
-void StripExtension (char *path)\r
-{\r
- int length;\r
-\r
- length = strlen(path)-1;\r
- while (length > 0 && path[length] != '.')\r
- {\r
- length--;\r
- if (path[length] == '/')\r
- return; // no extension\r
- }\r
- if (length)\r
- path[length] = 0;\r
-}\r
-\r
-\r
-/*\r
-====================\r
-Extract file parts\r
-====================\r
-*/\r
-void ExtractFilePath (const char *path, char *dest)\r
-{\r
- const char *src;\r
-\r
- src = path + strlen(path) - 1;\r
-\r
-//\r
-// back up until a \ or the start\r
-//\r
- while (src != path && *(src-1) != '/' && *(src-1) != '\\')\r
- src--;\r
-\r
- memcpy (dest, path, src-path);\r
- dest[src-path] = 0;\r
-}\r
-\r
-void ExtractFileName (const char *path, char *dest)\r
-{\r
- const char *src;\r
-\r
- src = path + strlen(path) - 1;\r
-\r
-//\r
-// back up until a \ or the start\r
-//\r
- while (src != path && *(src-1) != '/' \r
- && *(src-1) != '\\' )\r
- src--;\r
-\r
- while (*src)\r
- {\r
- *dest++ = *src++;\r
- }\r
- *dest = 0;\r
-}\r
-\r
-inline const char* path_get_filename_start(const char* path)\r
-{\r
- {\r
- const char* last_forward_slash = strrchr(path, '/');\r
- if(last_forward_slash != NULL)\r
- return last_forward_slash + 1;\r
- }\r
-\r
- {\r
- const char* last_backward_slash = strrchr(path, '\\');\r
- if(last_backward_slash != NULL)\r
- return last_backward_slash + 1;\r
- }\r
-\r
- return path;\r
-}\r
-\r
-inline unsigned int filename_get_base_length(const char* filename)\r
-{\r
- const char* last_period = strrchr(filename, '.');\r
- return (last_period != NULL) ? last_period - filename : strlen(filename);\r
-}\r
-\r
-void ExtractFileBase (const char *path, char *dest)\r
-{\r
- const char* filename = path_get_filename_start(path);\r
- unsigned int length = filename_get_base_length(filename);\r
- strncpy(dest, filename, length);\r
- dest[length] = '\0';\r
-}\r
-\r
-void ExtractFileExtension (const char *path, char *dest)\r
-{\r
- const char *src;\r
-\r
- src = path + strlen(path) - 1;\r
-\r
-//\r
-// back up until a . or the start\r
-//\r
- while (src != path && *(src-1) != '.')\r
- src--;\r
- if (src == path)\r
- {\r
- *dest = 0; // no extension\r
- return;\r
- }\r
-\r
- strcpy (dest,src);\r
-}\r
-\r
-\r
-void ConvertDOSToUnixName( char *dst, const char *src )\r
-{\r
- while ( *src )\r
- {\r
- if ( *src == '\\' )\r
- *dst = '/';\r
- else\r
- *dst = *src;\r
- dst++; src++;\r
- }\r
- *dst = 0;\r
-}\r
-\r
-\r
-char* StrDup(char* pStr)\r
-{ \r
- if (pStr)\r
- {\r
- return strcpy(new char[strlen(pStr)+1], pStr); \r
- }\r
- return NULL;\r
-}\r
-\r
-char* StrDup(const char* pStr)\r
-{ \r
- if (pStr)\r
- {\r
- return strcpy(new char[strlen(pStr)+1], pStr); \r
- }\r
- return NULL;\r
-}\r
-\r
-void CreateDirectoryPath (const char *path) {\r
- char base[PATH_MAX];\r
- char *src;\r
- char back;\r
- \r
- ExtractFilePath(path, base);\r
-\r
- src = base+1;\r
- while (1) {\r
- while (*src != '\0' && *src != '/' && *src != '\\') {\r
- src++;\r
- }\r
- if (*src == '\0') {\r
- break;\r
- }\r
- back = *src; *src = '\0';\r
- Q_mkdir(base, 0755);\r
- *src = back; src++;\r
- }\r
-}\r
-\r
-/*\r
-============================================================================\r
-\r
- BYTE ORDER FUNCTIONS\r
-\r
-============================================================================\r
-*/\r
-\r
-#ifdef _SGI_SOURCE\r
- #define __BIG_ENDIAN__\r
-#endif\r
-\r
-#ifdef __BIG_ENDIAN__\r
-\r
-short LittleShort (short l)\r
-{\r
- byte b1,b2;\r
-\r
- b1 = l&255;\r
- b2 = (l>>8)&255;\r
-\r
- return(b1<<8) + b2;\r
-}\r
-\r
-short BigShort (short l)\r
-{\r
- return l;\r
-}\r
-\r
-\r
-int LittleLong (int l)\r
-{\r
- byte b1,b2,b3,b4;\r
-\r
- b1 = l&255;\r
- b2 = (l>>8)&255;\r
- b3 = (l>>16)&255;\r
- b4 = (l>>24)&255;\r
-\r
- return((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;\r
-}\r
-\r
-int BigLong (int l)\r
-{\r
- return l;\r
-}\r
-\r
-\r
-float LittleFloat (float l)\r
-{\r
- union\r
- {\r
- byte b[4]; float f;\r
- } in, out;\r
-\r
- in.f = l;\r
- out.b[0] = in.b[3];\r
- out.b[1] = in.b[2];\r
- out.b[2] = in.b[1];\r
- out.b[3] = in.b[0];\r
-\r
- return out.f;\r
-}\r
-\r
-float BigFloat (float l)\r
-{\r
- return l;\r
-}\r
-\r
-#else\r
-\r
-short BigShort (short l)\r
-{\r
- byte b1,b2;\r
-\r
- b1 = l&255;\r
- b2 = (l>>8)&255;\r
-\r
- return(b1<<8) + b2;\r
-}\r
-\r
-short LittleShort (short l)\r
-{\r
- return l;\r
-}\r
-\r
-\r
-int BigLong (int l)\r
-{\r
- byte b1,b2,b3,b4;\r
-\r
- b1 = l&255;\r
- b2 = (l>>8)&255;\r
- b3 = (l>>16)&255;\r
- b4 = (l>>24)&255;\r
-\r
- return((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;\r
-}\r
-\r
-int LittleLong (int l)\r
-{\r
- return l;\r
-}\r
-\r
-float BigFloat (float l)\r
-{\r
- union\r
- {\r
- byte b[4]; float f;\r
- } in, out;\r
-\r
- in.f = l;\r
- out.b[0] = in.b[3];\r
- out.b[1] = in.b[2];\r
- out.b[2] = in.b[1];\r
- out.b[3] = in.b[0];\r
-\r
- return out.f;\r
-}\r
-\r
-float LittleFloat (float l)\r
-{\r
- return l;\r
-}\r
-\r
-#endif\r
+/*
+Copyright (C) 1999-2007 id Software, Inc. and contributors.
+For a list of contributors, see the accompanying CONTRIBUTORS file.
+
+This file is part of GtkRadiant.
+
+GtkRadiant is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+GtkRadiant is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GtkRadiant; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+//
+// start of shared cmdlib stuff
+//
+
+#include "cmdlib.h"
+
+#ifdef _WIN32
+ #include <windows.h>
+#endif
+#if defined (__linux__) || defined (__APPLE__)
+ #include <unistd.h>
+#endif
+
+// FIXME TTimo this should be cleaned up ..
+// NOTE: we don't use this crap .. with the total mess of mixing win32/unix paths we need to recognize both '/' and '\\'
+#define PATHSEPERATOR '/'
+
+#if defined (__linux__) || defined (__APPLE__)
+bool Q_Exec(const char *cmd, char *cmdline, const char *execdir, bool bCreateConsole)
+{
+ char fullcmd[2048];
+ char *pCmd;
+#ifdef _DEBUG
+ printf("Q_Exec damnit\n");
+#endif
+ switch (fork())
+ {
+ case -1:
+ return true;
+ break;
+ case 0:
+ // always concat the command on linux
+ if (cmd)
+ {
+ strcpy(fullcmd, cmd);
+ }
+ else
+ fullcmd[0] = '\0';
+ if (cmdline)
+ {
+ strcat(fullcmd, " ");
+ strcat(fullcmd, cmdline);
+ }
+ pCmd = fullcmd;
+ while (*pCmd == ' ')
+ pCmd++;
+#ifdef _DEBUG
+ printf("Running system...\n");
+ printf("Command: %s\n", pCmd);
+#endif
+ system( pCmd );
+#ifdef _DEBUG
+ printf ("system() returned\n");
+#endif
+ _exit (0);
+ break;
+ }
+ return true;
+}
+#endif
+
+#ifdef _WIN32
+// NOTE TTimo windows is VERY nitpicky about the syntax in CreateProcess
+bool Q_Exec(const char *cmd, char *cmdline, const char *execdir, bool bCreateConsole)
+{
+ PROCESS_INFORMATION ProcessInformation;
+ STARTUPINFO startupinfo = {0};
+ DWORD dwCreationFlags;
+ GetStartupInfo (&startupinfo);
+ if (bCreateConsole)
+ dwCreationFlags = CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS;
+ else
+ dwCreationFlags = DETACHED_PROCESS | NORMAL_PRIORITY_CLASS;
+ const char *pCmd;
+ char *pCmdline;
+ pCmd = cmd;
+ if (pCmd)
+ {
+ while (*pCmd == ' ')
+ pCmd++;
+ }
+ pCmdline = cmdline;
+ if (pCmdline)
+ {
+ while (*pCmdline == ' ')
+ pCmdline++;
+ }
+ if (CreateProcess(
+ pCmd,
+ pCmdline,
+ NULL,
+ NULL,
+ FALSE,
+ dwCreationFlags,
+ NULL,
+ execdir,
+ &startupinfo,
+ &ProcessInformation
+ ))
+ return true;
+ return false;
+}
+#endif
+
+#define MEM_BLOCKSIZE 4096
+void* qblockmalloc(size_t nSize)
+{
+ void *b;
+ // round up to threshold
+ int nAllocSize = nSize % MEM_BLOCKSIZE;
+ if ( nAllocSize > 0)
+ {
+ nSize += MEM_BLOCKSIZE - nAllocSize;
+ }
+ b = malloc(nSize + 1);
+ memset (b, 0, nSize);
+ return b;
+}
+
+//++timo NOTE: can be replaced by g_malloc0(nSize+1) when moving to glib memory handling
+void* qmalloc (size_t nSize)
+{
+ void *b;
+ b = malloc(nSize + 1);
+ memset (b, 0, nSize);
+ return b;
+}
+
+/*
+================
+Q_filelength
+================
+*/
+int Q_filelength (FILE *f)
+{
+ int pos;
+ int end;
+
+ pos = ftell (f);
+ fseek (f, 0, SEEK_END);
+ end = ftell (f);
+ fseek (f, pos, SEEK_SET);
+
+ return end;
+}
+
+void DefaultExtension (char *path, char *extension)
+{
+ char *src;
+//
+// if path doesn't have a .EXT, append extension
+// (extension should include the .)
+//
+ src = path + strlen(path) - 1;
+
+ while (*src != PATHSEPERATOR && src != path)
+ {
+ if (*src == '.')
+ return; // it has an extension
+ src--;
+ }
+
+ strcat (path, extension);
+}
+
+void DefaultPath (char *path, char *basepath)
+{
+ char temp[128];
+
+ if (path[0] == PATHSEPERATOR)
+ return; // absolute path location
+ strcpy (temp,path);
+ strcpy (path,basepath);
+ strcat (path,temp);
+}
+
+
+void StripFilename (char *path)
+{
+ int length;
+
+ length = strlen(path)-1;
+ while (length > 0 && path[length] != PATHSEPERATOR)
+ length--;
+ path[length] = 0;
+}
+
+void StripExtension (char *path)
+{
+ int length;
+
+ length = strlen(path)-1;
+ while (length > 0 && path[length] != '.')
+ {
+ length--;
+ if (path[length] == '/')
+ return; // no extension
+ }
+ if (length)
+ path[length] = 0;
+}
+
+
+/*
+====================
+Extract file parts
+====================
+*/
+void ExtractFilePath (const char *path, char *dest)
+{
+ const char *src;
+
+ src = path + strlen(path) - 1;
+
+//
+// back up until a \ or the start
+//
+ while (src != path && *(src-1) != '/' && *(src-1) != '\\')
+ src--;
+
+ memcpy (dest, path, src-path);
+ dest[src-path] = 0;
+}
+
+void ExtractFileName (const char *path, char *dest)
+{
+ const char *src;
+
+ src = path + strlen(path) - 1;
+
+//
+// back up until a \ or the start
+//
+ while (src != path && *(src-1) != '/'
+ && *(src-1) != '\\' )
+ src--;
+
+ while (*src)
+ {
+ *dest++ = *src++;
+ }
+ *dest = 0;
+}
+
+inline const char* path_get_filename_start(const char* path)
+{
+ {
+ const char* last_forward_slash = strrchr(path, '/');
+ if(last_forward_slash != NULL)
+ return last_forward_slash + 1;
+ }
+
+ {
+ const char* last_backward_slash = strrchr(path, '\\');
+ if(last_backward_slash != NULL)
+ return last_backward_slash + 1;
+ }
+
+ return path;
+}
+
+inline unsigned int filename_get_base_length(const char* filename)
+{
+ const char* last_period = strrchr(filename, '.');
+ return (last_period != NULL) ? last_period - filename : strlen(filename);
+}
+
+void ExtractFileBase (const char *path, char *dest)
+{
+ const char* filename = path_get_filename_start(path);
+ unsigned int length = filename_get_base_length(filename);
+ strncpy(dest, filename, length);
+ dest[length] = '\0';
+}
+
+void ExtractFileExtension (const char *path, char *dest)
+{
+ const char *src;
+
+ src = path + strlen(path) - 1;
+
+//
+// back up until a . or the start
+//
+ while (src != path && *(src-1) != '.')
+ src--;
+ if (src == path)
+ {
+ *dest = 0; // no extension
+ return;
+ }
+
+ strcpy (dest,src);
+}
+
+
+void ConvertDOSToUnixName( char *dst, const char *src )
+{
+ while ( *src )
+ {
+ if ( *src == '\\' )
+ *dst = '/';
+ else
+ *dst = *src;
+ dst++; src++;
+ }
+ *dst = 0;
+}
+
+
+char* StrDup(char* pStr)
+{
+ if (pStr)
+ {
+ return strcpy(new char[strlen(pStr)+1], pStr);
+ }
+ return NULL;
+}
+
+char* StrDup(const char* pStr)
+{
+ if (pStr)
+ {
+ return strcpy(new char[strlen(pStr)+1], pStr);
+ }
+ return NULL;
+}
+
+void CreateDirectoryPath (const char *path) {
+ char base[PATH_MAX];
+ char *src;
+ char back;
+
+ ExtractFilePath(path, base);
+
+ src = base+1;
+ while (1) {
+ while (*src != '\0' && *src != '/' && *src != '\\') {
+ src++;
+ }
+ if (*src == '\0') {
+ break;
+ }
+ back = *src; *src = '\0';
+ Q_mkdir(base, 0755);
+ *src = back; src++;
+ }
+}
+
+/*
+============================================================================
+
+ BYTE ORDER FUNCTIONS
+
+============================================================================
+*/
+
+#ifdef _SGI_SOURCE
+ #define __BIG_ENDIAN__
+#endif
+
+#ifdef __BIG_ENDIAN__
+
+short LittleShort (short l)
+{
+ byte b1,b2;
+
+ b1 = l&255;
+ b2 = (l>>8)&255;
+
+ return(b1<<8) + b2;
+}
+
+short BigShort (short l)
+{
+ return l;
+}
+
+
+int LittleLong (int l)
+{
+ byte b1,b2,b3,b4;
+
+ b1 = l&255;
+ b2 = (l>>8)&255;
+ b3 = (l>>16)&255;
+ b4 = (l>>24)&255;
+
+ return((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
+}
+
+int BigLong (int l)
+{
+ return l;
+}
+
+
+float LittleFloat (float l)
+{
+ union
+ {
+ byte b[4]; float f;
+ } in, out;
+
+ in.f = l;
+ out.b[0] = in.b[3];
+ out.b[1] = in.b[2];
+ out.b[2] = in.b[1];
+ out.b[3] = in.b[0];
+
+ return out.f;
+}
+
+float BigFloat (float l)
+{
+ return l;
+}
+
+#else
+
+short BigShort (short l)
+{
+ byte b1,b2;
+
+ b1 = l&255;
+ b2 = (l>>8)&255;
+
+ return(b1<<8) + b2;
+}
+
+short LittleShort (short l)
+{
+ return l;
+}
+
+
+int BigLong (int l)
+{
+ byte b1,b2,b3,b4;
+
+ b1 = l&255;
+ b2 = (l>>8)&255;
+ b3 = (l>>16)&255;
+ b4 = (l>>24)&255;
+
+ return((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
+}
+
+int LittleLong (int l)
+{
+ return l;
+}
+
+float BigFloat (float l)
+{
+ union
+ {
+ byte b[4]; float f;
+ } in, out;
+
+ in.f = l;
+ out.b[0] = in.b[3];
+ out.b[1] = in.b[2];
+ out.b[2] = in.b[1];
+ out.b[3] = in.b[0];
+
+ return out.f;
+}
+
+float LittleFloat (float l)
+{
+ return l;
+}
+
+#endif