Skip to content

Commit

Permalink
Expand fullfilename routine to better handle environment variables. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSagan authored Dec 23, 2024
1 parent 12d13bc commit ec94a40
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 44 deletions.
4 changes: 2 additions & 2 deletions regression_tests/README
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ than 0.01%

5) CMake setup for compiling/linking the program:
A) Add a "cmake.zzz" script in the regression_tests directory.
Use, say, the existing "cmake.twiss_track_test" file as a template. In the cmake.zzz
makefile substitute "zzz" for "twiss_track_test".
Use, say, the existing "cmake.twiss_track_test" file as a template.
In the cmake.zzz makefile substitute "zzz" for "twiss_track_test".
B) Edit the "CMakeLists.txt" in the regression_tests directory
and add "cmake.zzz" to the EXE_SPECS list.

Expand Down
13 changes: 7 additions & 6 deletions regression_tests/sim_utils_test/output.correct
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"Spline2" ABS 1E-10 4.89119141 3.05417392 0.00000000 0.00000000
"Spline3" ABS 1E-10 13.39405615 4.20134609 0.00000000 0.00000000
"Spline4" ABS 1E-10 27.53122777 5.11255684 0.00000000 0.00000000
"Spline5" ABS 1E-10 35.99999791 5.48872040 -0.00000000 -0.00000000
"Spline5" ABS 1E-10 35.99999791 5.48872040 0.00000000 0.00000000
"34pf12.3" STR "1(34pF12.3)"
"x" STR "1(0pX-1.-1)"
"(7(4pi17))" STR "7(4pI17.-1)"
Expand All @@ -76,14 +76,15 @@
"rot vecA" ABS 1E-14 -4.471674 2.284987 -1.944985
"drot vecB" ABS 1E-14 -8.88E-16 0.00E+00 4.44E-16
"drot vecC" ABS 1E-14 0.00E+00 0.00E+00 2.22E-16
"dRot0" ABS 1E-14 4.44E-16 -4.44E-16 0.00E+00
"dRot0" ABS 1E-14 0.00E+00 -4.44E-16 0.00E+00
"dRot1" ABS 1E-14 -1.11E-15 8.88E-16 -8.88E-16
"dRot2" ABS 1E-14 0.00E+00 8.88E-16 0.00E+00
"dRot2" ABS 1E-14 0.00E+00 1.33E-15 0.00E+00
"dRot3" ABS 1E-14 -2.22E-16 4.44E-16 -2.22E-15
"naff-freq1" REL 2E-6 7.53261723E-01 1.80201667E+00 1.99673895E-02
"naff-freq2" REL 2E-6 4.23577369E-01 3.00259151E-01 2.99279423E-01
"naff-freq3" REL 3E-6 1.73000375E-01 1.22833866E-02 1.54378002E-01
"naff-freq1" REL 2E-6 7.53261723E-01 1.80201666E+00 1.99673769E-02
"naff-freq2" REL 2E-6 4.23577368E-01 3.00259166E-01 2.99279413E-01
"naff-freq3" REL 3E-6 1.73000377E-01 1.22833540E-02 1.54378001E-01
"QuasiRan" ABS 0 4.3750000000E-01 1.8750000000E-01 8.1250000000E-01 4.3750000000E-01

"apply_energy_kick:0" REL 1E-12 9.069354381864E-01 1.001102639027E-01
"apply_energy_kick:1" REL 1E-12 9.729755708965E-01 1.153192339369E+00
"filename:1" STR "ZZZ/aaZZZzzZZZ"
6 changes: 6 additions & 0 deletions regression_tests/sim_utils_test/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import os
import sys

os.environ["Test_EV"] = "ZZZ"
exe = sys.argv[1] + 'sim_utils_test'
os.system(exe)
6 changes: 6 additions & 0 deletions regression_tests/sim_utils_test/sim_utils_test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ program math_test
logical match, ok, err

character(40) str, sub1, sub2, sub3
character(100) name
character(2) code
character(16) :: extrap(0:3) = [character(16):: 'ZERO', 'LINEAR', 'CONSTANT', 'SYMMETRIC']

Expand Down Expand Up @@ -324,6 +325,11 @@ program math_test
call apply_energy_kick (1d6, orbit, [0.0_rp, 0.0_rp])
write (1, '(a, 2es20.12)') '"apply_energy_kick:1" REL 1E-12 ', orbit%beta, orbit%vec(6)

! Note: run.py script that runs this program defines "Test_EV" environment variable to be "ZZZ"

call fullfilename("$Test_EV/aa${Test_EV}zz$Test_EV", name, ok)
write (1, '(2a)') '"filename:1" STR ', quote(name)

!

