Bugfix : Zooming works correct, no more errors on screen.
[xara-cairo.git] / wxOil / pathnmex.cpp
blob4f582c63ffd9ea170e47c4b153e82ecbdc37a371
1 // $Id: pathnmex.cpp 836 2006-04-18 16:06:15Z gerry $
2 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
3 ================================XARAHEADERSTART===========================
5 Xara LX, a vector drawing and manipulation program.
6 Copyright (C) 1993-2006 Xara Group Ltd.
7 Copyright on certain contributions may be held in joint with their
8 respective authors. See AUTHORS file for details.
10 LICENSE TO USE AND MODIFY SOFTWARE
11 ----------------------------------
13 This file is part of Xara LX.
15 Xara LX is free software; you can redistribute it and/or modify it
16 under the terms of the GNU General Public License version 2 as published
17 by the Free Software Foundation.
19 Xara LX and its component source files are distributed in the hope
20 that it will be useful, but WITHOUT ANY WARRANTY; without even the
21 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 See the GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License along
25 with Xara LX (see the file GPL in the root directory of the
26 distribution); if not, write to the Free Software Foundation, Inc., 51
27 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 ADDITIONAL RIGHTS
31 -----------------
33 Conditional upon your continuing compliance with the GNU General Public
34 License described above, Xara Group Ltd grants to you certain additional
35 rights.
37 The additional rights are to use, modify, and distribute the software
38 together with the wxWidgets library, the wxXtra library, and the "CDraw"
39 library and any other such library that any version of Xara LX relased
40 by Xara Group Ltd requires in order to compile and execute, including
41 the static linking of that library to XaraLX. In the case of the
42 "CDraw" library, you may satisfy obligation under the GNU General Public
43 License to provide source code by providing a binary copy of the library
44 concerned and a copy of the license accompanying it.
46 Nothing in this section restricts any of the rights you have under
47 the GNU General Public License.
50 SCOPE OF LICENSE
51 ----------------
53 This license applies to this program (XaraLX) and its constituent source
54 files only, and does not necessarily apply to other Xara products which may
55 in part share the same code base, and are subject to their own licensing
56 terms.
58 This license does not apply to files in the wxXtra directory, which
59 are built into a separate library, and are subject to the wxWindows
60 license contained within that directory in the file "WXXTRA-LICENSE".
62 This license does not apply to the binary libraries (if any) within
63 the "libs" directory, which are subject to a separate license contained
64 within that directory in the file "LIBS-LICENSE".
67 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
68 ----------------------------------------------
70 Subject to the terms of the GNU Public License (see above), you are
71 free to do whatever you like with your modifications. However, you may
72 (at your option) wish contribute them to Xara's source tree. You can
73 find details of how to do this at:
74 http://www.xaraxtreme.org/developers/
76 Prior to contributing your modifications, you will need to complete our
77 contributor agreement. This can be found at:
78 http://www.xaraxtreme.org/developers/contribute/
80 Please note that Xara will not accept modifications which modify any of
81 the text between the start and end of this header (marked
82 XARAHEADERSTART and XARAHEADEREND).
85 MARKS
86 -----
88 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
89 designs are registered or unregistered trademarks, design-marks, and/or
90 service marks of Xara Group Ltd. All rights in these marks are reserved.
93 Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
94 http://www.xara.com/
96 =================================XARAHEADEREND============================
98 #include "camtypes.h"
100 #include "camelot.h"
101 #include "pathnmex.h"
102 //#include "webster.h"
104 #include <sys/types.h>
105 #include <sys/stat.h>
106 #include <stdio.h>
107 #include <errno.h>
108 #include <string.h>
110 #if defined(__WXMSW__)
111 #include <io.h>
112 #include <direct.h>
113 #include <process.h>
115 const TCHAR chPathSep = _T('\\');
116 #else
117 const TCHAR chPathSep = _T('/');
118 #endif
120 /********************************************************************************************
122 > BOOL PathNameEx::CreateLocation()
124 Author: Adrian_Stoicar (Xara Group Ltd) <camelotdev@xara.com>
125 Created: 18/11/96
126 Inputs: none
127 Return: TRUE if successful, FALSE otherwise
128 Purpose: Creates the full directory path (as returned by GetLocation())
129 on the physical medium. This is useful when creating new
130 directory structures.
133 ********************************************************************************************/
135 BOOL PathNameEx::CreateLocation()
137 PORTNOTETRACE("other","PathNameEx::CreateLocation - do nothing");
138 #ifndef EXCLUDE_FROM_XARALX
139 if (!IsValid())
140 return FALSE;
142 // We'll walk the location string from left to right - if we come across non-existent directories,
143 // we create them
144 String_256 strLocation = GetLocation(FALSE);
145 String_256 strDirPath = drivename;
146 INT32 nPos = drivename.Length(); // start after the drivename
147 while( nPos < strLocation.Length() )
149 while( ( strLocation[nPos] != chPathSep ) && ( nPos < strLocation.Length() ) )
151 strDirPath += strLocation[nPos];
152 nPos++;
154 // strDirPath has been added a directory, we check if it exists
156 if (_access((TCHAR*) strDirPath, 0) == -1) // not found, try to create the directory
158 if (_mkdir((TCHAR*) strDirPath))
160 #ifdef _DEBUG
161 TCHAR szMsg[256];
162 TCHAR szError[128];
163 switch (errno)
165 case EACCES:
166 camStrcpy(szError, "access denied (EACCES)");
167 break;
168 case ENOENT:
169 camStrcpy(szError, "path not found (ENOENT)");
170 break;
171 default:
172 wsprintf(szError, "errno = %d", errno);
174 wsprintf(szMsg, "Create directory %s failed, %s", strDirPath, szError);
175 ERROR3(szMsg);
176 #endif
177 return FALSE;
180 strDirPath += chPathSep; // add a backslash in case there are further subdirectories
181 nPos++; // move to the next position
183 #endif
184 return TRUE;
189 /********************************************************************************************
191 > BOOL PathNameEx::RemoveRecursively()
193 Author: Adrian_Stoicar (Xara Group Ltd) <camelotdev@xara.com>
194 Created: 18/11/96
195 Inputs: none
196 Return: TRUE if the path is completely deleted, FALSE otherwise (access denied
197 or invalid parameters). In case some files cannot be removed, the function will
198 do its best to remove the all accessible ones without falling over, unlike the
199 NT system call RMDIR which stops at the first file it can't delete.
200 Purpose: Removes a file or a whole directory tree from the the physical medium (in
201 which case, the object should be pointing to the root directory of the tree
202 you want deleted)
205 ********************************************************************************************/
208 BOOL PathNameEx::RemoveRecursively(const String_256& rPath)
210 PORTNOTETRACE("other","PathNameEx::RemoveRecursively - do nothing");
211 #ifndef EXCLUDE_FROM_XARALX
212 String_256 strFilename(rPath);
213 strFilename.toLower();
214 // See if the path points to a file (the easy case) or a directory
215 if (strFilename[strFilename.Length() - 1] == chPathSep)
217 strFilename.Remove(strFilename.Length() - 1, 1);
218 goto DIRECTORY;
220 struct _stat fileData;
221 if (_stat((TCHAR*) strFilename, &fileData))
223 if (errno == ENOENT)
225 ERROR3("Filename or path not found");
227 else
229 ERROR3("_stat() failed with an unknown error");
231 return FALSE;
233 if (fileData.st_mode & _S_IFDIR) // directory
235 DIRECTORY:
236 // Make sure the directory is not the current one
237 TCHAR tchbuff[_MAX_PATH];
238 if (_getcwd(tchbuff, _MAX_PATH) == NULL)
240 ERROR3("Can't get working directory");
241 return FALSE;
243 if (strstr(_strlwr(tchbuff), (TCHAR*) strFilename))
245 // change to upper dir (we should never attempt to delete the root directory!)
246 PathName path(strFilename);
247 if (_chdir((TCHAR*) String_256(path.GetLocation(FALSE))))
249 ERROR3("Can't change directory");
250 return FALSE;
253 // Try to remove it in the hope that it's empty
254 if (_rmdir((TCHAR*) strFilename) == -1)
256 if (errno == ENOTEMPTY || errno == EACCES)
258 _finddata_t findData;
259 String_256 strSearchPattern(strFilename);
260 strSearchPattern += chPathSep;
261 strSearchPattern += _T("*"); // add wildcard
262 INT32 hSearch = _findfirst(strSearchPattern, &findData);
263 if (hSearch == -1)
264 return FALSE;
267 if (!(strcmp(findData.name, _T(".")) && strcmp(findData.name, _T(".."))))
268 continue; // skip this directory (.) or its parent (..)
269 String_256 strFoundFile(strFilename);
270 strFoundFile += chPathSep;
271 strFoundFile += findData.name;
272 RemoveRecursively(strFoundFile);
274 while (_findnext(hSearch, &findData) == 0);
275 _findclose(hSearch);
276 return (_rmdir((TCHAR*) strFilename) != -1);
278 else
280 return FALSE; // probably invalid path
283 else
284 return TRUE; // succedded
286 else if (fileData.st_mode & _S_IFREG) // file
287 return (remove((TCHAR*) strFilename) != -1);
288 else
289 #endif
290 return FALSE;