64 INTEGER,
INTENT(IN) :: unit_number
65 CHARACTER(LEN=*),
INTENT(IN),
OPTIONAL :: file_status
67 CHARACTER(LEN=11) :: status_string
68 CHARACTER(LEN=2*default_path_length) :: message
69 CHARACTER(LEN=default_path_length) :: iomsg_string
71 LOGICAL :: exists, is_named, is_open
73 INQUIRE (unit=unit_number, exist=exists, named=is_named, opened=is_open, iostat=istat)
76 WRITE (unit=message, fmt=
"(A,I0,A,I0,A)") &
77 "An error occurred inquiring the unit with the number ", unit_number, &
78 " (IOSTAT = ", istat,
")"
79 CALL cp_abort(__location__, trim(message))
80 ELSE IF (.NOT. exists)
THEN
81 WRITE (unit=message, fmt=
"(A,I0,A)") &
82 "The specified unit number ", unit_number, &
83 " cannot be closed, because it does not exist."
84 CALL cp_abort(__location__, trim(message))
92 SELECT CASE (unit_number)
94 WRITE (unit=message, fmt=
"(A,I0)") &
95 "Attempt to close the default error unit number ", unit_number
96 CALL cp_abort(__location__, trim(message))
98 WRITE (unit=message, fmt=
"(A,I0)") &
99 "Attempt to close the default input unit number ", unit_number
100 CALL cp_abort(__location__, trim(message))
102 WRITE (unit=message, fmt=
"(A,I0)") &
103 "Attempt to close the default output unit number ", unit_number
104 CALL cp_abort(__location__, trim(message))
108 IF (
PRESENT(file_status))
THEN
109 IF (len_trim(adjustl(file_status)) > len(status_string))
THEN
110 CALL cp_abort(__location__, &
111 "An invalid file STATUS="//trim(adjustl(file_status))// &
114 status_string = trim(adjustl(file_status))
116 ELSE IF (.NOT. is_named)
THEN
117 status_string =
"DELETE"
119 status_string =
"KEEP"
122 SELECT CASE (status_string)
123 CASE (
"DELETE",
"KEEP")
124 CLOSE (unit=unit_number, iostat=istat, iomsg=iomsg_string, status=trim(status_string))
126 CALL cp_abort(__location__, &
127 "An error occurred closing the file with the logical unit number "// &
128 trim(int_to_string(unit_number))//
" (IOSTAT="// &
129 trim(int_to_string(istat))//
"): "//trim(iomsg_string))
132 CALL cp_abort(__location__, &
133 "An invalid file STATUS="//trim(status_string)//
" was specified.")
194 SUBROUTINE open_file(file_name, unit_number, file_status, file_form, file_action, &
195 file_position, file_pad, file_access, debug)
197 CHARACTER(LEN=*),
INTENT(IN),
OPTIONAL :: file_name
198 INTEGER,
INTENT(OUT) :: unit_number
199 CHARACTER(LEN=*),
INTENT(IN),
OPTIONAL :: file_status, file_form, file_action, &
200 file_position, file_pad, file_access
201 LOGICAL,
INTENT(IN),
OPTIONAL :: debug
203 CHARACTER(LEN=*),
PARAMETER :: routinen =
'open_file'
205 CHARACTER(LEN=11) :: access_string, action_string, current_access, current_action, &
206 current_form, form_string, pad_string, position_string, status_string
207 CHARACTER(LEN=2*default_path_length) :: message
208 CHARACTER(LEN=default_path_length) :: cwd, iomsg_string, real_file_name
209 INTEGER :: inquire_stat, open_stat
210 LOGICAL :: connect, exists, is_named, is_open
215 IF (
PRESENT(file_access))
THEN
216 access_string = trim(adjustl(file_access))
218 IF (access_string ==
"DIRECT")
THEN
219 CALL cp_abort(__location__, &
220 "ACCESS=DIRECT is not supported.")
223 access_string =
"SEQUENTIAL"
226 IF (
PRESENT(file_status))
THEN
227 status_string = trim(adjustl(file_status))
230 status_string =
"OLD"
233 IF (
PRESENT(file_form))
THEN
234 form_string = trim(adjustl(file_form))
237 form_string =
"FORMATTED"
240 IF (
PRESENT(file_pad))
THEN
241 pad_string = trim(adjustl(file_pad))
243 IF (form_string ==
"UNFORMATTED")
THEN
244 CALL cp_abort(__location__, &
245 "The PAD specifier is not allowed for an UNFORMATTED file.")
251 IF (
PRESENT(file_action))
THEN
252 action_string = trim(adjustl(file_action))
256 SELECT CASE (status_string)
257 CASE (
"NEW",
"REPLACE",
"SCRATCH")
258 action_string =
"WRITE"
260 action_string =
"READ"
264 IF (
PRESENT(file_position))
THEN
265 position_string = trim(adjustl(file_position))
268 position_string =
"REWIND"
271 IF (
PRESENT(file_name))
THEN
272 IF (len_trim(file_name) == 0)
THEN
273 CALL cp_abort(__location__, &
274 "The specified FILE name is empty.")
276 IF (len_trim(file_name) > len(real_file_name))
THEN
277 CALL cp_abort(__location__, &
278 "The FILE name <"//trim(file_name)//
"> exceeds the maximum length of "// &
279 trim(int_to_string(len(real_file_name)))//
" characters.")
281 IF (file_name(1:1) ==
" ")
THEN
282 CALL cp_abort(__location__, &
283 "The FILE name <"//trim(file_name)//
"> has leading blanks.")
285 IF (status_string ==
"SCRATCH")
THEN
286 CALL cp_abort(__location__, &
287 "A FILE name specifier is not allowed for SCRATCH files.")
291 IF ((status_string ==
"OLD") .AND. (action_string ==
"READ"))
THEN
294 real_file_name = trim(adjustl(file_name))
298 SELECT CASE (status_string)
301 CASE (
"NEW",
"OLD",
"REPLACE",
"UNKNOWN")
302 IF (len_trim(real_file_name) == 0)
THEN
303 CALL cp_abort(__location__, &
304 "A FILE name length of zero for a file with STATUS="// &
305 trim(status_string)//
" is not allowed.")
308 CALL cp_abort(__location__,
"A file STATUS="//trim(status_string)//
" is not supported.")
321 IF (status_string ==
"SCRATCH")
THEN
324 INQUIRE (file=trim(real_file_name), exist=exists, opened=is_open, iostat=inquire_stat)
325 IF (inquire_stat == 0)
THEN
327 INQUIRE (file=trim(real_file_name), number=unit_number, &
328 action=current_action, form=current_form, access=current_access)
330 SELECT CASE (status_string)
332 connect = (.NOT. exists)
342 unit_number = get_unit_number()
343 IF (unit_number > 0)
THEN
344 IF (status_string ==
"SCRATCH")
THEN
345 IF (form_string ==
"FORMATTED")
THEN
346 OPEN (unit=unit_number, &
347 status=trim(status_string), &
348 access=trim(access_string), &
349 form=trim(form_string), &
350 position=trim(position_string), &
351 action=trim(action_string), &
352 pad=trim(pad_string), &
353 iomsg=iomsg_string, &
356 OPEN (unit=unit_number, &
357 status=trim(status_string), &
358 access=trim(access_string), &
359 form=trim(form_string), &
360 position=trim(position_string), &
361 action=trim(action_string), &
362 iomsg=iomsg_string, &
366 IF (form_string ==
"FORMATTED")
THEN
367 OPEN (unit=unit_number, &
368 file=trim(real_file_name), &
369 status=trim(status_string), &
370 access=trim(access_string), &
371 form=trim(form_string), &
372 position=trim(position_string), &
373 action=trim(action_string), &
374 pad=trim(pad_string), &
375 iomsg=iomsg_string, &
378 OPEN (unit=unit_number, &
379 file=trim(real_file_name), &
380 status=trim(status_string), &
381 access=trim(access_string), &
382 form=trim(form_string), &
383 position=trim(position_string), &
384 action=trim(action_string), &
385 iomsg=iomsg_string, &
393 IF (inquire_stat /= 0)
THEN
394 CALL cp_abort(__location__, &
395 "An error occurred inquiring the file <"//trim(real_file_name)// &
396 "> (IOSTAT="//trim(int_to_string(inquire_stat))//
").")
402 IF (trim(current_action) /= trim(action_string))
THEN
403 CALL cp_abort(__location__, &
404 "Attempt to re-open the existing file <"// &
405 trim(real_file_name)//
"> with the modified ACTION attribute "// &
406 trim(action_string)//
". The current ACTION attribute is "// &
407 trim(current_action)//
".")
410 IF (trim(current_form) /= trim(form_string))
THEN
411 CALL cp_abort(__location__, &
412 "Attempt to re-open the existing "// &
413 trim(current_form)//
" file <"//trim(real_file_name)// &
414 "> as "//trim(form_string)//
" file.")
417 IF (trim(current_access) /= trim(access_string))
THEN
418 CALL cp_abort(__location__, &
419 "Attempt to re-open the existing "// &
420 trim(current_access)//
" file <"//trim(real_file_name)// &
421 "> with ACCESS="//trim(access_string)//
".")
423 IF (status_string ==
"NEW")
THEN
424 CALL cp_abort(__location__, &
425 "Attempt to re-open the existing file <"// &
426 trim(real_file_name)//
"> with STATUS=NEW.")
428 IF (position_string ==
"REWIND") rewind(unit=unit_number)
431 SELECT CASE (status_string)
434 CALL cp_abort(__location__, &
435 "The file <"//trim(real_file_name)// &
436 "> cannot be opened with STATUS=NEW, because it already exists.")
439 IF (.NOT. exists)
THEN
440 IF (action_string ==
"READ")
THEN
441 CALL cp_abort(__location__, &
442 "The specified file <"//trim(real_file_name)// &
443 "> cannot be opened with STATUS=OLD. It does not exist. "// &
446 CALL cp_abort(__location__, &
447 "The specified file <"//trim(real_file_name)// &
448 "> cannot be opened with STATUS=OLD and ACTION="// &
449 trim(action_string)//
". It does not exist.")
453 IF (unit_number < 1)
THEN
454 IF (status_string ==
"SCRATCH")
THEN
455 CALL cp_abort(__location__, &
456 "Could not obtain a unit number to open a SCRATCH file.")
458 CALL cp_abort(__location__, &
459 "Cannot open the file <"//trim(real_file_name)// &
460 ">, because no unused logical unit number could be obtained.")
463 IF (open_stat /= 0)
THEN
464 IF (status_string ==
"SCRATCH")
THEN
465 CALL cp_abort(__location__, &
466 "An error occurred opening a SCRATCH file (UNIT="// &
467 trim(int_to_string(unit_number))//
", IOSTAT="// &
468 trim(int_to_string(open_stat))//
"): "//trim(iomsg_string))
471 CALL cp_abort(__location__, &
472 "An error occurred opening the file <"//trim(real_file_name)// &
473 "> (UNIT="//trim(int_to_string(unit_number))// &
474 ", IOSTAT="//trim(int_to_string(open_stat))//
"): "// &
475 trim(iomsg_string)//
". Current working directory: "//trim(cwd))
480 IF (
PRESENT(debug))
THEN
482 INQUIRE (unit=unit_number, &
485 position=position_string, &
486 access=access_string, &
488 action=action_string)
491 INQUIRE (unit=unit_number, name=message)
493 message =
"<unnamed>"
499 "NAME : "//trim(message), &
500 "POSITION: "//trim(position_string), &
501 "ACCESS : "//trim(access_string), &
502 "FORM : "//trim(form_string), &
503 "ACTION : "//trim(action_string), &
504 "END DEBUG "//trim(routinen)