File: read_30.f90

package info (click to toggle)
lfortran 0.60.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,412 kB
  • sloc: cpp: 173,406; f90: 80,491; python: 17,586; ansic: 9,610; yacc: 2,356; sh: 1,401; fortran: 895; makefile: 37; javascript: 15
file content (47 lines) | stat: -rw-r--r-- 1,388 bytes parent folder | download
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
program read_30
    implicit none

    integer :: io_w, io_r, stat
    integer :: filesize
    character(len=:), allocatable :: buffer

    character(len=*), parameter :: toml_text = &
        'name = "core_lib"' // new_line('a')

    open(newunit=io_w, file="config.toml", access="stream", &
         form="unformatted", status="replace", iostat=stat)
    if (stat /= 0) error stop "open(write) failed"

    write(io_w, iostat=stat) toml_text
    if (stat /= 0) error stop "write failed"

    close(io_w)

    open(newunit=io_r, file="config.toml", access="stream", &
         form="unformatted", position="append", status="old", iostat=stat)
    if (stat /= 0) error stop "open(read) failed"

    inquire(unit=io_r, size=filesize)
    if (filesize <= 1) error stop "config.toml too small"

    allocate(character(len=filesize) :: buffer)
    buffer(filesize:filesize) = "?"

    read(io_r, pos=1, iostat=stat) buffer(:filesize-1)
    if (stat /= 0) error stop "stream read failed"

    close(io_r, status='delete')

    if (index(buffer, 'name') == 0) then
        error stop "expected key 'name' not found"
    end if

    if (buffer(filesize:filesize) /= "?") then
        error stop "buffer overflow detected"
    end if

    print *, "CONFIG CONTENT:"
    print *, trim(buffer)
    if (trim(buffer) /= 'name = "core_lib"?') error stop "config content mismatch"

end program read_30