Fortran90ã§ä»»æã®ãã¡ã¤ã«å(æåå)ããæ¡å¼µåãåå¾ãããµãã«ã¼ãã³ãä½ãã¾ãããã¡ãã£ã¨ã ãã°ã°ã£ã¦ããããããã®ãç¡ãã£ãã®ã§ã
ã¢ã¸ã¥ã¼ã«ã®å½¢ã«ãã¦å¥ãã¡ã¤ã«ã«ãã¦ãã¾ãã
extension.f90
module m_extension implicit none public parse_filename integer(kind=8), parameter :: NAME_LEN = 80 integer(kind=8), parameter :: EXT_LEN = 80 contains subroutine parse_filename(name, base, ext) character(len=NAME_LEN), intent(in) :: name character(len=NAME_LEN), intent(out) :: base character(len=EXT_LEN), intent(out) :: ext integer(kind=8) :: idot integer(kind=8) :: name_len idot = index(name, ".", back=.true.) name_len = len_trim(name) if( idot == 0) then base = name elseif( idot > 1) then base = name(1:idot-1) else base = "" endif if ( idot /= 0) then ext = name(idot+1:name_len) else ext = "" endif end subroutine parse_filename end module m_extension
main.f90 (ãã¹ãç¨ã®ã¡ã¤ã³ã«ã¼ãã³ã§ãã)
program main use m_extension implicit none character(len=NAME_LEN) :: filename character(len=NAME_LEN) :: basename character(len=EXT_LEN) :: extension ! test 1 write(*,*) "# test 1" filename = "sample.hoge.txt" write(*,*) "filename = ", trim(filename) write(*,*) "## parse_filename" call parse_filename(filename, basename, extension) write(*,*) "basename = ", trim(basename) write(*,*) "extension = ", trim(extension) ! test 2 write(*,*) "# test 2" filename = "sample" write(*,*) "filename = ", trim(filename) write(*,*) "## parse_filename" call parse_filename(filename, basename, extension) write(*,*) "basename = ", trim(basename) write(*,*) "extension = ", trim(extension) end program main
FC=ifort FCFLAGS=-O3 LDFLAGS=-I. OBJS=m_extension.o MODULES=m_extension.mod main: main.f90 $(OBJS) $(FC) $(FCFLAGS) -o $@ $< $(OBJS) .SUFFIXES: .SUFFIXES: .f90 .o .mod .f90.o: $(FC) $(FCFLAGS) -c -o $@ $< .f90.mod: $(FC) $(FCFLAGS) -c -o $@ $< clean: $(RM) -f main $(OBJS) $(MODULES)