@@ -287,26 +287,85 @@ integer function open(filename, mode) result(u)
287
287
288
288
character (* ), intent (in ) :: filename
289
289
character (* ), intent (in ), optional :: mode
290
- character (:), allocatable :: mode_
291
- mode_ = " rt"
292
- if (present (mode)) mode_ = mode
293
- ! Note: the Fortran standard says that the default values for `status` and
294
- ! `action` are processor dependent, so we have to explicitly set them below
295
- if (mode_ == " r" .or. mode_ == ' rt' ) then
296
- open (newunit= u, file= filename, status= " old" , action= " read" , &
297
- access= ' sequential' , form= ' formatted' )
298
- else if (mode_ == " w" .or. mode_ == " wt" ) then
299
- open (newunit= u, file= filename, status= " replace" , action= " write" , &
300
- access= ' sequential' , form= ' formatted' )
301
- else if (mode_ == " a" .or. mode_ == " at" ) then
302
- open (newunit= u, file= filename, position= " append" , status= " old" , &
303
- action= " write" , access= ' sequential' , form= ' formatted' )
304
- else if (mode_ == " x" .or. mode_ == " xt" ) then
305
- open (newunit= u, file= filename, status= " new" , &
306
- action= " write" , access= ' sequential' , form= ' formatted' )
290
+ integer :: io
291
+ character (3 ):: mode_
292
+ character (:),allocatable :: action_, position_, status_, access_, form_
293
+
294
+
295
+ mode_ = " r t"
296
+ if (present (mode)) mode_ = parse_mode(mode)
297
+
298
+ if (mode_(1 :2 ) == ' r ' ) then
299
+ action_= ' read'
300
+ position_= ' asis'
301
+ status_= ' old'
302
+ else if (mode_(1 :2 ) == ' w ' ) then
303
+ action_= ' write'
304
+ position_= ' asis'
305
+ status_= ' replace'
306
+ else if (mode_(1 :2 ) == ' a ' ) then
307
+ action_= ' write'
308
+ position_= ' append'
309
+ status_= ' old'
310
+ else if (mode_(1 :2 ) == ' x ' ) then
311
+ action_= ' write'
312
+ position_= ' asis'
313
+ status_= ' new'
314
+ else if (mode_(1 :2 ) == ' r+' ) then
315
+ action_= ' readwrite'
316
+ position_= ' asis'
317
+ status_= ' old'
318
+ else if (mode_(1 :2 ) == ' w+' ) then
319
+ action_= ' readwrite'
320
+ position_= ' asis'
321
+ status_= ' replace'
322
+ else if (mode_(1 :2 ) == ' a+' ) then
323
+ action_= ' readwrite'
324
+ position_= ' append'
325
+ status_= ' old'
326
+ else if (mode_(1 :2 ) == ' x+' ) then
327
+ action_= ' readwrite'
328
+ position_= ' asis'
329
+ status_= ' new'
307
330
else
308
- call error_stop(" Unsupported mode" )
331
+ call error_stop(" Unsupported mode: " // mode_( 1 : 2 ) )
309
332
end if
333
+
334
+ if (mode_(3 :3 ) == ' t' ) then
335
+ access_= ' sequential'
336
+ form_= ' formatted'
337
+ else if (mode_(3 :3 ) == ' b' .or. mode_(3 :3 ) == ' s' ) then
338
+ access_= ' stream'
339
+ form_= ' unformatted'
340
+ else
341
+ call error_stop(" Unsupported mode: " // mode_(3 :3 ))
342
+ endif
343
+
344
+ open (newunit= u, file= filename, &
345
+ action = action_, position = position_, status = status_, &
346
+ access = access_, form = form_, &
347
+ iostat = io)
348
+
349
+ end function
350
+
351
+ character (3 ) function parse_mode(mode) result(mode_)
352
+ character (* ), intent (in ) :: mode
353
+
354
+ mode_ = ' r t'
355
+ mode_(1 :1 ) = mode(1 :1 )
356
+
357
+ if (len_trim (adjustl (mode)) > 1 ) then
358
+ if (mode(2 :2 ) == ' +' )then
359
+ mode_(2 :2 ) = ' +'
360
+ else
361
+ mode_(3 :3 ) = mode(2 :2 )
362
+ endif
363
+ end if
364
+
365
+ if (len_trim (adjustl (mode)) > 2 ) then
366
+ mode_(3 :3 ) = mode(3 :3 )
367
+ end if
368
+
310
369
end function
311
370
312
371
end module
0 commit comments