[2089] | 1 | !OPTIONS NOOPT |
---|
| 2 | MODULE strhandler |
---|
| 3 | |
---|
| 4 | #include "tsmbkind.h" |
---|
| 5 | |
---|
| 6 | IMPLICIT NONE |
---|
| 7 | |
---|
| 8 | PRIVATE |
---|
| 9 | |
---|
| 10 | PUBLIC :: tolower, toupper, expand_string |
---|
| 11 | PUBLIC :: sadjustl, sadjustr |
---|
| 12 | |
---|
| 13 | CONTAINS |
---|
| 14 | |
---|
| 15 | FUNCTION sadjustl(s) RESULT(c) |
---|
| 16 | |
---|
| 17 | character(len=*), intent(in) :: s |
---|
| 18 | character(len=max(1,len(s))) c |
---|
| 19 | c = ' ' |
---|
| 20 | if (len(s) > 0) then |
---|
| 21 | if (s /= ' ') c = adjustl(s) |
---|
| 22 | endif |
---|
| 23 | END FUNCTION sadjustl |
---|
| 24 | |
---|
| 25 | FUNCTION sadjustr(s) RESULT(c) |
---|
| 26 | |
---|
| 27 | character(len=*), intent(in) :: s |
---|
| 28 | character(len=max(1,len(s))) c |
---|
| 29 | c = ' ' |
---|
| 30 | if (len(s) > 0) then |
---|
| 31 | if (s /= ' ') c = adjustr(s) |
---|
| 32 | endif |
---|
| 33 | END FUNCTION sadjustr |
---|
| 34 | |
---|
| 35 | SUBROUTINE tolower(cds) |
---|
| 36 | |
---|
| 37 | character(len=*), intent(inout) :: cds |
---|
| 38 | INTEGER_M, parameter :: ich_a = ichar('a') |
---|
| 39 | INTEGER_M, parameter :: ichA = ichar('A') |
---|
| 40 | INTEGER_M, parameter :: ichZ = ichar('Z') |
---|
| 41 | INTEGER_M :: i, ich, new_ich |
---|
| 42 | character(len=1) ch |
---|
| 43 | do i=1,len(cds) |
---|
| 44 | ch = cds(i:i) |
---|
| 45 | ich = ichar(ch) |
---|
| 46 | if ( ich >= ichA .and. ich <= ichZ ) then |
---|
| 47 | new_ich = ich + (ich_a - ichA) |
---|
| 48 | ch = char(new_ich) |
---|
| 49 | cds(i:i) = ch |
---|
| 50 | endif |
---|
| 51 | enddo |
---|
| 52 | END SUBROUTINE tolower |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | SUBROUTINE toupper(cds) |
---|
| 56 | |
---|
| 57 | character(len=*), intent(inout) :: cds |
---|
| 58 | INTEGER_M, parameter :: ich_A = ichar('A') |
---|
| 59 | INTEGER_M, parameter :: icha = ichar('a') |
---|
| 60 | INTEGER_M, parameter :: ichz = ichar('z') |
---|
| 61 | INTEGER_M :: i, ich, new_ich |
---|
| 62 | character(len=1) ch |
---|
| 63 | do i=1,len(cds) |
---|
| 64 | ch = cds(i:i) |
---|
| 65 | ich = ichar(ch) |
---|
| 66 | if ( ich >= icha .and. ich <= ichz ) then |
---|
| 67 | new_ich = ich + (ich_A - icha) |
---|
| 68 | ch = char(new_ich) |
---|
| 69 | cds(i:i) = ch |
---|
| 70 | endif |
---|
| 71 | enddo |
---|
| 72 | END SUBROUTINE toupper |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | SUBROUTINE expand_string(& |
---|
| 76 | &myproc, &! %p |
---|
| 77 | &nproc, &! %n |
---|
| 78 | ×tep, &! %t |
---|
| 79 | &max_timestep,& |
---|
| 80 | &s) ! %s |
---|
| 81 | |
---|
| 82 | INTEGER_M, intent(in) :: myproc, nproc |
---|
| 83 | INTEGER_M, intent(in) :: timestep, max_timestep |
---|
| 84 | character(len=*), intent(inout) :: s(:) |
---|
| 85 | character(len=2*len(s)) t |
---|
| 86 | character(len=2*len(s)) tt |
---|
| 87 | INTEGER_M :: i, j, jj, loc_p, len_t, n |
---|
| 88 | INTEGER_M :: ndigs(4), num(4) |
---|
| 89 | character(len=6) fmt(4) |
---|
| 90 | |
---|
| 91 | n = size(s) |
---|
| 92 | |
---|
| 93 | if (n < 1) return |
---|
| 94 | |
---|
| 95 | !* Setup output formats |
---|
| 96 | num(1) = myproc |
---|
| 97 | num(2) = max(nproc,myproc) |
---|
| 98 | num(3) = n |
---|
| 99 | num(4) = max(max_timestep,timestep) |
---|
| 100 | |
---|
| 101 | !* Count number of digits in each integer |
---|
| 102 | do j=1,4 |
---|
| 103 | ndigs(j) = 1 |
---|
| 104 | if (num(j) /= 0) then |
---|
| 105 | ndigs(j) = 1 + log10(dble(abs(num(j)))) |
---|
| 106 | if (num(j) < 0) ndigs(j) = ndigs(j) + 1 ! Room for minus sign |
---|
| 107 | endif |
---|
| 108 | ndigs(j) = min(int(ndigs(j)),9) ! Max 9 digits supported; i.e. '999999999' |
---|
| 109 | write(fmt(j),'("(i",i1,")")') ndigs(j) |
---|
| 110 | enddo |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | !* Expand fields '%s', '%p', '%n' and '%t' with their values |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | !* A special treatment with the sequence numbering |
---|
| 117 | if (n>1) then |
---|
| 118 | loc_p = index(s(1),'%s') |
---|
| 119 | if (loc_p > 0) then |
---|
| 120 | s(2:) = s(1) |
---|
| 121 | endif |
---|
| 122 | endif |
---|
| 123 | |
---|
| 124 | do i=1,n |
---|
| 125 | t = adjustl(s(i))//' ' |
---|
| 126 | loc_p = index(t,'%') |
---|
| 127 | |
---|
| 128 | if (loc_p > 0) then |
---|
| 129 | len_t = len_trim(t) |
---|
| 130 | j = loc_p |
---|
| 131 | tt(:j-1) = t(:j-1) |
---|
| 132 | tt(j:) = ' ' |
---|
| 133 | jj = j-1 |
---|
| 134 | |
---|
| 135 | do while (j <= len_t) |
---|
| 136 | if (t(j:j) == '%') then |
---|
| 137 | j = j + 1 |
---|
| 138 | if (j <= len_t) then |
---|
| 139 | select case ( t(j:j) ) |
---|
| 140 | case ( 'p' ) ! myproc |
---|
| 141 | write(tt(jj+1:jj+ndigs(1)),fmt(1)) myproc |
---|
| 142 | jj = jj + ndigs(1) |
---|
| 143 | case ( 'n' ) ! nproc |
---|
| 144 | write(tt(jj+1:jj+ndigs(2)),fmt(2)) nproc |
---|
| 145 | jj = jj + ndigs(2) |
---|
| 146 | case ( 's' ) ! sequence number i=[1..n] |
---|
| 147 | write(tt(jj+1:jj+ndigs(3)),fmt(3)) i |
---|
| 148 | jj = jj + ndigs(3) |
---|
| 149 | case ( 't' ) ! timestep |
---|
| 150 | write(tt(jj+1:jj+ndigs(4)),fmt(4)) timestep |
---|
| 151 | jj = jj + ndigs(4) |
---|
| 152 | case default |
---|
| 153 | tt(jj+1:jj+2) = '%'//t(j:j) |
---|
| 154 | jj = jj + 2 |
---|
| 155 | end select |
---|
| 156 | else |
---|
| 157 | tt(jj+1:jj+1) = '%' |
---|
| 158 | jj = jj + 1 |
---|
| 159 | endif |
---|
| 160 | else |
---|
| 161 | tt(jj+1:jj+1) = t(j:j) |
---|
| 162 | jj = jj + 1 |
---|
| 163 | endif |
---|
| 164 | j = j + 1 |
---|
| 165 | enddo |
---|
| 166 | |
---|
| 167 | t = adjustl(tt) |
---|
| 168 | |
---|
| 169 | !* Get also rid of any blanks in the middle of the string |
---|
| 170 | |
---|
| 171 | len_t = len_trim(t) |
---|
| 172 | j = 1 |
---|
| 173 | do while (j < len_t) |
---|
| 174 | if (t(j:j) == ' ') then |
---|
| 175 | t(j:) = t(j+1:) |
---|
| 176 | len_t = len_trim(t) |
---|
| 177 | else |
---|
| 178 | j = j + 1 |
---|
| 179 | endif |
---|
| 180 | enddo |
---|
| 181 | |
---|
| 182 | endif |
---|
| 183 | |
---|
| 184 | s(i) = t |
---|
| 185 | enddo |
---|
| 186 | |
---|
| 187 | END SUBROUTINE expand_string |
---|
| 188 | |
---|
| 189 | END MODULE strhandler |
---|