ETS-Core  version:0.0.4-46-ge2d8
Core actors for the ETS-6
 All Classes Files Functions Variables Pages
stringtools.f90
Go to the documentation of this file.
1 module stringtools
2 
3  use iso_c_binding, only: c_double
4 
5  implicit none
6 
8  interface num2str
9  module procedure num2str_int, num2str_real, &
11  end interface num2str
12 
15  interface alloc2str
17  end interface alloc2str
18 
21  interface pointer2str
23  end interface pointer2str
24 
25  interface append_string
27  end interface append_string
28 
29 contains
30 
31  function num2str_int(arg) result(str)
32  integer, intent(in) :: arg
33  character(:), allocatable :: str
34  character(64) :: stmp
35  write(stmp,*)arg
36  str=trim(adjustl(stmp))
37  end function num2str_int
38 
39  function num2str_real(arg) result(str)
40  real(c_double), intent(in) :: arg
41  character(:), allocatable :: str
42  character(64) :: stmp
43  write(stmp,*)arg
44  str=trim(adjustl(stmp))
45  end function num2str_real
46 
47  function num2str_int_array(arg,separator) result(str)
48  integer, intent(in) :: arg(:)
49  character(*), intent(in), optional :: separator
50  character(:), allocatable :: str
51  integer :: j
52  str=""
53  do j=1,size(arg)
54  if (j > 1) then
55  if (present(separator)) then
56  str = str//separator
57  else
58  str = str//" "
59  end if
60  end if
61  str=str//num2str_int(arg(j))
62  end do
63  end function num2str_int_array
64 
65  function num2str_real_array(arg,separator) result(str)
66  real(c_double), intent(in) :: arg(:)
67  character(*), intent(in), optional :: separator
68  character(:), allocatable :: str
69  ! Internal
70  integer :: j
71  str=""
72  do j=1,size(arg)
73  if (j > 1) then
74  if (present(separator)) then
75  str = str//separator
76  else
77  str = str//" "
78  end if
79  end if
80  str=str//num2str_real(arg(j))
81  end do
82  end function num2str_real_array
83 
84 
85  function alloc2str_str(arg) result(str)
86  character(:), allocatable, intent(in) :: arg
87  character(:), allocatable :: str
88  if (allocated(arg)) then
89  str = arg
90  else
91  str = ""
92  end if
93  end function alloc2str_str
94 
95  function alloc2str_int_array(arg,separator) result(str)
96  integer, allocatable, intent(in) :: arg(:)
97  character(*), intent(in), optional :: separator
98  character(:), allocatable :: str
99  character(16) :: stmp
100  if (allocated(arg)) then
101  if (present(separator)) then
102  str = num2str_int_array(arg,separator)
103  else
104  str = num2str_int_array(arg)
105  end if
106  else
107  str = ""
108  end if
109  end function alloc2str_int_array
110 
111  function alloc2str_real_array(arg,separator) result(str)
112  real(c_double), allocatable, intent(in) :: arg(:)
113  character(*), intent(in), optional :: separator
114  character(:), allocatable :: str
115  if (allocated(arg)) then
116  if (present(separator)) then
117  str = num2str_real_array(arg,separator)
118  else
119  str = num2str_real_array(arg)
120  end if
121  else
122  str = ""
123  end if
124  end function alloc2str_real_array
125 
126 
127  function pointer2str_str(arg) result(str)
128  character(:), pointer, intent(in) :: arg
129  character(:), allocatable :: str
130  if (associated(arg)) then
131  str = arg
132  else
133  str = ""
134  end if
135  end function pointer2str_str
136 
137  function pointer2str_str_array(arg) result(str)
138  character(132), pointer, intent(in) :: arg(:)
139  character(:), allocatable :: str
140  integer :: ind
141  if (associated(arg)) then
142  str = ""
143  do ind=1,size(arg)
144  str = str//trim(arg(ind))
145  end do
146  else
147  str = ""
148  end if
149  end function pointer2str_str_array
150 
151  function pointer2str_int_array(arg,separator) result(str)
152  integer, pointer, intent(in) :: arg(:)
153  character(*), intent(in), optional :: separator
154  character(:), allocatable :: str
155  character(16) :: stmp
156  if (associated(arg)) then
157  if (present(separator)) then
158  str = num2str_int_array(arg,separator)
159  else
160  str = num2str_int_array(arg)
161  end if
162  else
163  str = ""
164  end if
165  end function pointer2str_int_array
166 
167  function pointer2str_real_array(arg,separator) result(str)
168  real(c_double), pointer, intent(in) :: arg(:)
169  character(*), intent(in), optional :: separator
170  character(:), allocatable :: str
171  if (associated(arg)) then
172  if (present(separator)) then
173  str = num2str_real_array(arg,separator)
174  else
175  str = num2str_real_array(arg)
176  end if
177  else
178  str = ""
179  end if
180  end function pointer2str_real_array
181 
182  function pointer2str_real_2darray(arg,separator) result(str)
183  real(c_double), pointer, intent(in) :: arg(:,:)
184  character(*), intent(in), optional :: separator
185  character(:), allocatable :: str
186  integer :: ind
187  str = ""
188  if (associated(arg)) then
189  do ind=1,size(arg,2)
190  if (present(separator)) then
191  if (ind==1) then
192  str = num2str_real_array(arg(:,ind),separator)
193  else
194  str = str//separator//num2str_real_array(arg(:,ind),separator)
195  end if
196  else
197  str = str//num2str_real_array(arg(:,ind))
198  end if
199  end do
200  end if
201  end function pointer2str_real_2darray
202 
203 
204  subroutine string_to_char132pointer(in,out)
205  character(*), intent(in) :: in
206  character(132), pointer, intent(out) :: out(:)
207 
208  ! Internal
209  integer :: nout, jout, jcount, jend
210 
211  nout = ceiling( real(len(in)) / real(132) )
212  allocate(out(nout))
213  jcount = 0
214  do jout=1,nout
215  jend = min( jcount+132 , len(in) )
216  out(jout) = in(jcount+1:jend)
217  end do
218  end subroutine string_to_char132pointer
219 
220 
221  subroutine char132pointer_to_string(out,in,linebreak)
222  character(*), intent(out) :: out
223  character(132), pointer, intent(in) :: in(:)
224  character(*), intent(in) :: linebreak
225 
226  ! Internal
227  integer :: count, step
228  integer :: j
229 
230  out=''
231  if (.not. associated(in)) return
232 
233  count = 0
234  loop_lines: do j=1,size(in)
235  if (count < len(out)) then
236  step = len(trim(in(j))) + len(linebreak)
237  if (count + step < len(out)) then
238  out(count+1:count+step) = trim(in(j)) // linebreak
239  count = count + step
240  elseif (count + len(trim(in(j))) + 3 < len(out)) then
241  step = len(trim(in(j)) // '...')
242  out(count+1:count+step) = trim(in(j)) // '...'
243  exit loop_lines
244  else
245  step = len(out) - count
246  out(count+1:count+step) = in(j)(1:step-3) // '...'
247  exit loop_lines
248  end if
249  end if
250  end do loop_lines
251  end subroutine char132pointer_to_string
252 
253  subroutine append_string_to_string(outString,valueString,separator)
254  character(*), intent(inout) :: outstring
255  character(*), intent(in) :: valuestring
256  character(*), intent(in) :: separator
257  character(:), allocatable :: tmp
258  integer :: string_len
259 
260  string_len = len( trim(outstring)//separator//trim(valuestring) )
261  allocate(character(string_len) :: tmp)
262  tmp = trim(outstring)//separator//trim(valuestring)
263  if (len(tmp) > len(outstring)) then
264  outstring = tmp(1:len(outstring))
265  else
266  outstring = tmp
267  end if
268  deallocate(tmp)
269  end subroutine append_string_to_string
270 
271 
272  recursive subroutine append_to_char132pointer(outString,valueString)
273  character(*), intent(in) :: valuestring
274  character(132), pointer, intent(inout) :: outstring(:)
275 
276  ! Internal
277  character(132), pointer :: tmpstring(:)
278  integer :: count
279  integer :: step
280 
281  if (len(valuestring) > 132) then
282  count = 0
283  do while (count < len(valuestring))
284  step = min(132 , len(valuestring)-count)
285  call append_to_char132pointer(outstring, valuestring(count+1:count+step))
286  count = count + step
287  end do
288  return
289  end if
290 
291  if (associated(outstring)) then
292  ! Resize outString
293  allocate(tmpstring(size(outstring)))
294  tmpstring = outstring
295  deallocate(outstring)
296  allocate(outstring(size(tmpstring)+1))
297  outstring(1:size(tmpstring)) = tmpstring(:)
298  deallocate(tmpstring)
299  else
300  allocate(outstring(1))
301  end if
302 
303  ! Add valueString to outString
304  outstring(size(outstring)) = valuestring
305  end subroutine append_to_char132pointer
306 
307 
308  recursive subroutine append_to_char_pointer(out,val)
309  character(*), intent(in) :: val
310  character(:), pointer, intent(inout) :: out
311 
312  ! Internal
313  character(:), pointer :: tmp
314 
315  nullify(tmp)
316  if (associated(out)) then
317  allocate(character( len(out) ) :: tmp)
318  tmp = out
319  deallocate(out)
320  allocate(character( len(tmp) + len(val) ) :: out)
321  out = tmp // val
322  deallocate(tmp)
323  else
324  allocate(character( len(val) ) :: out)
325  out = val
326  end if
327  end subroutine append_to_char_pointer
328 
329 end module stringtools
character(:) function, allocatable pointer2str_real_array(arg, separator)
character(:) function, allocatable alloc2str_int_array(arg, separator)
Definition: stringtools.f90:95
character(:) function, allocatable pointer2str_real_2darray(arg, separator)
character(:) function, allocatable num2str_int(arg)
Definition: stringtools.f90:31
character(:) function, allocatable pointer2str_str(arg)
subroutine string_to_char132pointer(in, out)
character(:) function, allocatable num2str_int_array(arg, separator)
Definition: stringtools.f90:47
subroutine char132pointer_to_string(out, in, linebreak)
character(:) function, allocatable alloc2str_real_array(arg, separator)
character(:) function, allocatable pointer2str_str_array(arg)
Translate pointer objects (reals, integers, string) into strings, i.e. character(*). Note that these function check if the object is associated. If it is not associated, then the empty string is returned.
Definition: stringtools.f90:21
Translate allocatable objects (reals, integers, string) into strings, i.e. character(*). Note that these function check if the object is allocated. If it is not allocated, then the empty string is returned.
Definition: stringtools.f90:15
recursive subroutine append_to_char132pointer(outString, valueString)
Translate number (reals or integers) into strings, i.e. character(*)
Definition: stringtools.f90:8
recursive subroutine append_to_char_pointer(out, val)
character(:) function, allocatable num2str_real_array(arg, separator)
Definition: stringtools.f90:65
character(:) function, allocatable alloc2str_str(arg)
Definition: stringtools.f90:85
character(:) function, allocatable num2str_real(arg)
Definition: stringtools.f90:39
character(:) function, allocatable pointer2str_int_array(arg, separator)
subroutine append_string_to_string(outString, valueString, separator)