gtool5 Fortran 90/95 Library 1.0.0-rc5
日本語
Loading...
Searching...
No Matches
dc_iounit.f90
Go to the documentation of this file.
1!-----------------------------------------------------------------------
2! Copyright (c) 2000-2026 Gtool Development Group. All rights reserved.
3!-----------------------------------------------------------------------
120
122 implicit none
123 private
124
125 public:: fileopen
126
127 character(*), parameter:: version = &
128 & '$Name: $' // &
129 & '$Id: dc_iounit.f90,v 1.1 2009-03-20 09:09:53 morikawa Exp $'
130
131 interface fileopen
132 module procedure fileopen
133 end interface
134
135contains
136
186 subroutine fileopen( &
187 & unit, file, mode, &
188 & err )
189 use dc_types, only: string, token
190 use dc_trace, only: beginsub, endsub
191 use dc_error, only: storeerror, dc_noerr, &
195 use dc_string, only: tochar, tolower
196 implicit none
197 integer, intent(out):: unit
198 character(*), intent(in):: file
199 character(*), intent(in), optional:: mode
200 logical, intent(out), optional:: err
201
202 !-----------------------------------
203 ! 作業変数
204 ! Work variables
205 integer, parameter:: max_unit = 99
206 ! NAMELIST ファイルをオープンするための
207 ! 装置番号の最大値. Fortran で使用可能な
208 ! 範囲 (0〜99) のうち,
209 ! 最大値が設定されている.
210 !
211 ! Maximum unit number for open of
212 ! NAMELIST file. An maximum
213 ! value within the bounds of available number
214 ! in Fortran (0 - 99) is specified.
215 integer, parameter:: min_unit = 0
216 ! NAMELIST ファイルをオープンするための
217 ! 装置番号の最小値. Fortran で使用可能な
218 ! 範囲 (0〜99) のうち,
219 ! 最小値が設定されている.
220 !
221 ! Minimum unit number for open of
222 ! NAMELIST file. An minimum
223 ! value within the bounds of available number
224 ! in Fortran (0 - 99) is specified.
225 character(TOKEN):: open_mode
226 integer:: unit_work
227 logical:: unit_exist_flag, unit_opend_flag
228 logical:: file_exist_flag
229 integer:: iostat
230 integer:: stat
231 character(STRING):: cause_c
232 character(*), parameter:: subname = 'FileOpen'
233 continue
234 call beginsub(subname, version)
235 stat = dc_noerr
236 cause_c = ''
237 unit = -1
238
239 !-----------------------------------------------------------------
240 ! オプショナル引数のチェック
241 ! Check optional arguments
242 !-----------------------------------------------------------------
243 if (present_and_not_empty(mode)) then
244 open_mode = mode
245 else
246 open_mode = 'r'
247 end if
248 call tolower(open_mode)
249
250 !-----------------------------------------------------------------
251 ! 引数の正当性のチェック
252 ! Validation of arguments
253 !-----------------------------------------------------------------
254 if ( trim(file) == '' ) then
255 stat = dc_efilenameempty
256 goto 999
257 end if
258
259 !----------------------------------------------------------------
260 ! 使用可能な装置番号の探査
261 ! Search available unit number
262 !----------------------------------------------------------------
263 unit_work = max_unit
264 do
265 inquire(unit=unit_work, exist=unit_exist_flag, opened=unit_opend_flag)
266 if (unit_exist_flag .and. .not. unit_opend_flag) then
267 exit
268 endif
269 unit_work = unit_work - 1
270 if (unit_work < min_unit) then
271 cause_c = tochar(min_unit) // ' - ' // tochar(max_unit)
272 stat = dc_enounitnum
273 goto 999
274 end if
275 enddo
276
277 !----------------------------------------------------------------
278 ! モードの書式のチェック
279 ! Check form of mode
280 !----------------------------------------------------------------
281 select case( trim(open_mode) )
282 case ('r', 'w', 'rw', 'a', 'ra')
283 case default
284 cause_c = open_mode
285 stat = dc_ebadfileopmode
286 goto 999
287 end select
288
289 !----------------------------------------------------------------
290 ! ファイルの存在のチェック
291 ! Check existance of a file
292 !----------------------------------------------------------------
293 select case( trim(open_mode) )
294 case ('r')
295 inquire(file=file, exist=file_exist_flag)
296 if (.not. file_exist_flag) then
297 cause_c = file
298 stat = dc_enofileexist
299 goto 999
300 end if
301 end select
302
303 !----------------------------------------------------------------
304 ! ファイルの読み込み可能のチェック
305 ! Check readable of a file
306 !----------------------------------------------------------------
307 select case( trim(open_mode) )
308 case ('r')
309 open(unit=unit_work, iostat=iostat, &
310 & file=file, status='OLD', action='READ')
311 if (.not. iostat == 0) then
312 cause_c = file
313 stat = dc_enofileread
314 goto 999
315 end if
316 close(unit=unit_work)
317 end select
318
319 !----------------------------------------------------------------
320 ! ファイルの書き込み可能のチェック
321 ! Check writable of a file
322 !----------------------------------------------------------------
323 select case( trim(open_mode) )
324 case ('w', 'a', 'rw', 'ra')
325 open(unit=unit_work, iostat=iostat, &
326 & file=file, status='UNKNOWN', action='WRITE')
327 if (.not. iostat == 0) then
328 cause_c = file
329 stat = dc_enofilewrite
330 goto 999
331 end if
332 close(unit=unit_work)
333 end select
334
335 !----------------------------------------------------------------
336 ! ファイルオープン
337 ! Open a file
338 !----------------------------------------------------------------
339 select case( trim(open_mode) )
340 case ('r')
341 open(unit=unit_work, file=file, &
342 & status='OLD', action='READ')
343
344 case ('w')
345 open(unit=unit_work, file=file, &
346 & status='REPLACE', action='WRITE')
347
348 case ('rw')
349 open(unit=unit_work, file=file, &
350 & status='REPLACE', action='READWRITE')
351
352 case ('a')
353 open(unit=unit_work, file=file, &
354 & status='UNKNOWN', position='APPEND', action='WRITE')
355
356 case ('ra')
357 open(unit=unit_work, file=file, &
358 & status='UNKNOWN', position='APPEND', action='READWRITE')
359
360 end select
361
362 unit = unit_work
363
364999 continue
365 call storeerror(stat, subname, err, cause_c)
366 call endsub(subname)
367 end subroutine fileopen
368
370end module dc_iounit
Error handling module.
Definition dc_error.f90:454
subroutine, public storeerror(number, where, err, cause_c, cause_i)
Definition dc_error.f90:891
integer, parameter, public dc_noerr
Error storage variables
Definition dc_error.f90:468
integer, parameter, public dc_ebadfileopmode
Definition dc_error.f90:540
integer, parameter, public dc_enofileread
Definition dc_error.f90:543
integer, parameter, public dc_efilenameempty
Definition dc_error.f90:539
integer, parameter, public dc_enofilewrite
Definition dc_error.f90:544
integer, parameter, public dc_enofileexist
Definition dc_error.f90:542
integer, parameter, public dc_enounitnum
Definition dc_error.f90:541
Unit number handling at file open.
Judge optional control parameters.
logical function, public present_and_not_empty(arg)
Handling character types.
Definition dc_string.f90:83
Debug tracing module.
Definition dc_trace.f90:150
subroutine, public beginsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca, version)
Definition dc_trace.f90:457
subroutine, public endsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca)
Definition dc_trace.f90:580
Provides kind type parameter values.
Definition dc_types.f90:55
integer, parameter, public token
Character length for word, token
Definition dc_types.f90:128
integer, parameter, public string
Character length for string
Definition dc_types.f90:137