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
144 use dc_trace, only: beginsub, endsub
148 use dc_present, only: present_and_not_empty
149 use dc_string, only: tochar, tolower
150 implicit none
151 integer, intent(out):: unit
152 character(*), intent(in):: file
153 character(*), intent(in), optional:: mode
154 logical, intent(out), optional:: err
155
156
157
158
159 integer, parameter:: max_unit = 99
160
161
162
163
164
165
166
167
168
169 integer, parameter:: min_unit = 0
170
171
172
173
174
175
176
177
178
179 character(TOKEN):: open_mode
180 integer:: unit_work
181 logical:: unit_exist_flag, unit_opend_flag
182 logical:: file_exist_flag
183 integer:: iostat
184 integer:: stat
185 character(STRING):: cause_c
186 character(*), parameter:: subname = 'FileOpen'
187 continue
188 call beginsub(subname, version)
190 cause_c = ''
191 unit = -1
192
193
194
195
196
197 if (present_and_not_empty(mode)) then
198 open_mode = mode
199 else
200 open_mode = 'r'
201 end if
202 call tolower(open_mode)
203
204
205
206
207
208 if ( trim(file) == '' ) then
210 goto 999
211 end if
212
213
214
215
216
217 unit_work = max_unit
218 do
219 inquire(unit=unit_work, exist=unit_exist_flag, opened=unit_opend_flag)
220 if (unit_exist_flag .and. .not. unit_opend_flag) then
221 exit
222 endif
223 unit_work = unit_work - 1
224 if (unit_work < min_unit) then
225 cause_c = tochar(min_unit) // ' - ' // tochar(max_unit)
227 goto 999
228 end if
229 enddo
230
231
232
233
234
235 select case( trim(open_mode) )
236 case ('r', 'w', 'rw', 'a', 'ra')
237 case default
238 cause_c = open_mode
240 goto 999
241 end select
242
243
244
245
246
247 select case( trim(open_mode) )
248 case ('r')
249 inquire(file=file, exist=file_exist_flag)
250 if (.not. file_exist_flag) then
251 cause_c = file
253 goto 999
254 end if
255 end select
256
257
258
259
260
261 select case( trim(open_mode) )
262 case ('r')
263 open(unit=unit_work, iostat=iostat, &
264 & file=file, status='OLD', action='READ')
265 if (.not. iostat == 0) then
266 cause_c = file
268 goto 999
269 end if
270 close(unit=unit_work)
271 end select
272
273
274
275
276
277 select case( trim(open_mode) )
278 case ('w', 'a', 'rw', 'ra')
279 open(unit=unit_work, iostat=iostat, &
280 & file=file, status='UNKNOWN', action='WRITE')
281 if (.not. iostat == 0) then
282 cause_c = file
284 goto 999
285 end if
286 close(unit=unit_work)
287 end select
288
289
290
291
292
293 select case( trim(open_mode) )
294 case ('r')
295 open(unit=unit_work, file=file, &
296 & status='OLD', action='READ')
297
298 case ('w')
299 open(unit=unit_work, file=file, &
300 & status='REPLACE', action='WRITE')
301
302 case ('rw')
303 open(unit=unit_work, file=file, &
304 & status='REPLACE', action='READWRITE')
305
306 case ('a')
307 open(unit=unit_work, file=file, &
308 & status='UNKNOWN', position='APPEND', action='WRITE')
309
310 case ('ra')
311 open(unit=unit_work, file=file, &
312 & status='UNKNOWN', position='APPEND', action='READWRITE')
313
314 end select
315
316 unit = unit_work
317
318999 continue
320 call endsub(subname)
subroutine, public storeerror(number, where, err, cause_c, cause_i)
integer, parameter, public dc_ebadfileopmode
integer, parameter, public dc_noerr
integer, parameter, public dc_enofileread
integer, parameter, public dc_efilenameempty
integer, parameter, public dc_enofilewrite
integer, parameter, public dc_enofileexist
integer, parameter, public dc_enounitnum
integer, parameter, public token
単語やキーワードを保持する文字型変数の種別型パラメタ
integer, parameter, public string
文字列を保持する 文字型変数の種別型パラメタ