-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpwhg_io_interface.f
350 lines (336 loc) · 11.6 KB
/
pwhg_io_interface.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
subroutine pwhg_io_open_read(path,iun,iret)
use, intrinsic :: ISO_C_BINDING
implicit none
include 'pwhg_io_interface.h'
character(len=*) :: path
integer iun,iret
call pwhg_io_open('rb',path,iun,iret)
allocate(character(len=szchunk*2)::pwhg_io_buffer(iun)%buffer)
c current is the pointer to the file position. Reading will start at
c the next character
pwhg_io_buffer(iun)%current=0
c upper is a pointer to the last character already in the buffer
pwhg_io_buffer(iun)%upper=0
c we must always have: 0 <= current <= upper.
c length buffer
pwhg_io_buffer(iun)%length=szchunk*2
pwhg_io_buffer(iun)%mode='rb'
end
subroutine pwhg_io_rewind(iun)
use, intrinsic :: ISO_C_BINDING
implicit none
include 'pwhg_io_interface.h'
integer iun,iret
if(.not. pwhg_io_buffer(iun)%opened) then
write(*,*) ' pwhg_io_rewind: unit ',iun,
1 ' is not opened, exiting ...'
call exit(-1)
endif
iret = gzrewind(files_handle(iun))
if(pwhg_io_buffer(iun)%mode == 'rb') then
c empty buffer
pwhg_io_buffer(iun)%current = 0
pwhg_io_buffer(iun)%upper = 0
endif
end
subroutine pwhg_io_open_write(path,iun,compress,iret)
use, intrinsic :: ISO_C_BINDING
implicit none
include 'pwhg_io_interface.h'
character(len=*) :: path
integer iun,iret
logical compress
if(compress) then
call pwhg_io_open('wb',path,iun,iret)
pwhg_io_buffer(iun)%mode='wb'
else
call pwhg_io_open('wT',path,iun,iret)
pwhg_io_buffer(iun)%mode='wT'
endif
end
subroutine pwhg_io_open(mode,path,iun,iret)
use, intrinsic :: ISO_C_BINDING
implicit none
include 'pwhg_io_interface.h'
character(*) path,mode
integer iun,iret
integer j
logical ok
call init
iun=0
do j=1,pwhg_io_maxfiles
c unit 6 is always opened if init was called
if(.not. pwhg_io_buffer(j)%opened ) then
iun=j
exit
endif
enddo
if(iun==0) then
write(*,*)
1 'pwhg_io_open : too many opened files'
write(*,*) 'increase pwhg_io_maxfiles'
write(*,*) 'exiting ...'
call exit(-1)
endif
if( mode == 'wT') then
c 'wT' yields to uncompressed files. On some zlib implementation
c this does not work. Use standard fortran style io
do j = 7,99
inquire(unit=j,opened=ok)
if(.not.ok) then
exit
endif
enddo
pwhg_io_buffer(iun)%fortran_unit=j
open(file=trim(path),unit=j,status='unknown')
else
pwhg_io_buffer(iun)%fortran_unit=0
files_handle(iun) = gzOpen(trim(path)//c_null_char,
1 trim(mode)//c_null_char)
if(.not. c_associated(files_handle(iun))) then
write(*,*) ' pwhg_io_open: cannot open ',trim(path)
iret = -1
return
endif
endif
pwhg_io_buffer(iun)%opened = .true.
iret = 0
contains
subroutine init
if(pwhg_io_initialized /= pwhg_io_weirdnum) then
pwhg_io_initialized = pwhg_io_weirdnum
pwhg_io_buffer(:)%opened = .false.
c Unit 6 is always open (standard output)
pwhg_io_buffer(6)%opened = .true.
endif
end subroutine init
end
subroutine pwhg_io_close(iun)
use, intrinsic :: ISO_C_BINDING
implicit none
include 'pwhg_io_interface.h'
integer iun,iret
if(.not. pwhg_io_buffer(iun)%opened) then
write(*,*) ' pwhg_io_close: unit ',iun,
1 ' is not opened, exiting ...'
call exit(-1)
endif
if(pwhg_io_buffer(iun)%fortran_unit>0) then
close(pwhg_io_buffer(iun)%fortran_unit)
pwhg_io_buffer(iun)%fortran_unit = 0
else
iret = gzClose(files_handle(iun))
if(iret /= 0) then
write(*,*)
1 ' pwhg_io_close: some error while closing unit ',iun
write(*,*) ' iret=',iret
endif
files_handle(iun) = c_null_ptr
if(pwhg_io_buffer(iun)%buffer == 'rb') then
deallocate(pwhg_io_buffer(iun)%buffer)
endif
endif
pwhg_io_buffer(iun)%opened = .false.
end
subroutine pwhg_io_read_buf(iun,buffer)
c reads len(buffer) characters from unit iun. No error checking (must make sure
c that the read operation can be performed
use, intrinsic :: ISO_C_BINDING
implicit none
include 'pwhg_io_interface.h'
integer iun,iret
character(len=*) :: buffer
if(.not. pwhg_io_buffer(iun)%opened) then
write(*,*) ' pwhg_io_read_buf: unit ',iun,
1 ' is not opened, exiting ...'
call exit(-1)
endif
buffer=pwhg_io_buffer(iun)%buffer(pwhg_io_buffer(iun)%current+1:
1 pwhg_io_buffer(iun)%current + len(buffer))
pwhg_io_buffer(iun)%current = pwhg_io_buffer(iun)%current +
1 len(buffer)
end
subroutine pwhg_io_read(iun,buffer,iret)
use, intrinsic :: ISO_C_BINDING
implicit none
include 'pwhg_io_interface.h'
integer iun,iret
character(len=*) :: buffer
integer nchar
if(.not. pwhg_io_buffer(iun)%opened) then
write(*,*) ' pwhg_io_read: unit ',iun,
1 ' is not opened, exiting ...'
call exit(-1)
endif
call pwhg_io_skip_until(iun,c_new_line,nchar)
if(nchar < 0) then
if(pwhg_io_buffer(iun)%current == 0) then
iret=-1
else
buffer =
1 pwhg_io_buffer(iun)%buffer(1:pwhg_io_buffer(iun)%current-2)
iret = 0
endif
else
buffer=pwhg_io_buffer(iun)%buffer(1:nchar-1)
iret = 0
endif
end
subroutine pwhg_io_write(iun,buffer)
use, intrinsic :: ISO_C_BINDING
implicit none
include 'pwhg_io_interface.h'
integer iun
character(*) buffer
integer j,l
if(.not. pwhg_io_buffer(iun)%opened) then
if(iun /= 6) then
c unit 6 may be used before the pwhg_io interface is ever accessed
write(*,*) ' pwhg_io_write: unit ',iun,
1 ' is not opened, exiting ...'
call exit(-1)
endif
endif
l=len(buffer)
if(iun == 6) then
write(*,'(a)') trim(buffer)
elseif(pwhg_io_buffer(iun)%fortran_unit > 0) then
write(pwhg_io_buffer(iun)%fortran_unit,'(a)') trim(buffer)
else
j = gzwrite(files_handle(iun),buffer,l)
if(j /= l ) then
write(*,*) ' pwhg_io_write: error writing'
write(*,*) ' exiting ...'
call exit(-1)
endif
j=gzPutc(files_handle(iun),c_new_line)
if(j == -1 ) then
write(*,*) ' pwhg_io_write: error writing new line'
write(*,*) ' exiting ...'
call exit(-1)
endif
endif
end
subroutine pwhg_io_skip(iun,nskip)
c moves file position by nskip bytes
use, intrinsic :: ISO_C_BINDING
implicit none
include 'pwhg_io_interface.h'
integer iun,nskip
integer k
if(.not. pwhg_io_buffer(iun)%opened) then
write(*,*) ' pwhg_io_skip: unit ',iun,
1 ' is not opened, exiting ...'
call exit(-1)
endif
k = pwhg_io_buffer(iun)%current + nskip
if(k<0 .or. k>pwhg_io_buffer(iun)%upper) then
write(*,*) ' pwhg_io_skip: cannot skip outside of buffer'
write(*,*) ' exiting ...'
call exit(-1)
endif
pwhg_io_buffer(iun)%current = k
end
subroutine pwhg_io_backspace(iun)
c moves the file position back before the previous operation
use, intrinsic :: ISO_C_BINDING
implicit none
include 'pwhg_io_interface.h'
integer iun,nskip
integer(z_off_t) offset,longskip
if(.not. pwhg_io_buffer(iun)%opened) then
write(*,*) ' pwhg_io_backspace: unit ',iun,
1 ' is not opened, exiting ...'
call exit(-1)
endif
if(pwhg_io_buffer(iun)%current == 0) then
write(*,*) ' pwhg_io_backspace: cannot backspace!'
write(*,*) ' curremt:',pwhg_io_buffer(iun)%current
write(*,*) ' exiting ...'
call exit(-1)
endif
pwhg_io_buffer(iun)%current=0
end
subroutine pwhg_io_skip_until(iun,mark,ntot)
c Search for the string <mark> in the file, and moves the file position
c at the first character after mark. Returns in ntot the number of bytes
c up to the last character in mark. If the mark is not found,
c typically because an end of file is found before the mark, ntot is set to -1,
c the buffer contains all characters that was able to get, and the current pointer
c is set to right after the last valid character in the buffer
use, intrinsic :: ISO_C_BINDING
implicit none
include 'pwhg_io_interface.h'
integer iun,ntot
character(len=*) :: mark
character(len=szchunk) buffer
integer upper,inum,imark
if(.not. pwhg_io_buffer(iun)%opened) then
write(*,*) ' pwhg_io_skip_until: unit ',iun,
1 ' is not opened, exiting ...'
call exit(-1)
endif
c discarde saved result from previous read
if(pwhg_io_buffer(iun)%current > 0) then
pwhg_io_buffer(iun)%buffer=pwhg_io_buffer(iun)%buffer(
1 pwhg_io_buffer(iun)%current+1:pwhg_io_buffer(iun)%upper)
pwhg_io_buffer(iun)%upper=pwhg_io_buffer(iun)%upper -
1 pwhg_io_buffer(iun)%current
pwhg_io_buffer(iun)%current=0
endif
c
do
upper = pwhg_io_buffer(iun)%upper
imark=index(pwhg_io_buffer(iun)%buffer(1:upper),mark)
if(imark > 0) then
pwhg_io_buffer(iun)%current = imark + len(mark)-1
ntot = pwhg_io_buffer(iun)%current
exit
else
if(upper+szchunk > pwhg_io_buffer(iun)%length) then
call pwhg_io_enlarge_buffer
endif
c read one more chunk into buffer. If it doesn't fit, increase buffer size.
inum = gzRead(files_handle(iun),buffer(1:szchunk),szchunk)
if(inum > 0) then
pwhg_io_buffer(iun)%buffer(upper+1:upper + inum) = buffer(1:inum)
pwhg_io_buffer(iun)%upper = upper + inum
cycle
else
ntot = -1
exit
endif
endif
enddo
contains
subroutine pwhg_io_enlarge_buffer
character (len=:), pointer :: buf
integer i1,h1,i2,h2,length,nu,nl,lg,olg,ibuf,cur,icur
logical, save :: ini=.true.
real(kind=8) :: powheginput
integer, save :: max_io_bufsize
if(ini) then
max_io_bufsize = powheginput("#max_io_bufsize")
if(max_io_bufsize < 0) max_io_bufsize=100000
ini = .false.
endif
c Put safety limit on buffer size
olg=pwhg_io_buffer(iun)%length
lg = olg + 1000
if(lg>max_io_bufsize) then
write(*,*) " pwhg_io_skip_until: we can't find mark '"
1 //mark//"'"
write(*,*) " and are above max_io_bufsize limit."
write(*,*) " if the mark is really there, you must."
write(*,*) " set max_io_bufsize to a value larger than ",
1 max_io_bufsize," in powheg.input"
write(*,*) " exting ..."
call exit(-1)
endif
allocate(character(len=lg)::buf)
buf=pwhg_io_buffer(iun)%buffer(1:olg)
deallocate(pwhg_io_buffer(iun)%buffer)
pwhg_io_buffer(iun)%buffer => buf
pwhg_io_buffer(iun)%length = lg
end subroutine pwhg_io_enlarge_buffer
end