]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/missing.cpp
added file copy tree for the game packs - lists supported games
[xonotic/netradiant.git] / radiant / missing.cpp
1 /*
2 Copyright (c) 2001, Loki software, inc.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification, 
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list 
9 of conditions and the following disclaimer.
10
11 Redistributions in binary form must reproduce the above copyright notice, this
12 list of conditions and the following disclaimer in the documentation and/or
13 other materials provided with the distribution.
14
15 Neither the name of Loki software nor the names of its contributors may be used 
16 to endorse or promote products derived from this software without specific prior 
17 written permission. 
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' 
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY 
23 DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
29 */
30
31 //
32 // Missing functions
33 //
34 // Leonardo Zide (leo@lokigames.com)
35 //
36
37 #if defined (__linux__) || defined (__APPLE__)
38
39 #include <stdio.h>
40 #include <unistd.h>
41 #include <sys/time.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <stdlib.h>
45 #include <dirent.h>
46 #include "missing.h"
47 #include "qsysprintf.h"
48
49 bool CopyFile(const char *lpExistingFileName, const char *lpNewFileName)
50 {
51   FILE *src, *dst;
52   void* buf;
53   int l;
54   bool ret = false;
55   char realsrc[PATH_MAX], realdest[PATH_MAX];
56
57   realpath (lpExistingFileName, realsrc);
58   realpath (lpNewFileName, realdest);
59
60   src = fopen (realsrc, "rb");
61   if ( !src ) {
62     return false;
63   }
64   dst = fopen (realdest, "wb");
65   if (!dst) {
66     fclose (src);
67     return false;
68   }
69  
70   fseek (src, 0, SEEK_END);
71   l = ftell (src);
72   rewind (src);
73   buf = g_malloc (l);
74
75   if (buf != NULL)
76     if (fread (buf, l, 1, src) == 1)
77       if (fwrite (buf, l, 1, dst) == 1)
78         ret = true;
79
80   g_free (buf);
81   fclose (src);
82   fclose (dst);
83
84   return ret;
85 }
86
87 bool CreateDirectory( const char *directory ) {
88         if ( mkdir( directory, 0777 ) == -1 ) {
89                 Sys_Printf( "mkdir %s failed\n", directory );
90                 return false;
91         }
92         return true;
93 }
94
95 bool CopyTree( const char *source, const char *dest ) {
96         DIR                             *dir;
97         struct dirent   *dirlist;
98         struct stat             sbuf;
99         Str                             srcEntry;
100         Str                             dstEntry;
101
102         dir = opendir( source );
103         if ( dir != NULL ) {
104                 while ( ( dirlist = readdir( dir ) ) != NULL ) {
105                         if ( strcmp( dirlist->d_name, "." ) == 0 || strcmp( dirlist->d_name, ".." ) == 0 ) {
106                                 continue;
107                         }
108                         if ( strcmp( dirlist->d_name, ".svn" ) == 0 ) {
109                                 continue;
110                         }
111                         srcEntry = source;
112                         srcEntry += "/";
113                         srcEntry += dirlist->d_name;
114                         dstEntry = dest;
115                         dstEntry += "/";
116                         dstEntry += dirlist->d_name;
117                         if ( stat( srcEntry.GetBuffer(), &sbuf ) == -1 ) {
118                                 Sys_Printf( "stat %s failed\n", srcEntry.GetBuffer() );
119                         }
120                         if ( S_ISDIR( sbuf.st_mode ) ) {
121                                 bool ret;
122                                 if ( stat( dstEntry.GetBuffer(), &sbuf ) == -1 ) {
123                                         ret = CreateDirectory( dstEntry.GetBuffer() );
124                                 }
125                                 ret = CopyTree( srcEntry.GetBuffer(), dstEntry.GetBuffer() );
126                                 if ( !ret ) {
127                                         return false;
128                                 }
129                         } else {
130                                 Sys_Printf( "copy %s -> %s\n", srcEntry.GetBuffer(), dstEntry.GetBuffer() );
131                                 bool ret = CopyFile( srcEntry.GetBuffer(), dstEntry.GetBuffer() );
132                                 if ( !ret ) {
133                                         return false;
134                                 }
135                         }
136                 }
137                 closedir( dir );
138         }
139         return true;
140 }
141
142 int GetFullPathName(const char *lpFileName, int nBufferLength, char *lpBuffer, char **lpFilePart)
143 {
144   if (lpFileName[0] == '/')
145   {
146     strcpy (lpBuffer, lpFileName);
147     *lpFilePart = strrchr (lpBuffer, '/');
148     return strlen (lpBuffer);
149   }
150
151   if (getcwd (lpBuffer, nBufferLength) == NULL)
152     return 0;
153
154   strcat (lpBuffer, "/");
155   *lpFilePart = lpBuffer + strlen (lpBuffer);
156   strcat (lpBuffer, lpFileName);
157
158   char *scr = lpBuffer, *dst = lpBuffer;
159   for (int i = 0; (i < nBufferLength) && (*scr != 0); i++)
160   {
161     if (*scr == '/' && *(scr+1) == '.' && *(scr+2) == '.')
162     {
163       scr += 4;
164       while (dst != lpBuffer && *dst != '/')
165       {
166         dst--;
167         i--;
168       }
169     }
170
171     *dst = *scr;
172
173     scr++; dst++;
174   }
175   *dst = 0;
176
177   return strlen (lpBuffer);
178 }
179
180 #endif