TreeMig Code
Loading...
Searching...
No Matches
FileHandlingModule.f90
Go to the documentation of this file.
1! uses preprocessor
2!==============================================================================
3! Module FileHandlingModule
4! *****************************************
5! contains
6! type declarations and constants
7! subroutines openFileR, openFileW, closeFile, deleteFile, extractWord, ExtractAndCountWords
8!==============================================================================
9
11 !==============================================================================
12 ! Type Declaration and constants
13 ! *****************
14 ! Type Declaration of a general file
15 ! different constants for file handling
16 !==============================================================================
17 TYPE file
18 INTEGER :: unit
19 character(len=:), allocatable :: path
20end TYPE file
21#if defined(_WIN32)
22character(2):: relativepathletter = ".\"
23character(1):: pathletter = "\"
24#else
25character(2):: relativepathletter = "./"
26character(1):: pathletter = "/"
27#endif
28character(len=3), parameter :: format_integer = "I15"
29character(len=5), parameter :: format_real = "F18.6"
30character(len=2), parameter :: format_logical = "L1"
31character(len=1), parameter :: format_character = "A"
32contains
33
34!==============================================================================
48!==============================================================================
49SUBROUTINE openfiler(theFile, err)
50 IMPLICIT NONE
51 TYPE(file), INTENT(in) :: theFile
52 INTEGER, INTENT(out) :: err
53 err = 0
54 open(action='read', file=thefile%path, unit=thefile%unit, iostat=err)
55end SUBROUTINE openfiler
56
57!==============================================================================
71!==============================================================================
72SUBROUTINE openfilew(theFile, err)
73 IMPLICIT NONE
74 TYPE(file), INTENT(in) :: theFile
75 INTEGER, INTENT(out) :: err
76 err = 0
77 open(action='write', file=thefile%path, unit=thefile%unit,status="replace", iostat=err)
78end SUBROUTINE openfilew
79
80!==============================================================================
96!==============================================================================
97SUBROUTINE closefile(theFile)
98 IMPLICIT NONE
99 TYPE(file), INTENT(in) :: theFile
100 close(thefile%unit)
101end SUBROUTINE closefile
102!==============================================================================
111!==============================================================================
112SUBROUTINE deletefile(theFile)
113 IMPLICIT NONE
114 TYPE(file), INTENT(in) :: theFile
115 INTEGER :: err
116 open(unit=thefile%unit, iostat=err, file=thefile%path, status='old')
117 if (err == 0) close(thefile%unit, status='delete')
118end SUBROUTINE deletefile
119
120!==============================================================================
140!==============================================================================
141SUBROUTINE extractword(theFile, n, word, rest, err)
142 IMPLICIT NONE
143 TYPE(file), INTENT(in) :: theFile
144 INTEGER, INTENT(in) :: n
145 character(len=:), allocatable, INTENT(out) :: word
146 character(len=:), allocatable, INTENT(out) :: rest
147 INTEGER, INTENT(out) :: err
148 character(len=1024) :: buffer
149 INTEGER startPos, endPos, i, j
150 !--- read line into buffer
151
152 read(thefile%unit, "("//format_character//")", iostat=err) buffer
153 !--- find start and end of nth non blank sequence in buffer
154 j= 1
155 do i = 1, n
156 do while(j < len(buffer) .and. buffer(j:j) == " ")
157 j = j+1
158 end do
159 endpos = j+index(buffer(j:len(buffer)), " ")-2
160 startpos = j
161 j = endpos+1
162 end do
163 !--- store this sequence into word
164 word = buffer(startpos:endpos)
165 rest = trim(buffer(endpos+1:len(buffer)))
166end SUBROUTINE extractword
167
168!==============================================================================
189!==============================================================================
190SUBROUTINE extractandcountwords(theFile, nwords, words, rest, err)
191 IMPLICIT NONE
192 TYPE(file), INTENT(in) :: theFile
193 INTEGER, INTENT(out) :: nwords
194 character(len=30) , INTENT(out) :: words (1:50)
195 character(len=:), INTENT(out), allocatable :: rest
196 INTEGER, INTENT(out) :: err
197 character(len=1024) :: buffer
198 character(len=:), allocatable :: word
199 INTEGER startPos, endPos, j
200 read(thefile%unit, "("//format_character//")", iostat=err) buffer
201 j= 1
202 nwords= 0
203 words = " "
204 word=" "
205 rest = trim(buffer)
206 ! search the words up to the "!" separator; find the start and end positions of these words within buffer.
207 do while(nwords <= 50 .and. len(rest) > 0 .and. word /="!")
208 nwords=nwords+1
209 do while(j < len(buffer) .and. buffer(j:j) == " ")
210 j = j+1
211 end do
212 endpos = j+index(buffer(j:len(buffer)), " ")-2
213 startpos = j
214 j = endpos+1
215 word = buffer(startpos:endpos) ! the new word
216 if (word =="!") then
217 nwords = nwords - 1
218 rest = trim(buffer(endpos :len(buffer)))
219 else
220 words(nwords)=word ! store to the vector of words
221 rest = trim(buffer(endpos+1:len(buffer)))
222 end if
223 end do
224end SUBROUTINE extractandcountwords
225!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
226end module filehandlingmodule
227
228
229
subroutine extractword(thefile, n, word, rest, err)
extractWord
character(2) relativepathletter
subroutine closefile(thefile)
closeFile
subroutine openfiler(thefile, err)
openFileR
character(len=1), parameter format_character
character(len=5), parameter format_real
character(len=2), parameter format_logical
character(len=3), parameter format_integer
subroutine extractandcountwords(thefile, nwords, words, rest, err)
ExtractAndCountWords
subroutine openfilew(thefile, err)
openFileW
subroutine deletefile(thefile)
deleteFile