]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/missing.cpp
towards compiling on windows again
[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 // FIXME: compile on Windows as well
38 #if defined (__linux__) || defined (__APPLE__)
39
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <sys/time.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <stdlib.h>
46 #include <dirent.h>
47 #include "missing.h"
48 #include "qsysprintf.h"
49
50 bool CopyFile(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 CreateDirectory( 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 bool CopyTree( const char *source, const char *dest ) {
97         DIR                             *dir;
98         struct dirent   *dirlist;
99         struct stat             sbuf;
100         Str                             srcEntry;
101         Str                             dstEntry;
102
103         dir = opendir( source );
104         if ( dir != NULL ) {
105                 while ( ( dirlist = readdir( dir ) ) != NULL ) {
106                         if ( strcmp( dirlist->d_name, "." ) == 0 || strcmp( dirlist->d_name, ".." ) == 0 ) {
107                                 continue;
108                         }
109                         if ( strcmp( dirlist->d_name, ".svn" ) == 0 ) {
110                                 continue;
111                         }
112                         srcEntry = source;
113                         srcEntry += "/";
114                         srcEntry += dirlist->d_name;
115                         dstEntry = dest;
116                         dstEntry += "/";
117                         dstEntry += dirlist->d_name;
118                         if ( stat( srcEntry.GetBuffer(), &sbuf ) == -1 ) {
119                                 Sys_Printf( "stat %s failed\n", srcEntry.GetBuffer() );
120                         }
121                         if ( S_ISDIR( sbuf.st_mode ) ) {
122                                 bool ret;
123                                 if ( stat( dstEntry.GetBuffer(), &sbuf ) == -1 ) {
124                                         ret = CreateDirectory( dstEntry.GetBuffer() );
125                                 }
126                                 ret = CopyTree( srcEntry.GetBuffer(), dstEntry.GetBuffer() );
127                                 if ( !ret ) {
128                                         return false;
129                                 }
130                         } else {
131                                 Sys_Printf( "copy %s -> %s\n", srcEntry.GetBuffer(), dstEntry.GetBuffer() );
132                                 bool ret = CopyFile( srcEntry.GetBuffer(), dstEntry.GetBuffer() );
133                                 if ( !ret ) {
134                                         return false;
135                                 }
136                         }
137                 }
138                 closedir( dir );
139         }
140         return true;
141 }
142
143 int GetFullPathName(const char *lpFileName, int nBufferLength, char *lpBuffer, char **lpFilePart)
144 {
145   if (lpFileName[0] == '/')
146   {
147     strcpy (lpBuffer, lpFileName);
148     *lpFilePart = strrchr (lpBuffer, '/');
149     return strlen (lpBuffer);
150   }
151
152   if (getcwd (lpBuffer, nBufferLength) == NULL)
153     return 0;
154
155   strcat (lpBuffer, "/");
156   *lpFilePart = lpBuffer + strlen (lpBuffer);
157   strcat (lpBuffer, lpFileName);
158
159   char *scr = lpBuffer, *dst = lpBuffer;
160   for (int i = 0; (i < nBufferLength) && (*scr != 0); i++)
161   {
162     if (*scr == '/' && *(scr+1) == '.' && *(scr+2) == '.')
163     {
164       scr += 4;
165       while (dst != lpBuffer && *dst != '/')
166       {
167         dst--;
168         i--;
169       }
170     }
171
172     *dst = *scr;
173
174     scr++; dst++;
175   }
176   *dst = 0;
177
178   return strlen (lpBuffer);
179 }
180
181 #endif