65 INTEGER,
INTENT(IN) :: unit_number
66 CHARACTER(LEN=*),
INTENT(IN),
OPTIONAL :: file_status
68 CHARACTER(LEN=11) :: status_string
69 CHARACTER(LEN=2*default_path_length) :: message
70 CHARACTER(LEN=default_path_length) :: iomsg_string
72 LOGICAL :: exists, is_named, is_open
74 INQUIRE (unit=unit_number, exist=exists, named=is_named, opened=is_open, iostat=istat)
77 WRITE (unit=message, fmt=
"(A,I0,A,I0,A)") &
78 "An error occurred inquiring the unit with the number ", unit_number, &
79 " (IOSTAT = ", istat,
")"
80 CALL cp_abort(__location__, trim(message))
81 ELSE IF (.NOT. exists)
THEN
82 WRITE (unit=message, fmt=
"(A,I0,A)") &
83 "The specified unit number ", unit_number, &
84 " cannot be closed, because it does not exist."
85 CALL cp_abort(__location__, trim(message))
93 SELECT CASE (unit_number)
95 WRITE (unit=message, fmt=
"(A,I0)") &
96 "Attempt to close the default error unit number ", unit_number
97 CALL cp_abort(__location__, trim(message))
99 WRITE (unit=message, fmt=
"(A,I0)") &
100 "Attempt to close the default input unit number ", unit_number
101 CALL cp_abort(__location__, trim(message))
103 WRITE (unit=message, fmt=
"(A,I0)") &
104 "Attempt to close the default output unit number ", unit_number
105 CALL cp_abort(__location__, trim(message))
109 IF (
PRESENT(file_status))
THEN
110 IF (len_trim(adjustl(file_status)) > len(status_string))
THEN
111 CALL cp_abort(__location__, &
112 "An invalid file STATUS="//trim(adjustl(file_status))// &
115 status_string = trim(adjustl(file_status))
117 ELSE IF (.NOT. is_named)
THEN
118 status_string =
"DELETE"
120 status_string =
"KEEP"
123 SELECT CASE (status_string)
124 CASE (
"DELETE",
"KEEP")
125 CLOSE (unit=unit_number, iostat=istat, iomsg=iomsg_string, status=trim(status_string))
127 CALL cp_abort(__location__, &
128 "An error occurred closing the file with the logical unit number "// &
129 trim(int_to_string(unit_number))//
" (IOSTAT="// &
130 trim(int_to_string(istat))//
"): "//trim(iomsg_string))
133 CALL cp_abort(__location__, &
134 "An invalid file STATUS="//trim(status_string)//
" was specified.")
195 SUBROUTINE open_file(file_name, unit_number, file_status, file_form, file_action, &
196 file_position, file_pad, file_access, debug)
198 CHARACTER(LEN=*),
INTENT(IN),
OPTIONAL :: file_name
199 INTEGER,
INTENT(OUT) :: unit_number
200 CHARACTER(LEN=*),
INTENT(IN),
OPTIONAL :: file_status, file_form, file_action, &
201 file_position, file_pad, file_access
202 LOGICAL,
INTENT(IN),
OPTIONAL :: debug
204 CHARACTER(LEN=*),
PARAMETER :: routinen =
'open_file'
206 CHARACTER(LEN=11) :: access_string, action_string, current_access, current_action, &
207 current_form, form_string, pad_string, position_string, status_string
208 CHARACTER(LEN=2*default_path_length) :: message
209 CHARACTER(LEN=default_path_length) :: cwd, iomsg_string, real_file_name
210 INTEGER :: inquire_stat, open_stat
211 LOGICAL :: connect, exists, is_named, is_open
216 IF (
PRESENT(file_access))
THEN
217 access_string = trim(adjustl(file_access))
219 IF (access_string ==
"DIRECT")
THEN
220 CALL cp_abort(__location__, &
221 "ACCESS=DIRECT is not supported.")
224 access_string =
"SEQUENTIAL"
227 IF (
PRESENT(file_status))
THEN
228 status_string = trim(adjustl(file_status))
231 status_string =
"OLD"
234 IF (
PRESENT(file_form))
THEN
235 form_string = trim(adjustl(file_form))
238 form_string =
"FORMATTED"
241 IF (
PRESENT(file_pad))
THEN
242 pad_string = trim(adjustl(file_pad))
244 IF (form_string ==
"UNFORMATTED")
THEN
245 CALL cp_abort(__location__, &
246 "The PAD specifier is not allowed for an UNFORMATTED file.")
252 IF (
PRESENT(file_action))
THEN
253 action_string = trim(adjustl(file_action))
257 SELECT CASE (status_string)
258 CASE (
"NEW",
"REPLACE",
"SCRATCH")
259 action_string =
"WRITE"
261 action_string =
"READ"
265 IF (
PRESENT(file_position))
THEN
266 position_string = trim(adjustl(file_position))
269 position_string =
"REWIND"
272 IF (
PRESENT(file_name))
THEN
273 IF (len_trim(file_name) == 0)
THEN
274 CALL cp_abort(__location__, &
275 "The specified FILE name is empty.")
277 IF (len_trim(file_name) > len(real_file_name))
THEN
278 CALL cp_abort(__location__, &
279 "The FILE name <"//trim(file_name)//
"> exceeds the maximum length of "// &
280 trim(int_to_string(len(real_file_name)))//
" characters.")
282 IF (file_name(1:1) ==
" ")
THEN
283 CALL cp_abort(__location__, &
284 "The FILE name <"//trim(file_name)//
"> has leading blanks.")
286 IF (status_string ==
"SCRATCH")
THEN
287 CALL cp_abort(__location__, &
288 "A FILE name specifier is not allowed for SCRATCH files.")
292 IF ((status_string ==
"OLD") .AND. (action_string ==
"READ"))
THEN
295 real_file_name = trim(adjustl(file_name))
299 SELECT CASE (status_string)
302 CASE (
"NEW",
"OLD",
"REPLACE",
"UNKNOWN")
303 IF (len_trim(real_file_name) == 0)
THEN
304 CALL cp_abort(__location__, &
305 "A FILE name length of zero for a file with STATUS="// &
306 trim(status_string)//
" is not allowed.")
309 CALL cp_abort(__location__,
"A file STATUS="//trim(status_string)//
" is not supported.")
322 IF (status_string ==
"SCRATCH")
THEN
325 INQUIRE (file=trim(real_file_name), exist=exists, opened=is_open, iostat=inquire_stat)
326 IF (inquire_stat == 0)
THEN
328 INQUIRE (file=trim(real_file_name), number=unit_number, &
329 action=current_action, form=current_form, access=current_access)
331 SELECT CASE (status_string)
333 connect = (.NOT. exists)
343 unit_number = get_unit_number()
344 IF (unit_number > 0)
THEN
345 IF (status_string ==
"SCRATCH")
THEN
346 IF (form_string ==
"FORMATTED")
THEN
347 OPEN (unit=unit_number, &
348 status=trim(status_string), &
349 access=trim(access_string), &
350 form=trim(form_string), &
351 position=trim(position_string), &
352 action=trim(action_string), &
353 pad=trim(pad_string), &
354 iomsg=iomsg_string, &
357 OPEN (unit=unit_number, &
358 status=trim(status_string), &
359 access=trim(access_string), &
360 form=trim(form_string), &
361 position=trim(position_string), &
362 action=trim(action_string), &
363 iomsg=iomsg_string, &
367 IF (form_string ==
"FORMATTED")
THEN
368 OPEN (unit=unit_number, &
369 file=trim(real_file_name), &
370 status=trim(status_string), &
371 access=trim(access_string), &
372 form=trim(form_string), &
373 position=trim(position_string), &
374 action=trim(action_string), &
375 pad=trim(pad_string), &
376 iomsg=iomsg_string, &
379 OPEN (unit=unit_number, &
380 file=trim(real_file_name), &
381 status=trim(status_string), &
382 access=trim(access_string), &
383 form=trim(form_string), &
384 position=trim(position_string), &
385 action=trim(action_string), &
386 iomsg=iomsg_string, &
394 IF (inquire_stat /= 0)
THEN
395 CALL cp_abort(__location__, &
396 "An error occurred inquiring the file <"//trim(real_file_name)// &
397 "> (IOSTAT="//trim(int_to_string(inquire_stat))//
").")
403 IF (trim(current_action) /= trim(action_string))
THEN
404 CALL cp_abort(__location__, &
405 "Attempt to re-open the existing file <"// &
406 trim(real_file_name)//
"> with the modified ACTION attribute "// &
407 trim(action_string)//
". The current ACTION attribute is "// &
408 trim(current_action)//
".")
411 IF (trim(current_form) /= trim(form_string))
THEN
412 CALL cp_abort(__location__, &
413 "Attempt to re-open the existing "// &
414 trim(current_form)//
" file <"//trim(real_file_name)// &
415 "> as "//trim(form_string)//
" file.")
418 IF (trim(current_access) /= trim(access_string))
THEN
419 CALL cp_abort(__location__, &
420 "Attempt to re-open the existing "// &
421 trim(current_access)//
" file <"//trim(real_file_name)// &
422 "> with ACCESS="//trim(access_string)//
".")
424 IF (status_string ==
"NEW")
THEN
425 CALL cp_abort(__location__, &
426 "Attempt to re-open the existing file <"// &
427 trim(real_file_name)//
"> with STATUS=NEW.")
429 IF (position_string ==
"REWIND") rewind(unit=unit_number)
432 SELECT CASE (status_string)
435 CALL cp_abort(__location__, &
436 "The file <"//trim(real_file_name)// &
437 "> cannot be opened with STATUS=NEW, because it already exists.")
440 IF (.NOT. exists)
THEN
441 IF (action_string ==
"READ")
THEN
442 CALL cp_abort(__location__, &
443 "The specified file <"//trim(real_file_name)// &
444 "> cannot be opened with STATUS=OLD. It does not exist. "// &
447 CALL cp_abort(__location__, &
448 "The specified file <"//trim(real_file_name)// &
449 "> cannot be opened with STATUS=OLD and ACTION="// &
450 trim(action_string)//
". It does not exist.")
454 IF (unit_number < 1)
THEN
455 IF (status_string ==
"SCRATCH")
THEN
456 CALL cp_abort(__location__, &
457 "Could not obtain a unit number to open a SCRATCH file.")
459 CALL cp_abort(__location__, &
460 "Cannot open the file <"//trim(real_file_name)// &
461 ">, because no unused logical unit number could be obtained.")
464 IF (open_stat /= 0)
THEN
465 IF (status_string ==
"SCRATCH")
THEN
466 CALL cp_abort(__location__, &
467 "An error occurred opening a SCRATCH file (UNIT="// &
468 trim(int_to_string(unit_number))//
", IOSTAT="// &
469 trim(int_to_string(open_stat))//
"): "//trim(iomsg_string))
472 CALL cp_abort(__location__, &
473 "An error occurred opening the file <"//trim(real_file_name)// &
474 "> (UNIT="//trim(int_to_string(unit_number))// &
475 ", IOSTAT="//trim(int_to_string(open_stat))//
"): "// &
476 trim(iomsg_string)//
". Current working directory: "//trim(cwd))
481 IF (
PRESENT(debug))
THEN
483 INQUIRE (unit=unit_number, &
486 position=position_string, &
487 access=access_string, &
489 action=action_string)
492 INQUIRE (unit=unit_number, name=message)
494 message =
"<unnamed>"
500 "NAME : "//trim(message), &
501 "POSITION: "//trim(position_string), &
502 "ACCESS : "//trim(access_string), &
503 "FORM : "//trim(form_string), &
504 "ACTION : "//trim(action_string), &
505 "END DEBUG "//trim(routinen)