Sometimes it is useful or necessary to write execution conditions based on compiler vendors or version, such as when a certain compiler version has a known bug or when a piece of code is required for a specific compiler. The following program shows how one may use the count procedure to determine a compiler and version at runtime:
program compiler_check
use, intrinsic :: iso_fortran_env, only: compiler_version
use io_fortran_lib, only: String
implicit none (type, external)
type(String) :: compiler
logical :: GCC, GCC_SUPPORTED, INTEL, INTEL_SUPPORTED
compiler = String(compiler_version())
GCC = compiler%count(match="GCC") > 0
GCC_SUPPORTED = GCC .and. ( compiler%count(match="16.") > 0 )
INTEL = compiler%count(match="Intel") > 0
INTEL_SUPPORTED = INTEL .and. ( compiler%count(match="2026.") > 0 )
write(*,*) compiler
if ( GCC ) then
if ( GCC_SUPPORTED ) then
write(*,*) "This GNU Fortran Compiler version is known to be supported."
else
write(*,*) "This GNU Fortran Compiler version may not be supported."
end if
else if ( INTEL ) then
if ( INTEL_SUPPORTED ) then
write(*,*) "This Intel Fortran Compiler version is known to be supported."
else
write(*,*) "This Intel Fortran Compiler version may not be supported."
end if
end if
end program compiler_check
Using operator techniques and string methods, we may easily perform complex string manipulations during run time.
The following program demonstrates the use of the String type and some type-bound procedures for manipulating a timestamp:
program split_join
use io_fortran_lib, only: String, join, split, IOFL_LF, operator(+), operator(-)
implicit none (type, external)
type(String) :: time_stamp, new_time_stamp
type(String), allocatable :: tokens(:)
character(len=10) :: date, time
call date_and_time(date=date, time=time)
time_stamp = String("Date : " + date + IOFL_LF + "Time : " + time)
write(*,"(a)") "ORIGINAL TIME STAMP:" + IOFL_LF + time_stamp%as_str() + IOFL_LF
tokens = split(time_stamp, separator=IOFL_LF) - "Date : " - "Time : " + [" : Date", " : Time"]
new_time_stamp = join(tokens, separator=" | ")
write(*,"(a)") "RECONSTRUCTED TIME STAMP:" + IOFL_LF + new_time_stamp%as_str()
end program split_join
This program produces the following sample output:
ORIGINAL TIME STAMP:
Date : 20240316
Time : 163641.569
RECONSTRUCTED TIME STAMP:
20240316 : Date | 163641.569 : Time