Skip to content

Commit 5fa1c0a

Browse files
committed
added a workaround for gfortran character bugs. Fixes #4.
1 parent 69bc5d7 commit 5fa1c0a

File tree

4 files changed

+89
-17
lines changed

4 files changed

+89
-17
lines changed

src/csv_module.F90

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@ module csv_module
4545

4646
character(len=1) :: quote = '"' !! quotation character
4747
character(len=1) :: delimiter = ',' !! delimiter character
48+
49+
! for reading a csv file:
4850
integer :: n_rows = 0 !! number of rows in the file
4951
integer :: n_cols = 0 !! number of columns in the file
5052
integer :: chunk_size = 100 !! for expanding vectors
51-
5253
type(csv_string),dimension(:),allocatable :: header !! the header
5354
type(csv_string),dimension(:,:),allocatable :: csv_data !! the data in the file
5455

55-
!for writing a csv file:
56+
! for writing a csv file:
5657
integer :: icol = 0 !! last column written in current row
5758
integer :: iunit = 0 !! file unit for writing
5859
logical :: enclose_strings_in_quotes = .true. !! if true, all string cells
@@ -124,7 +125,7 @@ module csv_module
124125

125126
!*****************************************************************************************
126127
!>
127-
! Constructor.
128+
! Initialize a [[csv_file(type)]].
128129

129130
subroutine initialize_csv_file(me,quote,delimiter,&
130131
enclose_strings_in_quotes,&
@@ -183,18 +184,11 @@ end subroutine initialize_csv_file
183184
!>
184185
! Destroy the data in a CSV file.
185186

186-
pure subroutine destroy_csv_file(me)
187+
subroutine destroy_csv_file(me)
187188

188189
implicit none
189190

190-
class(csv_file),intent(inout) :: me
191-
192-
if (allocated(me%csv_data)) deallocate(me%csv_data)
193-
if (allocated(me%header)) deallocate(me%header)
194-
195-
me%n_cols = 0
196-
me%n_rows = 0
197-
me%icol = 0
191+
class(csv_file),intent(out) :: me
198192

199193
end subroutine destroy_csv_file
200194
!*****************************************************************************************
@@ -348,7 +342,8 @@ subroutine open_csv_file(me,filename,n_cols,status_ok)
348342
if (istat==0) then
349343
status_ok = .true.
350344
else
351-
if (me%verbose) write(error_unit,'(A)') 'Error opening file: '//trim(filename)
345+
if (me%verbose) write(error_unit,'(A)') &
346+
'Error opening file: '//trim(filename)
352347
status_ok = .false.
353348
end if
354349

@@ -482,8 +477,6 @@ end subroutine add_cell
482477
!*****************************************************************************************
483478
!>
484479
! Add a vector to a CSV file. Each element is added as a cell to the current line.
485-
!
486-
!@warning There is some bug here with GFortran 6.1 when `val` is a character string.
487480

488481
subroutine add_vector(me,val,int_fmt,real_fmt,trim_str)
489482

@@ -500,7 +493,19 @@ subroutine add_vector(me,val,int_fmt,real_fmt,trim_str)
500493
integer :: i !! counter
501494

502495
do i=1,size(val)
496+
497+
#if defined __GFORTRAN__
498+
! This is a stupid workaround for gfortran bugs (tested with 7.2.0)
499+
select type (val)
500+
type is (character(len=*))
501+
call me%add(val(i),int_fmt,real_fmt,trim_str)
502+
class default
503+
call me%add(val(i),int_fmt,real_fmt,trim_str)
504+
end select
505+
#else
503506
call me%add(val(i),int_fmt,real_fmt,trim_str)
507+
#endif
508+
504509
end do
505510

506511
end subroutine add_vector

src/tests/csv_read_test.f90

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
program csv_read_test
2+
3+
use csv_module
4+
use iso_fortran_env, only: wp => real64
5+
6+
implicit none
7+
8+
type(csv_file) :: f
9+
character(len=30),dimension(:),allocatable :: header
10+
real(wp),dimension(:),allocatable :: x,y,z
11+
logical,dimension(:),allocatable :: t
12+
logical :: status_ok
13+
integer,dimension(:),allocatable :: itypes
14+
15+
! read the file
16+
call f%read('test.csv',header_row=1,status_ok=status_ok)
17+
18+
! get the header and type info
19+
call f%get_header(header,status_ok)
20+
call f%variable_types(itypes,status_ok)
21+
22+
! get some data
23+
call f%get(1,x,status_ok)
24+
call f%get(2,y,status_ok)
25+
call f%get(3,z,status_ok)
26+
call f%get(4,t,status_ok)
27+
28+
write(*,*) 'x=',x
29+
write(*,*) 'y=',y
30+
write(*,*) 'z=',z
31+
write(*,*) 't=',t
32+
33+
! destroy the file
34+
call f%destroy()
35+
36+
end program csv_read_test

src/tests/csv_test.f90

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ program csv_test
2929

3030
! read the file:
3131
if (ifile==1) then
32-
call f%read(trim(files_to_test(ifile)),header_row=1,status_ok=status_ok)
32+
call f%read(trim(files_to_test(ifile)),&
33+
header_row=1,status_ok=status_ok)
3334
else
3435
! also skip a row
35-
call f%read(trim(files_to_test(ifile)),header_row=1,skip_rows=[2],status_ok=status_ok)
36+
call f%read(trim(files_to_test(ifile)),&
37+
header_row=1,skip_rows=[2],status_ok=status_ok)
3638
end if
3739

3840
write(*,*) ''

src/tests/csv_write_test.f90

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
program csv_write_test
2+
3+
use csv_module
4+
use iso_fortran_env, only: wp => real64
5+
6+
implicit none
7+
8+
type(csv_file) :: f
9+
logical :: status_ok
10+
11+
! open the file
12+
call f%open('test.csv',n_cols=4,status_ok=status_ok)
13+
14+
! add header
15+
call f%add(['x','y','z','t'])
16+
call f%next_row()
17+
18+
! add some data:
19+
call f%add([1.0_wp,2.0_wp,3.0_wp],real_fmt='(F5.3)')
20+
call f%add(.true.)
21+
call f%next_row()
22+
call f%add([4.0_wp,5.0_wp,6.0_wp],real_fmt='(F5.3)')
23+
call f%add(.false.)
24+
call f%next_row()
25+
26+
! finished
27+
call f%close(status_ok)
28+
29+
end program csv_write_test

0 commit comments

Comments
 (0)