Skip to content

Commit c789d1f

Browse files
authored
Merge pull request #77 from jvdp1/parse_mode
Modification of the function parse_mode in stdlib_experimental_io
2 parents dbd314a + c65f196 commit c789d1f

File tree

5 files changed

+199
-59
lines changed

5 files changed

+199
-59
lines changed

Diff for: src/stdlib_experimental_io.f90

+21-16
Original file line numberDiff line numberDiff line change
@@ -356,26 +356,31 @@ integer function open(filename, mode) result(u)
356356
character(3) function parse_mode(mode) result(mode_)
357357
character(*), intent(in) :: mode
358358

359+
integer::i
360+
character(:),allocatable::a
361+
359362
mode_ = 'r t'
360-
if (len_trim(mode) == 0) return
361-
mode_(1:1) = mode(1:1)
362363

363-
if (len_trim(adjustl(mode)) > 1) then
364-
if (mode(2:2) == '+' )then
365-
mode_(2:2) = '+'
364+
if (len_trim(mode) == 0) return
365+
a=trim(adjustl(mode))
366+
367+
do i=1,len(a)
368+
if (a(i:i) == 'r' &
369+
.or. a(i:i) == 'w' &
370+
.or. a(i:i) == 'a' &
371+
.or. a(i:i) == 'x' &
372+
) then
373+
mode_(1:1) = a(i:i)
374+
else if (a(i:i) == '+') then
375+
mode_(2:2) = a(i:i)
376+
else if (a(i:i) == 't' .or. a(i:i) == 'b') then
377+
mode_(3:3) = a(i:i)
378+
else if (a(i:i) == ' ') then
379+
cycle
366380
else
367-
mode_(3:3) = mode(2:2)
381+
call error_stop("Wrong character: "//a(i:i))
368382
endif
369-
end if
370-
371-
if (len_trim(adjustl(mode)) > 2) then
372-
mode_(3:3) = mode(3:3)
373-
end if
374-
375-
if (mode_(1:1) == 'b') then
376-
mode_(1:1) = mode_(3:3)
377-
mode_(3:3) = 'b'
378-
end if
383+
end do
379384

380385
end function
381386

Diff for: src/tests/io/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ target_link_libraries(test_savetxt_qp fortran_stdlib)
1313
add_executable(test_open test_open.f90)
1414
target_link_libraries(test_open fortran_stdlib)
1515

16+
add_executable(test_parse_mode test_parse_mode.f90)
17+
target_link_libraries(test_parse_mode fortran_stdlib)
18+
1619
add_test(NAME loadtxt COMMAND $<TARGET_FILE:test_loadtxt> ${CMAKE_CURRENT_BINARY_DIR}
1720
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
1821
add_test(NAME savetxt COMMAND $<TARGET_FILE:test_savetxt> ${CMAKE_CURRENT_BINARY_DIR}
@@ -23,6 +26,8 @@ add_test(NAME savetxt_qp COMMAND $<TARGET_FILE:test_savetxt_qp> ${CMAKE_CURRENT_
2326
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
2427
add_test(NAME open COMMAND $<TARGET_FILE:test_open> ${CMAKE_CURRENT_BINARY_DIR}
2528
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
29+
add_test(NAME parse_mode COMMAND $<TARGET_FILE:test_parse_mode> ${CMAKE_CURRENT_BINARY_DIR}
30+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
2631

2732
set_tests_properties(loadtxt_qp PROPERTIES LABELS quadruple_precision)
2833
set_tests_properties(savetxt_qp PROPERTIES LABELS quadruple_precision)

Diff for: src/tests/io/Makefile.manual

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ PROGS_SRC = test_loadtxt.f90 \
22
test_savetxt.f90 \
33
test_loadtxt_qp.f90 \
44
test_savetxt_qp.f90 \
5+
test_parse_mode.f90 \
56
test_open.f90
67

78
CLEAN_FILES = tmp.dat tmp_qp.dat io_open.dat io_open.stream

Diff for: src/tests/io/test_open.f90

+1-43
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
program test_open
2-
use stdlib_experimental_io, only: open, parse_mode
2+
use stdlib_experimental_io, only: open
33
use stdlib_experimental_error, only: assert
44
implicit none
55

66
character(:), allocatable :: filename
77
integer :: u, a(3)
88

9-
call test_parse_mode()
109

1110
! Text file
1211
filename = get_outpath() // "/io_open.dat"
@@ -75,45 +74,4 @@ function get_outpath() result(outpath)
7574
endif
7675
end function get_outpath
7776

78-
subroutine test_parse_mode()
79-
character(3) :: m
80-
m = parse_mode("")
81-
call assert(m == "r t")
82-
83-
m = parse_mode("r")
84-
call assert(m == "r t")
85-
m = parse_mode("w")
86-
call assert(m == "w t")
87-
m = parse_mode("a")
88-
call assert(m == "a t")
89-
90-
m = parse_mode("rb")
91-
call assert(m == "r b")
92-
m = parse_mode("wb")
93-
call assert(m == "w b")
94-
m = parse_mode("ab")
95-
call assert(m == "a b")
96-
97-
m = parse_mode("br")
98-
call assert(m == "r b")
99-
m = parse_mode("bw")
100-
call assert(m == "w b")
101-
m = parse_mode("ba")
102-
call assert(m == "a b")
103-
104-
m = parse_mode("r+")
105-
call assert(m == "r+t")
106-
m = parse_mode("w+")
107-
call assert(m == "w+t")
108-
m = parse_mode("a+")
109-
call assert(m == "a+t")
110-
111-
m = parse_mode("r+b")
112-
call assert(m == "r+b")
113-
m = parse_mode("w+b")
114-
call assert(m == "w+b")
115-
m = parse_mode("a+b")
116-
call assert(m == "a+b")
117-
end subroutine
118-
11977
end program

Diff for: src/tests/io/test_parse_mode.f90

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
program test_parse_mode
2+
use stdlib_experimental_io, only: parse_mode
3+
use stdlib_experimental_error, only: assert
4+
implicit none
5+
6+
call test_parse_mode_expected_order()
7+
8+
call test_parse_mode_reverse_order()
9+
10+
call test_parse_mode_random_order()
11+
12+
contains
13+
14+
subroutine test_parse_mode_expected_order()
15+
character(3) :: m
16+
m = parse_mode("")
17+
call assert(m == "r t")
18+
19+
m = parse_mode("r")
20+
call assert(m == "r t")
21+
m = parse_mode("w")
22+
call assert(m == "w t")
23+
m = parse_mode("a")
24+
call assert(m == "a t")
25+
m = parse_mode("x")
26+
call assert(m == "x t")
27+
28+
m = parse_mode("rt")
29+
call assert(m == "r t")
30+
m = parse_mode("wt")
31+
call assert(m == "w t")
32+
m = parse_mode("at")
33+
call assert(m == "a t")
34+
m = parse_mode("xt")
35+
call assert(m == "x t")
36+
37+
m = parse_mode("rb")
38+
call assert(m == "r b")
39+
m = parse_mode("wb")
40+
call assert(m == "w b")
41+
m = parse_mode("ab")
42+
call assert(m == "a b")
43+
m = parse_mode("xb")
44+
call assert(m == "x b")
45+
46+
m = parse_mode("r+")
47+
call assert(m == "r+t")
48+
m = parse_mode("w+")
49+
call assert(m == "w+t")
50+
m = parse_mode("a+")
51+
call assert(m == "a+t")
52+
m = parse_mode("x+")
53+
call assert(m == "x+t")
54+
55+
m = parse_mode("r+t")
56+
call assert(m == "r+t")
57+
m = parse_mode("w+t")
58+
call assert(m == "w+t")
59+
m = parse_mode("a+t")
60+
call assert(m == "a+t")
61+
m = parse_mode("x+t")
62+
call assert(m == "x+t")
63+
64+
m = parse_mode("r+b")
65+
call assert(m == "r+b")
66+
m = parse_mode("w+b")
67+
call assert(m == "w+b")
68+
m = parse_mode("a+b")
69+
call assert(m == "a+b")
70+
m = parse_mode("x+b")
71+
call assert(m == "x+b")
72+
73+
end subroutine
74+
75+
subroutine test_parse_mode_reverse_order()
76+
character(3) :: m
77+
m = parse_mode("")
78+
call assert(m == "r t")
79+
80+
m = parse_mode("tr")
81+
call assert(m == "r t")
82+
m = parse_mode("tw")
83+
call assert(m == "w t")
84+
m = parse_mode("ta")
85+
call assert(m == "a t")
86+
m = parse_mode("tx")
87+
call assert(m == "x t")
88+
89+
m = parse_mode("br")
90+
call assert(m == "r b")
91+
m = parse_mode("bw")
92+
call assert(m == "w b")
93+
m = parse_mode("ba")
94+
call assert(m == "a b")
95+
m = parse_mode("bx")
96+
call assert(m == "x b")
97+
98+
m = parse_mode("+r")
99+
call assert(m == "r+t")
100+
m = parse_mode("+w")
101+
call assert(m == "w+t")
102+
m = parse_mode("+a")
103+
call assert(m == "a+t")
104+
m = parse_mode("+x")
105+
call assert(m == "x+t")
106+
107+
m = parse_mode("t+r")
108+
call assert(m == "r+t")
109+
m = parse_mode("t+w")
110+
call assert(m == "w+t")
111+
m = parse_mode("t+a")
112+
call assert(m == "a+t")
113+
m = parse_mode("t+x")
114+
call assert(m == "x+t")
115+
116+
m = parse_mode("b+r")
117+
call assert(m == "r+b")
118+
m = parse_mode("b+w")
119+
call assert(m == "w+b")
120+
m = parse_mode("b+a")
121+
call assert(m == "a+b")
122+
m = parse_mode("x+b")
123+
call assert(m == "x+b")
124+
125+
end subroutine
126+
127+
subroutine test_parse_mode_random_order()
128+
character(3) :: m
129+
m = parse_mode("")
130+
call assert(m == "r t")
131+
132+
m = parse_mode("t r")
133+
call assert(m == "r t")
134+
m = parse_mode(" tw ")
135+
call assert(m == "w t")
136+
m = parse_mode("ta ")
137+
call assert(m == "a t")
138+
m = parse_mode(" t x ")
139+
call assert(m == "x t")
140+
141+
m = parse_mode("+ r ")
142+
call assert(m == "r+t")
143+
m = parse_mode("w +")
144+
call assert(m == "w+t")
145+
m = parse_mode(" a+")
146+
call assert(m == "a+t")
147+
m = parse_mode(" x+ t ")
148+
call assert(m == "x+t")
149+
150+
m = parse_mode("tr+ ")
151+
call assert(m == "r+t")
152+
m = parse_mode("wtt + ")
153+
call assert(m == "w+t")
154+
m = parse_mode("a + t")
155+
call assert(m == "a+t")
156+
m = parse_mode(" xt + ")
157+
call assert(m == "x+t")
158+
159+
m = parse_mode("t + t")
160+
call assert(m == "r+t")
161+
m = parse_mode(" ww + b")
162+
call assert(m == "w+b")
163+
m = parse_mode("a + b")
164+
call assert(m == "a+b")
165+
m = parse_mode(" b + x ")
166+
call assert(m == "x+b")
167+
168+
end subroutine
169+
170+
171+
end program

0 commit comments

Comments
 (0)