]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/cmdlib/cmdlib.cpp
Merge commit '830125fad042fad35dc029b6eb57c8156ad7e176'
[xonotic/netradiant.git] / libs / cmdlib / cmdlib.cpp
index b27919ff7f0fc42265d922b2c42ea42293a09056..2a9ab55a35182430f3a94c202fb2d7d700ceaa7f 100644 (file)
@@ -1,5 +1,5 @@
 /*
-   Copyright (C) 1999-2007 id Software, Inc. and contributors.
+   Copyright (C) 1999-2006 Id Software, Inc. and contributors.
    For a list of contributors, see the accompanying CONTRIBUTORS file.
 
    This file is part of GtkRadiant.
 
 #include "cmdlib.h"
 
-#ifdef _WIN32
-  #include <windows.h>
-#endif
-#if defined ( __linux__ ) || defined ( __APPLE__ )
-  #include <unistd.h>
-#endif
+#include <string.h>
+#include <stdio.h>
+
+#include "string/string.h"
+#include "os/path.h"
+#include "container/array.h"
 
-// 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 ){
+#if defined ( POSIX )
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+bool Q_Exec( const char *cmd, char *cmdline, const char *, bool, bool waitfor ){
        char fullcmd[2048];
        char *pCmd;
+       pid_t pid;
 #ifdef _DEBUG
        printf( "Q_Exec damnit\n" );
 #endif
-       switch ( fork() )
+       switch ( ( pid = fork() ) )
        {
+       default:
+               if ( waitfor ) {
+                       waitpid( pid, NULL, 0 );
+               }
+               break;
        case -1:
                return true;
                break;
@@ -76,11 +84,13 @@ bool Q_Exec( const char *cmd, char *cmdline, const char *execdir, bool bCreateCo
        }
        return true;
 }
-#endif
 
-#ifdef _WIN32
+#elif defined( WIN32 )
+
+#include <windows.h>
+
 // NOTE TTimo windows is VERY nitpicky about the syntax in CreateProcess
-bool Q_Exec( const char *cmd, char *cmdline, const char *execdir, bool bCreateConsole ){
+bool Q_Exec( const char *cmd, char *cmdline, const char *execdir, bool bCreateConsole, bool waitfor ){
        PROCESS_INFORMATION ProcessInformation;
        STARTUPINFO startupinfo = {0};
        DWORD dwCreationFlags;
@@ -103,6 +113,7 @@ bool Q_Exec( const char *cmd, char *cmdline, const char *execdir, bool bCreateCo
                while ( *pCmdline == ' ' )
                        pCmdline++;
        }
+
        if ( CreateProcess(
                         pCmd,
                         pCmdline,
@@ -115,356 +126,12 @@ bool Q_Exec( const char *cmd, char *cmdline, const char *execdir, bool bCreateCo
                         &startupinfo,
                         &ProcessInformation
                         ) ) {
+               if ( waitfor ) {
+                       WaitForSingleObject( ProcessInformation.hProcess, INFINITE );
+               }
                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