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(64) :: 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(len=256) :: line
139 character(len=5) :: advance_
140
141 advance_ = "yes"
142 if (present(advance)) advance_ = advance
143
144 s = c_null_ptr
145 do
146 call loct_break_c_string(str, s, line)
147 if (.not. c_associated(s)) exit
148 if (present(pre)) then
149 write(iunit, '(a,a)', advance=advance_) pre, trim(line)
150 else
151 write(iunit, '(a)', advance=advance_) trim(line)
152 end if
153 end do
154 end subroutine print_c_string
155
156 ! ---------------------------------------------------------
158 subroutine conv_to_c_string(str)
159 character(len=*), intent(out) :: str
161 integer :: j
162
163 j = len(trim(str))
164 str(j+1:j+1) = achar(0)
165 end subroutine conv_to_c_string
166
167 ! Helper functions to convert between C and Fortran strings
168 ! Based on the routines by Joseph M. Krahn
169
170 ! ---------------------------------------------------------
171 function string_f_to_c(f_string) result(c_string)
172 character(len=*), intent(in) :: f_string
173 character(kind=c_char,len=1) :: c_string(c_str_len(f_string))
175 integer :: i, strlen
176
177 strlen = len_trim(f_string)
178
179 do i = 1, strlen
180 c_string(i) = f_string(i:i)
181 end do
182 c_string(strlen+1) = c_null_char
183
184 end function string_f_to_c
185
186 ! ---------------------------------------------------------
187 subroutine string_c_to_f(c_string, f_string)
188 character(kind=c_char,len=1), intent(in) :: c_string(*)
189 character(len=*), intent(out) :: f_string
190
191 integer :: i
192
193 i = 1
194 do while(c_string(i) /= c_null_char .and. i <= len(f_string))
195 f_string(i:i) = c_string(i)
196 i = i + 1
197 end do
198 if (i < len(f_string)) f_string(i:) = ' '
199
200 end subroutine string_c_to_f
201
202 ! ---------------------------------------------------------
203 subroutine string_c_ptr_to_f(c_string, f_string)
204 type(c_ptr), intent(in) :: c_string
205 character(len=*), intent(out) :: f_string
206
207 character(len=1, kind=c_char), pointer :: p_chars(:)
208 integer :: i
209
210 if (.not. c_associated(c_string)) then
211 f_string = ' '
212 else
213 call c_f_pointer(c_string, p_chars, [huge(0)])
214 i = 1
215 do while(p_chars(i) /= c_null_char .and. i <= len(f_string))
216 f_string(i:i) = p_chars(i)
217 i = i + 1
218 end do
219 if (i < len(f_string)) f_string(i:) = ' '
220 end if
221
222 end subroutine string_c_ptr_to_f
223
228 integer pure function c_str_len(fortran_char)
229 character(len=*), intent(in) :: fortran_char
230 c_str_len = len_trim(fortran_char) + 1
231 end function c_str_len
232
233end module string_oct_m
234
235!! Local Variables:
236!! mode: f90
237!! coding: utf-8
238!! End:
subroutine, public string_c_to_f(c_string, f_string)
Definition: string.F90:281
subroutine, public string_c_ptr_to_f(c_string, f_string)
Definition: string.F90:297
subroutine, public str_trim(str)
removes leading spaces from string
Definition: string.F90:175
subroutine, public conv_to_c_string(str)
converts to c string
Definition: string.F90:252
subroutine, public compact(str)
Removes all spaces from a string.
Definition: string.F90:140
subroutine, public print_c_string(iunit, str, pre, advance)
prints the C string given by the pointer str
Definition: string.F90:225
integer pure function, public c_str_len(fortran_char)
Convert fortran character length to C character length.
Definition: string.F90:322
character(len=80) function, public str_center(s_in, l_in)
puts space around string, so that it is centered
Definition: string.F90:197
character(kind=c_char, len=1) function, dimension(c_str_len(f_string)), public string_f_to_c(f_string)
Definition: string.F90:265
subroutine, public add_last_slash(str)
Adds a '/' in the end of the string, only if it missing. Useful for directories.
Definition: string.F90:161
int true(void)