(git:88442da)
Loading...
Searching...
No Matches
cp_files.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief Utility routines to open and close files.
10!> \par History
11!> - Creation CP2K_WORKSHOP 1.0 TEAM
12!> - Revised (18.02.2011, MK)
13!> - Enhanced error checking (22.02.2011, MK)
14!> - Preconnected units removed (03.10.2026, MK)
15!> \note Positive unit numbers are used instead of OPEN(NEWUNIT=...), because NEWUNIT returns
16!> negative numbers and CP2K treats a unit number > 0 as "output enabled".
17!> open_file is thread-safe: finding a free unit and connecting the file are done within
18!> the OpenMP critical region cp_files_connect. Concurrent use of the same unit (I/O,
19!> REWIND, close_file) must be avoided by the caller.
20!> \author Matthias Krack (MK)
21! **************************************************************************************************
23 USE iso_c_binding, ONLY: c_char,&
24 c_f_pointer,&
25 c_null_char,&
26 c_ptr
27 USE kinds, ONLY: default_path_length
28 USE machine, ONLY: default_error_unit,&
33#include "../base/base_uses.f90"
34
35 IMPLICIT NONE
36
37 PRIVATE
38
39 PUBLIC :: close_file, &
40 open_file, &
46
47 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_files'
48
49 ! Upper bound for the search of a free logical unit number
50 INTEGER, PARAMETER :: max_unit_number = 1024
51
52CONTAINS
53
54! **************************************************************************************************
55!> \brief Closes an open file given by its logical unit number.
56!> \param unit_number Logical unit number of the file to be closed
57!> \param file_status File status after closing: KEEP or DELETE (default: KEEP for named,
58!> DELETE for unnamed SCRATCH files)
59!> \author Matthias Krack (MK)
60!> \note The program is aborted, if the unit is one of the default units (stdin, stdout,
61!> stderr). A unit which is not open is ignored silently.
62! **************************************************************************************************
63 SUBROUTINE close_file(unit_number, file_status)
64
65 INTEGER, INTENT(IN) :: unit_number
66 CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: file_status
67
68 CHARACTER(LEN=11) :: status_string
69 CHARACTER(LEN=2*default_path_length) :: message
70 CHARACTER(LEN=default_path_length) :: iomsg_string
71 INTEGER :: istat
72 LOGICAL :: exists, is_named, is_open
73
74 INQUIRE (unit=unit_number, exist=exists, named=is_named, opened=is_open, iostat=istat)
75
76 IF (istat /= 0) THEN
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))
86 END IF
87
88 ! Close the specified file
89
90 IF (is_open) THEN
91
92 ! Refuse to close any default unit
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))
106 END SELECT
107
108 ! Define status after closing the file (KEEP for named, DELETE for unnamed SCRATCH files)
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))// &
113 " was specified.")
114 END IF
115 status_string = trim(adjustl(file_status))
116 CALL uppercase(status_string)
117 ELSE IF (.NOT. is_named) THEN
118 status_string = "DELETE"
119 ELSE
120 status_string = "KEEP"
121 END IF
122
123 SELECT CASE (status_string)
124 CASE ("DELETE", "KEEP")
125 CLOSE (unit=unit_number, iostat=istat, iomsg=iomsg_string, status=trim(status_string))
126 IF (istat /= 0) THEN
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))
131 END IF
132 CASE DEFAULT
133 CALL cp_abort(__location__, &
134 "An invalid file STATUS="//trim(status_string)//" was specified.")
135 END SELECT
136
137 END IF
138
139 ! A unit which is not open is ignored silently. close_file is called by cp_logger_release,
140 ! e.g. for a unit shared by several loggers and already closed by one of them. No warning can
141 ! be issued at that point, since the logger being released might still be the default logger.
142
143 END SUBROUTINE close_file
144
145! **************************************************************************************************
146!> \brief Returns the first free positive logical unit number.
147!> \return First free unit in 1..max_unit_number (excluding the default units), or -1 if none
148!> is free
149!> \author Matthias Krack (MK)
150!> \note Must be called within the critical region cp_files_connect together with the OPEN
151!> statement, so that no other thread can obtain the same unit number in between.
152! **************************************************************************************************
153 FUNCTION get_unit_number() RESULT(unit_number)
154
155 INTEGER :: unit_number
156
157 INTEGER :: istat
158 LOGICAL :: exists, is_open
159
160 DO unit_number = 1, max_unit_number
161 SELECT CASE (unit_number)
163 cycle
164 CASE DEFAULT
165 INQUIRE (unit=unit_number, exist=exists, opened=is_open, iostat=istat)
166 IF (exists .AND. (.NOT. is_open) .AND. (istat == 0)) RETURN
167 END SELECT
168 END DO
169
170 unit_number = -1
171
172 END FUNCTION get_unit_number
173
174! **************************************************************************************************
175!> \brief Opens a file and returns its logical unit number.
176!> \param file_name File name; must be omitted for an anonymous SCRATCH file
177!> \param unit_number Logical unit number of the opened file
178!> \param file_status Fortran file status OLD, NEW, REPLACE, UNKNOWN, or SCRATCH for an
179!> anonymous temporary file (default: OLD)
180!> \param file_form FORMATTED or UNFORMATTED (default: FORMATTED)
181!> \param file_action READ, WRITE, or READWRITE (default: WRITE for STATUS=NEW, REPLACE, and
182!> SCRATCH, otherwise READ)
183!> \param file_position ASIS, REWIND, or APPEND (default: REWIND)
184!> \param file_pad YES or NO (default: YES); only allowed for FORMATTED files
185!> \param file_access SEQUENTIAL or STREAM (default: SEQUENTIAL); DIRECT is not supported
186!> \param debug Print the connection properties to the default output unit
187!> \author Matthias Krack (MK)
188!> \note - If the file is already open, its unit number is returned and no new connection is
189!> made. ACTION, FORM, and ACCESS must match the existing connection; with
190!> POSITION=REWIND the file is rewound.
191!> - For STATUS=OLD and ACTION=READ, the file is also looked up in the data directory
192!> (see discover_file). Files opened for writing are never taken from there.
193!> - Any error aborts the program; there is no IOSTAT return.
194! **************************************************************************************************
195 SUBROUTINE open_file(file_name, unit_number, file_status, file_form, file_action, &
196 file_position, file_pad, file_access, debug)
197
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
203
204 CHARACTER(LEN=*), PARAMETER :: routinen = 'open_file'
205
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
212
213 real_file_name = ""
214 unit_number = -1
215
216 IF (PRESENT(file_access)) THEN
217 access_string = trim(adjustl(file_access))
218 CALL uppercase(access_string)
219 IF (access_string == "DIRECT") THEN
220 CALL cp_abort(__location__, &
221 "ACCESS=DIRECT is not supported.")
222 END IF
223 ELSE
224 access_string = "SEQUENTIAL"
225 END IF
226
227 IF (PRESENT(file_status)) THEN
228 status_string = trim(adjustl(file_status))
229 CALL uppercase(status_string)
230 ELSE
231 status_string = "OLD"
232 END IF
233
234 IF (PRESENT(file_form)) THEN
235 form_string = trim(adjustl(file_form))
236 CALL uppercase(form_string)
237 ELSE
238 form_string = "FORMATTED"
239 END IF
240
241 IF (PRESENT(file_pad)) THEN
242 pad_string = trim(adjustl(file_pad))
243 CALL uppercase(pad_string)
244 IF (form_string == "UNFORMATTED") THEN
245 CALL cp_abort(__location__, &
246 "The PAD specifier is not allowed for an UNFORMATTED file.")
247 END IF
248 ELSE
249 pad_string = "YES"
250 END IF
251
252 IF (PRESENT(file_action)) THEN
253 action_string = trim(adjustl(file_action))
254 CALL uppercase(action_string)
255 ELSE
256 ! A new or emptied file cannot be read, thus WRITE is the only sensible default
257 SELECT CASE (status_string)
258 CASE ("NEW", "REPLACE", "SCRATCH")
259 action_string = "WRITE"
260 CASE DEFAULT
261 action_string = "READ"
262 END SELECT
263 END IF
264
265 IF (PRESENT(file_position)) THEN
266 position_string = trim(adjustl(file_position))
267 CALL uppercase(position_string)
268 ELSE
269 position_string = "REWIND"
270 END IF
271
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.")
276 END IF
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.")
281 END IF
282 IF (file_name(1:1) == " ") THEN
283 CALL cp_abort(__location__, &
284 "The FILE name <"//trim(file_name)//"> has leading blanks.")
285 END IF
286 IF (status_string == "SCRATCH") THEN
287 CALL cp_abort(__location__, &
288 "A FILE name specifier is not allowed for SCRATCH files.")
289 END IF
290 ! Resolve the file name. The data directory is only searched for existing files
291 ! opened read-only, so that installed data files are never modified.
292 IF ((status_string == "OLD") .AND. (action_string == "READ")) THEN
293 real_file_name = discover_file(file_name)
294 ELSE
295 real_file_name = trim(adjustl(file_name))
296 END IF
297 END IF
298
299 SELECT CASE (status_string)
300 CASE ("SCRATCH")
301 ! No file name required
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.")
307 END IF
308 CASE DEFAULT
309 CALL cp_abort(__location__, "A file STATUS="//trim(status_string)//" is not supported.")
310 END SELECT
311
312 ! Inquire the file, find a free unit, and connect the file atomically with respect to other
313 ! threads. All checks and error messages follow outside the critical region, because
314 ! cp_abort and cp_warn may call open_file again via the logger, which would deadlock.
315 connect = .false.
316 exists = .false.
317 is_open = .false.
318 inquire_stat = 0
319 open_stat = 0
320 iomsg_string = ""
321!$OMP CRITICAL(cp_files_connect)
322 IF (status_string == "SCRATCH") THEN
323 connect = .true.
324 ELSE
325 INQUIRE (file=trim(real_file_name), exist=exists, opened=is_open, iostat=inquire_stat)
326 IF (inquire_stat == 0) THEN
327 IF (is_open) THEN
328 INQUIRE (file=trim(real_file_name), number=unit_number, &
329 action=current_action, form=current_form, access=current_access)
330 ELSE
331 SELECT CASE (status_string)
332 CASE ("NEW")
333 connect = (.NOT. exists)
334 CASE ("OLD")
335 connect = exists
336 CASE DEFAULT
337 connect = .true.
338 END SELECT
339 END IF
340 END IF
341 END IF
342 IF (connect) THEN
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, &
355 iostat=open_stat)
356 ELSE
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, &
364 iostat=open_stat)
365 END IF
366 ELSE
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, &
377 iostat=open_stat)
378 ELSE
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, &
387 iostat=open_stat)
388 END IF
389 END IF
390 END IF
391 END IF
392!$OMP END CRITICAL(cp_files_connect)
393
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))//").")
398 END IF
399
400 IF (is_open) THEN
401 ! The file is already connected: check the connection and reuse it
402 CALL uppercase(current_action)
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)//".")
409 END IF
410 CALL uppercase(current_form)
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.")
416 END IF
417 CALL uppercase(current_access)
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)//".")
423 END IF
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.")
428 END IF
429 IF (position_string == "REWIND") rewind(unit=unit_number)
430 ELSE
431 ! Check the outcome of the connection attempt
432 SELECT CASE (status_string)
433 CASE ("NEW")
434 IF (exists) THEN
435 CALL cp_abort(__location__, &
436 "The file <"//trim(real_file_name)// &
437 "> cannot be opened with STATUS=NEW, because it already exists.")
438 END IF
439 CASE ("OLD")
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. "// &
445 "Data directory path: "//trim(get_data_dir()))
446 ELSE
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.")
451 END IF
452 END IF
453 END SELECT
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.")
458 ELSE
459 CALL cp_abort(__location__, &
460 "Cannot open the file <"//trim(real_file_name)// &
461 ">, because no unused logical unit number could be obtained.")
462 END IF
463 END IF
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))
470 ELSE
471 CALL m_getcwd(cwd)
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))
477 END IF
478 END IF
479 END IF
480
481 IF (PRESENT(debug)) THEN
482 IF (debug) THEN
483 INQUIRE (unit=unit_number, &
484 opened=is_open, &
485 named=is_named, &
486 position=position_string, &
487 access=access_string, &
488 form=form_string, &
489 action=action_string)
490 ! The NAME specifier is undefined for unnamed (SCRATCH) files
491 IF (is_named) THEN
492 INQUIRE (unit=unit_number, name=message)
493 ELSE
494 message = "<unnamed>"
495 END IF
496 WRITE (unit=default_output_unit, fmt="(T2,A)") "BEGIN DEBUG "//trim(routinen)
497 WRITE (unit=default_output_unit, fmt="(T3,A,I0)") "NUMBER : ", unit_number
498 WRITE (unit=default_output_unit, fmt="(T3,A,L1)") "OPENED : ", is_open
499 WRITE (unit=default_output_unit, fmt="(T3,A)") &
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)
506 END IF
507 END IF
508
509 END SUBROUTINE open_file
510
511! **************************************************************************************************
512!> \brief Checks if a file exists, considering also the file discovery mechanism.
513!> \param file_name Name of the file
514!> \return True, if the file exists in the current or the data directory
515!> \author Ole Schuett
516! **************************************************************************************************
517 FUNCTION file_exists(file_name) RESULT(exist)
518 CHARACTER(LEN=*), INTENT(IN) :: file_name
519 LOGICAL :: exist
520
521 CHARACTER(LEN=default_path_length) :: real_file_name
522 INTEGER :: istat
523
524 real_file_name = discover_file(file_name)
525 INQUIRE (file=trim(real_file_name), exist=exist, iostat=istat)
526 IF (istat /= 0) exist = .false.
527
528 END FUNCTION file_exists
529
530! **************************************************************************************************
531!> \brief Searches for a file in the current directory and then in the data directory.
532!> \param file_name Name of the file
533!> \return File name as given, if it exists, otherwise its path in the data directory, if
534!> it exists there, else the file name stripped of blanks
535!> \author Ole Schuett
536! **************************************************************************************************
537 FUNCTION discover_file(file_name) RESULT(real_file_name)
538 CHARACTER(LEN=*), INTENT(IN) :: file_name
539 CHARACTER(LEN=default_path_length) :: real_file_name
540
541 CHARACTER(LEN=default_path_length) :: candidate, data_dir
542 INTEGER :: stat
543 LOGICAL :: exists
544
545 ! Strip leading and trailing blanks from file name
546 real_file_name = trim(adjustl(file_name))
547
548 IF (len_trim(real_file_name) == 0) THEN
549 CALL cp_abort(__location__, "A file name length of zero for an existing file is invalid.")
550 END IF
551
552 ! First try file name directly
553 INQUIRE (file=trim(real_file_name), exist=exists, iostat=stat)
554 IF (stat == 0 .AND. exists) RETURN
555
556 ! Then try the data directory
557 data_dir = get_data_dir()
558 IF (len_trim(data_dir) > 0) THEN
559 candidate = join_paths(data_dir, real_file_name)
560 INQUIRE (file=trim(candidate), exist=exists, iostat=stat)
561 IF (stat == 0 .AND. exists) THEN
562 real_file_name = candidate
563 RETURN
564 END IF
565 END IF
566
567 END FUNCTION discover_file
568
569! **************************************************************************************************
570!> \brief Returns path of data directory if set, otherwise an empty string
571!> \return Path of the data directory or an empty string
572!> \author Ole Schuett
573! **************************************************************************************************
574 FUNCTION get_data_dir() RESULT(res)
575 CHARACTER(len=default_path_length) :: res
576
577 CHARACTER(LEN=1, KIND=C_CHAR), DIMENSION(:), &
578 POINTER :: path_f
579 INTEGER :: i
580 TYPE(c_ptr) :: path_c
581 INTERFACE
582 FUNCTION get_data_dir_c() BIND(C, name="get_data_dir")
583 IMPORT :: c_ptr
584 TYPE(c_ptr) :: get_data_dir_c
585 END FUNCTION get_data_dir_c
586 END INTERFACE
587
588 path_c = get_data_dir_c()
589 CALL c_f_pointer(path_c, path_f, shape=[default_path_length])
590
591 res = ""
592 DO i = 1, default_path_length
593 IF (path_f(i) == c_null_char) RETURN
594 res(i:i) = path_f(i)
595 END DO
596
597 CALL cp_abort(__location__, "The CP2K_DATA_DIR path is too long")
598
599 END FUNCTION get_data_dir
600
601! **************************************************************************************************
602!> \brief Returns the build directory
603!> \return ...
604! **************************************************************************************************
605 FUNCTION get_compile_dir() RESULT(res)
606 CHARACTER(len=default_path_length) :: res
607
608 CHARACTER(LEN=1, KIND=C_CHAR), DIMENSION(:), &
609 POINTER :: path_f
610 INTEGER :: i
611 TYPE(c_ptr) :: path_c
612 INTERFACE
613 FUNCTION get_compile_dir_c() BIND(C, name="get_compile_dir")
614 IMPORT :: c_ptr
615 TYPE(c_ptr) :: get_compile_dir_c
616 END FUNCTION get_compile_dir_c
617 END INTERFACE
618
619 path_c = get_compile_dir_c()
620 CALL c_f_pointer(path_c, path_f, shape=[default_path_length])
621
622 res = ""
623 DO i = 1, default_path_length
624 IF (path_f(i) == c_null_char) RETURN
625 res(i:i) = path_f(i)
626 END DO
627
628 CALL cp_abort(__location__, "The compile directory path is too long")
629
630 END FUNCTION get_compile_dir
631
632! **************************************************************************************************
633!> \brief Joins two file paths, inserting '/' as needed.
634!> \param path1 Leading path, e.g. a directory
635!> \param path2 Trailing path; returned unchanged if it is an absolute path
636!> \return Joined path; the program is aborted, if it exceeds default_path_length
637!> \author Ole Schuett
638! **************************************************************************************************
639 FUNCTION join_paths(path1, path2) RESULT(joined_path)
640 CHARACTER(LEN=*), INTENT(IN) :: path1, path2
641 CHARACTER(LEN=default_path_length) :: joined_path
642
643 INTEGER :: joined_length, n1, n2
644 LOGICAL :: absolute
645
646 n1 = len_trim(path1)
647 n2 = len_trim(path2)
648
649 absolute = .false.
650 IF (n2 > 0) absolute = (path2(1:1) == '/')
651
652 ! Determine the length of the joined path before assigning it to avoid silent truncation
653 IF (absolute .OR. (n1 == 0)) THEN
654 joined_length = n2
655 ELSE IF (path1(n1:n1) == '/') THEN
656 joined_length = n1 + n2
657 ELSE
658 joined_length = n1 + 1 + n2
659 END IF
660 IF (joined_length > len(joined_path)) THEN
661 CALL cp_abort(__location__, &
662 "The joined path of <"//path1(1:n1)//"> and <"//path2(1:n2)// &
663 "> exceeds the maximum length of "// &
664 trim(int_to_string(len(joined_path)))//" characters.")
665 END IF
666
667 IF (absolute .OR. (n1 == 0)) THEN
668 joined_path = path2(1:n2)
669 ELSE IF (path1(n1:n1) == '/') THEN
670 joined_path = path1(1:n1)//path2(1:n2)
671 ELSE
672 joined_path = path1(1:n1)//'/'//path2(1:n2)
673 END IF
674
675 END FUNCTION join_paths
676
677! **************************************************************************************************
678!> \brief Converts an integer to a left-adjusted string.
679!> \param i Integer value
680!> \return String representation of i
681!> \author Matthias Krack (MK)
682! **************************************************************************************************
683 FUNCTION int_to_string(i) RESULT(string)
684 INTEGER, INTENT(IN) :: i
685 CHARACTER(LEN=11) :: string
686
687 WRITE (unit=string, fmt="(I0)") i
688
689 END FUNCTION int_to_string
690
691END MODULE cp_files
Utility routines to open and close files.
Definition cp_files.F:22
character(len=default_path_length) function, public get_compile_dir()
Returns the build directory.
Definition cp_files.F:606
character(len=default_path_length) function, public discover_file(file_name)
Searches for a file in the current directory and then in the data directory.
Definition cp_files.F:538
logical function, public file_exists(file_name)
Checks if a file exists, considering also the file discovery mechanism.
Definition cp_files.F:518
subroutine, public open_file(file_name, unit_number, file_status, file_form, file_action, file_position, file_pad, file_access, debug)
Opens a file and returns its logical unit number.
Definition cp_files.F:197
character(len=default_path_length) function, public join_paths(path1, path2)
Joins two file paths, inserting '/' as needed.
Definition cp_files.F:640
subroutine, public close_file(unit_number, file_status)
Closes an open file given by its logical unit number.
Definition cp_files.F:64
character(len=default_path_length) function, public get_data_dir()
Returns path of data directory if set, otherwise an empty string.
Definition cp_files.F:575
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public default_path_length
Definition kinds.F:58
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
integer, parameter, public default_output_unit
Definition machine.F:49
integer, parameter, public default_input_unit
Definition machine.F:49
subroutine, public m_getcwd(curdir)
...
Definition machine.F:628
integer, parameter, public default_error_unit
Definition machine.F:49
Utilities for string manipulations.
elemental subroutine, public uppercase(string)
Convert all lower case characters in a string to upper case.