cmtkFileUtils.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2388 $
00026 //
00027 //  $LastChangedDate: 2010-10-04 11:08:22 -0700 (Mon, 04 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkFileUtils.h"
00034 
00035 #include <string.h>
00036 #include <limits.h>
00037 #include <stdlib.h>
00038 
00039 #ifdef HAVE_UNISTD_H
00040 #  include <unistd.h>
00041 #endif
00042 
00043 #ifdef HAVE_SYS_TYPES_H
00044 #  include <sys/types.h>
00045 #endif
00046 
00047 #ifdef HAVE_SYS_STAT_H
00048 #  include <sys/stat.h>
00049 #endif
00050 
00051 #ifdef _MSC_VER
00052 #  include <direct.h>
00053 #endif
00054 
00055 namespace 
00056 cmtk
00057 {
00058 
00061 
00062 namespace
00063 FileUtils
00064 {
00065 
00066 int 
00067 RecursiveMkDir( const char *filename, const int permissions )
00068 {
00069   int result = RecursiveMkPrefixDir( filename, permissions );
00070   if ( result) return result;
00071 #ifdef _MSC_VER
00072   return _mkdir( filename );
00073 #else
00074   return mkdir( filename, permissions );
00075 #endif
00076 }
00077 
00078 int
00079 RecursiveMkPrefixDir
00080 ( const char *filename, const int permissions )
00081 {
00082   char prefix[PATH_MAX];
00083   struct stat buf;
00084   for ( unsigned i=0; filename[i]; ++i ) 
00085     {
00086     prefix[i] = filename[i];
00087     if ( (prefix[i] == CMTK_PATH_SEPARATOR) ) 
00088       {
00089       prefix[i+1] = 0;
00090       if ( stat( prefix, &buf ) != 0 ) 
00091         {
00092 #ifdef _MSC_VER
00093         int result = _mkdir( prefix );
00094 #else
00095         int result = mkdir( prefix, permissions );
00096 #endif
00097         if ( result ) return result;
00098         }
00099       }
00100     }
00101   return 0;
00102 }
00103 
00104 char* 
00105 GetAbsolutePath( char *absPath, const char* relPath )
00106 {
00107   if ( relPath[0] == CMTK_PATH_SEPARATOR )
00108     {
00109     strcpy( absPath, relPath );
00110     }
00111   else
00112     {
00113     getcwd( absPath, PATH_MAX );
00114     if ( absPath[ strlen( absPath )-1 ] != CMTK_PATH_SEPARATOR )
00115       strcat( absPath, CMTK_PATH_SEPARATOR_STR );
00116     
00117     strcat( absPath, relPath );
00118     }
00119   
00120   return absPath;
00121 }
00122 
00123 } // namespace FileUtils
00124 
00125 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines