]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - radiant/missing.cpp
* Tremulous patch by Ingar
[xonotic/netradiant.git] / radiant / missing.cpp
index 522980cdb470a24ee8ef8af1f2122ae6251b86c4..f83bed3c3e1e0055b022c456d3af0165c0ffbaa6 100644 (file)
@@ -2,30 +2,30 @@
 Copyright (c) 2001, Loki software, inc.
 All rights reserved.
 
-Redistribution and use in source and binary forms, with or without modification, 
+Redistribution and use in source and binary forms, with or without modification,
 are permitted provided that the following conditions are met:
 
-Redistributions of source code must retain the above copyright notice, this list 
+Redistributions of source code must retain the above copyright notice, this list
 of conditions and the following disclaimer.
 
 Redistributions in binary form must reproduce the above copyright notice, this
 list of conditions and the following disclaimer in the documentation and/or
 other materials provided with the distribution.
 
-Neither the name of Loki software nor the names of its contributors may be used 
-to endorse or promote products derived from this software without specific prior 
-written permission. 
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' 
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
-DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY 
-DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
-(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
-LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
-ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
-SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+Neither the name of Loki software nor the names of its contributors may be used
+to endorse or promote products derived from this software without specific prior
+written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
 //
@@ -34,6 +34,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 // Leonardo Zide (leo@lokigames.com)
 //
 
+#include "missing.h"
+#include "qsysprintf.h"
+
 #if defined (__linux__) || defined (__APPLE__)
 
 #include <stdio.h>
@@ -43,10 +46,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <dirent.h>
-#include "missing.h"
-#include "qsysprintf.h"
 
-bool CopyFile(const char *lpExistingFileName, const char *lpNewFileName)
+bool radCopyFile(const char *lpExistingFileName, const char *lpNewFileName)
 {
   FILE *src, *dst;
   void* buf;
@@ -66,7 +67,7 @@ bool CopyFile(const char *lpExistingFileName, const char *lpNewFileName)
     fclose (src);
     return false;
   }
+
   fseek (src, 0, SEEK_END);
   l = ftell (src);
   rewind (src);
@@ -84,7 +85,7 @@ bool CopyFile(const char *lpExistingFileName, const char *lpNewFileName)
   return ret;
 }
 
