V3FIT
file_opts.f
Go to the documentation of this file.
1 !*******************************************************************************
4 !
5 ! Note separating the Doxygen comment block here so detailed decription is
6 ! found in the Module not the file.
7 !
12 !*******************************************************************************
13  MODULE file_opts
14  USE system_mod
15 
16  IMPLICIT NONE
17 
18 !*******************************************************************************
19 ! file_opts module parameters
20 !*******************************************************************************
22  INTEGER, PARAMETER :: path_length = 300
23 
24  CONTAINS
25 
26 !-------------------------------------------------------------------------------
35 !-------------------------------------------------------------------------------
36  SUBROUTINE move_file(file_source, file_dest, error)
37 #if defined(__INTEL_COMPILER)
38  USE iflport
39 #endif
40 
41  IMPLICIT NONE
42 
43 ! Declare Arguments
44  CHARACTER (len=*), INTENT(in) :: file_source
45  CHARACTER (len=*), INTENT(in) :: file_dest
46  INTEGER, INTENT(out) :: error
47 
48 ! local parameters
49 ! To avoid any possible interactive command lines, make sure the command
50 ! overwrites the file if it exists.
51 #if defined(WIN32)
52  CHARACTER (len=*), PARAMETER :: cmd = 'move /y '
53 #else
54  CHARACTER (len=*), PARAMETER :: cmd = 'mv -f '
55 #endif
56 
57 ! Start of executable code
58 
59 #if defined(__GFORTRAN__) || defined(__INTEL_COMPILER)
60  error = rename(trim(file_source), trim(file_dest))
61 #else
62  CALL system(cmd // trim(file_source) // ' ' // file_dest, &
63  & error)
64 #endif
65  END SUBROUTINE
66 
67 !-------------------------------------------------------------------------------
76 !-------------------------------------------------------------------------------
77  SUBROUTINE copy_file(file_source, file_dest, error)
78  USE safe_open_mod
79 #if defined(FAST_COPY)
80  use, INTRINSIC :: iso_c_binding
81 #endif
82 
83  IMPLICIT NONE
84 
85 ! Declare Arguments
86  CHARACTER (len=*), INTENT(in) :: file_source
87  CHARACTER (len=*), INTENT(in) :: file_dest
88  INTEGER, INTENT(out) :: error
89 
90 ! local variables
91  LOGICAL :: l_exists
92  LOGICAL :: l_opened
93  INTEGER :: file_size
94  INTEGER :: max_block_size
95  INTEGER :: io_source
96  INTEGER :: io_dest
97  INTEGER :: n_rec
98  INTEGER :: i_rec
99  CHARACTER (len=1), ALLOCATABLE, DIMENSION(:) :: buffer
100 
101 ! local parameters
102  INTEGER, PARAMETER :: block_size = 1024
103 
104 #if defined(fast_copy)
105  INTERFACE
106  FUNCTION copy_file_c(src, dest) BIND(C)
107  use, INTRINSIC :: iso_c_binding
108 
109  IMPLICIT NONE
110 
111  INTEGER, PARAMETER :: length = 300
112  INTEGER (c_int) :: copy_file_c
113  CHARACTER (kind=c_char,len=1), DIMENSION(length), INTENT(in) :: &
114  & src
115  CHARACTER (kind=c_char,len=1), DIMENSION(length), INTENT(in) :: &
116  & dest
117  END FUNCTION
118  END INTERFACE
119 #endif
120 
121 ! Start of executable code
122 #if defined(FAST_COPY)
123  error = copy_file_c(trim(file_source) // c_null_char, &
124  & trim(file_dest) // c_null_char)
125 #else
126 
127 ! Check source file.
128  INQUIRE (file=file_source, exist=l_exists, opened=l_opened)
129  CALL getfilesize(file_source, file_size)
130 
131  IF (.not.l_exists) THEN
132  WRITE (*,1001) trim(file_source)
133  error = -1
134  RETURN
135  ELSE IF (l_opened) THEN
136  WRITE (*,1000) trim(file_source)
137  error = -1
138  RETURN
139  END IF
140 
141 ! Check destination file.
142  INQUIRE (file=file_dest, exist=l_exists, opened=l_opened)
143  IF (l_opened) THEN
144  WRITE (*,1000) trim(file_dest)
145  error = -1
146  RETURN
147  ELSE
148  CALL delete_file(file_dest, error)
149  END IF
150 
151  io_source = 10
152  io_dest = 11
153 
154  max_block_size = min(block_size, file_size)
155 
156  CALL safe_open(io_source, error, file_source, 'old', &
157  & 'unformatted', record_in=max_block_size, &
158  & access_in='direct')
159  IF (error .ne. 0) WRITE (*,1002) trim(file_source)
160 
161  CALL safe_open(io_dest, error, file_dest, 'new', 'unformatted', &
162  & record_in=max_block_size, access_in='direct')
163  IF (error .ne. 0) WRITE (*,1003) trim(file_dest)
164 
165  ALLOCATE (buffer(max_block_size), stat=error)
166  IF (error .ne. 0) WRITE (*,1004)
167 
168 ! Write file in block sized chunks.
169  n_rec = max(1, file_size/max_block_size)
170  DO i_rec = 1, n_rec
171  READ (io_source, rec=i_rec) buffer
172  WRITE (io_dest, rec=i_rec) buffer
173  END DO
174 
175  CLOSE (io_source)
176  CLOSE (io_dest)
177 
178 ! The block size may not bet an even multiple of the file size. Copy the
179 ! remaining part in 1 byte chunks.
180  IF (file_size - (i_rec - 1)*max_block_size .gt. 0) THEN
181  OPEN (unit=io_source, file=file_source, status='old', &
182  & access='direct', recl=1)
183  OPEN (unit=io_dest, file=file_dest, status='old', &
184  & access='direct', recl=1)
185 
186  DO i_rec = i_rec, file_size
187  READ (io_source, rec=i_rec) buffer(1)
188  WRITE (io_dest, rec=i_rec) buffer(1)
189  END DO
190 
191  CLOSE (io_source)
192  CLOSE (io_dest)
193  END IF
194 
195  DEALLOCATE (buffer)
196 
197 1000 FORMAT('ERROR: File ',a,' is already opened.')
198 1001 FORMAT('ERROR: File ',a,' does not exist.')
199 1002 FORMAT('ERROR: Fail to open ',a,'.')
200 1003 FORMAT('ERROR: Fail to create ',a,'.')
201 1004 FORMAT('ERROR: Fail to allocate copy buffer.')
202 #endif
203  END SUBROUTINE
204 
205 !-------------------------------------------------------------------------------
213 !-------------------------------------------------------------------------------
214  SUBROUTINE delete_file(file_source, error)
215 #if defined(__INTEL_COMPILER)
216  USE iflport
217 #endif
218  USE safe_open_mod
219 
220  IMPLICIT NONE
221 
222 ! Declare Arguments
223  CHARACTER (len=*), INTENT(in) :: file_source
224  INTEGER, INTENT(out) :: error
225 
226 ! local variables
227  INTEGER :: io_unit
228 
229 ! Start of executable code
230 #if defined(__GFORTRAN__) || defined(__INTEL_COMPILER)
231  error = unlink(file_source)
232 #else
233  io_unit = 10
234  CALL safe_open(io_unit, error, file_source, 'old', 'formatted')
235  CLOSE (io_unit, iostat=error, status='delete')
236 #endif
237 
238  END SUBROUTINE
239 
240 !-------------------------------------------------------------------------------
247 !-------------------------------------------------------------------------------
248  SUBROUTINE create_directory(directory_source, error)
249 
250  IMPLICIT NONE
251 
252 ! Declare Arguments
253  CHARACTER (len=*), INTENT(in) :: directory_source
254  INTEGER, INTENT(out) :: error
255 
256 ! local parameters
257 #if defined(WIN32)
258  CHARACTER (len=*), PARAMETER :: cmd = 'mkdir '
259 #else
260  CHARACTER (len=*), PARAMETER :: cmd = 'mkdir -p '
261 #endif
262 
263 ! Start of executable code
264 
265  CALL system(cmd // trim(directory_source), error)
266 
267  END SUBROUTINE
268 
269 !-------------------------------------------------------------------------------
277 !-------------------------------------------------------------------------------
278  SUBROUTINE delete_directory(directory_source, error)
279  IMPLICIT NONE
280 
281 ! Declare Arguments
282  CHARACTER (len=*), INTENT(in) :: directory_source
283  INTEGER, INTENT(out) :: error
284 
285 ! local parameters
286 #if defined(WIN32)
287  CHARACTER (len=*), PARAMETER :: cmd = 'rmdir /Q /S '
288 #else
289  CHARACTER (len=*), PARAMETER :: cmd = 'rm -rf '
290 #endif
291 
292 ! Start of executable code
293 
294  CALL system(cmd // trim(directory_source), error)
295 
296  END SUBROUTINE
297 
298 !-------------------------------------------------------------------------------
307 !-------------------------------------------------------------------------------
308  FUNCTION is_absolute_path(path)
309 
310  IMPLICIT NONE
311 
312 ! Declare Arguments
313  LOGICAL :: is_absolute_path
314  CHARACTER (len=*), INTENT(in) :: path
315 
316 ! Start of executable code
317 ! Treat empty paths as absolute.
318  IF (path .eq. '') THEN
319  is_absolute_path = .true.
320  RETURN
321  END IF
322 
323 #if defined(WIN32)
324 ! On windows systems, root paths start with a variey of cases. Taken from
325 !
326 ! http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#paths
327 !
328 ! * A UNC name of any format, which always start with two backslash characters
329 ! ("\\"). For more information, see the next section.
330 ! * A disk designator with a backslash, for example "C:\" or "d:\".
331 ! * A single backslash, for example, "\directory" or "\file.txt". This is
332 ! also referred to as an absolute path.
333 !
334  is_absolute_path = (path(1:2) .eq. '\\') .or. &
335  & (index(path, ':\') .eq. 0) .or. &
336  & (path(1:1) .eq. '\')
337 #else
338 ! On unix systems, an absolute path starts with either / or ~
339  is_absolute_path = (path(1:1) .eq. '/') .or. &
340  & (path(1:1) .eq. '~')
341 #endif
342 
343  END FUNCTION
344 
345 !-------------------------------------------------------------------------------
352 !-------------------------------------------------------------------------------
353  FUNCTION get_path_of_file(file)
354 
355  IMPLICIT NONE
356 
357 ! Declare Arguments
358  CHARACTER (len=path_length) :: get_path_of_file
359  CHARACTER (len=*), INTENT(in) :: file
360 
361 ! Start of executable code
362 #if defined(win32)
363  get_path_of_file = &
364  & file(1:(index(trim(file), '\', back = .true.) - 1))
365 #else
366  get_path_of_file = &
367  & file(1:(index(trim(file), '/', back = .true.) - 1))
368 #endif
369 
370  END FUNCTION
371 
372 !-------------------------------------------------------------------------------
380 !-------------------------------------------------------------------------------
381  FUNCTION build_path(path, name)
382 
383  IMPLICIT NONE
384 
385 ! Declare Arguments
386  CHARACTER (len=path_length) :: build_path
387  CHARACTER (len=*), INTENT(in) :: path
388  CHARACTER (len=*), INTENT(in) :: name
389 
390 ! Start of executable code
391  IF (trim(path) .eq. '') THEN
392  build_path = name
393  RETURN
394  END IF
395 
396 #if defined(WIN32)
397  build_path = trim(path) // '\' // trim(name)
398 #else
399  build_path = trim(path) // '/' // trim(name)
400 #endif
401 
402  END FUNCTION
403 
404 !-------------------------------------------------------------------------------
412 !-------------------------------------------------------------------------------
413  SUBROUTINE change_directory(path, error)
414 #if defined(__INTEL_COMPILER)
415  USE iflport
416 #endif
417 
418  IMPLICIT NONE
419 
420 ! Declare Arguments
421  CHARACTER (len=*), INTENT(in) :: path
422  INTEGER, INTENT(out) :: error
423 
424 ! local parameters
425  error = chdir(trim(path))
426 
427  END SUBROUTINE
428 
429  END MODULE
system_mod::chdir
Definition: system_mod.f:10
file_opts::change_directory
subroutine change_directory(path, error)
Change working directory.
Definition: file_opts.f:414
system_mod::system
Definition: system_mod.f:3
file_opts::copy_file
subroutine copy_file(file_source, file_dest, error)
Copies the source file to the destination.
Definition: file_opts.f:78
file_opts::get_path_of_file
character(len=path_length) function get_path_of_file(file)
Returns the directory of a file.
Definition: file_opts.f:354
file_opts::is_absolute_path
logical function is_absolute_path(path)
Queries if the a path is absoulte.
Definition: file_opts.f:309
file_opts::delete_file
subroutine delete_file(file_source, error)
Deletes a file.
Definition: file_opts.f:215
file_opts::delete_directory
subroutine delete_directory(directory_source, error)
Deletes a directory.
Definition: file_opts.f:279
file_opts::move_file
subroutine move_file(file_source, file_dest, error)
Moves the source file to the destination.
Definition: file_opts.f:37
file_opts
Contains cross platform routines for manipulating files on the file system. Defines a functions to mo...
Definition: file_opts.f:13
copy_file_c
int copy_file_c(const char *src, const char *dest)
Copy a file from the source to the destination.
Definition: copy_file.c:69
file_opts::build_path
character(len=path_length) function build_path(path, name)
Builds a path.
Definition: file_opts.f:382
file_opts::create_directory
subroutine create_directory(directory_source, error)
Makes a directory.
Definition: file_opts.f:249
file_opts::path_length
integer, parameter path_length
Length of file paths.
Definition: file_opts.f:22