pathnmex.cpp

Go to the documentation of this file.
00001 // $Id: pathnmex.cpp 836 2006-04-18 16:06:15Z gerry $
00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00003 ================================XARAHEADERSTART===========================
00004  
00005                Xara LX, a vector drawing and manipulation program.
00006                     Copyright (C) 1993-2006 Xara Group Ltd.
00007        Copyright on certain contributions may be held in joint with their
00008               respective authors. See AUTHORS file for details.
00009 
00010 LICENSE TO USE AND MODIFY SOFTWARE
00011 ----------------------------------
00012 
00013 This file is part of Xara LX.
00014 
00015 Xara LX is free software; you can redistribute it and/or modify it
00016 under the terms of the GNU General Public License version 2 as published
00017 by the Free Software Foundation.
00018 
00019 Xara LX and its component source files are distributed in the hope
00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License along
00025 with Xara LX (see the file GPL in the root directory of the
00026 distribution); if not, write to the Free Software Foundation, Inc., 51
00027 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00028 
00029 
00030 ADDITIONAL RIGHTS
00031 -----------------
00032 
00033 Conditional upon your continuing compliance with the GNU General Public
00034 License described above, Xara Group Ltd grants to you certain additional
00035 rights. 
00036 
00037 The additional rights are to use, modify, and distribute the software
00038 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00039 library and any other such library that any version of Xara LX relased
00040 by Xara Group Ltd requires in order to compile and execute, including
00041 the static linking of that library to XaraLX. In the case of the
00042 "CDraw" library, you may satisfy obligation under the GNU General Public
00043 License to provide source code by providing a binary copy of the library
00044 concerned and a copy of the license accompanying it.
00045 
00046 Nothing in this section restricts any of the rights you have under
00047 the GNU General Public License.
00048 
00049 
00050 SCOPE OF LICENSE
00051 ----------------
00052 
00053 This license applies to this program (XaraLX) and its constituent source
00054 files only, and does not necessarily apply to other Xara products which may
00055 in part share the same code base, and are subject to their own licensing
00056 terms.
00057 
00058 This license does not apply to files in the wxXtra directory, which
00059 are built into a separate library, and are subject to the wxWindows
00060 license contained within that directory in the file "WXXTRA-LICENSE".
00061 
00062 This license does not apply to the binary libraries (if any) within
00063 the "libs" directory, which are subject to a separate license contained
00064 within that directory in the file "LIBS-LICENSE".
00065 
00066 
00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00068 ----------------------------------------------
00069 
00070 Subject to the terms of the GNU Public License (see above), you are
00071 free to do whatever you like with your modifications. However, you may
00072 (at your option) wish contribute them to Xara's source tree. You can
00073 find details of how to do this at:
00074   http://www.xaraxtreme.org/developers/
00075 
00076 Prior to contributing your modifications, you will need to complete our
00077 contributor agreement. This can be found at:
00078   http://www.xaraxtreme.org/developers/contribute/
00079 
00080 Please note that Xara will not accept modifications which modify any of
00081 the text between the start and end of this header (marked
00082 XARAHEADERSTART and XARAHEADEREND).
00083 
00084 
00085 MARKS
00086 -----
00087 
00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00089 designs are registered or unregistered trademarks, design-marks, and/or
00090 service marks of Xara Group Ltd. All rights in these marks are reserved.
00091 
00092 
00093       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00094                         http://www.xara.com/
00095 
00096 =================================XARAHEADEREND============================
00097  */
00098 #include "camtypes.h"
00099 
00100 #include "camelot.h"
00101 #include "pathnmex.h"
00102 //#include "webster.h"
00103 
00104 #include <sys/types.h>
00105 #include <sys/stat.h>
00106 #include <stdio.h>
00107 #include <errno.h>
00108 #include <string.h>
00109 
00110 #if defined(__WXMSW__)
00111 #include <io.h>
00112 #include <direct.h>
00113 #include <process.h>
00114 
00115 const TCHAR             chPathSep = _T('\\');
00116 #else
00117 const TCHAR             chPathSep = _T('/');
00118 #endif
00119 
00120 /********************************************************************************************
00121 
00122   > BOOL PathNameEx::CreateLocation()
00123 
00124     Author:     Adrian_Stoicar (Xara Group Ltd) <camelotdev@xara.com>
00125     Created:    18/11/96
00126     Inputs:     none 
00127     Return:     TRUE if successful, FALSE otherwise
00128     Purpose:    Creates the full directory path (as returned by GetLocation()) 
00129                     on the physical medium. This is useful when creating new
00130                     directory structures.
00131              
00132     
00133 ********************************************************************************************/
00134 
00135 BOOL PathNameEx::CreateLocation()
00136 {
00137     PORTNOTETRACE("other","PathNameEx::CreateLocation - do nothing");
00138 #ifndef EXCLUDE_FROM_XARALX
00139     if (!IsValid())
00140         return FALSE;
00141     
00142     // We'll walk the location string from left to right - if we come across non-existent directories,
00143     // we create them
00144     String_256          strLocation = GetLocation(FALSE);
00145     String_256          strDirPath = drivename;
00146     INT32                   nPos =  drivename.Length(); // start after the drivename
00147     while( nPos < strLocation.Length() )
00148     {
00149         while( ( strLocation[nPos] != chPathSep ) && ( nPos < strLocation.Length() ) )
00150         {
00151             strDirPath += strLocation[nPos];
00152             nPos++;
00153         }
00154         // strDirPath has been added a directory, we check if it exists
00155         
00156         if (_access((TCHAR*) strDirPath, 0) == -1) // not found, try to create the directory
00157         {
00158             if (_mkdir((TCHAR*) strDirPath))
00159             {
00160 #ifdef _DEBUG
00161                 TCHAR szMsg[256];
00162                 TCHAR szError[128];
00163                 switch (errno)
00164                 {
00165                     case EACCES:
00166                         camStrcpy(szError, "access denied (EACCES)");
00167                         break;
00168                     case ENOENT:
00169                         camStrcpy(szError, "path not found (ENOENT)");
00170                         break;
00171                     default:
00172                         wsprintf(szError, "errno = %d", errno);
00173                 }
00174                 wsprintf(szMsg, "Create directory %s failed, %s", strDirPath, szError);
00175                 ERROR3(szMsg);
00176 #endif
00177                 return FALSE;
00178             }
00179         }
00180         strDirPath += chPathSep; // add a backslash in case there are further subdirectories  
00181         nPos++; // move to the next position
00182     }
00183 #endif
00184     return TRUE;
00185 }
00186 
00187 
00188 
00189 /********************************************************************************************
00190 
00191   > BOOL PathNameEx::RemoveRecursively()
00192 
00193     Author:     Adrian_Stoicar (Xara Group Ltd) <camelotdev@xara.com>
00194     Created:    18/11/96
00195     Inputs:     none 
00196     Return:     TRUE if the path is completely deleted, FALSE otherwise (access denied
00197                     or invalid parameters). In case some files cannot be removed, the function will
00198                     do its best to remove the all accessible ones without falling over, unlike the 
00199                     NT system call RMDIR which stops at the first file it can't delete.
00200     Purpose:    Removes a file or a whole directory tree from the the physical medium (in
00201                     which case, the object should be pointing to the root directory of the tree 
00202                     you want deleted)
00203              
00204     
00205 ********************************************************************************************/
00206 
00207 
00208 BOOL PathNameEx::RemoveRecursively(const String_256& rPath)
00209 {
00210     PORTNOTETRACE("other","PathNameEx::RemoveRecursively - do nothing");
00211 #ifndef EXCLUDE_FROM_XARALX
00212     String_256 strFilename(rPath);
00213     strFilename.toLower();
00214     // See if the path points to a file (the easy case) or a directory
00215     if (strFilename[strFilename.Length() - 1] == chPathSep)
00216     {
00217         strFilename.Remove(strFilename.Length() - 1, 1);
00218         goto DIRECTORY;
00219     }
00220     struct _stat fileData;
00221     if (_stat((TCHAR*) strFilename, &fileData))
00222     {
00223         if (errno == ENOENT)
00224         {
00225             ERROR3("Filename or path not found");
00226         }
00227         else
00228         {
00229             ERROR3("_stat() failed with an unknown error");
00230         }
00231         return FALSE;
00232     }
00233     if (fileData.st_mode & _S_IFDIR) // directory
00234     {
00235 DIRECTORY:
00236         // Make sure the directory is not the current one
00237         TCHAR tchbuff[_MAX_PATH];
00238         if (_getcwd(tchbuff, _MAX_PATH) == NULL)
00239         {
00240             ERROR3("Can't get working directory");
00241             return FALSE;
00242         }
00243         if (strstr(_strlwr(tchbuff), (TCHAR*) strFilename))
00244         {
00245             // change to upper dir (we should never attempt to delete the root directory!)
00246             PathName path(strFilename);
00247             if (_chdir((TCHAR*) String_256(path.GetLocation(FALSE))))
00248             {
00249                 ERROR3("Can't change directory");
00250                 return FALSE;
00251             }
00252         }
00253         // Try to remove it in the hope that it's empty
00254         if (_rmdir((TCHAR*) strFilename) == -1)
00255         {
00256             if (errno == ENOTEMPTY || errno == EACCES)
00257             {
00258                 _finddata_t findData;
00259                 String_256 strSearchPattern(strFilename);
00260                 strSearchPattern += chPathSep;
00261                 strSearchPattern += _T("*"); // add wildcard
00262                 INT32 hSearch = _findfirst(strSearchPattern, &findData);
00263                 if (hSearch == -1)
00264                     return FALSE;
00265                 do
00266                 {
00267                     if (!(strcmp(findData.name, _T(".")) &&  strcmp(findData.name, _T(".."))))
00268                         continue; // skip this directory (.) or its parent (..)
00269                     String_256 strFoundFile(strFilename);
00270                     strFoundFile += chPathSep; 
00271                     strFoundFile += findData.name;
00272                     RemoveRecursively(strFoundFile);
00273                 }
00274                 while (_findnext(hSearch, &findData) == 0);
00275                 _findclose(hSearch);
00276                 return (_rmdir((TCHAR*) strFilename) != -1);
00277             }
00278             else
00279             {
00280                 return FALSE; // probably invalid path
00281             }
00282         }
00283         else
00284             return TRUE; // succedded
00285     }
00286     else if (fileData.st_mode & _S_IFREG) // file
00287         return (remove((TCHAR*) strFilename) != -1);
00288     else
00289 #endif
00290         return FALSE;
00291 }
00292 
00293 
00294 

Generated on Sat Nov 10 03:48:46 2007 for Camelot by  doxygen 1.4.4