close(1)
Expand Down
88 changes: 53 additions & 35 deletions sim_utils/file/filename_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ module filename_mod
!+
! Subroutine FullFileName(filename, outfile, valid)
!
! Description:
! Returns the full filename with a leading environment variable expanded
! into the proper form (full path on Unix). NOTE: It is
! the responsibility of the calling routine to provide a large enough
! string to hold a fully expanded file name on return.
! Returns the full filename with environment variable expansion.
! NOTE: It is the responsibility of the calling routine to provide a large enough
! string to hold a fully expanded file name on return.
!
! Also see: SplitFileName
!
Expand All @@ -35,6 +33,7 @@ module filename_mod
! Filename outfile
! ----------------------- ---------------------------
! '$DUMMY/foo.bar' '/home/cesr/dummy/foo.bar'
! '${DUMMY}/foo.bar' '/home/cesr/dummy/foo.bar'
! '/home/cesr/dummy/foo.bar' '/home/cesr/dummy/foo.bar'
!-

Expand All @@ -49,7 +48,7 @@ subroutine FullFileName (filename, outfile, valid)
logical, optional :: valid

character(*), parameter :: r_name = 'FullFileName'
character(len(outfile)) expandname, name0 ! Expanded Name
character(len(outfile)+100) expandname, name0, ename ! Expanded Name

integer InLen, iDollar, iColon, iSlash
integer i, ix
Expand All @@ -73,20 +72,17 @@ subroutine FullFileName (filename, outfile, valid)
name0 = outfile
expandname = outfile

! Locate special characters (first dollar-sign, first colon, first slash)

iDollar = Index(name0(:InLen), '$' )
iColon = Index(name0(:InLen), ':' )
iSlash = Index(name0(:InLen), '/' )


!-----------------------------------------------------------------------
! Translation on WINDOWS (from unix to windows)

#if defined(CESR_WINCVF)

! A UNIX-style environment variable will have a leading '$'

iDollar = Index(name0(:InLen), '$' )
iColon = Index(name0(:InLen), ':' )
iSlash = Index(name0(:InLen), '/' )

if (iDollar == 1) then

! Expand Unix-style environment variable names
Expand Down Expand Up @@ -130,40 +126,62 @@ subroutine FullFileName (filename, outfile, valid)

! Tilde

if (name0(1:1) == '~') then
call GetEnv('HOME', expandname)
if (expandname == '') return
if (expandname(1:1) == '~') then
call GetEnv('HOME', ename)
iSlash = index(expandname, '/')

if (name0(2:2) == '/') then
expandname = trim(expandname) // name0(2:)
if (expandname(2:2) == '/' .or. (iSlash == 0 .and. len_trim(expandname) == 1)) then
expandname = trim(ename) // expandname(2:)
else
ix = index(expandname, '/', back = .true.)
if (ix == 0) then
expandname = trim(expandname) // '/' // name0
if (iSlash == 0) then
expandname = expandname(1:ix) // expandname
else
expandname = expandname(1:ix) // name0
ix = index(expandname, '/', back = .true.)
expandname = trim(ename) // expandname(iSlash:)
endif
endif
endif

! A UNIX-style environment variable will have a leading '$'

elseif (iDollar == 1) then
if (iSlash == 0) then

call GetEnv(name0(2:InLen), expandname)
if (len_trim(expandname) == 0) return
name0 = ''
do
iDollar = index(expandname, '$')

! Environment variable plus short name specifies the full name
if (iDollar == 0) then
name0 = trim(name0) // trim(expandname)
exit
endif

elseif (iSlash > 2) then
call GetEnv(name0(2:iSlash-1), expandname)
ExpLen = Len_Trim(expandname)
if (ExpLen == 0) return
expandname(ExpLen+1:) = name0(iSlash:InLen)
ix = max(iDollar-1, 1)
if (expandname(ix:ix) == '\') then ! '\'
name0 = trim(name0) // expandname(:iDollar)
expandname = expandname(iDollar+1:)
cycle
endif
endif

outfile = expandname
name0 = trim(name0) // expandname(:iDollar-1)
expandname = expandname(iDollar+1:)

if (expandname(1:1) == '{') then
ix = index(expandname, '}')
if (ix == 0) return
call GetEnv(expandname(2:ix-1), ename)
if (len_trim(ename) == 0) return
name0 = trim(name0) // trim(ename)
expandname = expandname(ix+1:)

else
ix = str_first_in_set(expandname, '/:')
if (ix == 0) ix = len_trim(expandname) + 1
call GetEnv(expandname(:ix-1), ename)
if (len_trim(ename) == 0) return
name0 = trim(name0) // trim(ename)
expandname = expandname(ix:)
endif
enddo

outfile = name0
if (present(valid)) valid = .true.

#endif
Expand Down
2 changes: 1 addition & 1 deletion tao/version/tao_version_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
!-

module tao_version_mod
character(*), parameter :: tao_version_date = "2024/12/13 00:15:40"
character(*), parameter :: tao_version_date = "2024/12/20 22:19:05"
end module

0 comments on commit ec94a40

Please sign in to comment.