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(len=256) :: line
116 character(len=5) :: advance_
117
118 advance_ = "yes"
119 if (present(advance)) advance_ = advance
120
121 s = c_null_ptr
122 do
123 call loct_break_c_string(str, s, line)
124 if (.not. c_associated(s)) exit
125 if (present(pre)) then
126 write(iunit, '(a,a)', advance=advance_) pre, trim(line)
127 else
128 write(iunit, '(a)', advance=advance_) trim(line)
129 end if
130 end do
131 end subroutine print_c_string
132
133 ! ---------------------------------------------------------
135 subroutine conv_to_c_string(str)
136 character(len=*), intent(inout) :: str
137
138 integer :: j
139
140 if (len(str) == 0) return
141
142 j = len(trim(str))
143 if (j < len(str)) then
144 str(j+1:j+1) = achar(0)
145 else
146 ! No extra room: terminate in-place by overwriting last character.
147 str(len(str):len(str)) = achar(0)
148 end if
149 end subroutine conv_to_c_string
150
151 ! Helper functions to convert between C and Fortran strings
152 ! Based on the routines by Joseph M. Krahn
153
154 ! ---------------------------------------------------------
157 !
158 function string_f_to_c(f_string) result(c_string)
159 character(len=*), intent(in) :: f_string
160 character(kind=c_char,len=1) :: c_string(c_str_len(f_string))
161
162 integer :: i, strlen
163
164 strlen = len_trim(f_string)
165
166 do i = 1, strlen
167 c_string(i) = f_string(i:i)
168 end do
169 c_string(strlen+1) = c_null_char
170
171 end function string_f_to_c
172
173 ! ---------------------------------------------------------
175 !
176 subroutine string_c_to_f(c_string, f_string)
177 character(kind=c_char,len=1), intent(in) :: c_string(*)
178 character(len=*), intent(out) :: f_string
179
180 integer :: i
181
182 f_string = ' '
183 i = 1
184 do while (i <= len(f_string))
185 if (c_string(i) == c_null_char) exit
186 f_string(i:i) = c_string(i)
187 i = i + 1
188 end do
189
190 end subroutine string_c_to_f
191
192 ! ---------------------------------------------------------
193 subroutine string_c_ptr_to_f(c_string, f_string)
194 type(c_ptr), intent(in) :: c_string
195 character(len=*), intent(out) :: f_string
196
197 character(len=1, kind=c_char), pointer :: p_chars(:)
198 integer :: i
199
200 if (.not. c_associated(c_string)) then
201 f_string = ' '
202 else
203 call c_f_pointer(c_string, p_chars, [len(f_string)+1])
204 f_string = ' '
205 i = 1
206 do while (i <= len(f_string))
207 if (p_chars(i) == c_null_char) exit
208 f_string(i:i) = p_chars(i)
209 i = i + 1
210 end do
211 end if
212
213 end subroutine string_c_ptr_to_f
214
219 integer pure function c_str_len(fortran_char)
220 character(len=*), intent(in) :: fortran_char
221 c_str_len = len_trim(fortran_char) + 1
222 end function c_str_len
223
224end module string_oct_m
225
226!! Local Variables:
227!! mode: f90
228!! coding: utf-8
229!! End:
subroutine, public string_c_to_f(c_string, f_string)
convert a C string to a Fortran string
Definition: string.F90:270
subroutine, public string_c_ptr_to_f(c_string, f_string)
Definition: string.F90:287
subroutine, public conv_to_c_string(str)
converts to c string
Definition: string.F90:229
subroutine, public compact(str)
Removes all spaces from a string.
Definition: string.F90:139
subroutine, public print_c_string(iunit, str, pre, advance)
prints the C string given by the pointer str
Definition: string.F90:202
integer pure function, public c_str_len(fortran_char)
Convert fortran character length to C character length.
Definition: string.F90:313
character(len=80) function, public str_center(s_in, l_in)
puts space around string, so that it is centered
Definition: string.F90:174
character(kind=c_char, len=1) function, dimension(c_str_len(f_string)), public string_f_to_c(f_string)
convert a Fortran string to a C string
Definition: string.F90:252
subroutine, public add_last_slash(str)
Adds a '/' in the end of the string, only if it missing. Useful for directories.
Definition: string.F90:160
int true(void)