]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - radiant/missing.cpp
fix
[xonotic/netradiant.git] / radiant / missing.cpp
index f9779908a79814ac2f2b4babc484867a817f7677..0245ec1e01758e215eb6ef6d6bc4a99f5502c434 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,7 +34,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 // Leonardo Zide (leo@lokigames.com)
 //
 
-// FIXME: compile on Windows as well
+#include "missing.h"
+#include "qsysprintf.h"
+
 #if defined (__linux__) || defined (__APPLE__)
 
 #include <stdio.h>
@@ -44,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;
@@ -67,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);
@@ -85,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;
@@ -93,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] == '/')
@@ -178,4 +131,129 @@ 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 );
+       }
+}
+
+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 != NULL ) {
+               FindClose( findHandle );
+       }
+}
+
+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 );
+               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;
+}