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.f90
1175 lines (1089 loc) · 48.9 KB
/
file_io.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
! file_io.f90
!> @brief This module contains routines to support file I/O operations.
module file_io
use iso_fortran_env
use iso_c_binding
use ferror
use fcore_constants
use strings
implicit none
private
public :: file_manager
public :: text_writer
public :: file_reader
public :: text_reader
public :: binary_writer
public :: binary_reader
public :: is_little_endian
public :: swap_bytes
public :: file_path
public :: split_path
public :: folder_contents
public :: get_folder_contents
public :: find_all_files
! ******************************************************************************
! TYPES
! ------------------------------------------------------------------------------
!> @brief Defines a base type for managint file I/O.
type file_manager
private
!> @brief The unit value.
integer(int32) :: m_unit = -1
!> @brief The filename.
character(len = :), allocatable :: m_fname
contains
!> @brief Forces closure of the file, if open, whenever the object goes
!! out of scope.
final :: fm_clean_up
!> @brief Returns the unit value for the file object.
!!
!! @par Syntax
!! @code{.f90}
!! integer(int32) get_unit(class(file_manager) this)
!! @endcode
!!
!! @param[in,out] this The file_manager object.
!!
!! @return The Fortran unit value for this stream.
procedure, public :: get_unit => fm_get_unit
!> @brief Sets the unit value for the file object.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine set_unit(class(file_manager) this, integer(int32) x)
!! @endcode
!!
!! @param[in,out] this The file_manager object.
!! @param[in] x The Fortran unit value.
procedure, private :: set_unit => fm_set_unit
! !> @brief Creates a new, unused unit value to identify the file.
! procedure, public :: create_new_unit => fm_new_unit
!> @brief Determines if the file is already opened.
!!
!! @par Syntax
!! @code{.f90}
!! logical is_open(class(file_manager) this)
!! @endcode
!!
!! @param[in] this The file_manager object.
!!
!! @return Returns true if the file is opened; else, false.
procedure, public :: is_open => fm_get_is_opened
!> @brief Closes the file.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine close(class(file_manager) this, logical del)
!! @endcode
!!
!! @param[in,out] this The file_manager object.
!! @param[in] del An optional input, that if set, determines if the file
!! should be deleted once closed. The default is false such that the
!! file remains.
procedure, public :: close => fm_close
!> @brief Gets the filename.
!!
!! @par Syntax
!! @code{.f90}
!! character(len = :) get_filename(class(file_manager) this)
!! @endcode
!!
!! @param[in] this The file_manager object.
!!
!! @return The filename.
procedure, public :: get_filename => fm_get_fname
!> @brief Sets the filename.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine set_filename(class(file_manager) this, character(len = *) x)
!! @endcode
!!
!! @param[in,out] this The file_manager object.
!! @param[in] x The filename.
procedure, public :: set_filename => fm_set_fname
!> @brief Gets the size of the currently open file.
!!
!! @par Syntax
!! @code{.f90}
!! integer(int32) get_file_size(class(file_manager) this)
!! @endcode
!!
!! @param[in] this The file_manager object.
!!
!! @return The file size, in bytes.
procedure, public :: get_file_size => fm_get_size
end type
! ------------------------------------------------------------------------------
!> @brief Defines a mechanism for writing text files.
type, extends(file_manager) :: text_writer
contains
!> @brief Opens a text file for writing.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine open(class(text_writer) this, character(len = *) fname, logical append, class(errors) err)
!! @endcode
!!
!! @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.
procedure, public :: open => tw_open
!> @brief Writes text to the file, but does not advance to the next
!! line.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine write(class(text_writer) this, character(len = *) txt, class(errors) err)
!! @endcode
!!
!! @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.
generic, public :: write => tw_write_txt, tw_write_txt_str
!> @brief Writes text to the file, but does advance to the next line.
!!
!! @par Syntax #1
!! @code{.f90}
!! subroutine write_line(class(text_writer) this, class(string) txt, class(errors) err)
!! @endcode
!!
!! @par Syntax #2
!! @code{.f90}
!! subroutine write_line(class(text_writer) this, character(len = *) txt, class(errors) err)
!! @endcode
!!
!! @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.
generic, public :: write_line => tw_write_txt_line, &
tw_write_txt_line_str
procedure :: tw_write_txt
procedure :: tw_write_txt_str
procedure :: tw_write_txt_line
procedure :: tw_write_txt_line_str
end type
! ------------------------------------------------------------------------------
!> @brief Defines a mechanism for reading files.
type, extends(file_manager) :: file_reader
private
!> @brief The current file position.
integer(int32) :: m_position = 0
contains
!> @brief Gets the current position within the file.
!!
!! @par Syntax
!! @code{.f90}
!! integer(int32) get_position(class(file_reader) this)
!! @endcode
!!
!! @param[in] this The file_reader object.
!! @return The current position.
procedure, public :: get_position => fr_get_position
!> @brief Sets the position within the file.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine set_position(class(file_reader) this, integer(int32) x)
!! @endcode
!!
!! @param[in,out] this The file_reader object.
!! @param[in] x The file position.
procedure, public :: set_position => fr_set_position
!> @brief Moves the current position to the start of the file.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine move_to_start(class(file_reader) this)
!! @endcode
!!
!! @param[in,out] this The file_reader object.
procedure, public :: move_to_start => fr_move_to_start
!> @brief Tests to see if the current position denotes the end-of-file.
!!
!! @par Syntax
!! @code{.f90}
!! logical is_end_of_file(class(file_reader) this)
!! @endcode
!!
!! @param[in] this The file_reader object.
!!
!! @return Returns true if the current position is the end-of-file;
!! else, false.
procedure, public :: is_end_of_file => fr_eof
end type
! ------------------------------------------------------------------------------
!> @brief Defines a mechanism for reading text files.
type, extends(file_reader) :: text_reader
contains
!> @brief Opens a text file for reading.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine text_reader(class(text_reader) this, character(len = *) fname, class(errors) err)
!! @endcode
!!
!! @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.
procedure, public :: open => tr_open
!> @brief Reads the entire contents of an ASCII text file into a string.
!!
!! @par Syntax
!! @code{.f90}
!! character(len = :) read_all(class(text_reader) this, class(errors) err)
!! @endcode
!!
!! @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.
procedure, public :: read_all => tr_read_full_file
!> @brief Reads a single character from an ASCII text file.
!!
!! @par Syntax
!! @code{.f90}
!! character read_char(class(text_reader) this, class(errors) err)
!! @endcode
!!
!! @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.
procedure, public :: read_char => tr_read_char
!> @brief Reads a single line from an ASCII text file.
!!
!! @par Syntax
!! @code{.f90}
!! character(len = :) read_line(class(text_reader) this, class(errors) err)
!! @endcode
!!
!! @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.
procedure, public :: read_line => tr_read_line
!> @brief Reads the entire contents of an ASCII text file into a string,
!! and breaks the contents into lines.
!!
!! @par Syntax
!! @code{.f90}
!! type(string)(:) read_lines(class(text_reader) this, class(errors) err)
!! @endcode
!!
!! @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.
procedure, public :: read_lines => tr_read_lines
end type
! ------------------------------------------------------------------------------
!> @brief Defines a mechanism for writing binary files.
type, extends(file_reader) :: binary_writer
private
!> @brief A buffer used to store data until flushed.
integer(int8), allocatable, dimension(:) :: m_buffer
!> @brief The actual number of items in the buffer
integer(int32) :: m_count = 0
contains
!> @brief Forces a write operation on all buffer contents, closes the
!! file, and performs any necessary clean-up operations.
final :: bw_clean_up
!> @brief Gets the capacity of the buffer, in bytes.
!!
!! @par Syntax
!! @code{.f90}
!! integer(int32) get_capacity(class(binary_writer) this)
!! @endcode
!!
!! @param[in] this The binary_writer object.
!!
!! @return The buffer capacity, in bytes.
procedure, public :: get_capacity => bw_get_capacity
!> @brief Sets the capacity of the buffer, in bytes.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine set_capacity(class(binary_writer) this, integer(int32) n, class(errors) err)
!! @endcode
!!
!! @param[in,out] this The binary_writer object.
!! @param[in] n The size, in bytes, to make the buffer. This value must
!! be greater than zero.
!! @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_INVALID_INPUT_ERROR: Occurs if @p n is less than or equal to
!! zero.
!! - FCORE_OUT_OF_MEMORY_ERROR: Occurs if there is insufficient memory
!! available.
procedure, public :: set_capacity => bw_set_capacity
!> @brief Gets the number of bytes stored within the buffer.
!!
!! @par Syntax
!! @code{.f90}
!! integer(int32) get_count(class(binary_writer) this)
!! @endcode
!!
!! @param[in] this The binary_writer object.
!!
!! @return The number of bytes stored within the buffer.
procedure, public :: get_count => bw_get_count
!> @brief Clears the buffer.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine clear_buffer(class(binary_writer) this)
!! @endcode
!!
!! @param[in,out] this The binary_writer object.
procedure, public :: clear_buffer => bw_clear_buffer
!> @brief Opens a binary file for writing.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine open(class(binary_writer) this, character(len = *) fname, logical append, class(errors) err)
!! @endcode
!!
!! @param[in,out] this The binary_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.
!! - FCORE_OUT_OF_MEMORY_ERROR: Occurs if there is insufficient memory
!! available.
procedure, public :: open => bw_open
!> @brief Closes the file. This will also force writing of all buffer
!! contents.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine close(class(binary_writer) this)
!! @endcode
!!
!! @param[in,out] this The binary_writer object.
!! @param[in] del An optional input, that if set, determines if the file
!! should be deleted once closed. The default is false such that the
!! file remains.
procedure, public :: close => bw_close
!> @brief Flushes the buffer by writing the contents to file.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine flush_buffer(class(binary_writer) this)
!! @endcode
!!
!! @param[in,out] this The binary_writer object.
procedure, public :: flush_buffer => bw_flush_buffer
!> @brief Pushes an item onto the buffer for writing.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine push(class(binary_writer) this, generic x, class(errors) err)
!! @endcode
!!
!! @param[in,out] this The binary_writer object.
!! @param[in] x The data to push onto the buffer.
!! @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_OUT_OF_MEMORY_ERROR: Occurs if there is insufficient memory
!! available.
generic, public :: push => bw_append_byte, bw_append_byte_array, &
bw_append_r64, bw_append_r64_array, bw_append_r64_matrix, &
bw_append_r32, bw_append_r32_array, bw_append_r32_matrix, &
bw_append_i16, bw_append_i16_array, bw_append_i16_matrix, &
bw_append_i32, bw_append_i32_array, bw_append_i32_matrix, &
bw_append_i64, bw_append_i64_array, bw_append_i64_matrix, &
bw_append_c64, bw_append_c64_array, bw_append_c64_matrix, &
bw_append_c32, bw_append_c32_array, bw_append_c32_matrix, &
bw_append_char, bw_append_str
procedure :: bw_append_byte
procedure :: bw_append_byte_array
procedure :: bw_append_r64
procedure :: bw_append_r64_array
procedure :: bw_append_r64_matrix
procedure :: bw_append_r32
procedure :: bw_append_r32_array
procedure :: bw_append_r32_matrix
procedure :: bw_append_i16
procedure :: bw_append_i16_array
procedure :: bw_append_i16_matrix
procedure :: bw_append_i32
procedure :: bw_append_i32_array
procedure :: bw_append_i32_matrix
procedure :: bw_append_i64
procedure :: bw_append_i64_array
procedure :: bw_append_i64_matrix
procedure :: bw_append_c64
procedure :: bw_append_c64_array
procedure :: bw_append_c64_matrix
procedure :: bw_append_c32
procedure :: bw_append_c32_array
procedure :: bw_append_c32_matrix
procedure :: bw_append_char
procedure :: bw_append_str
end type
! ------------------------------------------------------------------------------
!> @brief Defines a mechanism for reading binary files.
type, extends(file_reader) :: binary_reader
contains
!> @brief Opens a binary file for reading.
!!
!! @par Syntax
!! @code{.f90}
!! subroutine open(class(binary_reader) this, character(len = *) fname, class(errors) err)
!! @endcode
!!
!! @param[in,out] this The binary_reader 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.
!! - FCORE_OUT_OF_MEMORY_ERROR: Occurs if there is insufficient memory
!! available.
procedure, public :: open => br_open
!> @brief Reads a specified number of bytes from the file.
!!
!! @par Syntax
!! @code{.f90}
!! integer(int8)(:) read_bytes(class(binary_reader) this, integer(int32) n, class(errors) err)
!! @endcode
!!
!! @param[in,out] this The binary_reader object.
!! @param[in] n The number of bytes to read.
!! @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_OUT_OF_MEMORY_ERROR: Occurs if there is insufficient memory
!! available.
!! - FCORE_INVALID_INPUT_ERROR: Occurs if the requested read size is
!! less than or equal to zero.
!!
!! @result An array containing the results.
procedure, public :: read_bytes => br_read_byte_count
!> @brief Reads a single byte from the file.
!!
!! @par Syntax
!! @code{.f90}
!! integer(int8) read_byte(class(binary_reader) this, class(errors) err)
!! @endcode
!!
!! @param[in,out] this The binary_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_FILE_IO_ERROR: Occurs if the file could not be read.
!! - FCORE_OUT_OF_MEMORY_ERROR: Occurs if there is insufficient memory
!! available.
!!
!! @result The results.
procedure, public :: read_byte => br_read_byte
!> @brief Reads the entire contents of the file into a buffer.
!!
!! @par Syntax
!! @code{.f90}
!! integer(int8)(:) read_all(class(binary_reader) this, class(errors) err)
!! @endcode
!!
!! @param[in,out] this The binary_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_FILE_IO_ERROR: Occurs if the file could not be read.
!! - FCORE_OUT_OF_MEMORY_ERROR: Occurs if there is insufficient memory
!! available.
!!
!! @result An array containing the results.
procedure, public :: read_all => br_read_all
end type
! ------------------------------------------------------------------------------
!> @brief Defines a container for parts of a file path.
type file_path
!> @brief The drive.
character(len = :), allocatable :: drive
!> @brief The directory.
character(len = :), allocatable :: directory
!> @brief The filename.
character(len = :), allocatable :: filename
!> @brief The file extension. Notice, the '.' is included
!! (e.g. ".txt").
character(len = :), allocatable :: extension
end type
! ------------------------------------------------------------------------------
!> @brief Defins a container describing folder contents.
type folder_contents
!> @brief A list of files in the folder.
type(string), allocatable, dimension(:) :: files
!> @brief A list of folders with the folder.
type(string), allocatable, dimension(:) :: folders
!> @brief The folder
character(len = :), allocatable :: folder
end type
! ******************************************************************************
! C-INTEROP INTERFACES
! ------------------------------------------------------------------------------
interface
!> @brief An interface to the C split_file_path routine.
subroutine split_file_path_c(path, drive, dir, fname, ext) &
bind(C, name = "split_file_path_c")
use iso_c_binding
character(kind = c_char), intent(in) :: path(*)
character(kind = c_char), intent(out) :: drive(*), dir(*), &
fname(*), ext(*)
end subroutine
!> @brief An interface to the C get_directory_contents_c routine.
function get_directory_contents_c(dir, nbuffers, bufferSize, fnames, &
nnames, nameLengths, dirnames, ndir, dirLengths) &
bind(C, name = "get_directory_contents_c") result(rst)
use iso_c_binding
character(kind = c_char), intent(in) :: dir(*)
integer(c_int), intent(in), value :: nbuffers, bufferSize
type(c_ptr), intent(out) :: fnames(nbuffers), dirNames(nbuffers)
integer(c_int), intent(out) :: nnames, nameLengths(nbuffers), &
ndir, dirLengths(nbuffers)
logical(c_bool) :: rst
end function
end interface
! ******************************************************************************
! INTERFACES
! ------------------------------------------------------------------------------
interface
module function fm_get_unit(this) result(rst)
class(file_manager), intent(in) :: this
integer(int32) :: rst
end function
module subroutine fm_set_unit(this, x)
class(file_manager), intent(inout) :: this
integer(int32), intent(in) :: x
end subroutine
module subroutine fm_new_unit(this)
class(file_manager), intent(inout) :: this
end subroutine
module function fm_get_is_opened(this) result(rst)
class(file_manager), intent(in) :: this
logical :: rst
end function
module subroutine fm_close(this, del)
class(file_manager), intent(inout) :: this
logical, intent(in), optional :: del
end subroutine
module subroutine fm_clean_up(this)
type(file_manager), intent(inout) :: this
end subroutine
pure module function fm_get_fname(this) result(rst)
class(file_manager), intent(in) :: this
character(len = :), allocatable :: rst
end function
module subroutine fm_set_fname(this, x)
class(file_manager), intent(inout) :: this
character(len = *), intent(in) :: x
end subroutine
module function fm_get_size(this) result(rst)
class(file_manager), intent(in) :: this
integer(int32) :: rst
end function
end interface
! ------------------------------------------------------------------------------
interface
module subroutine tw_open(this, fname, append, err)
class(text_writer), intent(inout) :: this
character(len = *), intent(in) :: fname
logical, intent(in), optional :: append
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine tw_write_txt(this, txt, err)
class(text_writer), intent(in) :: this
character(len = *), intent(in) :: txt
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine tw_write_txt_str(this, txt, err)
class(text_writer), intent(in) :: this
class(string), intent(in) :: txt
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine tw_write_txt_line(this, txt, err)
class(text_writer), intent(in) :: this
character(len = *), intent(in) :: txt
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine tw_write_txt_line_str(this, txt, err)
class(text_writer), intent(in) :: this
class(string), intent(in) :: txt
class(errors), intent(inout), optional, target :: err
end subroutine
end interface
! ------------------------------------------------------------------------------
interface
pure module function fr_get_position(this) result(rst)
class(file_reader), intent(in) :: this
integer(int32) :: rst
end function
module subroutine fr_set_position(this, x)
class(file_reader), intent(inout) :: this
integer(int32), intent(in) :: x
end subroutine
module subroutine fr_move_to_start(this)
class(file_reader), intent(inout) :: this
end subroutine
module function fr_eof(this) result(rst)
class(file_reader), intent(in) :: this
logical :: rst
end function
end interface
! ------------------------------------------------------------------------------
interface
module subroutine tr_open(this, fname, err)
class(text_reader), intent(inout) :: this
character(len = *), intent(in) :: fname
class(errors), intent(inout), optional, target :: err
end subroutine
module function tr_read_full_file(this, err) result(rst)
class(text_reader), intent(in) :: this
class(errors), intent(inout), optional, target :: err
character(len = :), allocatable :: rst
end function
module function tr_read_char(this, err) result(rst)
class(text_reader), intent(inout) :: this
class(errors), intent(inout), optional, target :: err
character :: rst
end function
module function tr_read_line(this, err) result(rst)
class(text_reader), intent(inout) :: this
class(errors), intent(inout), optional, target :: err
character(len = :), allocatable :: rst
end function
module function tr_read_lines(this, err) result(rst)
class(text_reader), intent(inout) :: this
class(errors), intent(inout), optional, target :: err
type(string), allocatable, dimension(:) :: rst
end function
end interface
! ------------------------------------------------------------------------------
interface
pure module function bw_get_capacity(this) result(rst)
class(binary_writer), intent(in) :: this
integer(int32) :: rst
end function
module subroutine bw_set_capacity(this, n, err)
class(binary_writer), intent(inout) :: this
integer(int32), intent(in) :: n
class(errors), intent(inout), optional, target :: err
end subroutine
pure module function bw_get_count(this) result(rst)
class(binary_writer), intent(in) :: this
integer(int32) :: rst
end function
module subroutine bw_clear_buffer(this)
class(binary_writer), intent(inout) :: this
end subroutine
module subroutine bw_open(this, fname, append, err)
class(binary_writer), intent(inout) :: this
character(len = *), intent(in) :: fname
logical, intent(in), optional :: append
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_close(this, del)
class(binary_writer), intent(inout) :: this
logical, intent(in), optional :: del
end subroutine
module subroutine bw_flush_buffer(this)
class(binary_writer), intent(inout) :: this
end subroutine
module subroutine bw_append_byte(this, x, err)
class(binary_writer), intent(inout) :: this
integer(int8), intent(in) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_byte_array(this, x, err)
class(binary_writer), intent(inout) :: this
integer(int8), intent(in), dimension(:) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_r64(this, x, err)
class(binary_writer), intent(inout) :: this
real(real64), intent(in) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_r64_array(this, x, err)
class(binary_writer), intent(inout) :: this
real(real64), intent(in), dimension(:) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_r64_matrix(this, x, err)
class(binary_writer), intent(inout) :: this
real(real64), intent(in), dimension(:,:) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_r32(this, x, err)
class(binary_writer), intent(inout) :: this
real(real32), intent(in) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_r32_array(this, x, err)
class(binary_writer), intent(inout) :: this
real(real32), intent(in), dimension(:) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_r32_matrix(this, x, err)
class(binary_writer), intent(inout) :: this
real(real32), intent(in), dimension(:,:) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_i16(this, x, err)
class(binary_writer), intent(inout) :: this
integer(int16), intent(in) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_i16_array(this, x, err)
class(binary_writer), intent(inout) :: this
integer(int16), intent(in), dimension(:) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_i16_matrix(this, x, err)
class(binary_writer), intent(inout) :: this
integer(int16), intent(in), dimension(:,:) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_i32(this, x, err)
class(binary_writer), intent(inout) :: this
integer(int32), intent(in) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_i32_array(this, x, err)
class(binary_writer), intent(inout) :: this
integer(int32), intent(in), dimension(:) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_i32_matrix(this, x, err)
class(binary_writer), intent(inout) :: this
integer(int32), intent(in), dimension(:,:) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_i64(this, x, err)
class(binary_writer), intent(inout) :: this
integer(int64), intent(in) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_i64_array(this, x, err)
class(binary_writer), intent(inout) :: this
integer(int64), intent(in), dimension(:) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_i64_matrix(this, x, err)
class(binary_writer), intent(inout) :: this
integer(int64), intent(in), dimension(:,:) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_c64(this, x, err)
class(binary_writer), intent(inout) :: this
complex(real64), intent(in) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_c64_array(this, x, err)
class(binary_writer), intent(inout) :: this
complex(real64), intent(in), dimension(:) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_c64_matrix(this, x, err)
class(binary_writer), intent(inout) :: this
complex(real64), intent(in), dimension(:,:) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_c32(this, x, err)
class(binary_writer), intent(inout) :: this
complex(real32), intent(in) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_c32_array(this, x, err)
class(binary_writer), intent(inout) :: this
complex(real32), intent(in), dimension(:) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_c32_matrix(this, x, err)
class(binary_writer), intent(inout) :: this
complex(real32), intent(in), dimension(:,:) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_char(this, x, err)
class(binary_writer), intent(inout) :: this
character(len = *), intent(in) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_append_str(this, x, err)
class(binary_writer), intent(inout) :: this
class(string), intent(in) :: x
class(errors), intent(inout), optional, target :: err
end subroutine
module subroutine bw_clean_up(this)
type(binary_writer), intent(inout) :: this