]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/missing.cpp
Updating notes on regression tests. They are "mostly sort of fixed". Won't
[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 #include "missing.h"
38 #include "qsysprintf.h"
39
40 #if defined (__linux__) || defined (__APPLE__)
41
42 #include <stdio.h>
43 #include <unistd.h>
44 #include <sys/time.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <stdlib.h>
48 #include <dirent.h>
49
50 bool radCopyFile(const char *lpExistingFileName, const char *lpNewFileName)
51 {
52   FILE *src, *dst;
53   void* buf;
54   int l;
55   bool ret = false;
56   char realsrc[PATH_MAX], realdest[PATH_MAX];
57
58   realpath (lpExistingFileName, realsrc);
59   realpath (lpNewFileName, realdest);
60
61   src = fopen (realsrc, "rb");
62   if ( !src ) {
63     return false;
64   }
65   dst = fopen (realdest, "wb");
66   if (!dst) {
67     fclose (src);
68     return false;
69   }
70
71   fseek (src, 0, SEEK_END);
72   l = ftell (src);
73   rewind (src);
74   buf = g_malloc (l);
75
76   if (buf != NULL)
77     if (fread (buf, l, 1, src) == 1)
78       if (fwrite (buf, l, 1, dst) == 1)
79         ret = true;
80
81   g_free (buf);
82   fclose (src);
83   fclose (dst);
84
85   return ret;
86 }
87
88 bool radCreateDirectory( const char *directory ) {
89         if ( mkdir( directory, 0777 ) == -1 ) {
90                 Sys_Printf( "mkdir %s failed\n", directory );
91                 return false;
92         }
93         return true;
94 }
95
96 int GetFullPathName(const char *lpFileName, int nBufferLength, char *lpBuffer, char **lpFilePart)
97 {
98   if (lpFileName[0] == '/')
99   {
100     strcpy (lpBuffer, lpFileName);
101     *lpFilePart = strrchr (lpBuffer, '/');
102     return strlen (lpBuffer);
103   }
104
105   if (getcwd (lpBuffer, nBufferLength) == NULL)
106     return 0;
107
108   strcat (lpBuffer, "/");
109   *lpFilePart = lpBuffer + strlen (lpBuffer);
110   strcat (lpBuffer, lpFileName);
111
112   char *scr = lpBuffer, *dst = lpBuffer;
113   for (int i = 0; (i < nBufferLength) && (*scr != 0); i++)
114   {
115     if (*scr == '/' && *(scr+1) == '.' && *(scr+2) == '.')
116     {
117       scr += 4;
118       while (dst != lpBuffer && *dst != '/')
119       {
120         dst--;
121         i--;
122       }
123     }
124
125     *dst = *scr;
126
127     scr++; dst++;
128   }
129   *dst = 0;
130
131   return strlen (lpBuffer);
132 }
133
134 EPathCheck CheckFile( const char *path ) {
135         struct stat             sbuf;
136         if ( stat( path, &sbuf ) == -1 ) {
137                 return PATH_FAIL;
138         }
139         if ( S_ISDIR( sbuf.st_mode ) ) {
140                 return PATH_DIRECTORY;
141         }
142         return PATH_FILE;
143 }
144
145 FindFiles::FindFiles( const char *_directory ) {
146         findHandle = opendir( _directory );
147 }
148
149 FindFiles::~FindFiles() {
150         if ( findHandle != NULL ) {
151                 closedir( findHandle );
152                 findHandle = NULL;
153         }
154 }
155
156 const char* FindFiles::NextFile() {
157         struct dirent *d;
158
159         if ( findHandle == NULL )
160                 return NULL;
161
162         d = readdir( findHandle );
163         if ( d )
164                 return d->d_name;
165         return NULL;
166 }
167
168 #else
169
170 FindFiles::FindFiles( const char *_directory ) {
171         char endChar;
172         directory = _directory;
173         if (directory.GetLength() > 0) {
174                 endChar = directory.GetAt(directory.GetLength() - 1);
175                 if (!(endChar == '/' || endChar == '\\')) {
176                         // We're only using '/' as the path separator throughout this code, not '\'.
177                         // However, I'd hate to see the code silently fail due to a trailing '\', so
178                         // I added the check for it.
179                         directory += '/';
180                 }
181         }
182         directory += '*';
183         findHandle = INVALID_HANDLE_VALUE;
184 }
185
186 FindFiles::~FindFiles() {
187         if ( findHandle != INVALID_HANDLE_VALUE ) {
188                 FindClose( findHandle );
189                 findHandle = INVALID_HANDLE_VALUE;
190         }
191 }
192
193 const char* FindFiles::NextFile() {
194         if ( findHandle == INVALID_HANDLE_VALUE ) {
195                 findHandle = FindFirstFile( directory.GetBuffer(), &findFileData );
196                 if ( findHandle == INVALID_HANDLE_VALUE ) {
197                         return NULL;
198                 }
199                 return findFileData.cFileName;
200         }
201         if ( FindNextFile( findHandle, &findFileData ) == 0 ) {
202                 FindClose( findHandle );
203                 findHandle = INVALID_HANDLE_VALUE;
204                 return NULL;
205         }
206         return findFileData.cFileName;
207 }
208
209 EPathCheck CheckFile( const char *path ) {
210         struct _stat sbuf;
211         if ( _stat( path, &sbuf ) == -1 ) {
212                 return PATH_FAIL;
213         }
214         if ( ( sbuf.st_mode & _S_IFDIR ) != 0 ) {
215                 return PATH_DIRECTORY;
216         }
217         return PATH_FILE;
218 }
219
220 bool radCreateDirectory( const char *directory ) {
221         return ( CreateDirectory( directory, NULL ) != false );
222 }
223
224 bool radCopyFile( const char *lpExistingFileName, const char *lpNewFileName ) {
225         return ( CopyFile( lpExistingFileName, lpNewFileName, FALSE ) != false );
226 }
227
228 #endif
229
230 bool CopyTree( const char *source, const char *dest ) {
231         Str                             srcEntry;
232         Str                             dstEntry;
233         const char              *dirname;
234         FindFiles               fileScan( source );
235
236         while ( ( dirname = fileScan.NextFile() ) != NULL ) {
237                 if ( strcmp( dirname, "." ) == 0 || strcmp( dirname, ".." ) == 0 ) {
238                         continue;
239                 }
240                 if ( strcmp( dirname, ".svn" ) == 0 ) {
241                         continue;
242                 }
243                 srcEntry = source;
244                 srcEntry += "/";
245                 srcEntry += dirname;
246                 dstEntry = dest;
247                 dstEntry += "/";
248                 dstEntry += dirname;
249                 switch ( CheckFile( srcEntry.GetBuffer() ) ) {
250                         case PATH_DIRECTORY: {
251                                 if ( CheckFile( dstEntry.GetBuffer() ) == PATH_FAIL ) {
252                                         if ( !radCreateDirectory( dstEntry.GetBuffer() ) ) {
253                                                 Sys_Printf( "create directory %s failed\n", dstEntry.GetBuffer() );
254                                                 return false;
255                                         }
256                                 }
257                                 bool ret;
258                                 ret = CopyTree( srcEntry.GetBuffer(), dstEntry.GetBuffer() );
259                                 if ( !ret ) {
260                                         return false;
261                                 }
262                                 break;
263                         }
264                         case PATH_FILE: {
265                                 Sys_Printf( "copy %s -> %s\n", srcEntry.GetBuffer(), dstEntry.GetBuffer() );
266                                 bool ret = radCopyFile( srcEntry.GetBuffer(), dstEntry.GetBuffer() );
267                                 if ( !ret ) {
268                                         return false;
269                                 }
270                                 break;
271                         }
272                 }
273         }
274         return true;
275 }