Octopus
string.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19#include "global.h"
20
21module string_oct_m
22 use iso_c_binding
23 use loct_oct_m
24
25 implicit none
26
27 private
28 public :: &
29 compact, &
31 str_trim, &
32 str_center, &
39
40contains
41
42 ! ---------------------------------------------------------
46 subroutine compact(str)
47 character(len=*), intent(inout) :: str
48
49 integer :: i, j
50
51 j = 1
52 do i = 1, len(str)
53 if (str(i:i) /= ' ') then
54 str(j:j) = str(i:i)
55 j = j + 1
56 end if
57 end do
58 do i = j, len(str)
59 str(i:i) = ' '
60 end do
61
62 end subroutine compact
63
64 ! ---------------------------------------------------------
67 subroutine add_last_slash(str)
68 character(len=*), intent(inout) :: str
69
70 character(len=len(str)) :: tmp_str
71
72 if (index(str, '/', .true.) /= len_trim(str)) then
73 tmp_str = str
74 write(str,'(a,a1)') trim(tmp_str), '/'
75 end if
76 end subroutine add_last_slash
77
78
79 ! ---------------------------------------------------------
81 subroutine str_trim(str)
82 character (len=*), intent(inout) :: str
83 integer :: i, j, l
84
85 l = len(str)
86 do i = 1, l
87 if (str(i:i) /= ' ') exit
88 end do
89
90 do j = 1, l - i + 1
91 str(j:j) = str(i:i)
92 i = i + 1
93 end do
94
95 do i = j, l
96 str(j:j) = ' '
97 end do
98
99 end subroutine str_trim
100
101 ! ---------------------------------------------------------
103 character(len=80) function str_center(s_in, l_in) result(s_out)
104 character(len=*), intent(in) :: s_in
105 integer, intent(in) :: l_in
106
107 integer :: pad, i, li, l
108
109 l = min(80, l_in)
110 li = len(s_in)
111 if (l < li) then
112 s_out(1:l) = s_in(1:l)
113 return
114 end if
115
116 pad = (l - li)/2
117
118 s_out = ""
119 do i = 1, pad
120 s_out(i:i) = " "
121 end do
122 s_out(pad + 1:pad + li) = s_in(1:li)
123 do i = pad + li + 1, l
124 s_out(i:i) = " "
125 end do
126
127 end function str_center
128
129 ! ---------------------------------------------------------
131 subroutine print_c_string(iunit, str, pre, advance)
132 integer, intent(in) :: iunit
133 type(c_ptr), intent(in) :: str
134 character(len=*), optional, intent(in) :: pre
135 character(len=*), optional, intent(in) :: advance
136
137 type(c_ptr) :: s
138 character(kind=c_char) :: cline(257)
139 character(len=256) :: line
140 character(len=5) :: advance_
142 advance_ = "yes"
143 if (present(advance)) advance_ = advance
144
145 s = c_null_ptr
146 do
147 cline = c_null_char
148 call loct_break_c_string(str, s, cline)
149 call string_c_to_f(cline, line)
150 if (.not. c_associated(s)) exit
151 if (present(pre)) then
152 write(iunit, '(a,a)', advance=advance_) pre, trim(line)
153 else
154 write(iunit, '(a)', advance=advance_) trim(line)
155 end if
156 end do
157 end subroutine print_c_string
158
159 ! ---------------------------------------------------------
161 subroutine conv_to_c_string(str)
162 character(len=*), intent(out) :: str
163
164 integer :: j
165
166 j = len(trim(str))
167 str(j+1:j+1) = achar(0)
168 end subroutine conv_to_c_string
169
170 ! Helper functions to convert between C and Fortran strings
171 ! Based on the routines by Joseph M. Krahn
172
173 ! ---------------------------------------------------------
177 function string_f_to_c(f_string) result(c_string)
178 character(len=*), intent(in) :: f_string
179 character(kind=c_char,len=1), allocatable :: c_string(:)
180
181 integer :: i, strlen
182
183 strlen = len_trim(f_string)
184 allocate(c_string(c_str_len(f_string)))
185
186 do i = 1, strlen
187 c_string(i) = f_string(i:i)
188 end do
189 c_string(strlen+1) = c_null_char
190
191 end function string_f_to_c
192
193 ! ---------------------------------------------------------
195 !
196 subroutine string_c_to_f(c_string, f_string)
197 character(kind=c_char,len=1), intent(in) :: c_string(*)
198 character(len=*), intent(out) :: f_string
199
200 integer :: i
201
202 i = 1
203 do while(c_string(i) /= c_null_char .and. i <= len(f_string))
204 f_string(i:i) = c_string(i)
205 i = i + 1
206 end do
207 if (i < len(f_string)) f_string(i:) = ' '
208
209 end subroutine string_c_to_f
210
211 ! ---------------------------------------------------------
212 subroutine string_c_ptr_to_f(c_string, f_string)
213 type(c_ptr), intent(in) :: c_string
214 character(len=*), intent(out) :: f_string
215
216 character(len=1, kind=c_char), pointer :: p_chars(:)
217 integer :: i
218
219 if (.not. c_associated(c_string)) then
220 f_string = ' '
221 else
222 call c_f_pointer(c_string, p_chars, [huge(0)])
223 i = 1
224 do while(p_chars(i) /= c_null_char .and. i <= len(f_string))
225 f_string(i:i) = p_chars(i)
226 i = i + 1
227 end do
228 if (i < len(f_string)) f_string(i:) = ' '
229 end if
230
231 end subroutine string_c_ptr_to_f
232
237 integer pure function c_str_len(fortran_char)
238 character(len=*), intent(in) :: fortran_char
239 c_str_len = len_trim(fortran_char) + 1
240 end function c_str_len
241
242end module string_oct_m
243
244!! Local Variables:
245!! mode: f90
246!! coding: utf-8
247!! End:
character(kind=c_char, len=1) function, dimension(len_trim(f_string)+1), private string_f_to_c(f_string)
convert a Fortran string to a C string
Definition: loct.F90:244
subroutine string_c_to_f(c_string, f_string)
convert a C string to a Fortran string
Definition: loct.F90:262
subroutine, public string_c_ptr_to_f(c_string, f_string)
Definition: string.F90:308
subroutine, public str_trim(str)
removes leading spaces from string
Definition: string.F90:177
subroutine, public conv_to_c_string(str)
converts to c string
Definition: string.F90:257
subroutine, public compact(str)
Removes all spaces from a string.
Definition: string.F90:142
subroutine, public print_c_string(iunit, str, pre, advance)
prints the C string given by the pointer str
Definition: string.F90:227
integer pure function, public c_str_len(fortran_char)
Convert fortran character length to C character length.
Definition: string.F90:333
character(len=80) function, public str_center(s_in, l_in)
puts space around string, so that it is centered
Definition: string.F90:199
subroutine, public add_last_slash(str)
Adds a '/' in the end of the string, only if it missing. Useful for directories.
Definition: string.F90:163
int true(void)