TOML

TOML is a file format often used to store application settings and configurations. It is easy to read and write, and shares some similiarities with the older INI format. The syntax consists of key-value pairs, separated by optional sections. The TOML standard supports the data types String, Integer, Float, Boolean, Datetime, Array, and Table.

The file format is processed by TOML parsers, with the TOML wiki listing some them. The toml-f library is a pure Fortran 2008 implementation that is dual-licenced under MIT and Apache 2.0.

toml-f

On FreeBSD, the library is available as a package. Just install textproc/toml-f:

# pkg install textproc/toml-f

The build systems Meson and Ninja are supported to compile from source instead. Both dependencies are available on most Unix-like operating systems. On FreeBSD, install the packages devel/meson and devel/ninja:

# pkg install devel/meson devel/ninja

In the next step, clone the toml-f repository or download the master branch, and create a Ninja build script with Meson to compile the source code:

$ git clone https://github.com/toml-f/toml-f
$ cd toml-f/
$ FC=gfortran13 meson setup build
$ meson compile -C build

The FC parameter sets the Fortran compiler to GNU Fortran. The static library libtoml-f.a, the shared library libtoml-f.so, and the module file tomlf.mod for linking are written to build_gcc/ and its subdirectories.

Alternatively, the library may be built using the Fortran Package Manager instead:

$ fpm build --profile release

Example

We create a basic TOML file sample.toml with the following content:

# sample.toml
title = "TOML Example"
[owner]
name = "Tom Preston-Werner"
dob = "1979-05-27T07:32:00-08:00"
[database]
server = "10.0.0.1"
ports = [ 8001, 8002, 8003 ]
connection_max = 5000
enabled = true

The example program in Fortran 2008 will read the file and print some of its values to standard output:

! example.f90
program main
    use, intrinsic :: iso_fortran_env, only: stderr => error_unit
    use :: tomlf
    implicit none
    character(len=*), parameter :: FILE_NAME = 'sample.toml'

    character(len=:), allocatable :: title, server
    integer                       :: fu, rc
    logical                       :: file_exists
    type(toml_table), allocatable :: table
    type(toml_table), pointer     :: child

    inquire (file=FILE_NAME, exist=file_exists)

    if (.not. file_exists) then
        write (stderr, '("Error: TOML file ", a, " not found")') FILE_NAME
        stop
    end if

    open (action='read', file=FILE_NAME, iostat=rc, newunit=fu)

    if (rc /= 0) then
        write (stderr, '("Error: Reading TOML file ", a, " failed")') FILE_NAME
        stop
    end if

    call toml_parse(table, fu)
    close (fu)

    if (.not. allocated(table)) then
        write (stderr, '("Error: Parsing failed")')
        stop
    end if

    ! Output title.
    call get_value(table, 'title', title, 'N/A')
    print '(2a)', 'Title: ', title

    ! Get [database] section.
    call get_value(table, 'database', child, requested=.false.)

    if (associated(child)) then
        ! Output server address.
        call get_value(child, 'server', server, 'N/A')
        print '(2a)', 'Server: ', server
    end if
end program main

Compile, link, and run the example with:

$ gfortran13 -o example example.f90 libtoml-f.a
$ ./example
Title: TOML Example
Server: 10.0.0.1

Fortran Libraries

References