|
| 1 | +!-----------------------------------------------------------------------------! |
| 2 | +! \file example/example_utils.f90 |
| 3 | +! \brief example_utils module |
| 4 | +! \note Copyright (c) 2019 Oak Ridge National Laboratory, UT-Battelle, LLC. |
| 5 | +!-----------------------------------------------------------------------------! |
| 6 | + |
| 7 | +module example_utils |
| 8 | + use, intrinsic :: ISO_FORTRAN_ENV |
| 9 | + use, intrinsic :: ISO_C_BINDING |
| 10 | + implicit none |
| 11 | + integer, parameter :: STDOUT = OUTPUT_UNIT, STDIN = INPUT_UNIT |
| 12 | + public |
| 13 | + |
| 14 | +contains |
| 15 | + |
| 16 | +subroutine write_version() |
| 17 | + use flc |
| 18 | + implicit none |
| 19 | + ! Print version information |
| 20 | + write(STDOUT, "(a)") "========================================" |
| 21 | + write(STDOUT, "(a, a)") "Flibcpp version: ", get_flibcpp_version() |
| 22 | + write(STDOUT, "(a, 2(i1,'.'), (i1), a)") "(Numeric version: ", & |
| 23 | + flibcpp_version_major, flibcpp_version_minor, flibcpp_version_patch, & |
| 24 | + ")" |
| 25 | + write(STDOUT, "(a)") "========================================" |
| 26 | +end subroutine |
| 27 | + |
| 28 | +! Loop until the user inputs a positive integer. Catch error conditions. |
| 29 | +function read_positive_int(desc) result(result_int) |
| 30 | + use flc |
| 31 | + use flc_string, only : stoi |
| 32 | + implicit none |
| 33 | + character(len=*), intent(in) :: desc |
| 34 | + character(len=80) :: readstr |
| 35 | + integer :: result_int, io_ierr |
| 36 | + do |
| 37 | + write(STDOUT, *) "Enter " // desc // ": " |
| 38 | + read(STDIN, "(a)", iostat=io_ierr) readstr |
| 39 | + if (io_ierr == IOSTAT_END) then |
| 40 | + ! Error condition: ctrl-D during input |
| 41 | + write(STDOUT, *) "User terminated" |
| 42 | + stop 1 |
| 43 | + endif |
| 44 | + |
| 45 | + result_int = stoi(readstr) |
| 46 | + if (ierr == 0) then |
| 47 | + if (result_int <= 0) then |
| 48 | + ! Error condition: non-positive value |
| 49 | + write(STDOUT, *) "Invalid " // desc // ": ", result_int |
| 50 | + continue |
| 51 | + end if |
| 52 | + |
| 53 | + write(STDOUT, *) "Read " // desc // "=", result_int |
| 54 | + exit |
| 55 | + endif |
| 56 | + |
| 57 | + if (ierr == SWIG_OVERFLOWERROR) then |
| 58 | + ! Error condition: integer doesn't fit in native integer |
| 59 | + write(STDOUT,*) "Your integer is too darn big!" |
| 60 | + else if (ierr == SWIG_VALUEERROR) then |
| 61 | + ! Error condition: not an integer at all |
| 62 | + write(STDOUT,*) "That text you entered? It wasn't an integer." |
| 63 | + else |
| 64 | + write(STDOUT,*) "Unknown error", ierr |
| 65 | + end if |
| 66 | + write(STDOUT,*) "(Detailed error message: ", get_serr(), ")" |
| 67 | + |
| 68 | + ! Clear error flag so the next call to stoi succeeds |
| 69 | + ierr = 0 |
| 70 | + end do |
| 71 | +end function |
| 72 | + |
| 73 | +! Loop until the user inputs a positive integer. Catch error conditions. |
| 74 | +subroutine read_strings(vec) |
| 75 | + use flc |
| 76 | + use flc_string, only : String |
| 77 | + use flc_vector, only : VectorString |
| 78 | + use ISO_FORTRAN_ENV |
| 79 | + implicit none |
| 80 | + type(VectorString), intent(out) :: vec |
| 81 | + integer, parameter :: STDOUT = OUTPUT_UNIT, STDIN = INPUT_UNIT |
| 82 | + character(len=80) :: readstr |
| 83 | + integer :: io_ierr |
| 84 | + type(String) :: str |
| 85 | + |
| 86 | + ! Allocate the vector |
| 87 | + vec = VectorString() |
| 88 | + |
| 89 | + do |
| 90 | + ! Request and read a string |
| 91 | + write(STDOUT, "(a, i3, a)") "Enter string #", vec%size() + 1, & |
| 92 | + " or Ctrl-D/empty string to complete" |
| 93 | + read(STDIN, "(a)", iostat=io_ierr) readstr |
| 94 | + if (io_ierr == IOSTAT_END) then |
| 95 | + ! Break out of loop on ^D (EOF) |
| 96 | + exit |
| 97 | + end if |
| 98 | + |
| 99 | + ! Add string to the end of the vector |
| 100 | + call vec%push_back(trim(readstr)) |
| 101 | + ! Get a String object reference to the back to check if it's empty |
| 102 | + str = vec%back_ref() |
| 103 | + if (str%empty()) then |
| 104 | + ! Remove the empty string |
| 105 | + call vec%pop_back() |
| 106 | + exit |
| 107 | + end if |
| 108 | + end do |
| 109 | +end subroutine |
| 110 | + |
| 111 | +end module |
| 112 | + |
| 113 | +!-----------------------------------------------------------------------------! |
| 114 | +! end of example/example_utils.f90 |
| 115 | +!-----------------------------------------------------------------------------! |
0 commit comments