@@ -51,11 +51,19 @@ module csv_module
51
51
52
52
! for writing a csv file:
53
53
integer :: icol = 0 ! ! last column written in current row
54
- integer :: iunit = 0 ! ! file unit for writing
54
+ integer :: iunit = 0 ! ! file unit for writing
55
55
logical :: enclose_strings_in_quotes = .true. ! ! if true, all string cells
56
56
! ! will be enclosed in quotes.
57
- logical :: enclose_all_in_quotes = .false. ! ! if true, *all* cells will
58
- ! ! be enclosed in quotes.
57
+ logical :: enclose_all_in_quotes = .false. ! ! if true, *all* cells will
58
+ ! ! be enclosed in quotes.
59
+ character (len= 1 ) :: logical_true_string = ' T' ! ! when writing a logical `true`
60
+ ! ! value to a CSV file, this
61
+ ! ! is the string to use
62
+ ! ! (default is `T`)
63
+ character (len= 1 ) :: logical_false_string = ' F' ! ! when writing a logical `false`
64
+ ! ! value to a CSV file, this
65
+ ! ! is the string to use
66
+ ! ! (default is `F`)
59
67
60
68
contains
61
69
@@ -118,6 +126,8 @@ module csv_module
118
126
subroutine initialize_csv_file (me ,quote ,delimiter ,&
119
127
enclose_strings_in_quotes ,&
120
128
enclose_all_in_quotes ,&
129
+ logical_true_string ,&
130
+ logical_false_string ,&
121
131
verbose )
122
132
123
133
implicit none
@@ -129,6 +139,14 @@ subroutine initialize_csv_file(me,quote,delimiter,&
129
139
! ! will be enclosed in quotes.
130
140
logical ,intent (in ),optional :: enclose_all_in_quotes ! ! if true, *all* cells will
131
141
! ! be enclosed in quotes.
142
+ character (len= 1 ),intent (in ),optional :: logical_true_string ! ! when writing a logical `true`
143
+ ! ! value to a CSV file, this
144
+ ! ! is the string to use
145
+ ! ! (default is `T`)
146
+ character (len= 1 ),intent (in ),optional :: logical_false_string ! ! when writing a logical `false`
147
+ ! ! value to a CSV file, this
148
+ ! ! is the string to use
149
+ ! ! (default is `F`)
132
150
logical ,intent (in ),optional :: verbose
133
151
134
152
if (present (quote)) me% quote = quote
@@ -137,6 +155,10 @@ subroutine initialize_csv_file(me,quote,delimiter,&
137
155
me% enclose_strings_in_quotes = enclose_strings_in_quotes
138
156
if (present (enclose_all_in_quotes)) &
139
157
me% enclose_all_in_quotes = enclose_all_in_quotes
158
+ if (present (logical_true_string)) &
159
+ me% logical_true_string = logical_true_string
160
+ if (present (logical_false_string)) &
161
+ me% logical_false_string = logical_false_string
140
162
if (present (verbose)) me% verbose = verbose
141
163
142
164
end subroutine initialize_csv_file
@@ -176,6 +198,9 @@ subroutine read_csv_file(me,filename,header_row,skip_rows,status_ok)
176
198
integer ,intent (in ),optional :: header_row ! ! the header row
177
199
integer ,dimension (:),intent (in ),optional :: skip_rows ! ! rows to skip
178
200
201
+ type (csv_string),dimension (:),allocatable :: row_data ! ! a tokenized row
202
+ integer ,dimension (:),allocatable :: rows_to_skip ! ! the actual rows to skip
203
+ character (len= :),allocatable :: line ! ! a line from the file
179
204
integer :: i ! ! counter
180
205
integer :: j ! ! counter
181
206
integer :: irow ! ! row counter
@@ -184,11 +209,10 @@ subroutine read_csv_file(me,filename,header_row,skip_rows,status_ok)
184
209
integer :: n_cols ! ! number of columns in the file (and output data matrix)
185
210
integer :: istat ! ! open status flag
186
211
integer :: iunit ! ! open file unit
187
- integer ,dimension (:),allocatable :: rows_to_skip ! ! the actual rows to skip
188
- logical :: arrays_allocated ! ! if the arrays in the class have been allocated
189
- type (csv_string),dimension (:),allocatable :: row_data ! ! a tokenized row
190
- character (len= :),allocatable :: line ! ! a line from the file
191
- integer :: iheader ! ! row number of header row (0 if no header specified)
212
+ logical :: arrays_allocated ! ! if the arrays in the
213
+ ! ! class have been allocated
214
+ integer :: iheader ! ! row number of header row
215
+ ! ! (0 if no header specified)
192
216
193
217
call me% destroy()
194
218
arrays_allocated = .false.
@@ -264,7 +288,8 @@ subroutine read_csv_file(me,filename,header_row,skip_rows,status_ok)
264
288
status_ok = .true.
265
289
266
290
else
267
- if (me% verbose) write (error_unit,' (A)' ) ' Error opening file: ' // trim (filename)
291
+ if (me% verbose) write (error_unit,' (A)' ) &
292
+ ' Error opening file: ' // trim (filename)
268
293
status_ok = .false.
269
294
end if
270
295
@@ -354,7 +379,7 @@ subroutine add_cell(me,val,int_fmt,real_fmt,trim_str)
354
379
355
380
integer :: istat ! ! write `iostat` flag
356
381
character (len= :),allocatable :: ifmt ! ! actual format string to use for integers
357
- character (len= :),allocatable :: rfmt ! ! actual format string to use for REALS
382
+ character (len= :),allocatable :: rfmt ! ! actual format string to use for reals
358
383
logical :: trimstr ! ! if the strings are to be trimmed
359
384
character (len= max_real_str_len) :: real_val
360
385
character (len= max_integer_str_len) :: int_val
@@ -389,9 +414,9 @@ subroutine add_cell(me,val,int_fmt,real_fmt,trim_str)
389
414
write (me% iunit,fmt= ' (A)' ,advance= ' NO' ,iostat= istat) trim (adjustl (real_val))
390
415
type is (logical )
391
416
if (val) then
392
- write (me% iunit,fmt= ' (A)' ,advance= ' NO' ,iostat= istat) ' T ' ! TODO make these user-defined strings
417
+ write (me% iunit,fmt= ' (A)' ,advance= ' NO' ,iostat= istat) me % logical_true_string
393
418
else
394
- write (me% iunit,fmt= ' (A)' ,advance= ' NO' ,iostat= istat) ' F '
419
+ write (me% iunit,fmt= ' (A)' ,advance= ' NO' ,iostat= istat) me % logical_false_string
395
420
end if
396
421
type is (character (len=* ))
397
422
if (me% enclose_strings_in_quotes .and. .not. me% enclose_all_in_quotes) &
@@ -709,7 +734,7 @@ pure elemental subroutine to_logical(str,val,status_ok)
709
734
if ( any (tmp== true_str) ) then
710
735
val = .true.
711
736
status_ok = .true.
712
- elseif ( any (tmp== false_str) ) then
737
+ else if ( any (tmp== false_str) ) then
713
738
val = .false.
714
739
status_ok = .true.
715
740
else
@@ -1121,7 +1146,7 @@ subroutine read_line_from_file(me,iunit,line)
1121
1146
! add the last block of text before the end of record
1122
1147
if (nread> 0 ) line = line// buffer(1 :nread)
1123
1148
exit
1124
- elseif (istat== 0 ) then ! all the characters were read
1149
+ else if (istat== 0 ) then ! all the characters were read
1125
1150
line = line// buffer ! add this block of text to the string
1126
1151
else ! some kind of error
1127
1152
error stop ' Read error.'
0 commit comments