This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_io_text.f90
533 lines (478 loc) · 20.3 KB
/
file_io_text.f90
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
! file_io_text.f90
submodule (file_io) file_io_text
contains
! ------------------------------------------------------------------------------
!> @brief Opens a text file for writing.
!!
!! @param[in,out] this The text_writer object.
!! @param[in] fname The name of the file to open.
!! @param[in] append An optional argument that, if specified, determines
!! if the file should be appended. If not supplied, and a file exists,
!! the file will be overwritten. If no file exists, it simply will be
!! created.
!! @param[in,out] err An optional errors-based object that if provided can
!! be used to retrieve information relating to any errors encountered
!! during execution. If not provided, a default implementation of the
!! errors class is used internally to provide error handling. Possible
!! errors and warning messages that may be encountered are as follows.
!! - FCORE_FILE_IO_ERROR: Occurs if the file could not be opened.
module subroutine tw_open(this, fname, append, err)
! Arguments
class(text_writer), intent(inout) :: this
character(len = *), intent(in) :: fname
logical, intent(in), optional :: append
class(errors), intent(inout), optional, target :: err
! Local Variables
integer(int32) :: flag, val
logical :: append2File
class(errors), pointer :: errmgr
type(errors), target :: deferr
character(len = 256) :: errmsg
! Initialization
if (present(err)) then
errmgr => err
else
errmgr => deferr
end if
append2File = .false.
if (present(append)) append2File = append
! Close the file if already open
call this%close()
! Open the file
if (append2File) then
open(newunit = val, file = fname, position = "append", &
iostat = flag)
else
open(newunit = val, file = fname, iostat = flag)
end if
if (flag > 0) then
write(errmsg, "(AI0A)") &
"The file could not be opened/created. Error code ", flag, &
" was encountered."
call errmgr%report_error("tw_open", trim(errmsg), &
FCORE_FILE_IO_ERROR)
return
end if
call this%set_unit(val)
call this%set_filename(fname)
end subroutine
! ------------------------------------------------------------------------------
!> @brief Writes text to the file, but does not advance to the next line.
!!
!! @param[in] this The text_writer object.
!! @param[in] txt The text to write.
!! @param[in,out] err An optional errors-based object that if provided can
!! be used to retrieve information relating to any errors encountered
!! during execution. If not provided, a default implementation of the
!! errors class is used internally to provide error handling. Possible
!! errors and warning messages that may be encountered are as follows.
!! - FCORE_UNOPENED_ERROR: Occurs if the file has not yet been opened.
module subroutine tw_write_txt(this, txt, err)
! Arguments
class(text_writer), intent(in) :: this
character(len = *), intent(in) :: txt
class(errors), intent(inout), optional, target :: err
! Local Variables
class(errors), pointer :: errmgr
type(errors), target :: deferr
! Initialization
if (present(err)) then
errmgr => err
else
errmgr => deferr
end if
! Error Checking
if (.not.this%is_open()) then
call errmgr%report_error("tw_write_txt", &
"The file is not opened.", FCORE_UNOPENED_ERROR)
return
end if
! Process
write(this%get_unit(), '(A)', advance = 'no') txt
end subroutine
! --------------------
!> @brief Writes text to the file, but does not advance to the next line.
!!
!! @param[in] this The text_writer object.
!! @param[in] txt The text to write.
!! @param[in,out] err An optional errors-based object that if provided can
!! be used to retrieve information relating to any errors encountered
!! during execution. If not provided, a default implementation of the
!! errors class is used internally to provide error handling. Possible
!! errors and warning messages that may be encountered are as follows.
!! - FCORE_UNOPENED_ERROR: Occurs if the file has not yet been opened.
module subroutine tw_write_txt_str(this, txt, err)
! Arguments
class(text_writer), intent(in) :: this
class(string), intent(in) :: txt
class(errors), intent(inout), optional, target :: err
! Process
call this%write(txt%str, err)
end subroutine
! ------------------------------------------------------------------------------
!> @brief Writes text to the file, but does advance to the next line.
!!
!! @param[in] this The text_writer object.
!! @param[in] txt The text to write.
!! @param[in,out] err An optional errors-based object that if provided can
!! be used to retrieve information relating to any errors encountered
!! during execution. If not provided, a default implementation of the
!! errors class is used internally to provide error handling. Possible
!! errors and warning messages that may be encountered are as follows.
!! - FCORE_UNOPENED_ERROR: Occurs if the file has not yet been opened.
module subroutine tw_write_txt_line(this, txt, err)
! Arguments
class(text_writer), intent(in) :: this
character(len = *), intent(in) :: txt
class(errors), intent(inout), optional, target :: err
! Local Variables
class(errors), pointer :: errmgr
type(errors), target :: deferr
! Initialization
if (present(err)) then
errmgr => err
else
errmgr => deferr
end if
! Error Checking
if (.not.this%is_open()) then
call errmgr%report_error("tw_write_txt_line", &
"The file is not opened.", FCORE_UNOPENED_ERROR)
return
end if
! Process
write(this%get_unit(), '(A)') txt
end subroutine
! --------------------
!> @brief Writes text to the file, but does advance to the next line.
!!
!! @param[in] this The text_writer object.
!! @param[in] txt The text to write.
!! @param[in,out] err An optional errors-based object that if provided can
!! be used to retrieve information relating to any errors encountered
!! during execution. If not provided, a default implementation of the
!! errors class is used internally to provide error handling. Possible
!! errors and warning messages that may be encountered are as follows.
!! - FCORE_UNOPENED_ERROR: Occurs if the file has not yet been opened.
module subroutine tw_write_txt_line_str(this, txt, err)
! Arguments
class(text_writer), intent(in) :: this
class(string), intent(in) :: txt
class(errors), intent(inout), optional, target :: err
! Process
call this%write_line(txt%str, err)
end subroutine
! ******************************************************************************
!> @brief Opens a text file for reading.
!!
!! @param[in,out] this The text_reader object.
!! @param[in] fname The name of the file to open.
!! @param[in,out] err An optional errors-based object that if provided can
!! be used to retrieve information relating to any errors encountered
!! during execution. If not provided, a default implementation of the
!! errors class is used internally to provide error handling. Possible
!! errors and warning messages that may be encountered are as follows.
!! - FCORE_FILE_IO_ERROR: Occurs if the file could not be opened.
module subroutine tr_open(this, fname, err)
! Arguments
class(text_reader), intent(inout) :: this
character(len = *), intent(in) :: fname
class(errors), intent(inout), optional, target :: err
! Local Variables
integer(int32) :: flag, val
class(errors), pointer :: errmgr
type(errors), target :: deferr
character(len = 256) :: errmsg
! Initialization
if (present(err)) then
errmgr => err
else
errmgr => deferr
end if
! Close if already open
call this%close()
! Process
open(newunit = val, file = fname, form = "unformatted", &
access = "stream", iostat = flag)
if (flag > 0) then
write(errmsg, '(AI0A)') &
"The file could not be opened. Error code ", flag, &
" was encountered."
call errmgr%report_error("tr_open", trim(errmsg), &
FCORE_FILE_IO_ERROR)
return
end if
call this%move_to_start()
call this%set_filename(fname)
call this%set_unit(val)
end subroutine
! ------------------------------------------------------------------------------
!> @brief Reads the entire contents of an ASCII text file into a string.
!!
!! @param[in] this The text_reader object.
!! @param[in,out] err An optional errors-based object that if provided can
!! be used to retrieve information relating to any errors encountered
!! during execution. If not provided, a default implementation of the
!! errors class is used internally to provide error handling. Possible
!! errors and warning messages that may be encountered are as follows.
!! - FCORE_UNOPENED_ERROR: Occurs if the file hasn't been opened.
!! - FCORE_OUT_OF_MEMORY_ERROR: Occurs if there isn't sufficient memory
!! available.
!! - FCORE_FILE_IO_ERROR: Occurs if the file could not be read.
!!
!! @return The string containing the file contents. Notice, the line
!! termination characters have not been stripped out of the string.
!!
!! @par Remarks
!! Notice, the position indicator is not referenced, or utilized, for this
!! read operation. Regardless of its status, the entire file is read.
module function tr_read_full_file(this, err) result(rst)
! Arguments
class(text_reader), intent(in) :: this
class(errors), intent(inout), optional, target :: err
character(len = :), allocatable :: rst
! Local Variables
integer(int32) :: fsize, flag
class(errors), pointer :: errmgr
type(errors), target :: deferr
character(len = 256) :: errmsg
! Initialization
if (present(err)) then
errmgr => err
else
errmgr => deferr
end if
! Ensure the file is open
if (.not.this%is_open()) then
call errmgr%report_error("tr_read_full_file", &
"The file is not opened.", FCORE_UNOPENED_ERROR)
return
end if
! Determine the file size, and allocate a buffer
inquire(file = this%get_filename(), size = fsize)
if (fsize == 0) return
allocate(character(len = fsize) :: rst, stat = flag)
if (flag /= 0) then
call errmgr%report_error("tr_read_full_file", &
"Insufficient memory available.", FCORE_OUT_OF_MEMORY_ERROR)
return
end if
! Read the file
read(unit = this%get_unit(), pos = 1, iostat = flag) rst
if (flag > 0) then
write(errmsg, "(AI0A)") &
"The file could not be read. Error code ", flag, &
" was encountered."
call errmgr%report_error("tr_read_full_file", trim(errmsg), &
FCORE_FILE_IO_ERROR)
return
end if
end function
! ------------------------------------------------------------------------------
!> @brief Reads a single character from an ASCII text file.
!!
!! @param[in,out] this The text_reader object.
!! @param[in,out] err An optional errors-based object that if provided can
!! be used to retrieve information relating to any errors encountered
!! during execution. If not provided, a default implementation of the
!! errors class is used internally to provide error handling. Possible
!! errors and warning messages that may be encountered are as follows.
!! - FCORE_UNOPENED_ERROR: Occurs if the file hasn't been opened.
!! - FCORE_OUT_OF_MEMORY_ERROR: Occurs if there isn't sufficient memory
!! available.
!! - FCORE_FILE_IO_ERROR: Occurs if the file could not be read.
!!
!! @return The character.
!!
!! @par Remarks
!! On output, the position indicator is incremented by one character.
module function tr_read_char(this, err) result(rst)
! Arguments
class(text_reader), intent(inout) :: this
class(errors), intent(inout), optional, target :: err
character :: rst
! Local Variables
integer(int32) :: flag, pos
class(errors), pointer :: errmgr
type(errors), target :: deferr
character(len = 256) :: errmsg
! Initialization
if (present(err)) then
errmgr => err
else
errmgr => deferr
end if
! Ensure the file is open
if (.not.this%is_open()) then
call errmgr%report_error("tr_read_char", &
"The file is not opened.", FCORE_UNOPENED_ERROR)
return
end if
! Read the character
pos = this%get_position()
read(this%get_unit(), pos = pos, iostat = flag) rst
if (flag > 0) then
write(errmsg, '(AI0A)') &
"The file could not be read. Error code ", flag, &
" was encountered."
call errmgr%report_error("tr_read_char", trim(errmsg), &
FCORE_FILE_IO_ERROR)
return
else if (flag < 0) then
call errmgr%report_error("tr_read_char", &
"The end of the file has been encountered.", &
FCORE_END_OF_FILE_ERROR)
return
end if
! Increment the position
! FYI: storage_size returns the storage size of x in bits, but position
! is in bytes; hence division by 8
call this%set_position(pos + storage_size(rst) / 8)
end function
! ------------------------------------------------------------------------------
!> @brief Reads a single line from an ASCII text file.
!!
!! @param[in] this The text_reader object.
!! @param[in,out] err An optional errors-based object that if provided can
!! be used to retrieve information relating to any errors encountered
!! during execution. If not provided, a default implementation of the
!! errors class is used internally to provide error handling. Possible
!! errors and warning messages that may be encountered are as follows.
!! - FCORE_UNOPENED_ERROR: Occurs if the file hasn't been opened.
!! - FCORE_FILE_IO_ERROR: Occurs if the file could not be read.
!! - FCORE_OUT_OF_MEMORY_ERROR: Occurs if there isn't sufficient memory
!! available.
!!
!! @return The string containing the line contents.
!!
!! @par Remarks
!! On output, the position indicator is incremented to account for the
!! length of the line, including any termination characters.
module function tr_read_line(this, err) result(rst)
! Arguments
class(text_reader), intent(inout) :: this
class(errors), intent(inout), optional, target :: err
character(len = :), allocatable :: rst
! Local Variables
integer(int32) :: i, flag, fsize, pos
class(errors), pointer :: errmgr
type(errors), target :: deferr
character(len = 256) :: errmsg
character(len = :), allocatable :: buffer
character :: c, eol, cr
! Initialization
if (present(err)) then
errmgr => err
else
errmgr => deferr
end if
cr = char(13) ! Carriage Return Character
eol = new_line(eol)
i = 0
! Ensure the file is open
if (.not.this%is_open()) then
call errmgr%report_error("tr_read_line", &
"The file is not opened.", FCORE_UNOPENED_ERROR)
return
end if
! Allocate space for a buffer that is sufficiently sized to hold an
! entire line.
inquire(file = this%get_filename(), size = fsize)
if (fsize == 0) return
allocate(character(len = fsize) :: buffer, stat = flag)
if (flag /= 0) then
call errmgr%report_error("tr_read_full_file", &
"Insufficient memory available.", FCORE_OUT_OF_MEMORY_ERROR)
return
end if
! Read in each character until we reach EOF or EOL
do
! Read
pos = this%get_position()
read(unit = this%get_unit(), pos = pos, iostat = flag) c
if (flag < 0) then
! EOF reached
exit
else if (flag > 0) then
write(errmsg, "(AI0A)") &
"The file could not be read. Error code ", flag, &
" was encountered."
call errmgr%report_error("tr_read_line", trim(errmsg), &
FCORE_FILE_IO_ERROR)
return
end if
! Increment the file position
call this%set_position(pos + storage_size(c) / 8)
! If we encounter a carriage return, simply cycle the loop
if (c == cr) cycle
! See if we're at the EOL yet
if (c == eol) exit
! Store the value
i = i + 1
buffer(i:i) = c
end do
! Trim the buffer to fit tightly
rst = buffer(1:i)
end function
! ------------------------------------------------------------------------------
!> @brief Reads the entire contents of an ASCII text file into a string,
!! and breaks the contents into lines.
!!
!! @param[in] this The text_reader object.
!! @param[in,out] err An optional errors-based object that if provided can
!! be used to retrieve information relating to any errors encountered
!! during execution. If not provided, a default implementation of the
!! errors class is used internally to provide error handling. Possible
!! errors and warning messages that may be encountered are as follows.
!! - FCORE_UNOPENED_ERROR: Occurs if the file hasn't been opened.
!! - FCORE_OUT_OF_MEMORY_ERROR: Occurs if there isn't sufficient memory
!! available.
!! - FCORE_FILE_IO_ERROR: Occurs if the file could not be read.
!!
!! @return An array of strings
!!
!! @par Remarks
!! Notice, the position indicator is not referenced, or utilized, for this
!! read operation. Regardless of its status, the entire file is read.
module function tr_read_lines(this, err) result(rst)
! Arguments
class(text_reader), intent(inout) :: this
class(errors), intent(inout), optional, target :: err
type(string), allocatable, dimension(:) :: rst
! Local Variables
class(errors), pointer :: errmgr
type(errors), target :: deferr
character(len = :), allocatable :: contents
character :: cr, eol
integer(int32) :: i, n, j
! Initialization
if (present(err)) then
errmgr => err
else
errmgr => deferr
end if
cr = char(13) ! Carriage Return Character
eol = new_line(eol)
! Read the contents of the file
contents = this%read_all(errmgr)
if (errmgr%has_error_occurred()) return
! Split the contents by EOL character - remove any carriage return
! characters
rst = split_string(remove(contents, cr), eol)
! Trim empty lines from the end of the file
n = size(rst)
j = 0
do i = n, 1, -1
if (len(rst(i)%str) == 0) then
j = j + 1
else
exit
end if
end do
if (j /= 0) then
n = n - j
rst = rst(1:n)
end if
end function
! ------------------------------------------------------------------------------
end submodule