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_center, &
38
39contains
40
41 ! ---------------------------------------------------------
45 subroutine compact(str)
46 character(len=*), intent(inout) :: str
47
48 integer :: i, j
49
50 j = 1
51 do i = 1, len(str)
52 if (str(i:i) /= ' ') then
53 str(j:j) = str(i:i)
54 j = j + 1
55 end if
56 end do
57 do i = j, len(str)
58 str(i:i) = ' '
59 end do
60
61 end subroutine compact
62
63 ! ---------------------------------------------------------
66 subroutine add_last_slash(str)
67 character(len=*), intent(inout) :: str
68
69 character(len=len(str)) :: tmp_str
70
71 if (index(str, '/', .true.) /= len_trim(str)) then
72 tmp_str = str
73 write(str,'(a,a1)') trim(tmp_str), '/'
74 end if
75 end subroutine add_last_slash
76
77
78 ! ---------------------------------------------------------
80 character(len=80) function str_center(s_in, l_in) result(s_out)
81 character(len=*), intent(in) :: s_in
82 integer, intent(in) :: l_in
83
84 integer :: pad, i, li, l
85
86 l = min(80, l_in)
87 li = len(s_in)
88 if (l < li) then
89 s_out(1:l) = s_in(1:l)
90 return
91 end if
92
93 pad = (l - li)/2
94
95 s_out = ""
96 do i = 1, pad
97 s_out(i:i) = " "
98 end do
99 s_out(pad + 1:pad + li) = s_in(1:li)
100 do i = pad + li + 1, l
101 s_out(i:i) = " "
102 end do
103
104 end function str_center
105
106 ! ---------------------------------------------------------
108 subroutine print_c_string(iunit, str, pre, advance)
109 integer, intent(in) :: iunit
110 type(c_ptr), intent(in) :: str
111 character(len=*), optional, intent(in) :: pre
112 character(len=*), optional, intent(in) :: advance
113
114 type(c_ptr) :: s
115 character(kind=c_char) :: cline(257)
116 character(len=256) :: line
117 character(len=5) :: advance_
118
119 advance_ = "yes"
120 if (present(advance)) advance_ = advance
121
122 s = c_null_ptr
123 do
124 cline = c_null_char
125 call loct_break_c_string(str, s, cline)
126 call string_c_to_f(cline, line)
127 if (.not. c_associated(s)) exit
128 if (present(pre)) then
129 write(iunit, '(a,a)', advance=advance_) pre, trim(line)
130 else
131 write(iunit, '(a)', advance=advance_) trim(line)
132 end if
133 end do
134 end subroutine print_c_string
135
136 ! ---------------------------------------------------------
138 subroutine conv_to_c_string(str)
139 character(len=*), intent(inout) :: str
141 integer :: j
142
143 if (len(str) == 0) return
144
145 j = len(trim(str))
146 if (j < len(str)) then
147 str(j+1:j+1) = achar(0)
148 else
149 ! No extra room: terminate in-place by overwriting last character.
150 str(len(str):len(str)) = achar(0)
151 end if
152 end subroutine conv_to_c_string
153
154 ! Helper functions to convert between C and Fortran strings
155 ! Based on the routines by Joseph M. Krahn
156
157 ! ---------------------------------------------------------
160 !
161 function string_f_to_c(f_string) result(c_string)
162 character(len=*), intent(in) :: f_string
163 character(kind=c_char,len=1), allocatable :: c_string(:)
164
165 integer :: i, strlen
166
167 strlen = len_trim(f_string)
168 allocate(c_string(c_str_len(f_string)))
169
170 do i = 1, strlen
171 c_string(i) = f_string(i:i)
172 end do
173 c_string(strlen+1) = c_null_char
174
175 end function string_f_to_c
176
177 ! ---------------------------------------------------------
179 !
180 subroutine string_c_to_f(c_string, f_string)
181 character(kind=c_char,len=1), intent(in) :: c_string(*)
182 character(len=*), intent(out) :: f_string
183
184 integer :: i
185
186 f_string = ' '
187 i = 1
188 do while (i <= len(f_string))
189 if (c_string(i) == c_null_char) exit
190 f_string(i:i) = c_string(i)
191 i = i + 1
192 end do
193
194 end subroutine string_c_to_f
195
196 ! ---------------------------------------------------------
197 subroutine string_c_ptr_to_f(c_string, f_string)
198 type(c_ptr), intent(in) :: c_string
199 character(len=*), intent(out) :: f_string
200
201 character(len=1, kind=c_char), pointer :: p_chars(:)
202 integer :: i
204 if (.not. c_associated(c_string)) then
205 f_string = ' '
206 else
207 call c_f_pointer(c_string, p_chars, [len(f_string)+1])
208 f_string = ' '
209 i = 1
210 do while (i <= len(f_string))
211 if (p_chars(i) == c_null_char) exit
212 f_string(i:i) = p_chars(i)
213 i = i + 1
214 end do
215 end if
216
217 end subroutine string_c_ptr_to_f
218
223 integer pure function c_str_len(fortran_char)
224 character(len=*), intent(in) :: fortran_char
225 c_str_len = len_trim(fortran_char) + 1
226 end function c_str_len
227
228end module string_oct_m
229
230!! Local Variables:
231!! mode: f90
232!! coding: utf-8
233!! End:
System information (time, memory, sysname)
Definition: loct.F90:117
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:240
subroutine string_c_to_f(c_string, f_string)
convert a C string to a Fortran string
Definition: loct.F90:258
subroutine, public string_c_ptr_to_f(c_string, f_string)
Definition: string.F90:293
subroutine, public conv_to_c_string(str)
converts to c string
Definition: string.F90:234
subroutine, public compact(str)
Removes all spaces from a string.
Definition: string.F90:141
subroutine, public print_c_string(iunit, str, pre, advance)
prints the C string given by the pointer str
Definition: string.F90:204
integer pure function, public c_str_len(fortran_char)
Convert fortran character length to C character length.
Definition: string.F90:319
character(len=80) function, public str_center(s_in, l_in)
puts space around string, so that it is centered
Definition: string.F90:176
subroutine, public add_last_slash(str)
Adds a '/' in the end of the string, only if it missing. Useful for directories.
Definition: string.F90:162
int true(void)