From 9dc730dea47548cf9f3d56f9afead8fb9c224a1f Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Tue, 21 Feb 2012 16:04:16 +0000 Subject: [PATCH] now getting path separator by counting slashes and backslashes in $PATH environment variable --- code/DAMASK_spectral_interface.f90 | 161 ++++++++++++++++++----------- 1 file changed, 102 insertions(+), 59 deletions(-) diff --git a/code/DAMASK_spectral_interface.f90 b/code/DAMASK_spectral_interface.f90 index 877b32767..d8f8b814c 100644 --- a/code/DAMASK_spectral_interface.f90 +++ b/code/DAMASK_spectral_interface.f90 @@ -21,12 +21,11 @@ !******************************************************************** MODULE DAMASK_interface - use prec, only: pInt, pReal implicit none character(len=64), parameter :: FEsolver = 'Spectral' character(len=5), parameter :: InputFileExtension = '.geom' - character(len=4), parameter :: LogFileExtension = '.log' !until now, we don't have a log file. But IO.f90 requires it + character(len=4), parameter :: LogFileExtension = '.log' !until now, we don't have a log file. But IO.f90 requires it character(len=1024) :: geometryParameter,loadcaseParameter CONTAINS @@ -35,19 +34,21 @@ CONTAINS ! !******************************************************************** subroutine DAMASK_interface_init() - use, intrinsic :: iso_fortran_env ! to get compiler_version and compiler_options (at least for gfortran 4.6 at the moment) + use, intrinsic :: iso_fortran_env ! to get compiler_version and compiler_options (at least for gfortran 4.6 at the moment) + use prec, only: pInt implicit none character(len=1024) commandLine, hostName, userName integer :: i, start = 0, length=0 - integer, dimension(8) :: date_and_time_values ! type default integer + integer, dimension(8) :: date_and_time_values ! type default integer call get_command(commandLine) call DATE_AND_TIME(VALUES=date_and_time_values) - do i=1,len(commandLine) ! remove capitals - if(640 .or. index(commandLine,' --help ',.true.)>0) then ! search for ' -h ' or '--help' + if(index(commandLine,' -h ',.true.)>0 .or. index(commandLine,' --help ',.true.)>0) then ! search for ' -h ' or '--help' write(6,*) '$Id$' #include "compilation_info.f90" print '(a)', '#############################################################' @@ -83,53 +84,62 @@ subroutine DAMASK_interface_init() print '(a)', ' --help' print '(a)', ' Prints this message and exits' print '(a)', ' ' - stop + call quit(0_pInt) endif - if (.not.(command_argument_count()==4 .or. command_argument_count()==6)) & ! check for correct number of given arguments (no --help) - stop 'Wrong Nr. of Arguments. Run DAMASK_spectral.exe --help' ! Could not find valid keyword (position 0 +3). Functions from IO.f90 are not available - start = index(commandLine,'-g',.true.) + 3 ! search for '-g' and jump to first char of geometry - if (index(commandLine,'--geom',.true.)>0) then ! if '--geom' is found, use that (contains '-g') + if (.not.(command_argument_count()==4 .or. command_argument_count()==6)) & ! check for correct number of given arguments (no --help) + stop 'Wrong Nr. of Arguments. Run DAMASK_spectral.exe --help' ! Could not find valid keyword (position 0 +3). Functions from IO.f90 are not available + start = index(commandLine,'-g',.true.) + 3 ! search for '-g' and jump to first char of geometry + if (index(commandLine,'--geom',.true.)>0) then ! if '--geom' is found, use that (contains '-g') start = index(commandLine,'--geom',.true.) + 7 endif - if (index(commandLine,'--geometry',.true.)>0) then ! again, now searching for --geometry' + if (index(commandLine,'--geometry',.true.)>0) then ! again, now searching for --geometry' start = index(commandLine,'--geometry',.true.) + 11 endif - if(start==3_pInt) stop 'No Geometry specified, terminating DAMASK'! Could not find valid keyword (position 0 +3). Functions from IO.f90 are not available + if(start==3_pInt) then ! Could not find valid keyword (position 0 +3). Functions from IO.f90 are not available + print '(a)', 'No Geometry specified' + call quit(9999) + endif length = index(commandLine(start:len(commandLine)),' ',.false.) - call get_command(commandLine) ! may contain capitals - geometryParameter = '' ! should be empty + call get_command(commandLine) ! may contain capitals + geometryParameter = '' ! should be empty geometryParameter(1:length)=commandLine(start:start+length) - - do i=1,len(commandLine) ! remove capitals - if(640) then ! if '--load' is found, use that (contains '-l') + start = index(commandLine,'-l',.true.) + 3 ! search for '-l' and jump forward iby 3 to given name + if (index(commandLine,'--load',.true.)>0) then ! if '--load' is found, use that (contains '-l') start = index(commandLine,'--load',.true.) + 7 endif - if (index(commandLine,'--loadcase',.true.)>0) then ! again, now searching for --loadcase' + if (index(commandLine,'--loadcase',.true.)>0) then ! again, now searching for --loadcase' start = index(commandLine,'--loadcase',.true.) + 11 endif - if(start==3_pInt) stop 'No Loadcase specified, terminating DAMASK'! Could not find valid keyword (position 0 +3). Functions from IO.f90 are not available + if(start==3_pInt) then ! Could not find valid keyword (position 0 +3). Functions from IO.f90 are not available + print '(a)', 'No Loadcase specified' + call quit(9999) + endif length = index(commandLine(start:len(commandLine)),' ',.false.) - - call get_command(commandLine) ! may contain capitals - loadcaseParameter = '' ! should be empty + + call get_command(commandLine) ! may contain capitals + loadcaseParameter = '' ! should be empty loadcaseParameter(1:length)=commandLine(start:start+length) - do i=1,len(commandLine) ! remove capitals - if(640) then ! if '--restart' is found, use that (contains '-l') + start = index(commandLine,'-r',.true.) + 3 ! search for '-r' and jump forward iby 3 to given name + if (index(commandLine,'--restart',.true.)>0) then ! if '--restart' is found, use that (contains '-l') start = index(commandLine,'--restart',.true.) + 7 endif length = index(commandLine(start:len(commandLine)),' ',.false.) - call get_command(commandLine) ! may contain capitals + call get_command(commandLine) ! may contain capitals call GET_ENVIRONMENT_VARIABLE('HOST',hostName) call GET_ENVIRONMENT_VARIABLE('USER',userName) @@ -145,6 +155,7 @@ subroutine DAMASK_interface_init() date_and_time_values(7) write(6,*) 'Host Name: ', trim(hostName) write(6,*) 'User Name: ', trim(userName) + write(6,*) 'Path Separator: ', getPathSep() write(6,*) 'Command line call: ', trim(commandLine) write(6,*) 'Geometry Parameter: ', trim(geometryParameter) write(6,*) 'Loadcase Parameter: ', trim(loadcaseParameter) @@ -158,17 +169,18 @@ endsubroutine DAMASK_interface_init !******************************************************************** function getSolverWorkingDirectoryName() - use prec, only: pInt implicit none character(len=1024) cwd,getSolverWorkingDirectoryName - character(len=*), parameter :: pathSep = achar(47) //achar(92) !forwardslash, backwardslash + character :: pathSep - if (scan(geometryParameter,pathSep) == 1) then ! absolute path given as command line argument + pathSep = getPathSep() + + if (geometryParameter(1:1) == pathSep) then ! absolute path given as command line argument getSolverWorkingDirectoryName = geometryParameter(1:scan(geometryParameter,pathSep,back=.true.)) else call getcwd(cwd) - getSolverWorkingDirectoryName = trim(cwd)//'/'//geometryParameter(1:scan(geometryParameter,pathSep,back=.true.)) + getSolverWorkingDirectoryName = trim(cwd)//pathSep//geometryParameter(1:scan(geometryParameter,pathSep,back=.true.)) endif getSolverWorkingDirectoryName = rectifyPath(getSolverWorkingDirectoryName) @@ -200,16 +212,17 @@ function getModelName() implicit none character(1024) getModelName, cwd - character(len=*), parameter :: pathSep = achar(47)//achar(92) ! forwardslash, backwardslash integer :: posExt,posSep - + character :: pathSep + + pathSep = getPathSep() posExt = scan(geometryParameter,'.',back=.true.) posSep = scan(geometryParameter,pathSep,back=.true.) - if (posExt <= posSep) posExt = len_trim(geometryParameter)+1 ! no extension present - getModelName = geometryParameter(1:posExt-1_pInt) ! path to geometry file (excl. extension) + if (posExt <= posSep) posExt = len_trim(geometryParameter)+1 ! no extension present + getModelName = geometryParameter(1:posExt-1_pInt) ! path to geometry file (excl. extension) - if (scan(getModelName,pathSep) /= 1) then ! relative path given as command line argument + if (scan(getModelName,pathSep) /= 1) then ! relative path given as command line argument call getcwd(cwd) getModelName = rectifyPath(trim(cwd)//'/'//getModelName) else @@ -230,9 +243,10 @@ function getLoadCase() implicit none character(1024) :: getLoadCase - character(len=*), parameter :: pathSep = achar(47)//achar(92) ! forwardslash, backwardslash integer :: posExt,posSep + character :: pathSep + pathSep = getPathSep() posExt = scan(loadcaseParameter,'.',back=.true.) posSep = scan(loadcaseParameter,pathSep,back=.true.) @@ -248,13 +262,13 @@ endfunction getLoadCase !******************************************************************** function getLoadcaseName() - use prec, only: pInt - implicit none character(len=1024) :: getLoadcaseName,cwd - character(len=*), parameter :: pathSep = achar(47)//achar(92) ! forwardslash, backwardslash integer :: posExt = 0, posSep + character :: pathSep + + pathSep = getPathSep() getLoadcaseName = loadcaseParameter posExt = scan(getLoadcaseName,'.',back=.true.) posSep = scan(getLoadcaseName,pathSep,back=.true.) @@ -262,7 +276,7 @@ function getLoadcaseName() if (posExt <= posSep) getLoadcaseName = trim(getLoadcaseName)//('.load') ! no extension present if (scan(getLoadcaseName,pathSep) /= 1) then ! relative path given as command line argument call getcwd(cwd) - getLoadcaseName = rectifyPath(trim(cwd)//'/'//getLoadcaseName) + getLoadcaseName = rectifyPath(trim(cwd)//pathSep//getLoadcaseName) else getLoadcaseName = rectifyPath(getLoadcaseName) endif @@ -278,67 +292,96 @@ endfunction getLoadcaseName !******************************************************************** function rectifyPath(path) - use prec, only: pInt - implicit none character(len=*) :: path character(len=len_trim(path)) :: rectifyPath + character :: pathSep integer :: i,j,k,l !no pInt + pathSep = getPathSep() + !remove ./ from path l = len_trim(path) rectifyPath = path do i = l,3,-1 - if ( rectifyPath(i-1:i) == './' .and. rectifyPath(i-2:i-2) /= '.' ) & + if ( rectifyPath(i-1:i) == '.'//pathSep .and. rectifyPath(i-2:i-2) /= '.' ) & rectifyPath(i-1:l) = rectifyPath(i+1:l)//' ' enddo !remove ../ and corresponding directory from rectifyPath l = len_trim(rectifyPath) - i = index(rectifyPath(i:l),'../') + i = index(rectifyPath(i:l),'..'//pathSep) j = 0 do while (i > j) - j = scan(rectifyPath(1:i-2),'/',back=.true.) + j = scan(rectifyPath(1:i-2),pathSep,back=.true.) rectifyPath(j+1:l) = rectifyPath(i+3:l)//repeat(' ',2+i-j) - if (rectifyPath(j+1:j+1) == '/') then !search for '//' that appear in case of XXX/../../XXX + if (rectifyPath(j+1:j+1) == pathSep) then !search for '//' that appear in case of XXX/../../XXX k = len_trim(rectifyPath) rectifyPath(j+1:k-1) = rectifyPath(j+2:k) rectifyPath(k:k) = ' ' endif - i = j+index(rectifyPath(j+1:l),'../') + i = j+index(rectifyPath(j+1:l),'..'//pathSep) enddo - if(len_trim(rectifyPath) == 0) rectifyPath = '/' + if(len_trim(rectifyPath) == 0) rectifyPath = pathSep end function rectifyPath - !******************************************************************** ! relative path from absolute a to absolute b ! !******************************************************************** function makeRelativePath(a,b) - use prec, only: pInt - implicit none character (len=*) :: a,b character (len=1024) :: makeRelativePath + character :: pathSep integer :: i,posLastCommonSlash,remainingSlashes !no pInt + pathSep = getPathSep() posLastCommonSlash = 0 remainingSlashes = 0 + do i = 1, min(1024,len_trim(a),len_trim(b)) if (a(i:i) /= b(i:i)) exit - if (a(i:i) == '/') posLastCommonSlash = i + if (a(i:i) == pathSep) posLastCommonSlash = i enddo do i = posLastCommonSlash+1,len_trim(a) - if (a(i:i) == '/') remainingSlashes = remainingSlashes + 1 + if (a(i:i) == pathSep) remainingSlashes = remainingSlashes + 1 enddo - makeRelativePath = repeat('../',remainingSlashes)//b(posLastCommonSlash+1:len_trim(b)) + makeRelativePath = repeat('..'//pathSep,remainingSlashes)//b(posLastCommonSlash+1:len_trim(b)) endfunction makeRelativePath + +!******************************************************************** +! counting / and \ in $PATH System variable +! the character occuring more often is assumed to be the path separator +!******************************************************************** +function getPathSep() + + use prec, only: pInt + implicit none + character :: getPathSep + character(len=2048) path + integer(pInt) :: backslash = 0_pInt, slash = 0_pInt + integer :: i + + call get_environment_variable('PATH',path) + do i=1, len(trim(path)) + if (path(i:i)=='/') slash = slash + 1_pInt + if (path(i:i)=='\') backslash = backslash + 1_pInt + enddo + + if (backslash>slash) then + getPathSep = '\' + else + getPathSep = '/' + endif + +end function + END MODULE