-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvarying_string_getarg.f90
150 lines (101 loc) · 3.97 KB
/
varying_string_getarg.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
!
! Copyright 2011 Sebastian Heimann
!
! Licensed under the Apache License, Version 2.0 (the "License");
! you may not use this file except in compliance with the License.
! You may obtain a copy of the License at
!
! http://www.apache.org/licenses/LICENSE-2.0
!
! Unless required by applicable law or agreed to in writing, software
! distributed under the License is distributed on an "AS IS" BASIS,
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
! See the License for the specific language governing permissions and
! limitations under the License.
!
module varying_string_getarg
use better_varying_string
! use f90_unix_env
implicit none
private
public vs_getarg
public vs_getenv
interface vs_getenv
module procedure vs_getenv_c
module procedure vs_getenv_vs
end interface
public real_getarg
public int_getarg
integer, parameter :: max_arg_len = 100000
contains
subroutine vs_getarg( ipos, vs )
integer, intent(in) :: ipos
type(varying_string), intent(inout) :: vs
! get argument at position ipos as varying_string
! argument 0 is the command
integer :: leng
character(len=max_arg_len) :: dummy
! get length of the argument
call getarg( ipos, dummy )
leng = len_trim(dummy)
! extract it to vs
call extract_arg( ipos, leng, vs )
end subroutine
subroutine vs_getenv_c( key, vs )
character(len=*), intent(in) :: key
type(varying_string), intent(inout) :: vs
! get argument at position ipos as varying_string
! argument 0 is the command
integer :: leng
character(len=max_arg_len) :: dummy
! get length of the argument
call getenv( key, dummy )
leng = len_trim(dummy)
! extract it to vs
call extract_env( key, leng, vs )
end subroutine
subroutine vs_getenv_vs( key, vs )
type(varying_string), intent(in) :: key
type(varying_string), intent(inout) :: vs
call vs_getenv( char(key), vs )
end subroutine
subroutine extract_arg( ipos, length, vs )
integer, intent(in) :: ipos, length
type(varying_string), intent(inout) :: vs
character(len=length) :: buffer
call getarg( ipos, buffer )
vs = buffer
end subroutine
subroutine extract_env( key, length, vs )
character(len=*), intent(in) :: key
integer, intent(in) :: length
type(varying_string), intent(inout) :: vs
character(len=length) :: buffer
call getenv(key, buffer )
vs = buffer
end subroutine
subroutine real_getarg( iarg, mini, maxi, val )
integer, intent(in) :: iarg
real, intent(in) :: mini, maxi
real, intent(out) :: val
! reads argument iarg from the argument list and converts it to real number val
! it is checked, that val is in the range [min, max]
type(varying_string) :: vs
call vs_getarg( iarg, vs )
val = vs
if (val < mini) val = mini
if (val > maxi) val = maxi
end subroutine
subroutine int_getarg( iarg, mini, maxi, val )
integer, intent(in) :: iarg
integer, intent(in) :: mini, maxi
integer, intent(out) :: val
! reads argument iarg from the argument list and converts it to real number val
! it is checked, that val is in the range [min, max]
type(varying_string) :: vs
call vs_getarg( iarg, vs )
val = vs
if (val < mini) val = mini
if (val > maxi) val = maxi
end subroutine
end module