-bool CreateDirectory( const char *directory ) {
+bool radCreateDirectory( const char *directory ) {
        if ( mkdir( directory, 0777 ) == -1 ) {
                Sys_Printf( "mkdir %s failed\n", directory );
                return false;
@@ -92,53 +93,6 @@ bool CreateDirectory( const char *directory ) {
        return true;
 }
 
-bool CopyTree( const char *source, const char *dest ) {
-       DIR                             *dir;
-       struct dirent   *dirlist;
-       struct stat             sbuf;
-       Str                             srcEntry;
-       Str                             dstEntry;
-
-       dir = opendir( source );
-       if ( dir != NULL ) {
-               while ( ( dirlist = readdir( dir ) ) != NULL ) {
-                       if ( strcmp( dirlist->d_name, "." ) == 0 || strcmp( dirlist->d_name, ".." ) == 0 ) {
-                               continue;
-                       }
-                       if ( strcmp( dirlist->d_name, ".svn" ) == 0 ) {
-                               continue;
-                       }
-                       srcEntry = source;
-                       srcEntry += "/";
-                       srcEntry += dirlist->d_name;
-                       dstEntry = dest;
-                       dstEntry += "/";
-                       dstEntry += dirlist->d_name;
-                       if ( stat( srcEntry.GetBuffer(), &sbuf ) == -1 ) {
-                               Sys_Printf( "stat %s failed\n", srcEntry.GetBuffer() );
-                       }
-                       if ( S_ISDIR( sbuf.st_mode ) ) {
-                               bool ret;
-                               if ( stat( dstEntry.GetBuffer(), &sbuf ) == -1 ) {
-                                       ret = CreateDirectory( dstEntry.GetBuffer() );
-                               }
-                               ret = CopyTree( srcEntry.GetBuffer(), dstEntry.GetBuffer() );
-                               if ( !ret ) {
-                                       return false;
-                               }
-                       } else {
-                               Sys_Printf( "copy %s -> %s\n", srcEntry.GetBuffer(), dstEntry.GetBuffer() );
-                               bool ret = CopyFile( srcEntry.GetBuffer(), dstEntry.GetBuffer() );
-                               if ( !ret ) {
-                                       return false;
-                               }
-                       }
-               }
-               closedir( dir );
-       }
-       return true;
-}
-
 int GetFullPathName(const char *lpFileName, int nBufferLength, char *lpBuffer, char **lpFilePart)
 {
   if (lpFileName[0] == '/')
@@ -177,4 +131,132 @@ int GetFullPathName(const char *lpFileName, int nBufferLength, char *lpBuffer, c
   return strlen (lpBuffer);
 }
 
+EPathCheck CheckFile( const char *path ) {
+       struct stat             sbuf;
+       if ( stat( path, &sbuf ) == -1 ) {
+               return PATH_FAIL;
+       }
+       if ( S_ISDIR( sbuf.st_mode ) ) {
+               return PATH_DIRECTORY;
+       }
+       return PATH_FILE;
+}
+
+FindFiles::FindFiles( const char *_directory ) {
+       findHandle = opendir( _directory );
+}
+
+FindFiles::~FindFiles() {
+       if ( findHandle != NULL ) {
+               closedir( findHandle );
+               findHandle = NULL;
+       }
+}
+
+const char* FindFiles::NextFile() {
+       struct dirent *d;
+
+       if ( findHandle == NULL )
+               return NULL;
+
+       d = readdir( findHandle );
+       if ( d )
+               return d->d_name;
+       return NULL;
+}
+
+#else
+
+FindFiles::FindFiles( const char *_directory ) {
+       directory = _directory;
+       directory += '*';
+       findHandle = INVALID_HANDLE_VALUE;
+}
+
+FindFiles::~FindFiles() {
+       if ( findHandle != INVALID_HANDLE_VALUE ) {
+               FindClose( findHandle );
+               findHandle = INVALID_HANDLE_VALUE;
+       }
+}
+
+const char* FindFiles::NextFile() {
+       if ( findHandle == INVALID_HANDLE_VALUE ) {
+               findHandle = FindFirstFile( directory.GetBuffer(), &findFileData );
+               if ( findHandle == INVALID_HANDLE_VALUE ) {
+                       return NULL;
+               }
+               return findFileData.cFileName;
+       }
+       if ( FindNextFile( findHandle, &findFileData ) == 0 ) {
+               FindClose( findHandle );
+               findHandle = INVALID_HANDLE_VALUE;
+               return NULL;
+       }
+       return findFileData.cFileName;
+}
+
+EPathCheck CheckFile( const char *path ) {
+       struct _stat sbuf;
+       if ( _stat( path, &sbuf ) == -1 ) {
+               return PATH_FAIL;
+       }
+       if ( ( sbuf.st_mode & _S_IFDIR ) != 0 ) {
+               return PATH_DIRECTORY;
+       }
+       return PATH_FILE;
+}
+
+bool radCreateDirectory( const char *directory ) {
+       return CreateDirectory( directory, NULL );
+}
+
+bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName ) {
+       return CopyFile( lpExistingFileName, lpNewFileName, FALSE );
+}
+
 #endif
+
+bool CopyTree( const char *source, const char *dest ) {
+       Str                             srcEntry;
+       Str                             dstEntry;
+       const char              *dirname;
+       FindFiles               fileScan( source );
+
+       while ( ( dirname = fileScan.NextFile() ) != NULL ) {
+               if ( strcmp( dirname, "." ) == 0 || strcmp( dirname, ".." ) == 0 ) {
+                       continue;
+               }
+               if ( strcmp( dirname, ".svn" ) == 0 ) {
+                       continue;
+               }
+               srcEntry = source;
+               srcEntry += "/";
+               srcEntry += dirname;
+               dstEntry = dest;
+               dstEntry += "/";
+               dstEntry += dirname;
+               switch ( CheckFile( srcEntry.GetBuffer() ) ) {
+                       case PATH_DIRECTORY: {
+                               if ( CheckFile( dstEntry.GetBuffer() ) == PATH_FAIL ) {
+                                       radCreateDirectory( dstEntry.GetBuffer() );
+                               }
+                               bool ret;
+                               ret = CopyTree( srcEntry.GetBuffer(), dstEntry.GetBuffer() );
+                               if ( !ret ) {
+                                       return false;
+                               }
+                               break;
+                       }
+                       case PATH_FILE: {
+                               Sys_Printf( "copy %s -> %s\n", srcEntry.GetBuffer(), dstEntry.GetBuffer() );
+                               bool ret = radCopyFile( srcEntry.GetBuffer(), dstEntry.GetBuffer() );
+                               if ( !ret ) {
+                                       return false;
+                               }
+                               break;
+                       }
+               }
+       }
+       return true;
+}